github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/get_address_book/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/hashgraph/hedera-sdk-go/v2" 8 ) 9 10 func main() { 11 var client *hedera.Client 12 var err error 13 14 // Retrieving network type from environment variable HEDERA_NETWORK 15 client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 16 if err != nil { 17 panic(fmt.Sprintf("%v : error creating client", err)) 18 } 19 20 // Retrieving operator ID from environment variable OPERATOR_ID 21 operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) 22 if err != nil { 23 panic(fmt.Sprintf("%v : error converting string to AccountID", err)) 24 } 25 26 // Retrieving operator key from environment variable OPERATOR_KEY 27 operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) 28 if err != nil { 29 panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) 30 } 31 32 // Setting the client operator ID and key 33 client.SetOperator(operatorAccountID, operatorKey) 34 35 fileQuery := hedera.NewFileContentsQuery(). 36 // Set the file ID for address book which is 0.0.102 37 SetFileID(hedera.FileIDForAddressBook()) 38 39 println("the network that address book is for:", client.GetNetworkName().String()) 40 41 cost, err := fileQuery.GetCost(client) 42 if err != nil { 43 panic(fmt.Sprintf("%v : error getting file contents query cost", err)) 44 } 45 46 println("file contents cost:", cost.String()) 47 48 // Have to always set the cost a little bigger, otherwise it is possible it won't go through 49 fileQuery.SetMaxQueryPayment(hedera.NewHbar(1)) 50 51 // Execute the file content query 52 contents, err := fileQuery.Execute(client) 53 if err != nil { 54 panic(fmt.Sprintf("%v : error executing file contents query", err)) 55 } 56 57 fileByte, err := os.OpenFile("address-book-byte.pb", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) 58 if err != nil { 59 panic(fmt.Sprintf("%v : error opening address-book-byte.pb", err)) 60 } 61 62 fileString, err := os.OpenFile("address-book-string.pb", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) 63 if err != nil { 64 panic(fmt.Sprintf("%v : error opening address-book-string.pb", err)) 65 } 66 67 // Write the contents (string([]byte)) into the string file 68 leng, err := fileString.WriteString(string(contents)) 69 if err != nil { 70 panic(fmt.Sprintf("%v : error writing contents to file", err)) 71 } 72 // Write the contents ([]byte) into the byte file 73 _, err = fileByte.Write(contents) 74 if err != nil { 75 panic(fmt.Sprintf("%v : error writing contents to file", err)) 76 } 77 78 temp := make([]byte, leng) 79 80 _, err = fileString.Read(temp) 81 82 // Close the files 83 err = fileString.Close() 84 if err != nil { 85 panic(fmt.Sprintf("%v : error closing the file", err)) 86 } 87 err = fileByte.Close() 88 if err != nil { 89 panic(fmt.Sprintf("%v : error closing the file", err)) 90 } 91 }