-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example for a multi app transfer scenario
- Loading branch information
1 parent
0d3ba65
commit 7d415f3
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashgraph/hedera-sdk-go" | ||
"os" | ||
"time" | ||
) | ||
|
||
func main() { | ||
|
||
// Our hypothetical primary service only knows the operator/sender's account ID and the recipient's accountID | ||
operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
recipientAccountID := hedera.AccountID{Account: 3} | ||
|
||
// We create a client without a set operator | ||
client := hedera.ClientForTestnet() | ||
|
||
// We must manually construct a TransactionID with the accountID of the operator/sender | ||
// and an appropriate valid start time | ||
txID := hedera.TransactionID{ | ||
AccountID: operatorAccountID, | ||
ValidStart: time.Now(), | ||
} | ||
|
||
// The following steps are required for manually signing | ||
transaction, err := hedera.NewCryptoTransferTransaction(). | ||
// 1. Manually set the transaction ID | ||
SetTransactionID(txID). | ||
// 2. Add your sender and amount to be send | ||
AddSender(operatorAccountID, hedera.NewHbar(1)). | ||
// 3. add the recipient(s) and amount to be received | ||
AddRecipient(recipientAccountID, hedera.NewHbar(1)). | ||
SetTransactionMemo("go sdk example multi_app_transfer/main.go"). | ||
// 4. build the transaction using the client that does not have a set operator | ||
Build(client) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// marshal your transaction to bytes | ||
txBytes, err := transaction.Bytes() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("marshalled the unsigned transaction to bytes \n%v\n", txBytes) | ||
|
||
// | ||
// Send the bytes to the application or service that acts as a signer for your transactions | ||
// | ||
signedTxBytes, err := signingService(txBytes) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("received bytes for signed transaction \n%v\n", signedTxBytes) | ||
|
||
// unmarshal your bytes into the signed transaction | ||
signedTx, err := hedera.TransactionFromBytes(signedTxBytes) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// execute the transaction | ||
txID, err = signedTx.Execute(client) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// get the receipt of the transaction to check the status | ||
receipt, err := txID.GetReceipt(client) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// if Status Success is returned then everything is good | ||
fmt.Printf("crypto transfer status: %v\n", receipt.Status) | ||
} | ||
|
||
// signingService represents a non-local service which knows the private keys needed for signing | ||
// a transaction and returns the byte representation of the transaction | ||
func signingService(txBytes []byte) ([]byte, error) { | ||
fmt.Println("signing service has received the transaction") | ||
|
||
// Your signing service is aware of the operator's private key | ||
operatorPrivateKey, err := hedera.Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) | ||
if err != nil { | ||
return txBytes, err | ||
} | ||
|
||
// unmarshal the unsigned transaction's bytes | ||
unsignedTx, err := hedera.TransactionFromBytes(txBytes) | ||
|
||
if err != nil { | ||
return txBytes, err | ||
} | ||
|
||
fmt.Printf("The Signing service is signing the transaction with key %v\n", operatorPrivateKey) | ||
|
||
// sign your unsigned transaction and marshal back to bytes | ||
return unsignedTx. | ||
Sign(operatorPrivateKey). | ||
Bytes() | ||
} |