github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/consensus_pub_sub_with_submit_key/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "math/rand" 6 "os" 7 "strconv" 8 "time" 9 10 "github.com/hashgraph/hedera-sdk-go/v2" 11 ) 12 13 func main() { 14 var client *hedera.Client 15 var err error 16 17 // Retrieving network type from environment variable HEDERA_NETWORK 18 client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 19 if err != nil { 20 panic(fmt.Sprintf("%v : error creating client", err)) 21 } 22 23 // Retrieving operator ID from environment variable OPERATOR_ID 24 operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) 25 if err != nil { 26 panic(fmt.Sprintf("%v : error converting string to AccountID", err)) 27 } 28 29 // Retrieving operator key from environment variable OPERATOR_KEY 30 operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) 31 if err != nil { 32 panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) 33 } 34 35 // Setting the client operator ID and key 36 client.SetOperator(operatorAccountID, operatorKey) 37 38 //generate new submit key 39 submitKey, err := hedera.GeneratePrivateKey() 40 if err != nil { 41 panic(fmt.Sprintf("%v : error generating PrivateKey", err)) 42 } 43 44 println("acc", client.GetOperatorAccountID().String()) 45 46 // Create new topic ID 47 transactionResponse, err := hedera.NewTopicCreateTransaction(). 48 // You don't need any of this to create a topic 49 // If key is not set all submissions are allowed 50 SetTransactionMemo("HCS topic with submit key"). 51 // Access control for TopicSubmitMessage. 52 // If unspecified, no access control is performed, all submissions are allowed. 53 SetSubmitKey(submitKey.PublicKey()). 54 Execute(client) 55 if err != nil { 56 panic(fmt.Sprintf("%v : error creating topic", err)) 57 } 58 59 // Get receipt 60 transactionReceipt, err := transactionResponse.GetReceipt(client) 61 62 if err != nil { 63 panic(fmt.Sprintf("%v : error retrieving topic create transaction receipt", err)) 64 } 65 66 // Get topic ID from receipt 67 topicID := *transactionReceipt.TopicID 68 69 println("Created new topic", topicID.String(), "with ED25519 submitKey of", submitKey.String()) 70 71 time.Sleep(5 * time.Second) 72 73 // Setup a mirror client to print out messages as we receive them 74 _, err = hedera.NewTopicMessageQuery(). 75 // Sets for which topic 76 SetTopicID(topicID). 77 // Set when the query starts 78 SetStartTime(time.Unix(0, 0)). 79 // What to do when messages are received 80 Subscribe(client, func(message hedera.TopicMessage) { 81 // Print out the timestamp and the message 82 println(message.ConsensusTimestamp.String(), " received topic message:", string(message.Contents)) 83 }) 84 if err != nil { 85 panic(fmt.Sprintf("%v : error subscribing", err)) 86 } 87 88 for i := 0; i < 3; i++ { 89 message := "random message " + strconv.Itoa(rand.Int()) 90 91 println("Publishing message:", message) 92 93 // Prepare a message send transaction that requires a submit key from "somewhere else" 94 submitTx, err := hedera.NewTopicMessageSubmitTransaction(). 95 // Sets the topic ID we want to send to 96 SetTopicID(topicID). 97 // Sets the message 98 SetMessage([]byte(message)). 99 FreezeWith(client) 100 if err != nil { 101 panic(fmt.Sprintf("%v : error freezing topic message submit transaction", err)) 102 } 103 104 // Sign with that submit key we gave the topic 105 submitTx.Sign(submitKey) 106 107 // Now actually submit the transaction 108 submitTxResponse, err := submitTx.Execute(client) 109 if err != nil { 110 panic(fmt.Sprintf("%v : error executing topic message submit transaction", err)) 111 } 112 113 // Get the receipt to ensure there were no errors 114 _, err = submitTxResponse.GetReceipt(client) 115 if err != nil { 116 panic(fmt.Sprintf("%v : error retrieving topic message submit transaction receipt", err)) 117 } 118 119 // Wait a bit for it to propagate 120 time.Sleep(2 * time.Second) 121 } 122 }