forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (68 loc) · 2.48 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
package main
import (
"fmt"
"github.com/hashgraph/hedera-sdk-go/v2"
"os"
)
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 key to use with new account
newKey, err := hedera.GeneratePrivateKey()
if err != nil {
println(err.Error(), ": error generating PrivateKey}")
return
}
fmt.Printf("private = %v\n", newKey)
fmt.Printf("public = %v\n", newKey.PublicKey())
// Create account
// The only required property here is `key`
transactionResponse, err := hedera.NewAccountCreateTransaction().
// The key that must sign each transfer out of the account.
SetKey(newKey.PublicKey()).
// If true, this account's key must sign any transaction depositing into this account (in
// addition to all withdrawals)
SetReceiverSignatureRequired(false).
// The maximum number of tokens that an Account can be implicitly associated with. Defaults to 0
// and up to a maximum value of 1000.
SetMaxAutomaticTokenAssociations(1).
// The memo associated with the account
SetTransactionMemo("go sdk example create_account/main.go").
// The account is charged to extend its expiration date every this many seconds. If it doesn't
// have enough balance, it extends as long as possible. If it is empty when it expires, then it
// is deleted.
Execute(client)
if err != nil {
println(err.Error(), ": error executing account create transaction}")
return
}
// Get receipt to see if transaction succeeded, and has the account ID
transactionReceipt, err := transactionResponse.GetReceipt(client)
if err != nil {
println(err.Error(), ": error getting receipt}")
return
}
// Get account ID out of receipt
newAccountID := *transactionReceipt.AccountID
fmt.Printf("account = %v\n", newAccountID)
}