DEV Community

Cover image for How Do You Implement a Multisig Wallet in Solidity?
Neville Adam
Neville Adam

Posted on

How Do You Implement a Multisig Wallet in Solidity?

**Problem Faced:
**Single-owner wallets are prone to security risks, such as hacks or losing a private key.

Solution:
A multisig wallet (multi-signature) requires multiple owners to approve transactions before execution.

solidity

pragma solidity ^0.8.0;

contract MultiSigWallet {
    address[] public owners;
    uint public required;

    struct Transaction {
        address to;
        uint value;
        bool executed;
        uint approvalCount;
    }

    mapping(uint => mapping(address => bool)) public approvals;
    Transaction[] public transactions;

    constructor(address[] memory _owners, uint _required) {
        owners = _owners;
        required = _required;
    }

    function submitTransaction(address _to, uint _value) external {
        transactions.push(Transaction({ to: _to, value: _value, executed: false, approvalCount: 0 }));
    }

    function approveTransaction(uint _txIndex) external {
        Transaction storage txn = transactions[_txIndex];
        require(!approvals[_txIndex][msg.sender], "Already approved");
        approvals[_txIndex][msg.sender] = true;
        txn.approvalCount++;

        if (txn.approvalCount >= required) {
            txn.executed = true;
            payable(txn.to).transfer(txn.value);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Build secure, scalable, and customized blockchain solutions tailored to your business needs. From smart contract development to decentralized applications, get end-to-end services for your blockchain projects. Our blockchain development ensures seamless integration, high security, and innovative solutions for Web3, DeFi, and enterprise blockchain applications. Let’s shape the future of decentralized technology together!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay