forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (80 loc) · 2.36 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
package main
import (
"github.com/hashgraph/hedera-sdk-go"
"os"
)
func main() {
var client *hedera.Client
var err error
if os.Getenv("HEDERA_NETWORK") == "previewnet" {
client = hedera.ClientForPreviewnet()
} else {
client, err = hedera.ClientFromConfigFile(os.Getenv("CONFIG_FILE"))
if err != nil {
client = hedera.ClientForTestnet()
}
}
configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")
if configOperatorID != "" && configOperatorKey != "" && client.GetOperatorPublicKey().Bytes() == nil {
operatorAccountID, err := hedera.AccountIDFromString(configOperatorID)
if err != nil {
panic(err)
}
operatorKey, err := hedera.PrivateKeyFromString(configOperatorKey)
if err != nil {
panic(err)
}
client.SetOperator(operatorAccountID, operatorKey)
}
key1, err := hedera.GeneratePrivateKey()
if err != nil {
panic(err)
}
key2, err := hedera.GeneratePrivateKey()
if err != nil {
panic(err)
}
accountTxResponse, err := hedera.NewAccountCreateTransaction().
SetKey(key1.PublicKey()).
SetInitialBalance(hedera.ZeroHbar).
SetTransactionID(hedera.TransactionIDGenerate(client.GetOperatorAccountID())).
SetTransactionMemo("sdk example create_account__with_manual_signing/main.go").
Execute(client)
if err != nil {
panic(err)
}
println("transaction ID:", accountTxResponse.TransactionID.String())
accountTxReceipt, err := accountTxResponse.GetReceipt(client)
if err != nil {
panic(err)
}
accountID := *accountTxReceipt.AccountID
println("account =", accountID.String())
println("key =", key1.PublicKey().String())
println(":: update public key of account", accountID.String())
println("set key =", key2.PublicKey().String())
accountUpdateTx, err := hedera.NewAccountUpdateTransaction().
SetAccountID(accountID).
SetKey(key2.PublicKey()).
FreezeWith(client)
if err != nil {
panic(err)
}
accountUpdateTx.Sign(key1)
accountUpdateTx.Sign(key2)
accountUpdateTxResponse, err := accountUpdateTx.Execute(client)
if err != nil {
panic(err)
}
println("transaction ID:", accountUpdateTxResponse.TransactionID.String())
_, err = accountUpdateTxResponse.GetReceipt(client)
println(":: getAccount and check our current key")
info, err := hedera.NewAccountInfoQuery().
SetAccountID(accountID).
Execute(client)
if err != nil {
panic(err)
}
println("key =", info.Key.String())
}