Using the JavaScript and Golang Client Libraries

To make working with the Signchain API even easier, we provide client libraries for both JavaScript (Node.js) and Golang. These libraries handle API interactions, authentication, and request formatting, allowing you to focus on integrating Signchain’s capabilities into your application.


Prerequisites

To follow along, you’ll need:

  • An active Signchain account with a Vault ID and API Key
  • Familiarity with JavaScript or Go
  • Installed versions of Node.js (for JavaScript) or Golang

Setting Up the JavaScript Client

Installation

Install the JavaScript client library via npm or yarn:

npm install @grexie/signchain
# or
yarn add @grexie/signchain

Basic Usage

The JavaScript client library simplifies accessing Signchain's endpoints. Here’s how to create a client instance and make a request:

const { SignchainClient } = require('@grexie/signchain');

const client = new SignchainClient({
  apiKey: '<your-api-key>',
  vaultId: '<your-vault-id>',
});

Example: Creating a Wallet

To create a wallet, call the createWallet method:

async function createWallet() {
  const wallet = await client.createWallet({ name: 'My Wallet' });
  console.log('Wallet Created:', wallet);
}

createWallet().catch(console.error);

Example: Signing a Transaction

To sign a transaction, use the sign method:

const transactionOptions = {
  contract: '<contract-address>',
  chain: 'ethereum',
  sender: '<sender-address>',
  abi: [
    {
      "name": "transfer",
      "type": "function",
      "inputs": [{ "name": "to", "type": "address" }, { "name": "value", "type": "uint256" }]
    }
  ],
  functionName: 'transfer',
  args: ['<recipient-address>', '<amount>']
};

async function signTransaction() {
  const signature = await client.sign(transactionOptions);
  console.log('Transaction Signature:', signature);
}

signTransaction().catch(console.error);

Setting Up the Golang Client

Installation

Install the Golang client library using go get:

go get github.com/grexie/signchain-go/v2

Basic Usage

To set up the Golang client, initialize a new client with your API Key and Vault ID:

package main

import (
  "context"
  "fmt"
  "github.com/grexie/signchain-go/v2/pkg/client"
)

func main() {
  options := client.NewClientOptions().
    SetAPIKey("<your-api-key>").
    SetVaultID("<your-vault-id>")

  signchainClient, err := client.NewClient(options)
  if err != nil {
    fmt.Println("Error creating client:", err)
    return
  }
  
  // Further code here
}

Example: Creating a Wallet

To create a wallet, use the CreateWallet method:

ctx := context.Background()
wallet, err := signchainClient.CreateWallet(ctx, client.NewCreateWalletOptions().SetName("My Wallet"))
if err != nil {
  fmt.Println("Error creating wallet:", err)
  return
}
fmt.Println("Wallet created:", wallet)

Example: Signing a Transaction

Here’s an example of signing a transaction:

signOptions := client.NewSignOptions().
  SetChain(client.ChainEthereum).
  SetContract("<contract-address>").
  SetSender("<sender-address>").
  SetABI(map[string]any{"name": "transfer", "inputs": []any{{"name": "to", "type": "address"}, {"name": "value", "type": "uint256"}}}).
  SetArgs([]any{"<recipient-address>", "<amount>"})

signResult, err := signchainClient.Sign(ctx, signOptions)
if err != nil {
  fmt.Println("Error signing transaction:", err)
  return
}
fmt.Println("Transaction signature:", signResult)

Further Exploration

These examples cover the basics of using the JavaScript and Golang libraries. With these clients, you can handle a range of actions, from creating and managing wallets to performing secure transaction signing.

For more details on available methods, refer to the library documentation:


Integrating Signchain’s capabilities through these libraries ensures secure, scalable blockchain operations within your backend, without the complexity of manually handling API requests.