forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (134 loc) · 4.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"os"
"github.com/arhtur007/hedera-sdk-go/v2"
)
func main() {
var client *hedera.Client
var err error
// Retrieving network type from environment variable HEDERA_NETWORK
client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
if err != nil {
println(err.Error(), ": error creating client")
return
}
// Retrieving operator ID from environment variable OPERATOR_ID
operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
println(err.Error(), ": error converting string to AccountID")
return
}
// Retrieving operator key from environment variable OPERATOR_KEY
operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
println(err.Error(), ": error converting string to PrivateKey")
return
}
// Setting the client operator ID and key
client.SetOperator(operatorAccountID, operatorKey)
initialAdminKeys := make([]hedera.PrivateKey, 3)
// Generating the keys for the KeyList
for i, _ := range initialAdminKeys {
key, err := hedera.GeneratePrivateKey()
if err != nil {
println(err.Error(), ": error generating PrivateKey")
return
}
initialAdminKeys[i] = key
}
// Creating KeyList with a threshold 2
keyList := hedera.KeyListWithThreshold(2)
for _, key := range initialAdminKeys {
keyList.Add(key.PublicKey())
}
topicTx, err := hedera.NewTopicCreateTransaction().
SetTopicMemo("demo topic").
// Access control for UpdateTopicTransaction/DeleteTopicTransaction.
// Anyone can increase the topic's expirationTime via UpdateTopicTransaction, regardless of the adminKey.
// If no adminKey is specified, UpdateTopicTransaction may only be used to extend the topic's expirationTime,
// and DeleteTopicTransaction is disallowed.
SetAdminKey(keyList).
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing topic create transaction")
return
}
// Signing ConsensusTopicCreateTransaction with initialAdminKeys
for i := 0; i < 2; i++ {
println("Signing ConsensusTopicCreateTransaction with key ", initialAdminKeys[i].String())
topicTx.Sign(initialAdminKeys[i])
}
// Executing ConsensusTopicCreateTransaction
response, err := topicTx.Execute(client)
if err != nil {
println(err.Error(), ": error creating topic")
return
}
// Make sure it executed properly
receipt, err := response.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving topic creation receipt")
return
}
// Get the topic ID out of the receipt
topicID := *receipt.TopicID
println("Created new topic ", topicID.String(), " with 2-of-3 threshold key as adminKey.")
newAdminKeys := make([]hedera.PrivateKey, 4)
// Generating the keys
for i, _ := range newAdminKeys {
key, err := hedera.GeneratePrivateKey()
if err != nil {
println(err.Error(), ": error generating PrivateKey")
return
}
newAdminKeys[i] = key
}
// Creating KeyList with a threshold 3
keyList = hedera.KeyListWithThreshold(3)
for _, key := range newAdminKeys {
keyList.Add(key.PublicKey())
}
topicUpdate, err := hedera.NewTopicUpdateTransaction().
SetTopicID(topicID).
SetTopicMemo("updated topic demo").
// Updating with new KeyList here
SetAdminKey(keyList).
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing topic update transaction")
return
}
// Have to sign with the initial admin keys first
for i := 0; i < 2; i++ {
println("Signing ConsensusTopicCreateTransaction with initial admin key ", initialAdminKeys[i].String())
topicUpdate.Sign(initialAdminKeys[i])
}
// Then the new ones we updated the topic with
for i := 0; i < 3; i++ {
println("Signing ConsensusTopicCreateTransaction with new admin key ", newAdminKeys[i].String())
topicUpdate.Sign(newAdminKeys[i])
}
// Now to execute the topic update transaction
response, err = topicUpdate.Execute(client)
if err != nil {
println(err.Error(), ": error updating topic")
return
}
// Make sure the transaction ran properly
receipt, err = response.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving topic update receipt")
return
}
println("Updated topic ", topicID.String(), " with 3-of-4 threshold key as adminKey")
// Make sure everything worked by checking the topic memo
topicInfo, err := hedera.NewTopicInfoQuery().
SetTopicID(topicID).
Execute(client)
if err != nil {
println(err.Error(), ": error executing topic info query")
return
}
// Should be "updated topic demo"
println(topicInfo.TopicMemo)
}