Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 47 ยท Projects

Project: A Multisig Wallet

Build a multisignature wallet in Solidity: owners, proposals, confirmations and execution through low-level calls, plus a runnable JavaScript stand-in.

A multisig wallet is a contract that holds funds and only moves them when several owners agree. Instead of one private key that can drain everything, you might need 2 of 3 or 3 of 5 signers to approve each action. That is why teams use a multisig to own a token contract, hold a treasury, or control the pause switch from Modifiers and Access Control.

In this project you design the whole contract: who the owners are, how a transaction is proposed, how owners confirm it, and how it finally executes. The Solidity code is read-only, because the site cannot run an EVM. The same logic is rebuilt in JavaScript so you can run it, break it and watch the safety checks fire. The wallet is a teaching design, not an audited product: for real money, use a battle-tested one such as Safe.

The multisig life cycle

Submit An owner proposes a transaction.
Confirm Owners approve it one by one.
Execute Enough approvals run the call.

Nothing moves until the threshold is reached.

1 / 2

The design

The wallet stores a list of owners and a number required: how many different owners must confirm before a proposal can run. A proposal, which we call a transaction, has a destination to, an amount of ether value, and data, the encoded function call to make. If data is empty, the wallet just sends ether. If it holds a selector and arguments (see ABI Encoding and Low-Level Calls), the wallet calls a function on another contract.

stateDiagram-v2
    [*] --> Pending: submit
    Pending --> Pending: confirm or revoke
    Pending --> Executed: execute when confirmations >= required
    Executed --> [*]

A proposal stays pending, collecting or losing confirmations, until one owner executes it.

Every owner action is checked by a modifier, and each important step emits an event so that wallets and block explorers can show what is happening.

The contract

SOLIDITY
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract MultiSig {
    event Deposit(address indexed sender, uint256 amount);
    event Submitted(uint256 indexed txId, address indexed owner, address indexed to, uint256 value, bytes data);
    event Confirmed(uint256 indexed txId, address indexed owner);
    event Revoked(uint256 indexed txId, address indexed owner);
    event Executed(uint256 indexed txId);

    error InvalidSetup();
    error NotOwner();
    error TxDoesNotExist();
    error AlreadyExecuted();
    error AlreadyConfirmed();
    error NotConfirmed();
    error NotEnoughConfirmations(uint256 have, uint256 need);
    error CallFailed(bytes reason);

    struct Transaction {
        address to;
        uint256 value;
        bytes data;
        bool executed;
        uint256 confirmations;
    }

    address[] public owners;
    mapping(address => bool) public isOwner;
    uint256 public immutable required;
    Transaction[] public transactions;
    mapping(uint256 => mapping(address => bool)) public confirmedBy;

    modifier onlyOwner() {
        if (!isOwner[msg.sender]) revert NotOwner();
        _;
    }

    modifier exists(uint256 txId) {
        if (txId >= transactions.length) revert TxDoesNotExist();
        _;
    }

    modifier notExecuted(uint256 txId) {
        if (transactions[txId].executed) revert AlreadyExecuted();
        _;
    }

    constructor(address[] memory _owners, uint256 _required) {
        if (_owners.length == 0 || _required == 0 || _required > _owners.length) {
            revert InvalidSetup();
        }
        for (uint256 i = 0; i < _owners.length; i++) {
            address o = _owners[i];
            if (o == address(0) || isOwner[o]) revert InvalidSetup();
            isOwner[o] = true;
            owners.push(o);
        }
        required = _required;
    }

    receive() external payable {
        emit Deposit(msg.sender, msg.value);
    }

    function submit(address to, uint256 value, bytes calldata data)
        external
        onlyOwner
        returns (uint256 txId)
    {
        txId = transactions.length;
        transactions.push(Transaction(to, value, data, false, 0));
        emit Submitted(txId, msg.sender, to, value, data);
    }

    function confirm(uint256 txId) external onlyOwner exists(txId) notExecuted(txId) {
        if (confirmedBy[txId][msg.sender]) revert AlreadyConfirmed();
        confirmedBy[txId][msg.sender] = true;
        transactions[txId].confirmations += 1;
        emit Confirmed(txId, msg.sender);
    }

    function revoke(uint256 txId) external onlyOwner exists(txId) notExecuted(txId) {
        if (!confirmedBy[txId][msg.sender]) revert NotConfirmed();
        confirmedBy[txId][msg.sender] = false;
        transactions[txId].confirmations -= 1;
        emit Revoked(txId, msg.sender);
    }

    function execute(uint256 txId) external onlyOwner exists(txId) notExecuted(txId) {
        Transaction storage t = transactions[txId];
        if (t.confirmations < required) {
            revert NotEnoughConfirmations(t.confirmations, required);
        }
        t.executed = true; // effects first, interaction last
        (bool ok, bytes memory ret) = t.to.call{value: t.value}(t.data);
        if (!ok) revert CallFailed(ret);
        emit Executed(txId);
    }
}

