github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/token_update_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 /** 11 * @summary HIP-540 https://hips.hedera.com/hip/hip-540 12 * @description Change or remove existing keys from a token 13 */ 14 func main() { 15 var client *hedera.Client 16 var err error 17 18 // Retrieving network type from environment variable HEDERA_NETWORK 19 client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 20 if err != nil { 21 panic(fmt.Sprintf("%v : error creating client", err)) 22 } 23 24 // Retrieving operator ID from environment variable OPERATOR_ID 25 operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) 26 if err != nil { 27 panic(fmt.Sprintf("%v : error converting string to AccountID", err)) 28 } 29 30 // Retrieving operator key from environment variable OPERATOR_KEY 31 operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) 32 if err != nil { 33 panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) 34 } 35 36 // Setting the client operator ID and key 37 client.SetOperator(operatorAccountID, operatorKey) 38 39 // Create admin key 40 adminKey, err := hedera.PrivateKeyGenerateEd25519() 41 if err != nil { 42 panic(fmt.Sprintf("%v : error creating admin key", err)) 43 } 44 fmt.Println("create wipe key: ", adminKey.String()) 45 46 // Create supply key 47 supplyKey, err := hedera.PrivateKeyGenerateEd25519() 48 if err != nil { 49 panic(fmt.Sprintf("%v : error creating supply key", err)) 50 } 51 fmt.Println("create supply key: ", supplyKey.String()) 52 53 // Create wipe key 54 wipeKey, err := hedera.PrivateKeyGenerateEd25519() 55 if err != nil { 56 panic(fmt.Sprintf("%v : error creating supply key", err)) 57 } 58 fmt.Println("create wipe key: ", supplyKey.String()) 59 60 // Create the token 61 tx, err := hedera.NewTokenCreateTransaction(). 62 SetTokenName("ffff"). 63 SetTokenSymbol("F"). 64 SetDecimals(3). 65 SetTokenType(hedera.TokenTypeFungibleCommon). 66 SetInitialSupply(1000000). 67 SetTreasuryAccountID(client.GetOperatorAccountID()). 68 SetFreezeDefault(false). 69 SetSupplyKey(supplyKey). 70 SetWipeKey(wipeKey). 71 SetAdminKey(adminKey). 72 FreezeWith(client) 73 if err != nil { 74 panic(fmt.Sprintf("%v : error creating token", err)) 75 } 76 resp, err := tx.Sign(adminKey).Execute(client) 77 if err != nil { 78 panic(fmt.Sprintf("%v : error creating token", err)) 79 } 80 81 receipt, err := resp.SetValidateStatus(true).GetReceipt(client) 82 if err != nil { 83 panic(fmt.Sprintf("%v : error creating token", err)) 84 } 85 tokenID := receipt.TokenID 86 fmt.Println("created token: ", tokenID) 87 88 // Query the token info after creation 89 info, err := hedera.NewTokenInfoQuery(). 90 SetTokenID(*receipt.TokenID). 91 Execute(client) 92 if err != nil { 93 panic(fmt.Sprintf("%v : error getting token info", err)) 94 } 95 fmt.Println("token's supply key after creation: ", info.SupplyKey) 96 fmt.Println("token's wipe key after creation: ", info.WipeKey) 97 fmt.Println("token's admin key after creation: ", info.AdminKey) 98 99 removeWipeKeyFullValidation(client, *receipt.TokenID, adminKey) 100 newSupplyKey := updateSupplyKeyFullValidation(client, *receipt.TokenID, supplyKey) 101 removeSupplyKeyNoValidation(client, *receipt.TokenID, newSupplyKey) 102 removeAdminKeyNoValidation(client, *receipt.TokenID, adminKey) 103 } 104 105 // With "full" verification mode, our required key 106 // structure is a 1/2 threshold with components: 107 // - Admin key 108 // - A 2/2 list including the role key and its replacement key 109 func removeWipeKeyFullValidation(client *hedera.Client, tokenID hedera.TokenID, adminKey hedera.PrivateKey) { 110 // Remove wipe key by setting it to a empty key list 111 tx, err := hedera.NewTokenUpdateTransaction(). 112 SetTokenID(tokenID). 113 SetWipeKey(hedera.NewKeyList()). 114 SetKeyVerificationMode(hedera.FULL_VALIDATION). 115 FreezeWith(client) 116 if err != nil { 117 panic(fmt.Sprintf("%v : error updating token", err)) 118 } 119 resp, err := tx.Sign(adminKey).Execute(client) 120 if err != nil { 121 panic(fmt.Sprintf("%v : error updating token", err)) 122 } 123 _, err = resp.SetValidateStatus(true).GetReceipt(client) 124 if err != nil { 125 panic(fmt.Sprintf("%v : error updating token", err)) 126 } 127 128 // Query the token info to get the wipe key after removal 129 info, err := hedera.NewTokenInfoQuery(). 130 SetTokenID(tokenID). 131 Execute(client) 132 if err != nil { 133 panic(fmt.Sprintf("%v : error getting token info", err)) 134 } 135 fmt.Println("token's wipe key after removal: ", info.WipeKey) 136 } 137 138 func updateSupplyKeyFullValidation(client *hedera.Client, tokenID hedera.TokenID, oldSupplyKey hedera.PrivateKey) hedera.PrivateKey { 139 newSupplyKey, _ := hedera.GeneratePrivateKey() 140 141 // Update supply key by setting it to a new key 142 tx, err := hedera.NewTokenUpdateTransaction(). 143 SetTokenID(tokenID). 144 SetSupplyKey(newSupplyKey.PublicKey()). 145 SetKeyVerificationMode(hedera.FULL_VALIDATION). 146 FreezeWith(client) 147 if err != nil { 148 panic(fmt.Sprintf("%v : error updating token", err)) 149 } 150 151 // Sign with old and new supply keys 152 _, err = newSupplyKey.SignTransaction(&tx.Transaction) 153 if err != nil { 154 panic(fmt.Sprintf("%v : error signing tx", err)) 155 } 156 _, err = oldSupplyKey.SignTransaction(&tx.Transaction) 157 if err != nil { 158 panic(fmt.Sprintf("%v : error signing tx", err)) 159 } 160 161 resp, err := tx.Execute(client) 162 if err != nil { 163 panic(fmt.Sprintf("%v : error updating token", err)) 164 } 165 _, err = resp.SetValidateStatus(true).GetReceipt(client) 166 if err != nil { 167 panic(fmt.Sprintf("%v : error updating token", err)) 168 } 169 170 // Query the token info to get the supply key after update 171 info, err := hedera.NewTokenInfoQuery(). 172 SetTokenID(tokenID). 173 Execute(client) 174 if err != nil { 175 panic(fmt.Sprintf("%v : error getting token info", err)) 176 } 177 fmt.Println("token's supply key after update: ", info.SupplyKey) 178 179 return newSupplyKey 180 } 181 182 func removeSupplyKeyNoValidation(client *hedera.Client, tokenID hedera.TokenID, oldSupplyKey hedera.PrivateKey) { 183 zeroNewKey, _ := hedera.ZeroKey() 184 185 // Remove supply key by setting it to a zero key 186 tx, err := hedera.NewTokenUpdateTransaction(). 187 SetTokenID(tokenID). 188 SetSupplyKey(zeroNewKey). 189 SetKeyVerificationMode(hedera.NO_VALIDATION). 190 FreezeWith(client) 191 if err != nil { 192 panic(fmt.Sprintf("%v : error updating token", err)) 193 } 194 195 // Sign with old supply key 196 resp, err := tx.Sign(oldSupplyKey).Execute(client) 197 if err != nil { 198 panic(fmt.Sprintf("%v : error updating token", err)) 199 } 200 _, err = resp.SetValidateStatus(true).GetReceipt(client) 201 if err != nil { 202 panic(fmt.Sprintf("%v : error updating token", err)) 203 } 204 205 // Query the token info to get the supply key after removal 206 info, err := hedera.NewTokenInfoQuery(). 207 SetTokenID(tokenID). 208 Execute(client) 209 if err != nil { 210 panic(fmt.Sprintf("%v : error getting token info", err)) 211 } 212 213 newSupplyKey := info.SupplyKey.(hedera.PublicKey) 214 fmt.Println("token's supply key after zero out: ", newSupplyKey.StringRaw()) 215 } 216 217 func removeAdminKeyNoValidation(client *hedera.Client, tokenID hedera.TokenID, oldAdminKey hedera.PrivateKey) { 218 // Remove admin key by setting it to a zero key 219 tx, err := hedera.NewTokenUpdateTransaction(). 220 SetTokenID(tokenID). 221 SetAdminKey(hedera.NewKeyList()). 222 SetKeyVerificationMode(hedera.NO_VALIDATION). 223 FreezeWith(client) 224 if err != nil { 225 panic(fmt.Sprintf("%v : error updating token", err)) 226 } 227 228 // Sign with old supply key 229 resp, err := tx.Sign(oldAdminKey).Execute(client) 230 if err != nil { 231 panic(fmt.Sprintf("%v : error updating token", err)) 232 } 233 _, err = resp.SetValidateStatus(true).GetReceipt(client) 234 if err != nil { 235 panic(fmt.Sprintf("%v : error updating token", err)) 236 } 237 238 // Query the token info to get the admin key after removal 239 info, err := hedera.NewTokenInfoQuery(). 240 SetTokenID(tokenID). 241 Execute(client) 242 if err != nil { 243 panic(fmt.Sprintf("%v : error getting token info", err)) 244 } 245 246 fmt.Println("token's admin key after removal: ", info.AdminKey) 247 }