github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/account_create_with_hts/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 11 client, err := hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 12 if err != nil { 13 panic(fmt.Sprintf("%v : error creating client", err)) 14 } 15 16 operatorId, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) 17 if err != nil { 18 panic(fmt.Sprintf("%v : error converting string to AccountID", err)) 19 } 20 21 // Retrieving operator key from environment variable OPERATOR_KEY 22 operatorKey, err := hedera.PrivateKeyFromStringEd25519(os.Getenv("OPERATOR_KEY")) 23 if err != nil { 24 panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) 25 } 26 27 // Setting the client operator ID and key 28 client.SetOperator(operatorId, operatorKey) 29 30 supplyKey, err := hedera.PrivateKeyGenerateEcdsa() 31 if err != nil { 32 panic(fmt.Sprintf("%v : error creating supply key", err)) 33 } 34 freezeKey, err := hedera.PrivateKeyGenerateEcdsa() 35 if err != nil { 36 panic(fmt.Sprintf("%v : error creating freeze key", err)) 37 } 38 wipeKey, err := hedera.PrivateKeyGenerateEcdsa() 39 if err != nil { 40 panic(fmt.Sprintf("%v : error creating wipe key", err)) 41 } 42 /** 43 * Example 1 44 * 45 * Step 1 46 * 47 * Create an NFT using the Hedera Token Service 48 */ 49 fmt.Println("Example 1") 50 // IPFS content identifiers for the NFT metadata 51 cid := []string{"QmNPCiNA3Dsu3K5FxDPMG5Q3fZRwVTg14EXA92uqEeSRXn", 52 "QmZ4dgAgt8owvnULxnKxNe8YqpavtVCXmc1Lt2XajFpJs9", 53 "QmPzY5GxevjyfMUF5vEAjtyRoigzWp47MiKAtLBduLMC1T", 54 "Qmd3kGgSrAwwSrhesYcY7K54f3qD7MDo38r7Po2dChtQx5", 55 "QmWgkKz3ozgqtnvbCLeh7EaR1H8u5Sshx3ZJzxkcrT3jbw"} 56 // Creating the transaction for token creation 57 nftCreateTransaction, err := hedera.NewTokenCreateTransaction(). 58 SetTokenName("HIP-542 Example Collection").SetTokenSymbol("HIP-542"). 59 SetTokenType(hedera.TokenTypeNonFungibleUnique).SetDecimals(0). 60 SetInitialSupply(0).SetMaxSupply(int64(len(cid))). 61 SetTreasuryAccountID(operatorId).SetSupplyType(hedera.TokenSupplyTypeFinite). 62 SetAdminKey(operatorKey).SetFreezeKey(freezeKey).SetWipeKey(wipeKey).SetSupplyKey(supplyKey).FreezeWith(client) 63 if err != nil { 64 panic(fmt.Sprintf("%v : error creating token transaction", err)) 65 } 66 // Sign the transaction with the operator key 67 nftSignTransaction := nftCreateTransaction.Sign(operatorKey) 68 // Submit the transaction to the Hedera network 69 nftCreateSubmit, err := nftSignTransaction.Execute(client) 70 if err != nil { 71 panic(fmt.Sprintf("%v : error submitting transaction", err)) 72 } 73 // Get transaction receipt information 74 nftCreateReceipt, err := nftCreateSubmit.GetReceipt(client) 75 if err != nil { 76 panic(fmt.Sprintf("%v : error receiving receipt", err)) 77 } 78 // Get token id from the transaction 79 nftTokenID := *nftCreateReceipt.TokenID 80 fmt.Println("Created NFT with token id: ", nftTokenID) 81 82 /** 83 * Step 2 84 * 85 * Mint the NFT 86 */ 87 88 nftCollection := []hedera.TransactionReceipt{} 89 90 for i, s := range cid { 91 mintTransaction, err := hedera.NewTokenMintTransaction().SetTokenID(nftTokenID).SetMetadata([]byte(s)).FreezeWith(client) 92 if err != nil { 93 panic(fmt.Sprintf("%v : error creating mint transaction", err)) 94 } 95 mintTransactionSubmit, err := mintTransaction.Sign(supplyKey).Execute(client) 96 if err != nil { 97 panic(fmt.Sprintf("%v : error submitting transaction", err)) 98 } 99 receipt, err := mintTransactionSubmit.GetReceipt(client) 100 if err != nil { 101 panic(fmt.Sprintf("%v : error receiving receipt", err)) 102 } 103 nftCollection = append(nftCollection, receipt) 104 fmt.Println("Created NFT ", nftTokenID.String(), " with serial: ", nftCollection[i].SerialNumbers[0]) 105 } 106 exampleNftId := nftTokenID.Nft(nftCollection[0].SerialNumbers[0]) 107 /** 108 * Step 3 109 * 110 * Create an ECDSA public key alias 111 */ 112 113 fmt.Println("Creating new account...") 114 privateKey, err := hedera.PrivateKeyGenerateEcdsa() 115 if err != nil { 116 panic(fmt.Sprintf("%v : error generating private key", err)) 117 } 118 publicKey := privateKey.PublicKey() 119 // Assuming that the target shard and realm are known. 120 // For now they are virtually always 0 and 0. 121 aliasAccountId := publicKey.ToAccountID(0, 0) 122 fmt.Println("New account ID: ", aliasAccountId) 123 fmt.Println("Just the aliasKey: ", aliasAccountId.AliasKey) 124 125 /** 126 * Step 4 127 * 128 * Tranfer the NFT to the public key alias using the transfer transaction 129 */ 130 nftTransferTransaction, err := hedera.NewTransferTransaction().AddNftTransfer(exampleNftId, operatorId, *aliasAccountId).FreezeWith(client) 131 if err != nil { 132 panic(fmt.Sprintf("%v : error creating transaction", err)) 133 } 134 // Sign the transaction with the operator key 135 nftTransferTransactionSign := nftTransferTransaction.Sign(operatorKey) 136 // Submit the transaction to the Hedera network 137 nftTransferTransactionSubmit, err := nftTransferTransactionSign.Execute(client) 138 if err != nil { 139 panic(fmt.Sprintf("%v : error submitting transaction", err)) 140 } 141 // Get transaction receipt information here 142 fmt.Println(nftTransferTransactionSubmit.GetReceipt(client)) 143 144 /** 145 * Step 5 146 * 147 * Return the new account ID in the child record 148 */ 149 150 //Returns the info for the specified NFT id 151 nftInfo, err := hedera.NewTokenNftInfoQuery().SetNftID(exampleNftId).Execute(client) 152 if err != nil { 153 panic(fmt.Sprintf("%v : error info query transaction", err)) 154 } 155 nftOwnerAccountId := nftInfo[0].AccountID 156 fmt.Println("Current owner account id: ", nftOwnerAccountId) 157 158 /** 159 * Step 6 160 * 161 * Show the new account ID owns the NFT 162 */ 163 accountInfo, err := hedera.NewAccountInfoQuery().SetAccountID(*aliasAccountId).Execute(client) 164 if err != nil { 165 panic(fmt.Sprintf("%v : error account info query", err)) 166 } 167 fmt.Println("The normal account ID of the given alias ", accountInfo.AccountID) 168 169 if nftOwnerAccountId == accountInfo.AccountID { 170 fmt.Println("The NFT owner accountId matches the accountId created with the HTS") 171 } else { 172 fmt.Println("The two account IDs does not match") 173 } 174 175 /** 176 * Example 2 177 * 178 * Step 1 179 * 180 * Create a fungible HTS token using the Hedera Token Service 181 */ 182 fmt.Println("Example 2") 183 184 tokenCreateTransaction, err := hedera.NewTokenCreateTransaction().SetTokenName("HIP-542 Token"). 185 SetTokenSymbol("H542").SetTokenType(hedera.TokenTypeFungibleCommon).SetTreasuryAccountID(operatorId). 186 SetInitialSupply(10000).SetDecimals(2).SetAutoRenewAccount(operatorId).FreezeWith(client) 187 if err != nil { 188 panic(fmt.Sprintf("%v : error creating transaction", err)) 189 } 190 // Sign the transaction with the operator key 191 tokenCreateTransactionSign := tokenCreateTransaction.Sign(operatorKey) 192 // Submit the transaction to the Hedera network 193 tokenCreateTransactionSubmit, err := tokenCreateTransactionSign.Execute(client) 194 if err != nil { 195 panic(fmt.Sprintf("%v : error submitting transaction", err)) 196 } 197 198 // Get transaction receipt information 199 tokenCreateTransactionReceipt, err := tokenCreateTransactionSubmit.GetReceipt(client) 200 if err != nil { 201 panic(fmt.Sprintf("%v : error retrieving receipt", err)) 202 } 203 tokenId := *tokenCreateTransactionReceipt.TokenID 204 fmt.Println("Created token with token id: ", tokenId) 205 206 /** 207 * Step 2 208 * 209 * Create an ECDSA public key alias 210 */ 211 privateKey2, err := hedera.PrivateKeyGenerateEcdsa() 212 if err != nil { 213 panic(fmt.Sprintf("%v : error generating private key", err)) 214 } 215 publicKey2 := privateKey2.PublicKey() 216 // Assuming that the target shard and realm are known. 217 // For now they are virtually always 0 and 0. 218 aliasAccountId2 := *publicKey2.ToAccountID(0, 0) 219 fmt.Println("New account ID: ", aliasAccountId2) 220 fmt.Println("Just the aliasKey: ", aliasAccountId2.AliasKey) 221 222 /** 223 * Step 3 224 * 225 * Transfer the fungible token to the public key alias 226 */ 227 228 tokenTransferTransaction, err := hedera.NewTransferTransaction(). 229 AddTokenTransfer(tokenId, operatorId, -10).AddTokenTransfer(tokenId, aliasAccountId2, 10). 230 FreezeWith(client) 231 if err != nil { 232 panic(fmt.Sprintf("%v : error creating transaction", err)) 233 } 234 // Sign the transaction with the operator key 235 tokenTransferTransactionSign := tokenTransferTransaction.Sign(operatorKey) 236 // Submit the transaction to the Hedera network 237 tokenTransferTransactionSubmit, err := tokenTransferTransactionSign.Execute(client) 238 if err != nil { 239 panic(fmt.Sprintf("%v : error submitting transaction", err)) 240 } 241 // Get transaction receipt information 242 fmt.Println(tokenTransferTransactionSubmit.GetReceipt(client)) 243 244 /** 245 * Step 4 246 * 247 * Return the new account ID in the child record 248 */ 249 250 accountId2Info, err := hedera.NewAccountInfoQuery().SetAccountID(aliasAccountId2).Execute(client) 251 if err != nil { 252 panic(fmt.Sprintf("%v : error executing acount info query", err)) 253 } 254 accountId2 := accountId2Info.AccountID 255 fmt.Println("The normal account ID of the given alias: ", accountId2) 256 257 /** 258 * Step 5 259 * 260 * Show the new account ID owns the fungible token 261 */ 262 263 accountBalances, err := hedera.NewAccountBalanceQuery().SetAccountID(aliasAccountId2).Execute(client) 264 if err != nil { 265 panic(fmt.Sprintf("%v : error receiving account balance", err)) 266 } 267 268 tokenBalanceAccountId2 := accountBalances.Tokens.Get(tokenId) 269 if tokenBalanceAccountId2 == 10 { 270 fmt.Println(`Account is created succesfully using HTS "TransferTransaction"`) 271 } else { 272 fmt.Println("Creating account with HTS using public key alias failed") 273 } 274 275 }