A multisig wallet is a smart contract that requires multiple signatures (usually from different addresses) to authorize transactions. It increases security for managing funds because it prevents single-point failures or attacks.
To create a multisig wallet in Solidity, follow these steps:
- Define a new smart contract:
pragma solidity ^0.6.0;
contract MultiSigWallet {
// Continue with the contract code
}
- Declare required variables:
- The threshold is the minimum number of signatures required to execute transfers.
- The owners are the addresses that can confirm transactions.
- The transaction structure will store transaction details and signers.
- The transactions mapping is for storing transactions and their confirmations.
uint256 private threshold;
address[] private owners;
mapping (uint256 => Transaction) private transactions;
uint256 public transactionCount;
struct Transaction {
address destination;
uint256 value;
bytes data;
bool executed;
mapping (address => bool) confirmations;
}
- Add the constructor to initialize the wallet with owners and the threshold:
constructor(address[] memory _owners, uint256 _threshold) public {
require(_owners.length > 0, "Owners required");
require(_threshold > 0 && _threshold <= _owners.length, "Invalid threshold");
owners = _owners;
threshold = _threshold;
}
- Implement a function to submit a new transaction:
function submitTransaction(address _destination, uint256 _value, bytes memory _data) public onlyOwners returns (uint256) {
uint256 transactionId = transactionCount;
transactions[transactionId] = Transaction({
destination: _destination,
value: _value,
data: _data,
executed: false
});
transactionCount += 1;
return transactionId;
}
- Implement a function to confirm the transaction:
function confirmTransaction(uint256 _transactionId) public onlyOwners {
require(!transactions[_transactionId].executed, "Transaction already executed");
require(!transactions[_transactionId].confirmations[msg.sender], "Transaction already confirmed");
transactions[_transactionId].confirmations[msg.sender] = true;
}
- Add a function to execute the transaction if it has enough confirmations:
function executeTransaction(uint256 _transactionId) public onlyOwners {
require(getConfirmationCount(_transactionId) >= threshold, "Insufficient confirmations");
Transaction storage txn = transactions[_transactionId];
require(!txn.executed, "Transaction already executed");
(bool success,) = txn.destination.call{value: txn.value}(txn.data);
require(success, "Transaction execution failed");
txn.executed = true;
}
- Add helper functions to check owners and confirmations:
modifier onlyOwners() {
bool isOwner = false;
for (uint256 i = 0; i < owners.length; i++) {
if (owners[i] == msg.sender) {
isOwner = true;
break;
}
}
require(isOwner, "Only owners can perform this action");
_;
}
function getConfirmationCount(uint256 _transactionId) public view returns (uint256) {
uint256 count = 0;
for (uint256 i = 0; i < owners.length; i++) {
if (transactions[_transactionId].confirmations[owners[i]]) {
count += 1;
}
}
return count;
}
That's it! Now you have a basic multisig wallet contract in Solidity. You can also add more functionalities like removing owners, changing the threshold or adding a revokeConfirmation function to remove confirmations. Make sure you test this contract thoroughly and have it audited before deploying it on the main network.