-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: init client with mirror network
Signed-off-by: Ivan Ivanov <[email protected]>
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
examples/initialize-client-with-mirror-node-adress-book/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/hashgraph/hedera-sdk-go/v2" | ||
) | ||
|
||
func main() { | ||
var client *hedera.Client | ||
var err error | ||
|
||
// Initialize the client with the testnet mirror node. This will also get the address book from the mirror node and | ||
// use it to populate the Client's consensus network. | ||
client, err = hedera.ClientForMirrorNetwork([]string{"testnet.mirrornode.hedera.com:443"}) | ||
if err != nil { | ||
panic(fmt.Sprintf("%v : error creating client", err)) | ||
} | ||
|
||
// Retrieving operator ID from environment variable OPERATOR_ID | ||
operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) | ||
if err != nil { | ||
panic(fmt.Sprintf("%v : error converting string to AccountID", err)) | ||
} | ||
|
||
// Retrieving operator key from environment variable OPERATOR_KEY | ||
operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) | ||
if err != nil { | ||
panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) | ||
} | ||
|
||
// Setting the client operator ID and key | ||
client.SetOperator(operatorAccountID, operatorKey) | ||
|
||
privateKey, err := hedera.PrivateKeyGenerateEcdsa() | ||
if err != nil { | ||
panic(err) | ||
} | ||
publicKey := privateKey.PublicKey() | ||
|
||
txResponse, err := hedera.NewAccountCreateTransaction(). | ||
SetInitialBalance(hedera.NewHbar(1)). | ||
SetKey(publicKey). | ||
Execute(client) | ||
if err != nil { | ||
panic(fmt.Sprintf("%v : error executing account create transaction", err)) | ||
} | ||
|
||
receipt, err := txResponse.GetReceipt(client) | ||
if err != nil { | ||
panic(fmt.Sprintf("%v : error getting receipt", err)) | ||
} | ||
|
||
fmt.Printf("New account id, %s", receipt.AccountID.String()) | ||
} |