Implementing a multisig wallet in Solidity involves creating a smart contract that enforces the multisig workflow on the Ethereum blockchain. Here's a step-by-step guide to implementing a simple multisig wallet:
- Start with the basic structure of the contract:
pragma solidity ^0.8.0;
contract MultisigWallet {
// contract implementation goes here
}
- Define the state variables required for the multisig wallet:
address[] public owners;
uint public requiredSignatures;
mapping(address => bool) public isOwner;
- Create a constructor to initialize the required state variables:
constructor(address[] memory _owners, uint _requiredSignatures) {
require(_owners.length >= _requiredSignatures && _requiredSignatures > 0, "Invalid parameters");
for (uint i = 0; i < _owners.length; i++) {
isOwner[_owners[i]] = true;
}
owners = _owners;
requiredSignatures = _requiredSignatures;
}
- Define the data structures to manage the transactions and implement a basic submitTransaction function:
struct Transaction {
uint id;
uint value;
address payable to;
uint signaturesCount;
bool executed;
mapping(address => bool) signatures;
}
uint public transactionCount;
mapping(uint => Transaction) public transactions;
event TransactionSubmitted(uint id, address indexed submitter);
function submitTransaction(address payable _to, uint _value) public returns (uint) {
require(isOwner[msg.sender], "Not an owner");
uint txId = transactionCount++;
transactions[txId] = Transaction({
id: txId,
value: _value,
to: _to,
signaturesCount: 0,
executed: false
});
emit TransactionSubmitted(txId, msg.sender);
return txId;
}
- Implement the function to sign transactions:
event TransactionSigned(uint indexed id, address indexed signer);
function signTransaction(uint _txId) public {
require(isOwner[msg.sender], "Not an owner");
require(!transactions[_txId].executed, "Transaction already executed");
require(!transactions[_txId].signatures[msg.sender], "Transaction already signed");
transactions[_txId].signatures[msg.sender] = true;
transactions[_txId].signaturesCount++;
emit TransactionSigned(_txId, msg.sender);
}
- Implement the function to execute transactions:
event TransactionExecuted(uint indexed id, address indexed executor);
function executeTransaction(uint _txId) public {
require(isOwner[msg.sender], "Not an owner");
require(!transactions[_txId].executed, "Transaction already executed");
require(transactions[_txId].signaturesCount >= requiredSignatures, "Not enough signatures");
transactions[_txId].executed = true;
transactions[_txId].to.transfer(transactions[_txId].value);
emit TransactionExecuted(_txId, msg.sender);
}
- Combine the steps to form the complete smart contract:
pragma solidity ^0.8.0;
contract MultisigWallet {
address[] public owners;
uint public requiredSignatures;
mapping(address => bool) public isOwner;
constructor(address[] memory _owners, uint _requiredSignatures) {
require(_owners.length >= _requiredSignatures && _requiredSignatures > 0, "Invalid parameters");
for (uint i = 0; i < _owners.length; i++) {
isOwner[_owners[i]] = true;
}
owners = _owners;
requiredSignatures = _requiredSignatures;
}
struct Transaction {
uint id;
uint value;
address payable to;
uint signaturesCount;
bool executed;
mapping(address => bool) signatures;
}
uint public transactionCount;
mapping(uint => Transaction) public transactions;
event TransactionSubmitted(uint id, address indexed submitter);
function submitTransaction(address payable _to, uint _value) public returns (uint) {
require(isOwner[msg.sender], "Not an owner");
uint txId = transactionCount++;
transactions[txId] = Transaction({
id: txId,
value: _value,
to: _to,
signaturesCount: 0,
executed: false
});
emit TransactionSubmitted(txId, msg.sender);
return txId;
}
event TransactionSigned(uint indexed id, address indexed signer);
function signTransaction(uint _txId) public {
require(isOwner[msg.sender], "Not an owner");
require(!transactions[_txId].executed, "Transaction already executed");
require(!transactions[_txId].signatures[msg.sender], "Transaction already signed");
transactions[_txId].signatures[msg.sender] = true;
transactions[_txId].signaturesCount++;
emit TransactionSigned(_txId, msg.sender);
}
event TransactionExecuted(uint indexed id, address indexed executor);
function executeTransaction(uint _txId) public {
require(isOwner[msg.sender], "Not an owner");
require(!transactions[_txId].executed, "Transaction already executed");
require(transactions[_txId].signaturesCount >= requiredSignatures, "Not enough signatures");
transactions[_txId].executed = true;
transactions[_txId].to.transfer(transactions[_txId].value);
emit TransactionExecuted(_txId, msg.sender);
}
}
This is a simple implementation of a multisig wallet in Solidity. You can deploy this contract, submit transactions, sign them with multiple owners, and finally, execute them when the required number of signatures is reached. Note that this isn't a production-ready implementation and may contain vulnerabilities, so use it with caution and consider auditing before deploying it to a live network.