Protecting Your Digital Treasury: A Guide to Setting Up a Multisignature Wallet
What is a Multisignature Wallet?
A multisignature wallet is a smart contract that enables you to secure funds for multiple participants, requiring a certain number of signatures to approve transactions. This ensures that no single person can drain your treasury.
Setting Up a Multisignature Treasury
To set up a multisignature treasury, you’ll need to deploy the multisignature wallet smart contracts to a development network. First, clone the multisignature wallet smart contract from its GitHub repo and install Hardhat inside the contract directory.
git clone https://github.com/gnosis/safe-contracts.git
cd safe-contracts
npm install hardhat
Then, run the Hardhat development network and deploy the multisignature wallet smart contracts.
npx hardhat node
npx hardhat deploy
Understanding the Multisignature Wallet Smart Contracts
The multisignature wallet smart contracts consist of three core contracts:
- MultiSend: A helper contract that batches multiple transactions into one.
- GnosisSafe: The core safe smart contract.
- GnosisSafeProxyFactory: Creates a proxy for each user, allowing you to interact with the GnosisSafe contract.
Installing the Multisignature Wallet SDK Libraries
To interact with the multisignature wallet smart contracts, you’ll need to install the multisignature wallet SDK libraries. Create a Node project, install the ethers.js library, and then install the multisignature wallet SDK libraries.
npm init -y
npm install ethers.js @gnosis.pm/safe-sdk
Setting Up the.env File
Instead of hard-coding the Ethereum addresses, create a.env file to store them as environment variables. This will make it easier to switch between different networks and addresses.
ADDRESS_1=0x...
ADDRESS_2=0x...
ADDRESS_3=0x...
Creating the Treasury
Create an index.js file and import the multisignature wallet SDK library. Then, set up multisignature authorization by loading the addresses from the.env file and creating a provider from the ethers.js library.
const { Safe } = require('@gnosis.pm/safe-sdk');
const { ethers } = require('ethers');
const addresses = [process.env.ADDRESS_1, process.env.ADDRESS_2, process.env.ADDRESS_3];
const provider = new ethers.providers.JsonRpcProvider('https://example.com/rpc');
const safe = await Safe.createSafe({ owners: addresses, threshold: 2, provider });
Approving Transactions
To approve a transaction, you’ll need to create a safe transaction object, get its hash, and then approve it using the approveTransactionHash method. You can also execute the transaction using the executeTransaction method, which will approve and execute the transaction in one step.
const tx = await safe.createTransaction({
to: '0x...',
value: '1.0 ether',
data: '0x...',
});
const txHash = await tx.getTransactionHash();
await safe.approveTransactionHash(txHash);
// or execute the transaction in one step
await safe.executeTransaction(tx);