Skip to main content

Command Palette

Search for a command to run...

智能合约实例-2: 实现一个多签钱包

Updated
3 min read

智能合约工程师, 业余Youtuber, 投机套利爱好者.

让我们尝试创建一个多重签名钱包:

多签钱包功能

  • 提交交易

  • 批准和撤销对待处理交易的批准

  • 在足够多的所有者批准后,任何人都可以执行交易

 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract MultiSigWallet {

    // 存钱 记录
    event Deposit(address indexed sender, uint amount, uint balance);

    // 提交交易 记录
    event SubmitTransaction(
        address indexed owner,
        uint indexed txIndex,
        address indexed to,
        uint value,
        bytes data
    );

    // 确认交易 记录
    event ConfirmTransaction(address indexed owner, uint indexed txIndex);

    // 驳回交易 记录
    event RevokeConfirmation(address indexed owner, uint indexed txIndex);

    // 执行交易 记录
    event ExecuteTransaction(address indexed owner, uint indexed txIndex);

    // 多签成员 
    address[] public owners;
    mapping(address => bool) public isOwner;

    // 交易多签通过 人数
    uint public numConfirmationsRequired;

    // 交易数据
    struct Transaction {
        address to;
        uint value;
        bytes data;
        bool executed;
        uint numConfirmations;
    }

    // mapping from tx index => owner => bool  每个多签者确认情况
    mapping(uint => mapping(address => bool)) public isConfirmed;

    // 全部交易数据
    Transaction[] public transactions;

    modifier onlyOwner() {
        require(isOwner[msg.sender], "not owner");
        _;
    }

    // 交易存在
    modifier txExists(uint _txIndex) {
        require(_txIndex < transactions.length, "tx does not exist");
        _;
    }

    // 交易未执行
    modifier notExecuted(uint _txIndex) {
        require(!transactions[_txIndex].executed, "tx already executed");
        _;
    }

    // 交易未确认
    modifier notConfirmed(uint _txIndex) {
        require(!isConfirmed[_txIndex][msg.sender], "tx already confirmed");
        _;
    }

    // 初始化, 设置好签名者, 还有签名通过权重
    constructor(address[] memory _owners, uint _numConfirmationsRequired) {
        require(_owners.length > 0, "owners required");
        require(
            _numConfirmationsRequired > 0 &&
                _numConfirmationsRequired <= _owners.length,
            "invalid number of required confirmations"
        );

        // 
        for (uint i = 0; i < _owners.length; i++) {
            address owner = _owners[i];

            require(owner != address(0), "invalid owner");
            require(!isOwner[owner], "owner not unique"); // 确保签名者 不重复 

            isOwner[owner] = true;
            owners.push(owner);
        }

        numConfirmationsRequired = _numConfirmationsRequired;
    }

    // 实现 receive 函数, 可接受 ETH 
    receive() external payable {
        emit Deposit(msg.sender, msg.value, address(this).balance);
    }

    // 提交交易 
    function submitTransaction(
        address _to,
        uint _value,
        bytes memory _data
    ) public onlyOwner {
        uint txIndex = transactions.length;

        transactions.push(
            Transaction({
                to: _to,
                value: _value,
                data: _data,
                executed: false,
                numConfirmations: 0
            })
        );

        emit SubmitTransaction(msg.sender, txIndex, _to, _value, _data);
    }

    // 确认交易 
    function confirmTransaction(uint _txIndex)
        public
        onlyOwner
        txExists(_txIndex)
        notExecuted(_txIndex)
        notConfirmed(_txIndex)
    {
        Transaction storage transaction = transactions[_txIndex];
        transaction.numConfirmations += 1;
        isConfirmed[_txIndex][msg.sender] = true;

        emit ConfirmTransaction(msg.sender, _txIndex);
    }

    // 执行交易
    function executeTransaction(uint _txIndex)
        public
        onlyOwner
        txExists(_txIndex)
        notExecuted(_txIndex)
    {
        Transaction storage transaction = transactions[_txIndex];

        require(
            transaction.numConfirmations >= numConfirmationsRequired,
            "cannot execute tx"
        );

        transaction.executed = true;

        (bool success, ) = transaction.to.call{value: transaction.value}(
            transaction.data
        );
        require(success, "tx failed");

        emit ExecuteTransaction(msg.sender, _txIndex);
    }

    // 驳回交易 
    function revokeConfirmation(uint _txIndex)
        public
        onlyOwner
        txExists(_txIndex)
        notExecuted(_txIndex)
    {
        Transaction storage transaction = transactions[_txIndex];

        require(isConfirmed[_txIndex][msg.sender], "tx not confirmed");

        transaction.numConfirmations -= 1;
        isConfirmed[_txIndex][msg.sender] = false;

        emit RevokeConfirmation(msg.sender, _txIndex);
    }

    // 获得多签者 
    function getOwners() public view returns (address[] memory) {
        return owners;
    }

    // 获得交易数量
    function getTransactionCount() public view returns (uint) {
        return transactions.length;
    }

    // 获得交易
    function getTransaction(uint _txIndex)
        public
        view
        returns (
            address to,
            uint value,
            bytes memory data,
            bool executed,
            uint numConfirmations
        )
    {
        Transaction storage transaction = transactions[_txIndex];

        return (
            transaction.to,
            transaction.value,
            transaction.data,
            transaction.executed,
            transaction.numConfirmations
        );
    }
}

