github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/serialize-deserialize/example1/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(err)
    38  	}
    39  	resp, err := hedera.NewAccountCreateTransaction().SetKey(newKey).Execute(client)
    40  	receipt, err := resp.GetReceipt(client)
    41  	newAccountId := *receipt.AccountID
    42  
    43  	bytes, err := hedera.NewTransferTransaction().AddHbarTransfer(operatorAccountID, hedera.NewHbar(1)).
    44  		ToBytes()
    45  
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	txFromBytes, err := hedera.TransactionFromBytes(bytes)
    51  
    52  	transaction := txFromBytes.(hedera.TransferTransaction)
    53  	_, err = transaction.AddHbarTransfer(newAccountId, hedera.NewHbar(-1)).SignWithOperator(client)
    54  
    55  	_, err = transaction.Execute(client)
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  	// Get the `AccountInfo` on the new account and show it is a hollow account by not having a public key
    60  	info, err := hedera.NewAccountInfoQuery().SetAccountID(newAccountId).Execute(client)
    61  	fmt.Println("Balance of new account: ", info.Balance)
    62  }