Usage Examples

This page provides examples of the main methods in the Signchain JavaScript Client Library, including how to manage wallets, sign transactions, and check vault status.


1. Signing a Transaction

The sign method allows you to obtain a signature for a blockchain transaction. The returned signature struct (args) is used to execute the transaction securely within a connected wallet.

Example Usage

import { SignchainClient } from '@grexie/signchain-client';
import MyContractABI from './contract-abi.json';

const signchain = new SignchainClient({ apiKey: 'YOUR_API_KEY', vaultId: 'YOUR_VAULT_ID' });

async function signTransaction() {
  try {
    const options = {
      chain: 'ethereum',
      contract: '0xYourContractAddress',
      sender: '0xYourWalletAddress',
      abi: MyContractABI,
      functionName: 'yourContractFunction',
      args: ['arg1', 'arg2'], // Exclude the signature from these arguments
    };

    const { submissionHash, args } = await signchain.sign(options);
    console.log('Transaction Signed:', submissionHash);
    console.log('Arguments for Execution:', args);

  } catch (error) {
    console.error('Error Signing Transaction:', error);
  }
}

Executing the Signed Transaction with ethers.js

Pass the args struct to ethers.js or a similar library to submit the transaction from a connected wallet.

import { ethers } from 'ethers';
import MyContractABI from './contract-abi.json';

async function executeTransaction(args) {
  const provider = new ethers.providers.Web3Provider(window.ethereum);
  const signer = provider.getSigner();
  const contract = new ethers.Contract('0xYourContractAddress', MyContractABI, signer);

  try {
    const tx = await contract.yourContractFunction(...args);
    console.log('Transaction Executed:', tx);
  } catch (error) {
    console.error('Error Executing Transaction:', error);
  }
}

2. Wallet Management

The following methods allow you to create, retrieve, list, update, expire, and unexpire wallets within your Signchain Vault.

Creating a Wallet

Use the createWallet method to create a new wallet in your vault.

async function createWallet() {
  try {
    const wallet = await signchain.createWallet({ name: 'My New Wallet' });
    console.log('Wallet Created:', wallet);
  } catch (error) {
    console.error('Error Creating Wallet:', error);
  }
}

Retrieving a Wallet

Retrieve details of a specific wallet by its address.

async function getWallet(address) {
  try {
    const wallet = await signchain.getWallet(address);
    console.log('Wallet Retrieved:', wallet);
  } catch (error) {
    console.error('Error Retrieving Wallet:', error);
  }
}

Listing Wallets

List all wallets within a vault, with optional pagination settings.

async function listWallets() {
  try {
    const wallets = await signchain.listWallets({ offset: 0, count: 10 });
    console.log('Wallets List:', wallets);
  } catch (error) {
    console.error('Error Listing Wallets:', error);
  }
}

Updating a Wallet

Update the name or other configurable properties of a wallet.

async function updateWallet(address) {
  try {
    const updatedWallet = await signchain.updateWallet(address, { name: 'Updated Wallet Name' });
    console.log('Wallet Updated:', updatedWallet);
  } catch (error) {
    console.error('Error Updating Wallet:', error);
  }
}

Expiring a Wallet

Expire a wallet, marking it for deletion after a set period. Expired wallets can be unexpired before deletion.

async function expireWallet(address) {
  try {
    const expiredWallet = await signchain.expireWallet(address);
    console.log('Wallet Expired:', expiredWallet);
  } catch (error) {
    console.error('Error Expiring Wallet:', error);
  }
}

Unexpiring a Wallet

Restore an expired wallet to active status, canceling the deletion schedule.

async function unexpireWallet(address) {
  try {
    const activeWallet = await signchain.unexpireWallet(address);
    console.log('Wallet Unexpired:', activeWallet);
  } catch (error) {
    console.error('Error Unexpiring Wallet:', error);
  }
}

3. Vault Status

Use the vaultStatus method to check the status of your Signchain Vault. This provides information on uptime, available keys, and other status indicators.

async function checkVaultStatus() {
  try {
    const status = await signchain.vaultStatus();
    console.log('Vault Status:', status);
  } catch (error) {
    console.error('Error Checking Vault Status:', error);
  }
}

Summary

The Signchain JavaScript Client Library provides robust tools for secure transaction signing, efficient wallet management, and real-time vault monitoring, all designed to integrate seamlessly with blockchain applications. By leveraging these features, you can maintain control over your private key data while streamlining interaction with blockchain networks. Whether you're signing transactions, managing wallets, or monitoring your vault, the Signchain library simplifies secure blockchain operations, making it ideal for developers building decentralized applications with high security requirements.