Ethers.js is a powerful library for interacting with the Ethereum blockchain. This guide will show you how to integrate Capsule’s signing capabilities with both Ethers v6 and v5, allowing you to securely manage and sign transactions using Capsule’s secure infrastructure.

Prerequisites

Before integrating Capsule with Ethers.js, ensure you have:

  • Set up authentication with Capsule. See our Getting Started guides for detailed instructions.
  • Configured the Capsule client in your application
  • Access to an Ethereum node or RPC provider URL

If you haven’t set up Capsule authentication yet, complete one of our authentication tutorials first and return to this guide when you’re ready to implement signing.

Ethers v6

Installation

Choose your preferred package manager to install the required dependencies:

Usage

First, set up the Capsule Ethers signer:

import { CapsuleEthersSigner } from "@usecapsule/ethers-v6-integration";
import { ethers } from "ethers";
import Capsule from "@usecapsule/web-sdk";

// Initialize Capsule
const capsule = new Capsule(Environment.BETA, YOUR_API_KEY);

// Set up the provider
const provider = new ethers.JsonRpcProvider(YOUR_RPC_URL);

// Create the Capsule Ethers Signer
const ethersSigner = new CapsuleEthersSigner(capsule, provider);

To sign transactions:

import { TransactionRequest } from "ethers";

// Construct the transaction
const transaction: TransactionRequest = {
  from: "0x...", // Your address
  to: "0x...", // Recipient address
  value: ethers.parseUnits("0.1", "ether"),
  // Configure other transaction parameters as needed
};

// Call the signTransaction method to sign the transaction
const signedTx = await ethersSigner.signTransaction(transaction);

The v6 signer supports all standard signing operations including signMessage() and signTypedData() for EIP-712 formatted data.

Ethers v5

Installation

Install the v5-specific integration package:

Usage

Set up the v5 signer:

import { CapsuleEthersSigner } from "@usecapsule/ethers-v5-integration";
import { ethers } from "ethers";
import Capsule from "@usecapsule/web-sdk";

// Initialize Capsule
const capsule = new Capsule(Environment.BETA, YOUR_API_KEY);

// Set up the provider
const provider = new ethers.providers.JsonRpcProvider(YOUR_RPC_URL);

// Create the Capsule Ethers Signer
const ethersSigner = new CapsuleEthersSigner(capsule, provider);

// Basic operations
const address = await ethersSigner.getAddress();
const balance = await provider.getBalance(address);
console.log("Balance:", ethers.utils.formatEther(balance));

Server-Side Usage

Capsule can also be used for server-side signing alongside Ethers.js. See our Server-Side Signing Guide for more details on setting up server-side signing with Capsule.

Additional Resources