forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (82 loc) · 2.65 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 (
"os"
"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)
fileQuery := hedera.NewFileContentsQuery().
// Set the file ID for address book which is 0.0.102
SetFileID(hedera.FileIDForAddressBook())
println("the network that address book is for:", client.GetNetworkName().String())
cost, err := fileQuery.GetCost(client)
if err != nil {
println(err.Error(), ": error getting file contents query cost")
return
}
println("file contents cost:", cost.String())
// Have to always set the cost a little bigger, otherwise it is possible it won't go through
fileQuery.SetMaxQueryPayment(hedera.NewHbar(1))
// Execute the file content query
contents, err := fileQuery.Execute(client)
if err != nil {
println(err.Error(), ": error executing file contents query")
return
}
fileByte, err := os.OpenFile("address-book-byte.pb", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
println(err.Error(), ": error opening address-book-byte.pb")
return
}
fileString, err := os.OpenFile("address-book-string.pb", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
println(err.Error(), ": error opening address-book-string.pb")
return
}
// Write the contents (string([]byte)) into the string file
leng, err := fileString.WriteString(string(contents))
if err != nil {
println(err.Error(), ": error writing contents to file")
return
}
// Write the contents ([]byte) into the byte file
_, err = fileByte.Write(contents)
if err != nil {
println(err.Error(), ": error writing contents to file")
return
}
temp := make([]byte, leng)
_, err = fileString.Read(temp)
// Close the files
err = fileString.Close()
if err != nil {
println(err.Error(), ": error closing the file")
return
}
err = fileByte.Close()
if err != nil {
println(err.Error(), ": error closing the file")
return
}
}