github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/multi_app_transfer/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/hashgraph/hedera-sdk-go/v2" 8 ) 9 10 func main() { 11 // Our hypothetical primary service only knows the operator/sender's account ID and the recipient's accountID 12 operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) 13 if err != nil { 14 panic(fmt.Sprintf("%v : error converting string to AccountID", err)) 15 } 16 17 operatorPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) 18 if err != nil { 19 panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) 20 } 21 22 recipientAccountID := hedera.AccountID{Account: 3} 23 24 // We create a client without a set operator 25 client, err := hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 26 if err != nil { 27 panic(fmt.Sprintf("%v : error creating client", err)) 28 } 29 30 client.SetOperator(operatorAccountID, operatorPrivateKey) 31 32 // We must manually construct a TransactionID with the accountID of the operator/sender 33 // This is the account that will be charged the transaction fee 34 txID := hedera.TransactionIDGenerate(operatorAccountID) 35 36 // The following steps are required for manually signing 37 transaction, err := hedera.NewTransferTransaction(). 38 // 1. Manually set the transaction ID 39 SetTransactionID(txID). 40 // 2. Add your sender and amount to be send 41 AddHbarTransfer(operatorAccountID, hedera.NewHbar(-1)). 42 // 3. add the recipient(s) and amount to be received 43 AddHbarTransfer(recipientAccountID, hedera.NewHbar(1)). 44 SetTransactionMemo("go sdk example multi_app_transfer/main.go"). 45 // 4. build the transaction using the client that does not have a set operator 46 FreezeWith(client) 47 48 if err != nil { 49 panic(fmt.Sprintf("%v : error freezing Transfer Transaction", err)) 50 } 51 52 // Marshal your transaction to bytes 53 txBytes, err := transaction.ToBytes() 54 if err != nil { 55 panic(fmt.Sprintf("%v : error converting transfer transaction to bytes", err)) 56 } 57 58 fmt.Printf("Marshalled the unsigned transaction to bytes \n%v\n", txBytes) 59 60 // 61 // Send the bytes to the application or service that acts as a signer for your transactions 62 // 63 signedTxBytes, err := signingService(txBytes) 64 65 if err != nil { 66 panic(fmt.Sprintf("%v : error signing transfer transaction", err)) 67 } 68 69 fmt.Printf("Received bytes for signed transaction: \n%v\n", signedTxBytes) 70 71 // Unmarshal your bytes into the signed transaction 72 var signedTx hedera.TransferTransaction 73 tx, err := hedera.TransactionFromBytes(signedTxBytes) 74 if err != nil { 75 panic(fmt.Sprintf("%v : error converting bytes to transfer transaction", err)) 76 } 77 78 // Converting from interface{} to TransferTransaction, if that's what we got 79 switch t := tx.(type) { 80 case hedera.TransferTransaction: 81 signedTx = t 82 default: 83 panic("Did not receive `TransferTransaction` back from signed bytes") 84 } 85 86 // Execute the transaction 87 response, err := signedTx.Execute(client) 88 89 if err != nil { 90 panic(fmt.Sprintf("%v : error executing the transfer transaction", err)) 91 } 92 93 // Get the receipt of the transaction to check the status 94 receipt, err := response.GetReceipt(client) 95 96 if err != nil { 97 panic(fmt.Sprintf("%v : error retrieving transfer transaction receipt", err)) 98 } 99 100 // If Status Success is returned then everything is good 101 fmt.Printf("Crypto transfer status: %v\n", receipt.Status) 102 } 103 104 // signingService represents an offline service which knows the private keys needed for signing 105 // a transaction and returns the byte representation of the transaction 106 func signingService(txBytes []byte) ([]byte, error) { 107 fmt.Println("\nSigning service has received the transaction") 108 109 // Your signing service is aware of the operator's private key 110 operatorPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) 111 if err != nil { 112 return txBytes, err 113 } 114 115 // Unmarshal the unsigned transaction's bytes 116 var unsignedTx hedera.TransferTransaction 117 tx, err := hedera.TransactionFromBytes(txBytes) 118 if err != nil { 119 return txBytes, err 120 } 121 122 // Converting from interface{} to TransferTransaction, if that's what we got 123 switch t := tx.(type) { 124 case hedera.TransferTransaction: 125 unsignedTx = t 126 default: 127 panic("Did not receive `TransferTransaction` back from signed bytes") 128 } 129 130 fmt.Printf("The Signing service is signing the transaction with key: %v\n", operatorPrivateKey) 131 132 // sign your unsigned transaction and marshal back to bytes 133 return unsignedTx. 134 Sign(operatorPrivateKey). 135 ToBytes() 136 }