Reading the design choices

Checks at the door. The constructor refuses a threshold of zero, a threshold above the owner count, the zero address and duplicate owners. Any of those would create a wallet that is unusable or, worse, one that anybody can drive. required is immutable (see Constructors, Constants and Immutables), so the rules cannot change after deployment. A production wallet usually lets owners change the rules, but only through the wallet itself: a function guarded by require(msg.sender == address(this)), reachable only by a proposal that enough owners confirmed.

One confirmation per owner. The nested mapping confirmedBy makes it impossible for one owner to confirm twice and reach the threshold alone. The counter confirmations is kept in step with the mapping, so execute does not loop over owners (loops cost gas, see Gas Costs and Optimization).

Effects before interaction. execute marks the transaction executed before it makes the external call. If the target calls back into the wallet, as in Reentrancy Step by Step, notExecuted stops a second run. Because the whole function reverts when the call fails, the executed = true write is undone, and the proposal can be tried again later.

Surface the failure. A low-level call does not revert by itself: it returns false. Ignoring that value is the classic "unchecked return value" bug, so the wallet checks ok and bubbles the reason up in CallFailed.

A confirmation approves an exact to, value and data. Owners should read these before confirming, ideally by decoding data with the target's ABI. A wallet that shows owners only a bare hash is asking them to sign blind.

Building it in JavaScript

The stand-in below keeps the same state and the same checks. It is not a blockchain: sender is a plain argument rather than a signature-proven msg.sender, accounts live in a Map, and a data field is an object { fn, args } instead of ABI bytes. A JavaScript throw stands in for revert, and because JavaScript does not undo state by itself, execute restores the balances by hand when the call fails. Inside the real EVM that rollback is automatic.

JS A multisig in JavaScript

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the output from top to bottom. An early execute is refused with the counts, a repeat confirmation is refused, a stranger is refused, and the second execute is refused because the proposal is already done. The shop only accepts setFee from the wallet, so the same call made by Bob alone fails: the wallet is the owner, and the wallet only acts when two owners agree.

Failures, revocations and self-governance

Now the harder cases. A call that fails must leave the proposal pending and the balances intact, a revoked confirmation must stop counting, and the owners should be able to change the threshold only through the wallet itself. The second program adds a setRequired function that accepts only calls from the wallet's own address.

JS Failed calls, revoke and self-calls

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Once the threshold is raised to 3, every later proposal needs all three owners, and the rule changed through the same process that guards everything else. That is the key idea: the wallet is its own admin.

Real: the owner list, the threshold, the confirmation mapping and counter, the checks, the order of effects and interaction, and the idea that a failed call reverts the whole execution. Stand-in: accounts, balances and { fn, args } calls are simple objects, there are no signatures, gas or events, and the rollback after a failed call is written by hand.

Where to go next

Before trusting a design like this with real value, work through the checklist from Audits, Tools and Best Practices and write the tests from Testing Contracts: reaching the threshold, a repeat confirmation, a revoked confirmation, a failing target and a reentrant target. Some ideas to extend the project:

  • Add addOwner, removeOwner and setRequired as functions callable only by the wallet itself. Decide what happens to confirmations that a removed owner already gave: counting them forever is a bug, so a safe design recounts or clears them.
  • Add an expiry time, so a proposal made months ago cannot be revived unexpectedly.
  • Add a delay between reaching the threshold and executing, a simple timelock that gives owners time to notice a bad proposal.
  • Let owners approve by signature instead of by transaction, so confirming costs no gas. That needs a typed-data signature and a nonce to prevent replays.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does execute set executed = true before making the external call?

    Updating state before the interaction (checks-effects-interactions) means that if the target calls back into the wallet, the notExecuted check already sees the proposal as done.
  2. 2. A low-level call to the target returns ok == false. What should the wallet do?

    A low-level call does not revert by itself. Checking the flag and reverting undoes the executed flag, so the owners can retry or revoke.
  3. 3. What stops one owner from reaching a threshold of 2 alone?

    Each owner can confirm a proposal once. The mapping records who has confirmed, and the counter only grows when a new owner confirms.
  4. 4. Which constructor input must be rejected?

    If required is larger than the owner count, no proposal can ever reach the threshold and all funds are locked. Zero thresholds, the zero address and duplicate owners are also rejected.

Summary

  • A multisig stores owners and a threshold, and runs a proposal only after enough different owners confirm it.
  • A proposal is to, value and data: empty data sends ether, encoded data calls a function on another contract.
  • Guard every action with modifiers, validate the setup in the constructor, and emit events for deposits, submissions, confirmations, revocations and executions.
  • Set executed before the external call, and check the ok flag of the low-level call so failures revert.
  • Let the wallet change its own rules only through proposals that its owners confirmed.
  • The JavaScript version is a stand-in: it models the logic, not signatures, gas or automatic rollback.

Next: Practice Exercises With Solutions.