github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/topic_with_admin_key/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 // Setting the client operator ID and key 33 client.SetOperator(operatorAccountID, operatorKey) 34 35 initialAdminKeys := make([]hedera.PrivateKey, 3) 36 37 // Generating the keys for the KeyList 38 for i := range initialAdminKeys { 39 key, err := hedera.GeneratePrivateKey() 40 if err != nil { 41 panic(fmt.Sprintf("%v : error generating PrivateKey", err)) 42 } 43 initialAdminKeys[i] = key 44 } 45 46 // Creating KeyList with a threshold 2 47 keyList := hedera.KeyListWithThreshold(2) 48 for _, key := range initialAdminKeys { 49 keyList.Add(key.PublicKey()) 50 } 51 52 topicTx, err := hedera.NewTopicCreateTransaction(). 53 SetTopicMemo("demo topic"). 54 // Access control for UpdateTopicTransaction/DeleteTopicTransaction. 55 // Anyone can increase the topic's expirationTime via UpdateTopicTransaction, regardless of the adminKey. 56 // If no adminKey is specified, UpdateTopicTransaction may only be used to extend the topic's expirationTime, 57 // and DeleteTopicTransaction is disallowed. 58 SetAdminKey(keyList). 59 FreezeWith(client) 60 if err != nil { 61 panic(fmt.Sprintf("%v : error freezing topic create transaction", err)) 62 } 63 64 // Signing ConsensusTopicCreateTransaction with initialAdminKeys 65 for i := 0; i < 2; i++ { 66 println("Signing ConsensusTopicCreateTransaction with key ", initialAdminKeys[i].String()) 67 topicTx.Sign(initialAdminKeys[i]) 68 } 69 70 // Executing ConsensusTopicCreateTransaction 71 response, err := topicTx.Execute(client) 72 if err != nil { 73 panic(fmt.Sprintf("%v : error creating topic", err)) 74 } 75 76 // Make sure it executed properly 77 receipt, err := response.GetReceipt(client) 78 if err != nil { 79 panic(fmt.Sprintf("%v : error retrieving topic creation receipt", err)) 80 } 81 82 // Get the topic ID out of the receipt 83 topicID := *receipt.TopicID 84 85 println("Created new topic ", topicID.String(), " with 2-of-3 threshold key as adminKey.") 86 87 newAdminKeys := make([]hedera.PrivateKey, 4) 88 89 // Generating the keys 90 for i := range newAdminKeys { 91 key, err := hedera.GeneratePrivateKey() 92 if err != nil { 93 panic(fmt.Sprintf("%v : error generating PrivateKey", err)) 94 } 95 newAdminKeys[i] = key 96 } 97 98 // Creating KeyList with a threshold 3 99 keyList = hedera.KeyListWithThreshold(3) 100 for _, key := range newAdminKeys { 101 keyList.Add(key.PublicKey()) 102 } 103 104 topicUpdate, err := hedera.NewTopicUpdateTransaction(). 105 SetTopicID(topicID). 106 SetTopicMemo("updated topic demo"). 107 // Updating with new KeyList here 108 SetAdminKey(keyList). 109 FreezeWith(client) 110 if err != nil { 111 panic(fmt.Sprintf("%v : error freezing topic update transaction", err)) 112 } 113 114 // Have to sign with the initial admin keys first 115 for i := 0; i < 2; i++ { 116 println("Signing ConsensusTopicCreateTransaction with initial admin key ", initialAdminKeys[i].String()) 117 topicUpdate.Sign(initialAdminKeys[i]) 118 } 119 120 // Then the new ones we updated the topic with 121 for i := 0; i < 3; i++ { 122 println("Signing ConsensusTopicCreateTransaction with new admin key ", newAdminKeys[i].String()) 123 topicUpdate.Sign(newAdminKeys[i]) 124 } 125 126 // Now to execute the topic update transaction 127 response, err = topicUpdate.Execute(client) 128 if err != nil { 129 panic(fmt.Sprintf("%v : error updating topic", err)) 130 } 131 132 // Make sure the transaction ran properly 133 receipt, err = response.GetReceipt(client) 134 if err != nil { 135 panic(fmt.Sprintf("%v : error retrieving topic update receipt", err)) 136 } 137 138 println("Updated topic ", topicID.String(), " with 3-of-4 threshold key as adminKey") 139 140 // Make sure everything worked by checking the topic memo 141 topicInfo, err := hedera.NewTopicInfoQuery(). 142 SetTopicID(topicID). 143 Execute(client) 144 if err != nil { 145 panic(fmt.Sprintf("%v : error executing topic info query", err)) 146 } 147 148 // Should be "updated topic demo" 149 println(topicInfo.TopicMemo) 150 }