github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/account_create_token_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 var client *hedera.Client 12 var err error 13 14 // Retrieving network type from environment variable HEDERA_NETWORK 15 client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 16 if err != nil { 17 panic(fmt.Sprintf("%v : error creating client", err)) 18 } 19 20 // Retrieving operator ID from environment variable OPERATOR_ID 21 operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) 22 if err != nil { 23 panic(fmt.Sprintf("%v : error converting string to AccountID", err)) 24 } 25 26 // Retrieving operator key from environment variable OPERATOR_KEY 27 operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) 28 if err != nil { 29 panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) 30 } 31 32 // Setting the client operator ID and key 33 client.SetOperator(operatorAccountID, operatorKey) 34 35 // ## Example 36 // Create a ECDSA private key 37 // Extract the ECDSA public key public key 38 // Extract the Ethereum public address 39 // Transfer tokens using the `TransferTransaction` to the Etherum Account Address 40 // The From field should be a complete account that has a public address 41 // The To field should be to a public address (to create a new account) 42 // Get the child receipt or child record to return the Hedera Account ID for the new account that was created 43 // Get the `AccountInfo` on the new account and show it is a hollow account by not having a public key 44 // This is a hollow account in this state 45 // Use the hollow account as a transaction fee payer in a HAPI transaction 46 // Sign the transaction with ECDSA private key 47 // Get the `AccountInfo` of the account and show the account is now a complete account by returning the public key on the account 48 49 // Create a ECDSA private key 50 privateKey, err := hedera.PrivateKeyGenerateEcdsa() 51 if err != nil { 52 panic(err) 53 } 54 // Extract the ECDSA public key public key 55 publicKey := privateKey.PublicKey() 56 // Extract the Ethereum public address 57 evmAddress := publicKey.ToEvmAddress() 58 59 // Create an AccountID struct with EVM address 60 evmAddressAccount, err := hedera.AccountIDFromEvmPublicAddress(evmAddress) 61 if err != nil { 62 panic(fmt.Sprintf("%v : error creating account from EVM address", err)) 63 } 64 // Transfer tokens using the `TransferTransaction` to the Etherum Account Address 65 tx, err := hedera.NewTransferTransaction().AddHbarTransfer(evmAddressAccount, hedera.NewHbar(4)). 66 AddHbarTransfer(operatorAccountID, hedera.NewHbar(-4)).Execute(client) 67 if err != nil { 68 panic(err) 69 } 70 71 // Get the child receipt or child record to return the Hedera Account ID for the new account that was created 72 receipt, err := tx.GetReceiptQuery().SetIncludeChildren(true).Execute(client) 73 if err != nil { 74 panic(fmt.Sprintf("%v : error with receipt: ", err)) 75 } 76 newAccountId := *receipt.Children[0].AccountID 77 78 // Get the `AccountInfo` on the new account and show it is a hollow account by not having a public key 79 info, err := hedera.NewAccountInfoQuery().SetAccountID(newAccountId).Execute(client) 80 if err != nil { 81 panic(err) 82 } 83 // Verify account is created with the public address provided 84 fmt.Println(info.ContractAccountID == publicKey.ToEvmAddress()) 85 // Verify the account Id is the same from the create account transaction 86 fmt.Println(info.AccountID.String() == newAccountId.String()) 87 // Verify the account does not have a Hedera public key /hollow account/ 88 fmt.Println(info.Key.String() == "{[]}") 89 90 // Use the hollow account as a transaction fee payer in a HAPI transaction 91 // Sign the transaction with ECDSA private key 92 client.SetOperator(newAccountId, privateKey) 93 tx, err = hedera.NewTransferTransaction().AddHbarTransfer(operatorAccountID, hedera.NewHbar(1)). 94 AddHbarTransfer(newAccountId, hedera.NewHbar(-1)).Execute(client) 95 if err != nil { 96 panic(err) 97 } 98 receipt, err = tx.GetReceiptQuery().Execute(client) 99 if err != nil { 100 panic(fmt.Sprintf("%v : error with receipt: ", err)) 101 } 102 103 // Get the `AccountInfo` of the account and show the account is now a complete account by returning the public key on the account 104 info, err = hedera.NewAccountInfoQuery().SetAccountID(newAccountId).Execute(client) 105 if err != nil { 106 panic(err) 107 } 108 // Verify account is created with the public address provided 109 fmt.Println(info.ContractAccountID == publicKey.ToEvmAddress()) 110 // Verify the account Id is the same from the create account transaction 111 fmt.Println(info.AccountID.String() == newAccountId.String()) 112 // Verify the account does have a Hedera public key /complete Hedera account/ 113 fmt.Println(info.Key.String() == publicKey.String()) 114 115 }