Solana Web3.js is the official Solana JavaScript API for interacting with the Solana network. This guide will show you how to integrate Capsule’s wallet and signing capabilities with Solana using our integration package.

Prerequisites

Before integrating Capsule with Solana, ensure you have:

  • Set up authentication with Capsule. See our Getting Started guides for details.
  • Configured the Capsule client in your application
  • Enabled Solana wallet support in the Capsule Developer Portal

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 Solana integration.

Installation

Choose your preferred package manager to install the required dependencies:

Setting Up the Signer

Initialize the Capsule Solana signer:

import { CapsuleSolanaWeb3Signer } from "@usecapsule/solana-web3.js-v1-integration";
import { Connection, clusterApiUrl } from "@solana/web3.js";
import { capsuleClient } from "./capsuleClient"; // Your Capsule client initialization

// Set up Solana connection
const solanaConnection = new Connection(clusterApiUrl("testnet"));

// Create the Capsule Solana Signer
const solanaSigner = new CapsuleSolanaWeb3Signer(capsuleClient, solanaConnection);

Transaction Signing

Signing Transactions

import { Transaction, SystemProgram, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";

// Create transaction
const transaction = new Transaction().add(
  SystemProgram.transfer({
    fromPubkey: solanaSigner.sender,
    toPubkey: new PublicKey("RECIPIENT_ADDRESS"),
    lamports: LAMPORTS_PER_SOL * 0.1, // 0.1 SOL
  })
);

// Sign the transaction
try {
  const signedTx = await solanaSigner.signTransaction(transaction);
  console.log("Signed transaction:", signedTx);
} catch (error) {
  console.error("Error signing transaction:", error);
}

Sending Transactions

try {
  const signature = await solanaSigner.sendTransaction(transaction, {
    skipPreflight: false,
    preflightCommitment: "confirmed",
  });
  console.log("Transaction signature:", signature);
} catch (error) {
  console.error("Error sending transaction:", error);
}

Additional Methods

The CapsuleSolanaWeb3Signer provides several additional methods and properties:

  • signBytes(bytes: Buffer): Sign arbitrary bytes directly
  • signVersionedTransaction(transaction: VersionedTransaction): Sign a versioned transaction
  • address: Get the wallet address as a string
  • sender: Get the wallet public key as a solana.PublicKey object

Server-Side Usage

For server-side signing operations with Solana, please refer to our Server-Side Signing Guide for specific instructions.

When using Solana Web3.js server-side, ensure you’re following proper security practices and handling authentication appropriately.

Additional Resources