Creating a Multi-Signature Wallet Using Safe SDK

ยท

Introduction to Safe

Safe (formerly known as Gnosis Safe) is a smart contract wallet platform built on the Ethereum network, specializing in secure and flexible digital asset management solutions. Widely adopted for personal asset management, DAO governance, and corporate finance, Safe stands as one of the most trusted multi-signature (multisig) wallet solutions in the Ethereum ecosystem.

This guide will walk you through using the Safe SDK to create and manage a multisig wallet efficiently.


Step-by-Step Tutorial

Safe offers a robust JavaScript SDK, enabling developers to integrate its functionalities seamlessly. Below is a detailed example of interacting with a wallet using the SDK.

Prerequisites

Ensure your development environment has:

Install the required dependencies:

npm install ethers @safe-global/protocol-kit @safe-global/api-kit
# OR
yarn add ethers @safe-global/protocol-kit @safe-global/api-kit

Deploying a Multisig Wallet

const { SafeFactory } = require('@safe-global/protocol-kit');
const safeFactory = await SafeFactory.init({
  provider: '<provider-url>', // Replace with your RPC URL
  signer: '<sender-private-key>', // Private key for deploying the multisig contract
});

// Configure a 2-of-3 multisig wallet
const safeAccountConfig = {
  owners: [
    '<signer-address-1>', // Replace with signer addresses
    '<signer-address-2>',
    '<signer-address-3>',
  ],
  threshold: 2,
};

// Deploy the multisig contract
const safeWallet = await safeFactory.deploySafe({ safeAccountConfig });
const safeAddress = await safeWallet.getAddress();
console.log('Safe Wallet Deployed:', `https://app.safe.global/sep:${safeAddress}`);

Reading Multisig Wallet Information

const Safe = require('@safe-global/protocol-kit').default;
const safeWallet = await Safe.init({
  provider: '<provider-url>',
  safeAddress: '<safe-address>', // Replace with your Safe address
});

const threshold = await safeWallet.getThreshold();
const owners = await safeWallet.getOwners();
console.log(`Threshold: ${threshold}`); // Minimum required signatures
console.log(`Owners: ${owners.join(', ')}`); // List of signers

Initiating a Multisig ETH Transfer

Ensure the Safe wallet has sufficient ETH balance.

const signerPrivateKey = "0x..."; // Replace with a signer's private key
const safeTransactionData = {
  to: '<receiver-address>', // Recipient address
  data: '0x',
  value: ethers.parseUnits('0.01', 'ether').toString(), // 0.01 ETH
};

// Create the transaction
const safeTransaction = await safeWallet.createTransaction({ transactions: [safeTransactionData] });
const safeTxHash = await safeWallet.getTransactionHash(safeTransaction);

// Sign the transaction
const senderSignature = await safeWallet.signHash(safeTxHash);

// Submit to Safe service for other signers
const apiKit = new SafeApiKit({ chainId: await safeWallet.getChainId() });
await apiKit.proposeTransaction({
  safeAddress: '<safe-address>',
  safeTransactionData: safeTransaction.data,
  safeTxHash,
  senderAddress: new Wallet(signerPrivateKey).address,
  senderSignature: senderSignature.data,
});

Confirming and Executing the Transaction

// Confirm the transaction
const transaction = (await apiKit.getPendingTransactions(safeAddress)).results[0];
if (!transaction) return;

const safeTxHash = transaction.safeTxHash;
const signature = await safeWallet.signHash(safeTxHash);
await apiKit.confirmTransaction(safeTxHash, signature.data);

// Execute once thresholds are met
if (transaction.confirmations.length >= transaction.confirmationsRequired) {
  const safeTransaction = await apiKit.getTransaction(safeTxHash);
  const executeTxResponse = await safeWallet.executeTransaction(safeTransaction);
  const receipt = await executeTxResponse.transactionResponse?.wait();
  console.log('Transaction Executed:', receipt.hash);
}

๐Ÿ‘‰ Explore Advanced Safe Features


FAQs

1. What is a multisig wallet?

A multisig wallet requires multiple signatures (from predefined owners) to approve transactions, enhancing security for shared or organizational funds.

2. How many owners can a Safe wallet have?

Safe supports flexible configurations (e.g., 2-of-3, 3-of-5), with no hard limit on the number of owners.

3. Can I change the threshold after deployment?

Yes, owners can propose and approve threshold changes via a multisig transaction.


Conclusion

This guide covered the fundamentals of creating and managing a multisig wallet with Safe SDK. For advanced features like modular extensions or batch transactions, refer to:

๐Ÿ‘‰ Start Building with Safe Today