forked from hashgraph/hedera-sdk-go
/
main.go
131 lines (108 loc) · 3.63 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
package main
import (
"math/rand"
"os"
"strconv"
"time"
"github.com/hashgraph/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)
//generate new submit key
submitKey, err := hedera.GeneratePrivateKey()
if err != nil {
println(err.Error(), ": error generating PrivateKey")
return
}
println("acc", client.GetOperatorAccountID().String())
// Create new topic ID
transactionResponse, err := hedera.NewTopicCreateTransaction().
// You don't need any of this to create a topic
// If key is not set all submissions are allowed
SetTransactionMemo("HCS topic with submit key").
// Access control for TopicSubmitMessage.
// If unspecified, no access control is performed, all submissions are allowed.
SetSubmitKey(submitKey.PublicKey()).
Execute(client)
if err != nil {
println(err.Error(), ": error creating topic")
return
}
// Get receipt
transactionReceipt, err := transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving topic create transaction receipt")
return
}
// Get topic ID from receipt
topicID := *transactionReceipt.TopicID
println("Created new topic", topicID.String(), "with ED25519 submitKey of", submitKey.String())
time.Sleep(5 * time.Second)
// Setup a mirror client to print out messages as we receive them
_, err = hedera.NewTopicMessageQuery().
// Sets for which topic
SetTopicID(topicID).
// Set when the query starts
SetStartTime(time.Unix(0, 0)).
// What to do when messages are received
Subscribe(client, func(message hedera.TopicMessage) {
// Print out the timestamp and the message
println(message.ConsensusTimestamp.String(), " received topic message:", string(message.Contents))
})
if err != nil {
println(err.Error(), ": error subscribing")
return
}
for i := 0; i < 3; i++ {
message := "random message " + strconv.Itoa(rand.Int())
println("Publishing message:", message)
// Prepare a message send transaction that requires a submit key from "somewhere else"
submitTx, err := hedera.NewTopicMessageSubmitTransaction().
// Sets the topic ID we want to send to
SetTopicID(topicID).
// Sets the message
SetMessage([]byte(message)).
FreezeWith(client)
if err != nil {
println(err.Error(), ": error freezing topic message submit transaction")
return
}
// Sign with that submit key we gave the topic
submitTx.Sign(submitKey)
// Now actually submit the transaction
submitTxResponse, err := submitTx.Execute(client)
if err != nil {
println(err.Error(), ": error executing topic message submit transaction")
return
}
// Get the receipt to ensure there were no errors
_, err = submitTxResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error retrieving topic message submit transaction receipt")
return
}
// Wait a bit for it to propagate
time.Sleep(2 * time.Second)
}
}