github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/create_account_with_alias/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  	// Use the `AccountCreateTransaction` and populate `setAlias(evmAddress)` field with the Ethereum public address
    40  	// Sign the `AccountCreateTransaction` transaction with the new private key
    41  	// Get the `AccountInfo` on the new account and show that the account has contractAccountId
    42  
    43  	// Create a ECDSA private key
    44  	privateKey, err := hedera.PrivateKeyGenerateEcdsa()
    45  	if err != nil {
    46  		println(err.Error())
    47  	}
    48  	// Extract the ECDSA public key public key
    49  	publicKey := privateKey.PublicKey()
    50  	// Extract the Ethereum public address
    51  	evmAddress := publicKey.ToEvmAddress()
    52  
    53  	// Use the `AccountCreateTransaction` and set the EVM address field to the Ethereum public address
    54  	frozenTxn, err := hedera.NewAccountCreateTransaction().SetInitialBalance(hedera.HbarFromTinybar(100)).
    55  		SetKey(operatorKey).SetAlias(evmAddress).FreezeWith(client)
    56  	if err != nil {
    57  		println(err.Error())
    58  	}
    59  	response, err := frozenTxn.Sign(privateKey).Execute(client)
    60  	if err != nil {
    61  		println(err.Error())
    62  	}
    63  
    64  	transactionReceipt, err := response.GetReceipt(client)
    65  	if err != nil {
    66  		panic(fmt.Sprintf("%v : error getting receipt}", err))
    67  	}
    68  
    69  	newAccountId := *transactionReceipt.AccountID
    70  
    71  	// Get the `AccountInfo` on the new account and show that the account has contractAccountId
    72  	info, err := hedera.NewAccountInfoQuery().SetAccountID(newAccountId).Execute(client)
    73  	if err != nil {
    74  		println(err.Error())
    75  	}
    76  	// Verify account is created with the provided EVM address
    77  	fmt.Println(info.ContractAccountID == evmAddress)
    78  	// Verify the account Id is the same from the create account transaction
    79  	fmt.Println(info.AccountID.String() == newAccountId.String())
    80  }