github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/create_account_with_threshold_keys/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  	// make the key arrays
    36  	keys := make([]hedera.PrivateKey, 3)
    37  	pubKeys := make([]hedera.PublicKey, 3)
    38  
    39  	fmt.Println("threshold key example")
    40  	fmt.Println("Keys: ")
    41  
    42  	// generate the keys and put them in their respective arrays
    43  	for i := range keys {
    44  		newKey, err := hedera.GeneratePrivateKey()
    45  		if err != nil {
    46  			panic(fmt.Sprintf("%v : error generating PrivateKey}", err))
    47  		}
    48  
    49  		fmt.Printf("Key %v:\n", i)
    50  		fmt.Printf("private = %v\n", newKey)
    51  		fmt.Printf("public = %v\n", newKey.PublicKey())
    52  
    53  		keys[i] = newKey
    54  		pubKeys[i] = newKey.PublicKey()
    55  	}
    56  
    57  	// A threshold key with a threshold of 2 and length of 3 requires
    58  	// at least 2 of the 3 keys to sign anything modifying the account
    59  	thresholdPublicKeys := hedera.KeyListWithThreshold(2).
    60  		AddAllPublicKeys(pubKeys)
    61  
    62  	println()
    63  	fmt.Printf("threshold keys: %v\n", thresholdPublicKeys)
    64  	println()
    65  
    66  	// setup account create transaction with the public threshold keys, then freeze it for singing
    67  	transaction, err := hedera.NewAccountCreateTransaction().
    68  		// Only thing required to create account is the key
    69  		SetKey(thresholdPublicKeys).
    70  		// Setting the initial balance to be 6 Hbars
    71  		SetInitialBalance(hedera.NewHbar(6)).
    72  		// Presetting transaction ID, this is not required
    73  		SetTransactionID(hedera.TransactionIDGenerate(client.GetOperatorAccountID())).
    74  		SetTransactionMemo("sdk example create_account_with_threshold_keys/main.go").
    75  		FreezeWith(client)
    76  	if err != nil {
    77  		panic(fmt.Sprintf("%v : error freezing create account transaction", err))
    78  	}
    79  
    80  	// Sign with all the private keys
    81  	for i := range keys {
    82  		transaction = transaction.Sign(keys[i])
    83  	}
    84  
    85  	// Finally, execute the transaction getting the response
    86  	transactionResponse, err := transaction.Execute(client)
    87  
    88  	if err != nil {
    89  		panic(fmt.Sprintf("%v : error creating account", err))
    90  	}
    91  
    92  	// Get the receipt to see everything worked
    93  	transactionReceipt, err := transactionResponse.GetReceipt(client)
    94  
    95  	if err != nil {
    96  		panic(fmt.Sprintf("%v : error retrieving account create receipt", err))
    97  	}
    98  
    99  	// Get the new account ID
   100  	newAccountID := *transactionReceipt.AccountID
   101  
   102  	fmt.Printf("account = %v\n", newAccountID)
   103  
   104  	// Now we have to make sure everything worked with a transfer transaction using the new account ID
   105  	transferTx, err := hedera.NewTransferTransaction().
   106  		// Presetting transaction ID is not required
   107  		SetTransactionID(hedera.TransactionIDGenerate(newAccountID)).
   108  		// Setting node id is not required, but it guarantees the account will be available without waiting for propagation
   109  		SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
   110  		// Negate the Hbar if its being taken out of the account
   111  		AddHbarTransfer(newAccountID, hedera.HbarFrom(-5, hedera.HbarUnits.Hbar)).
   112  		AddHbarTransfer(client.GetOperatorAccountID(), hedera.HbarFrom(5, hedera.HbarUnits.Hbar)).
   113  		FreezeWith(client)
   114  
   115  	if err != nil {
   116  		panic(fmt.Sprintf("%v : error freezing transfer transaction", err))
   117  	}
   118  
   119  	// Manually sign with 2 of the private keys provided in the threshold
   120  	transactionResponse, err = transferTx.
   121  		Sign(keys[0]).
   122  		Sign(keys[1]).
   123  		Execute(client)
   124  	if err != nil {
   125  		panic(fmt.Sprintf("%v : error freezing create account transaction", err))
   126  	}
   127  
   128  	// Make sure the transaction executes properly
   129  	transactionReceipt, err = transactionResponse.GetReceipt(client)
   130  	if err != nil {
   131  		panic(fmt.Sprintf("%v : error retrieving transfer receipt", err))
   132  	}
   133  
   134  	fmt.Printf("status of transfer transaction: %v\n", transactionReceipt.Status)
   135  
   136  	// This query is free
   137  	// Here we check if transfer transaction actually succeeded
   138  	balance, err := hedera.NewAccountBalanceQuery().
   139  		// The account ID to check balance of
   140  		SetAccountID(newAccountID).
   141  		SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
   142  		Execute(client)
   143  	if err != nil {
   144  		panic(fmt.Sprintf("%v : error executing account balance query", err))
   145  	}
   146  
   147  	fmt.Printf("account balance after transfer: %v\n", balance.Hbars.String())
   148  }