Usage Examples for Golang Client Library
This guide provides practical examples for using the Signchain Golang Client Library, covering key features such as wallet management, transaction signing, and vault status retrieval.
Example 1: Checking Vault Status
Retrieve and display the status of your vault. This is a good first step to ensure your client is correctly configured.
func checkVaultStatus(client signchain.Client) {
ctx := context.Background()
status, err := client.VaultStatus(ctx)
if err != nil {
fmt.Printf("Error retrieving vault status: %v\n", err)
} else {
fmt.Printf("Vault Online: %v\nVersion: %s\n", status.Online(), status.Version())
}
}
Example 2: Creating a Wallet
To create a new wallet in your Signchain Vault:
func createWallet(client signchain.Client) {
ctx := context.Background()
options := signchain.NewCreateWalletOptions().SetName("Example Wallet")
wallet, err := client.CreateWallet(ctx, options)
if err != nil {
fmt.Printf("Error creating wallet: %v\n", err)
} else {
fmt.Printf("Created wallet with address: %s\n", wallet.Address())
}
}
Example 3: Listing Wallets
To retrieve a list of all wallets within your vault, you can specify options such as offset and count to control pagination.
func listWallets(client signchain.Client) {
ctx := context.Background()
options := signchain.NewListWalletsOptions().SetOffset(0).SetCount(10)
result, err := client.ListWallets(ctx, options)
if err != nil {
fmt.Printf("Error listing wallets: %v\n", err)
} else {
fmt.Printf("Total Wallets: %d\n", result.Count())
for _, wallet := range result.Page() {
fmt.Printf("Wallet: %s\n", wallet.Address())
}
}
}
Example 4: Retrieving a Wallet
Get detailed information for a specific wallet by address:
func getWallet(client signchain.Client, address string) {
ctx := context.Background()
wallet, err := client.GetWallet(ctx, common.HexToAddress(address))
if err != nil {
fmt.Printf("Error retrieving wallet: %v\n", err)
} else {
fmt.Printf("Wallet Address: %s\nWallet Name: %s\n", wallet.Address(), wallet.Name())
}
}
Example 5: Updating a Wallet
Change the name of an existing wallet:
func updateWallet(client signchain.Client, address, newName string) {
ctx := context.Background()
options := signchain.NewUpdateWalletOptions().SetName(newName)
wallet, err := client.UpdateWallet(ctx, common.HexToAddress(address), options)
if err != nil {
fmt.Printf("Error updating wallet: %v\n", err)
} else {
fmt.Printf("Updated wallet name to: %s\n", wallet.Name())
}
}
Example 6: Signing a Transaction
Sign a transaction with Signchain, which returns a signature struct containing all necessary parameters to execute the transaction on-chain.
func signTransaction(client signchain.Client, contractAddress, senderAddress string) {
ctx := context.Background()
options := signchain.NewSignOptions().
SetChain(signchain.ChainEthereum).
SetContract(common.HexToAddress(contractAddress)).
SetSender(common.HexToAddress(senderAddress)).
SetABI(map[string]interface{}{
"name": "transfer",
"inputs": []map[string]interface{}{
{"name": "to", "type": "address"},
{"name": "value", "type": "uint256"},
},
}).
SetArgs([]interface{}{common.HexToAddress("0xRecipientAddress"), 1000})
result, err := client.Sign(ctx, options)
if err != nil {
fmt.Printf("Error signing transaction: %v\n", err)
} else {
fmt.Printf("Signature Submission Hash: %s\n", result.SubmissionHash())
}
}
Example 7: Expiring and Unexpiring Wallets
To mark a wallet as expired or restore it, use ExpireWallet
or UnexpireWallet
.
func expireWallet(client signchain.Client, address string) {
ctx := context.Background()
wallet, err := client.ExpireWallet(ctx, common.HexToAddress(address))
if err != nil {
fmt.Printf("Error expiring wallet: %v\n", err)
} else {
fmt.Printf("Expired wallet: %s\n", wallet.Address())
}
}
func unexpireWallet(client signchain.Client, address string) {
ctx := context.Background()
wallet, err := client.UnexpireWallet(ctx, common.HexToAddress(address))
if err != nil {
fmt.Printf("Error unexpiring wallet: %v\n", err)
} else {
fmt.Printf("Restored wallet: %s\n", wallet.Address())
}
}
Summary
In this guide, you learned how to:
- Check your vault’s status.
- Create, retrieve, list, update, and manage wallets.
- Sign transactions for on-chain execution.
- Expire and unexpire wallets as needed.
These examples demonstrate the flexibility of the Signchain Golang Client Library, enabling you to securely and efficiently manage blockchain interactions within your applications. For more details, visit the API Reference.