测试调用合约

这是一个测试从多重签名钱包发送交易的合约

通过以下合约部署完成后, 用多签名调用该合约的方法, 修改函数内容 i 的值.

最终实现, 通过多重签名授权的方式, 进行合约交互操作的功能.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract TestContract {
    uint public i;

    function callMe(uint j) public {
        i += j;
    }

    function getData() public pure returns (bytes memory) {
        return abi.encodeWithSignature("callMe(uint256)", 123);
    }
}

补充知识

知识点: 数组的值赋给结构体变量, 该变量的值被修改后, 数组内元素同样被修改

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract TestContract {

    // 测试数据
    struct Transaction {
        address to;
        uint value;
    }

    // 数据 数组
    Transaction[] public transactions;

    // 初始化 赋值 
    constructor () {
        for (uint i = 0; i < 4; i++) {
            transactions.push(
                Transaction({
                    to: address(0),
                    value: 6666
                })
            );
        }
    }


    // 改变数组的值 
    // 知识点: 数组的值赋给结构体变量, 该变量的值被修改后, 数组内元素同样被修改 
    function doChangeArray(uint _txIndex)
        public
    {
        Transaction storage transaction = transactions[_txIndex];
        transaction.to = address(0);
        transaction.value = _txIndex;
    }

    // 获得交易
    function getTransaction(uint _txIndex)
        public
        view
        returns (
            address to,
            uint value
        )
    {
        Transaction storage transaction = transactions[_txIndex];
        return (
            transaction.to,
            transaction.value
        );
    }

}

关注更多

Web3 技术与应用,研究实践分享!

Twitter: @AntCaveClub

蚁穴Web3社区: https://antcave.club/

L

Roblox Unblocked is more than just a gaming platform; it's an expansive universe that beckons players to explore the limitless realms of creativity. https://heardleunlimited.io/roblox-unblocked

王刚3y ago

大佬你好,关于交易多签,理论上来说是不是仅使用msg.sender来验证签署人身份就够了?但是我又看到其他的一些交易多签的流程,是需要签署人在链下通过钱包创建signature哈希,再把到链上通过ecrecover和keccak256解包出来signature的签署地址进行校验,感觉这样做是否有点多余?或者有什么其他的考虑?

More from this blog

现在谁继续赚取数百万美元?正确答案:鲸鱼和内幕钱包。

现在谁继续赚取数百万美元?正确答案:鲸鱼和内幕钱包。 因为市场完全依赖于他们的操纵, 他们在任何游戏中,拥有绝对优势。 因为Axiom等工具具备监控功能,所以他们也会经常替换钱包: 💰钱包实时监控:https://axiom.trade/@0xcii 📱钱包监控:https://debot.ai?inviteCode=174709 分享一批上周的链上顶级内幕钱包🧵👇 我花了很多时间在链上筛选这些钱包! 请关注我 @AntCaveClub,点赞并转发——确保不再...

Apr 18, 20251 min read34

这个钱包昨天一笔交易用 0.9 Eth 赚了 8.4 Eth

几乎是交易市场的一条定律: 不管新闻是真假, 它带来的影响是真的就行。 昨天,我们知道 特朗普团队发币这一消息后, 第一时间关注市场上的新币。 发现了 ETH 链上的 $DJT 代币, 当时市值仅 $450K , 而现在达到了 $ 3.3 M 。 由于信息较少, 小入了一手, 然后发公告了,估计都跌麻木了没人上车,结果今天获利 8 倍。 另一个值得 关注的信息是特朗普最大主题代币 $TRUMP 今日突然下跌 -40% 我们通过链上数据观察, 这部分游资进入了 $DJT 而这个...

Jun 18, 20241 min read355

Ton 会成为真正的独角兽,因为它拥有

TON 才是独角兽。 正确使用TON 工具能给你带来财富。 SOL / ETH 的 memcoins 交易良好,但 TON 正在超越它。 以下 5 个基本工具可帮助你获得 10-100 倍收益 1⃣️ TON 的土壤 显然,TON 上的 memcoin 越来越受欢迎,TON 市值已超过 160 亿美元。 $NOT 上线币安,其涨幅众所周知。其他大量 TON memcoin 会增值 10-100 倍。让我们学习一下用于搜寻早期 TON 山寨币的主要工具…… 2⃣️ Defilla...

Jun 5, 20241 min read109

昨晚用 1 Eth 赚了 10 Eth ! 没想到睡觉起来就这样了。

昨天睡觉前搞了 $DEW 建仓时市值 500K 附近, 现在 6M 左右了, 一晚上 10 倍。 最近确实以太坊链上出金狗,千万市值天天都有。 也主要因为我睡着了, 睡之前和朋友讲了下,建议他上车,他说他在打球🏀 推荐 $DEW 的主要原因是, 我前几天不是推了 $PEW ,推的时候市值 $2 M 附近,结果 3天一路干到了 $94 M , 大概快有 50 倍... 当时跟朋友讲让他上点车, 他在追剧,忘上车了, 我说下次一定。 他推荐我看《庆余年》,说这个好看。 昨晚看到出...

Jun 1, 20241 min read324

专注 Web3 网络赚钱模式, 区块链实践教程与交流分享

127 posts

专注 Web3 网络赚钱模式, 实践教程与交流分享 !

加入社群 | 订阅Youtube