github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/alias_id_example/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 // Defaults the operator account ID and key such that all generated transactions will be paid for 33 // by this account and be signed by this key 34 client.SetOperator(operatorAccountID, operatorKey) 35 36 /* 37 * Hedera supports a form of auto account creation. 38 * 39 * You can "create" an account by generating a private key, and then deriving the public key, 40 * without any need to interact with the Hedera network. The public key more or less acts as the user's 41 * account ID. This public key is an account's aliasKey: a public key that aliases (or will eventually alias) 42 * to a Hedera account. 43 * 44 * An AccountId takes one of two forms: a normal AccountId with a null aliasKey member takes the form 0.0.123, 45 * while an account ID with a non-null aliasKey member takes the form 46 * 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777 47 * Note the prefix of "0.0." indicating the shard and realm. Also note that the aliasKey is stringified 48 * as a hex-encoded ASN1 DER representation of the key. 49 * 50 * An AccountId with an aliasKey can be used just like a normal AccountId for the purposes of queries and 51 * transactions, however most queries and transactions involving such an AccountId won't work until Hbar has 52 * been transferred to the aliasKey account. 53 * 54 * There is no record in the Hedera network of an account associated with a given aliasKey 55 * until an amount of Hbar is transferred to the account. The moment that Hbar is transferred to that aliasKey 56 * AccountId is the moment that that account actually begins to exist in the Hedera ledger. 57 */ 58 59 println("Creating a new account") 60 61 key, err := hedera.GeneratePrivateKey() 62 if err != nil { 63 panic(fmt.Sprintf("%v : error generating private key", err)) 64 } 65 publicKey := key.PublicKey() 66 67 // Assuming that the target shard and realm are known. 68 // For now they are virtually always 0 and 0. 69 aliasAccountID := publicKey.ToAccountID(0, 0) 70 71 println("New account ID:", aliasAccountID.String()) 72 println("Just the key:", aliasAccountID.AliasKey.String()) 73 74 /* 75 * 76 * Note that no queries or transactions have taken place yet. 77 * This account "creation" process is entirely local. 78 * 79 * AccountId.fromString() can construct an AccountId with an aliasKey. 80 * It expects a string of the form 0.0.123 in the case of a normal AccountId, or of the form 81 * 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777 82 * in the case of an AccountId with aliasKey. Note the prefix of "0.0." to indicate the shard and realm. 83 * 84 * If the shard and realm are known, you may use PublicKeyFromString() then PublicKey.toAccountId() to construct the 85 * aliasKey AccountID 86 * fromStr, err := hedera.AccountIDFromString("0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777") 87 * publicKey2, err := hedera.PublicKeyFromString("302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777") 88 * fromKeyString := publicKey2.ToAccountID(0,0) 89 */ 90 91 println("Transferring some Hbar to the new account") 92 resp, err := hedera.NewTransferTransaction(). 93 AddHbarTransfer(client.GetOperatorAccountID(), hedera.NewHbar(1).Negated()). 94 AddHbarTransfer(*aliasAccountID, hedera.NewHbar(1)). 95 Execute(client) 96 if err != nil { 97 panic(fmt.Sprintf("%v : error executing transfer transaction", err)) 98 } 99 100 receipt, err := resp.GetReceipt(client) 101 println(receipt.Status.String()) 102 if receipt.AccountID != nil { 103 println(receipt.AccountID.String()) 104 } 105 if err != nil { 106 panic(fmt.Sprintf("%v : error getting transfer transaction receipt", err)) 107 } 108 109 balance, err := hedera.NewAccountBalanceQuery(). 110 SetAccountID(*aliasAccountID). 111 Execute(client) 112 if err != nil { 113 panic(fmt.Sprintf("%v : error retrieving balance", err)) 114 } 115 116 println("Balance of the new account:", balance.Hbars.String()) 117 118 info, err := hedera.NewAccountInfoQuery(). 119 SetAccountID(*aliasAccountID). 120 Execute(client) 121 if err != nil { 122 panic(fmt.Sprintf("%v : error retrieving account info", err)) 123 } 124 125 /* 126 * Note that once an account exists in the ledger, it is assigned a normal AccountId, which can be retrieved 127 * via an AccountInfoQuery. 128 * 129 * Users may continue to refer to the account by its aliasKey AccountId, but they may also 130 * now refer to it by its normal AccountId 131 */ 132 133 println("New account info:") 134 println("The normal account ID:", info.AccountID.String()) 135 println("The alias key:", info.AliasKey.String()) 136 println("Example complete") 137 err = client.Close() 138 if err != nil { 139 panic(fmt.Sprintf("%v : error closing client", err)) 140 } 141 }