Getting Started with the Golang Client Library
This guide provides step-by-step instructions for setting up and using the Signchain Golang Client Library. You’ll learn how to initialize the client, authenticate, and make your first API calls to manage wallets and sign transactions.
Prerequisites
To get started, ensure you have the following:
- Go version 1.18+ installed.
- API Key and Vault ID from your Signchain Dashboard to authenticate requests.
Installation
Install the library using Go’s package manager:
go get github.com/grexie/signchain-go/v2
Then, import it in your code:
import "github.com/grexie/signchain-go/v2"
Initializing the Client
To set up the client, provide the necessary options such as APIKey
and VaultID
. Here’s how to initialize the client:
package main
import (
"context"
"fmt"
"github.com/grexie/signchain-go/v2"
)
func main() {
options := signchain.NewClientOptions().
SetAPIKey("<your-api-key>").
SetVaultID("<your-vault-id>")
client, err := signchain.NewClient(options)
if err != nil {
panic(err)
}
fmt.Println("Signchain client initialized")
}
Making Your First API Call
Let’s make a basic call to fetch the status of your vault. This verifies that your client is correctly configured and can communicate with the Signchain API.
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 is online: %v\n", status.Online())
fmt.Printf("Vault version: %v\n", status.Version())
}
}
Wallet Management
After initializing the client, you can begin using Signchain’s wallet management features. Here’s an example of how to create a wallet:
func createWallet(client signchain.Client) {
ctx := context.Background()
options := signchain.NewCreateWalletOptions().SetName("My Wallet")
wallet, err := client.CreateWallet(ctx, options)
if err != nil {
fmt.Printf("Error creating wallet: %v\n", err)
} else {
fmt.Printf("Wallet created with address: %s\n", wallet.Address())
}
}
You can also retrieve, update, and manage wallets with methods like GetWallet
, UpdateWallet
, and ListWallets
.
Signing a Transaction
To sign a transaction, use the Sign
method. Provide the contract address, sender, ABI, and function arguments in the SignOptions
:
func signTransaction(client signchain.Client) {
ctx := context.Background()
options := signchain.NewSignOptions().
SetChain(signchain.ChainEthereum).
SetContract("<contract-address>").
SetSender("<sender-address>").
SetABI(map[string]interface{}{
"name": "myFunction",
"inputs": []map[string]interface{}{
{"name": "param1", "type": "uint256"},
},
}).
SetArgs([]interface{}{123})
result, err := client.Sign(ctx, options)
if err != nil {
fmt.Printf("Error signing transaction: %v\n", err)
} else {
fmt.Printf("Transaction signature: %v\n", result.SubmissionHash())
}
}
Summary
In this guide, we covered the following:
- Initializing the Signchain client with your API key and vault ID.
- Checking vault status.
- Creating a new wallet.
- Signing a transaction using the
Sign
method.
For additional functionality, explore the Usage Examples section to see how you can further integrate Signchain’s Golang Client Library into your project.