github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/create_account/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashgraph/hedera-sdk-go/v2"
     6  	"os"
     7  )
     8  
     9  func main() {
    10  	var client *hedera.Client
    11  	var err error
    12  
    13  	// Retrieving network type from environment variable HEDERA_NETWORK
    14  	client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
    15  	if err != nil {
    16  		panic(fmt.Sprintf("%v : error creating client", err))
    17  	}
    18  
    19  	// Retrieving operator ID from environment variable OPERATOR_ID
    20  	operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
    21  	if err != nil {
    22  		panic(fmt.Sprintf("%v : error converting string to AccountID", err))
    23  	}
    24  
    25  	// Retrieving operator key from environment variable OPERATOR_KEY
    26  	operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    27  	if err != nil {
    28  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    29  	}
    30  
    31  	// Setting the client operator ID and key
    32  	client.SetOperator(operatorAccountID, operatorKey)
    33  
    34  	// Generate new key to use with new account
    35  	newKey, err := hedera.GeneratePrivateKey()
    36  	if err != nil {
    37  		panic(fmt.Sprintf("%v : error generating PrivateKey}", err))
    38  	}
    39  
    40  	fmt.Printf("private = %v\n", newKey)
    41  	fmt.Printf("public = %v\n", newKey.PublicKey())
    42  
    43  	// Create account
    44  	// The only required property here is `key`
    45  	transactionResponse, err := hedera.NewAccountCreateTransaction().
    46  		// The key that must sign each transfer out of the account.
    47  		SetKey(newKey.PublicKey()).
    48  		// If true, this account's key must sign any transaction depositing into this account (in
    49  		// addition to all withdrawals)
    50  		SetReceiverSignatureRequired(false).
    51  		// The maximum number of tokens that an Account can be implicitly associated with. Defaults to 0
    52  		// and up to a maximum value of 1000.
    53  		SetMaxAutomaticTokenAssociations(1).
    54  		// The memo associated with the account
    55  		SetTransactionMemo("go sdk example create_account/main.go").
    56  		// The account is charged to extend its expiration date every this many seconds. If it doesn't
    57  		// have enough balance, it extends as long as possible. If it is empty when it expires, then it
    58  		// is deleted.
    59  		Execute(client)
    60  	if err != nil {
    61  		panic(fmt.Sprintf("%v : error executing account create transaction}", err))
    62  	}
    63  
    64  	// Get receipt to see if transaction succeeded, and has the account ID
    65  	transactionReceipt, err := transactionResponse.GetReceipt(client)
    66  	if err != nil {
    67  		panic(fmt.Sprintf("%v : error getting receipt}", err))
    68  	}
    69  
    70  	// Get account ID out of receipt
    71  	newAccountID := *transactionReceipt.AccountID
    72  
    73  	fmt.Printf("account = %v\n", newAccountID)
    74  }