github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/update_account_public_key/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  	// Generating key for the new account
    36  	key1, err := hedera.GeneratePrivateKey()
    37  	if err != nil {
    38  		panic(fmt.Sprintf("%v : error generating PrivateKey", err))
    39  	}
    40  
    41  	// Generating the key to update to
    42  	key2, err := hedera.GeneratePrivateKey()
    43  	if err != nil {
    44  		panic(fmt.Sprintf("%v : error generating PrivateKey", err))
    45  	}
    46  
    47  	// Creating new account
    48  	accountTxResponse, err := hedera.NewAccountCreateTransaction().
    49  		// The key that must sign each transfer out of the account. If receiverSigRequired is true, then
    50  		// it must also sign any transfer into the account.
    51  		// Using the public key for this, but a PrivateKey or a KeyList can also be used
    52  		SetKey(key1.PublicKey()).
    53  		SetInitialBalance(hedera.ZeroHbar).
    54  		SetTransactionID(hedera.TransactionIDGenerate(client.GetOperatorAccountID())).
    55  		SetTransactionMemo("sdk example create_account__with_manual_signing/main.go").
    56  		Execute(client)
    57  	if err != nil {
    58  		panic(fmt.Sprintf("%v : error creating account", err))
    59  	}
    60  
    61  	println("transaction ID:", accountTxResponse.TransactionID.String())
    62  
    63  	// Get the receipt making sure transaction worked
    64  	accountTxReceipt, err := accountTxResponse.GetReceipt(client)
    65  	if err != nil {
    66  		panic(fmt.Sprintf("%v : error retrieving account creation receipt", err))
    67  	}
    68  
    69  	// Retrieve the account ID out of the Receipt
    70  	accountID := *accountTxReceipt.AccountID
    71  	println("account =", accountID.String())
    72  	println("key =", key1.PublicKey().String())
    73  	println(":: update public key of account", accountID.String())
    74  	println("set key =", key2.PublicKey().String())
    75  
    76  	// Updating the account with the new key
    77  	accountUpdateTx, err := hedera.NewAccountUpdateTransaction().
    78  		SetAccountID(accountID).
    79  		// The new key
    80  		SetKey(key2.PublicKey()).
    81  		FreezeWith(client)
    82  	if err != nil {
    83  		panic(fmt.Sprintf("%v : error freezing account update transaction", err))
    84  	}
    85  
    86  	// Have to sign with both keys, the initial key first
    87  	accountUpdateTx.Sign(key1)
    88  	accountUpdateTx.Sign(key2)
    89  
    90  	// Executing the account update transaction
    91  	accountUpdateTxResponse, err := accountUpdateTx.Execute(client)
    92  	if err != nil {
    93  		panic(fmt.Sprintf("%v : error updating account", err))
    94  	}
    95  
    96  	println("transaction ID:", accountUpdateTxResponse.TransactionID.String())
    97  
    98  	// Make sure the transaction went through
    99  	_, err = accountUpdateTxResponse.GetReceipt(client)
   100  
   101  	println(":: getAccount and check our current key")
   102  	info, err := hedera.NewAccountInfoQuery().
   103  		SetAccountID(accountID).
   104  		Execute(client)
   105  	if err != nil {
   106  		panic(fmt.Sprintf("%v : error executing account info query", err))
   107  	}
   108  
   109  	// This should be same as key2
   110  	println("key =", info.Key.String())
   111  }