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