When it comes to transaction signing, using Capsule is just as straightforward as using any regular or default wallet.
Signing Transactions
The below examples assume you have already setup the Capsule object and created a user and an associated wallet. If you have not completed these steps yet, please refer to the Initial Setupand Creating Users & Wallets sections first.
For ease of integration, Capsule has created class extensions that are fully compatible with two popular libraries, ethersjs and viem
Server-Side Signing
In addition to the Web SDK, Capsule offers a Node.js compatible server side SDK. This can be used to import an active session from client-side after a user has authenticated and sign from the backend. This can be useful in cases where transactions signing is already occurring from a backend. This functionality relies upon the developer exporting a session from the client side and importing it server-side, as shown in the code snippets below.
// in frontend - once a user is created/signed in, you can export the sessionconstserializedSession=capsule.exportSession();// then transfer `serializedSession` to your backend over some secure method// in backend - import `serializedSession` to enable signing functionalityimport { CapsuleEthersSigner } from'@usecapsule/ethers-v6-integration';import Capsule, { Environment } from'@usecapsule/server-sdk';import { ethers } from'ethers';constcapsule=newCapsule(Environment.DEVELOPMENT,YOUR_API_KEY);// read in `serializedSession` from your secure transfer methodawaitcapsule.importSession(serializedSession);constethersSigner=newCapsuleEthersSigner(capsule,undefined);constsignature=awaitethersSigner.signMessage('hello-world');
// in frontend - once a user is created/signed in, you can export the sessionconstserializedSession=capsule.exportSession();// then transfer `serializedSession` to your backend over some secure method// in backend - import `serializedSession` to enable signing functionalityimport Capsule, { Environment } from'@usecapsule/server-sdk';import { createCapsuleViemClient } from'@usecapsule/viem-v1-integration';import { http } from'viem';constcapsule=newCapsule(Environment.DEVELOPMENT,YOUR_API_KEY);// read in `serializedSession` from your secure transfer methodawaitcapsule.importSession(serializedSession);constviemClient=createCapsuleViemClient(capsule, { chain:CHAIN, transport:http(CHAIN_PROVIDER),});constexample_tx= { value:BigInt(101010100000000), to:DEFAULT_TO_ADDRESS, chain:CHAIN, gas:BigInt(21000), maxPriorityFeePerGas:BigInt(1000000000), maxFeePerGas:BigInt(3000000000), account:viemClient.account!, nonce:0,};awaitviemClient.sendTransaction(example_tx);
Cosmos - Cosm.js
Capsule has also created a cosmjs-compatible signer for builders in the Cosmos Ecosystem.
import { CapsuleProtoSigner } from'@usecapsule/cosmjs-v0-integration';import { SigningStargateClient } from'@cosmjs/stargate';// prefix is optional and used to indicate a non cosmos address// eg 'celestia' to use 'celestia1...' addressesconstprotoSigner=newCapsuleProtoSigner(capsule, prefix);constclient=awaitSigningStargateClient.connectWithSigner(COSMOS_TESTNET_RPC, protoSigner,);awaitclient.getAccount(protoSigner.address));awaitclient.getAllBalances(protoSigner.address));constfromAddress=protoSigner.address;awaitclient.sendTokens( fromAddress,COSMOS_DEFAULT_TO_ADDRESS, [ { denom:'uatom', amount:'9500', }, ], { amount: [ { amount:'500', denom:'uatom', }, ], gas:'200000', }, );
// in frontend - once a user is created/signed in, you can export the sessionconstserializedSession=capsule.exportSession();// then transfer `serializedSession` to your backend over some secure method// in backend - import `serializedSession` to enable signing functionalityimport Capsule, { Environment } from'@usecapsule/server-sdk';import { CapsuleProtoSigner } from'@usecapsule/cosmjs-v0-integration';import { SigningStargateClient } from'@cosmjs/stargate';constcapsule=newCapsule(Environment.DEVELOPMENT,YOUR_API_KEY);// read in `serializedSession` from your secure transfer methodawaitcapsule.importSession(serializedSession);constprotoSigner=newCapsuleProtoSigner(capsule);constclient=awaitSigningStargateClient.connectWithSigner(COSMOS_TESTNET_RPC, protoSigner,);awaitclient.getAccount(protoSigner.address);awaitclient.getAllBalances(protoSigner.address);constfromAddress=protoSigner.address;awaitclient.sendTokens( fromAddress,COSMOS_DEFAULT_TO_ADDRESS, [ { denom:'uatom', amount:'9500', }, ], { amount: [ { amount:'500', denom:'uatom', }, ], gas:'200000', }, );
Signing a transaction directly
If you are already directly managing transactions encodings and aren't using any of the above signer libraries, you can use the following functions to directly sign an rlpEncodedTxBase64 transaction and receive a txHash.
This code initiates a transaction, where walletId is the unique identifier of your wallet (this is different than your wallet address and is only used within the context of Capsule), rlpEncodedTxBase64 represents the transaction details, and chainId specifies the blockchain on which the transaction will take place.