Signing Transactions
When it comes to transaction signing, using Capsule is just as straightforward as using any regular or default wallet.
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.Web
React Native
const provider = new ethers.JsonRpcProvider(
CHAIN_PROVIDER,
CHAIN,
);
const example_tx = {
from: CURRENT_WALLET_ADDRESS,
to: DEFAULT_TO_ADDRESS,
value: 10101010000000,
gasLimit: 21000,
maxPriorityFeePerGas: 1000000000,
maxFeePerGas: 3000000000,
nonce: await provider.getTransactionCount(currentWallet.address),
chainId: '11155111',
type: 2,
};
const ethersSigner = new CapsuleEthersSigner(capsule, provider);
const res = await ethersSigner.sendTransaction(example_tx)
const provider = new ethers.JsonRpcProvider(
CHAIN__PROVIDER,
CHAIN,
);
await capsule.init(); // Mobile only
const example_tx = {
from: CURRENT_WALLET_ADDRESS,
to: DEFAULT_TO_ADDRESS,
value: 10101010000000,
gasLimit: 21000,
maxPriorityFeePerGas: 1000000000,
maxFeePerGas: 3000000000,
nonce: await provider.getTransactionCount(currentWallet.address),
chainId: '11155111',
type: 2,
};
const ethersSigner = new CapsuleEthersSigner(capsule, provider);
const res = await ethersSigner.sendTransaction(example_tx)
Web
React Native
const provider = new ethers.JsonRpcProvider(
CHAIN_PROVIDER,
CHAIN,
);
const viemClient = createCapsuleViemClient(capsule, {
chain: CHAIN,
transport: http(CHAIN_PROVIDER),
});
const example_tx = {
value: BigInt(101010100000000),
to: DEFAULT_TO_ADDRESS,
chain: CHAIN,
gas: BigInt(21000),
maxPriorityFeePerGas: BigInt(1000000000),
maxFeePerGas: BigInt(3000000000),
account: viemClient.account!,
nonce: await provider.getTransactionCount(currentWallet.address),
};
await viemClient.sendTransaction(example_tx);
const provider = new ethers.JsonRpcProvider(
CHAIN_PROVIDER,
CHAIN,
);
const viemClient = createCapsuleViemClient(capsule, {
chain: CHAIN,
transport: http(CHAIN_PROVIDER),
});
await capsule.init(); // Mobile only
const example_tx = {
value: BigInt(101010100000000),
to: DEFAULT_TO_ADDRESS,
chain: CHAIN,
gas: BigInt(21000),
maxPriorityFeePerGas: BigInt(1000000000),
maxFeePerGas: BigInt(3000000000),
account: viemClient.account!,
nonce: await provider.getTransactionCount(currentWallet.address),
};
await viemClient.sendTransaction(example_tx);
Web
React Native
const protoSigner = new CapsuleProtoSigner(capsule);
const client = await SigningStargateClient.connectWithSigner(
COSMOS_TESTNET_RPC,
protoSigner,
);
await client.getAccount(protoSigner.address));
await client.getAllBalances(protoSigner.address));
const fromAddress = protoSigner.address;
await client.sendTokens(
fromAddress,
COSMOS_DEFAULT_TO_ADDRESS,
[
{
denom: 'uatom',
amount: '9500',
},
],
{
amount: [
{
amount: '500',
denom: 'uatom',
},
],
gas: '200000',
},
);
const protoSigner = new CapsuleProtoSigner(capsule);
const client = await SigningStargateClient.connectWithSigner(
COSMOS_TESTNET_RPC,
protoSigner,
);
await capsule.init(); // Mobile only
await client.getAccount(protoSigner.address));
await client.getAllBalances(protoSigner.address));
const fromAddress = protoSigner.address;
await client.sendTokens(
fromAddress,
COSMOS_DEFAULT_TO_ADDRESS,
[
{
denom: 'uatom',
amount: '9500',
},
],
{
amount: [
{
amount: '500',
denom: 'uatom',
},
],
gas: '200000',
},
);
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
.Web
React Native
const signedTx = await capsule.sendTransaction(
walletId,
rlpEncodedTxBase64,
chainId
)
const signedTypedMessage = await wallet.signTypedData(address, payload)
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.