多重签名(Multi-Signature)是一种需要多个私钥签名才能执行交易的账户类型,大大提高了资金安全性。本文详细介绍多重签名的原理、实现方式、使用场景和最佳实践,帮助开发者理解和使用这一重要的安全机制。
一、什么是多重签名 1. 基本概念 多重签名(Multi-Signature,简称Multisig)是一种需要多个私钥签名才能执行交易的账户类型。与普通账户只需要一个私钥不同,多重签名账户需要多个授权者(通常是多个私钥持有者)中的一定数量同意,才能执行交易。
2. 工作原理 签名要求 设置N个授权者 要求M个签名(M ≤ N) 例如:3个授权者中需要2个签名(2/3) 只有达到要求的签名数,交易才能执行 安全优势 单点故障风险降低 需要多人协作才能操作 防止单个私钥泄露导致资金损失 适合企业和高价值账户 3. 常见配置 2/3多重签名 3个授权者,需要2个签名 平衡安全性和便利性 适合大多数场景 3/5多重签名 5个授权者,需要3个签名 更高安全性 适合重要资金管理 自定义配置 二、如何实现多重签名 1. 使用智能合约实现 基础多重签名合约 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 pragma solidity ^0 .8 .0 contract MultiSigWallet { address[] public owners uint public required struct Transaction { address to uint value bytes data bool executed } Transaction[] public transactions mapping( uint => mapping( address => bool) ) public confirmations event Submission( uint indexed transactionId) event Confirmation( address indexed sender, uint indexed transactionId) event Execution( uint indexed transactionId) event Deposit( address indexed sender, uint value) modifier onlyOwner( ) { require( isOwner( msg.sender) , "Not owner" ) _ } modifier validRequirement( uint ownerCount, uint _required) { require( _required > 0 && _required <= ownerCount, "Invalid requirement" ) _ } constructor( address[] memory _owners, uint _required) validRequirement( _owners.length, _required) { owners = _owners required = _required } function isOwner( address addr) public view returns ( bool) { for ( uint i = 0 if ( owners[i] == addr) return true } return false } function submitTransaction( address _to, uint _value, bytes memory _data) public onlyOwner returns ( uint) { uint txIndex = transactions.length transactions.push( Transaction( { to: _to, value: _value, data: _data, executed: false }) ) emit Submission( txIndex) confirmTransaction( txIndex) return txIndex } function confirmTransaction( uint _txIndex) public onlyOwner { require( !confirmations[_txIndex][msg.sender], "Already confirmed" ) require( !transactions[_txIndex].executed, "Already executed" ) confirmations[_txIndex][msg.sender] = true emit Confirmation( msg.sender, _txIndex) if ( getConfirmationCount( _txIndex) >= required) { executeTransaction( _txIndex) } function executeTransaction( uint _txIndex) public { require( getConfirmationCount( _txIndex) >= required, "Not enough confirmations" ) Transaction storage txn = transactions[_txIndex] txn.executed = true ( bool success, ) = txn.to.call{value: txn.value}( txn.data) require( success, "Transaction failed" ) emit Execution( _txIndex) } function getConfirmationCount( uint _txIndex) public view returns ( uint) { uint count = 0 if ( confirmations[_txIndex][owners[i]]) { count++ } return count } function getTransactionCount( bool pending, bool executed) view { for ( uint i = 0 if ( pending && !transactions[i].executed) count++ if ( executed && transactions[i].executed) count++ } } receive( ) external payable { if ( msg.value > 0 ) { emit Deposit( msg.sender, msg.value) }
2. 部署和使用 部署合约 1 2 3 4 5 6 7 8 9 10 11 12 const owners = [ '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb' , '0x8ba1f109551bD432803012645Hac136c22C9e8c' , '0x1234567890123456789012345678901234567890' ];const required = 2 ;const MultiSigWallet = artifacts.require ("MultiSigWallet" );const wallet = await MultiSigWallet .new (owners, required); 提交交易:
// 所有者1提交交易 await wallet.submitTransaction( recipientAddress, web3.utils.toWei(‘1’, ‘ether’), ‘0x’, { from: owners[0] } ); 确认交易:
1 2 3 await wallet.confirmTransaction (0 , { from : owners[1 ] });
3. 使用Gnosis Safe Gnosis Safe简介 成熟的多重签名钱包解决方案 提供友好的用户界面 支持多种功能 经过安全审计 创建Safe钱包 访问 https://gnosis-safe.io/ 连接钱包 设置所有者列表 设置确认数量 创建钱包 使用Safe 通过Web界面管理 支持多种操作 可以添加/移除所有者 修改确认要求 三、如何使用多重签名 1. 创建多重签名钱包 1 2 使用合约创建:const { ethers } = require ('ethers' );
// 准备所有者地址 ];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 const MultiSigWalletFactory = await ethers.getContractFactory ('MultiSigWallet' );const wallet = await MultiSigWalletFactory .deploy (owners, 2 ); await wallet.deployed (); 使用Gnosis Safe :1. 访问Gnosis Safe 网站2. 连接MetaMask 3. 添加所有者 #### 2. 执行交易 提交交易:const tx = await wallet.submitTransaction ( ethers.utils .parseEther ('1.0' ), '0x' , { from : owner1 } );await wallet.confirmTransaction (0 , { from : owner2 }); 查看状态:
// 查看交易详情 const transaction = await wallet.transactions(0); console.log(‘To:’, transaction.to); console.log(‘Value:’, transaction.value.toString()); console.log(‘Executed:’, transaction.executed);
// 查看确认数 const count = await wallet.getConfirmationCount(0); console.log(‘Confirmations:’, count.toString());
添加所有者(需要修改合约): function addOwner(address owner) public onlyOwner { require(!isOwner(owner), “Already owner”); owners.push(owner); emit OwnerAddition(owner);
function removeOwner(address owner) public onlyOwner { require(isOwner(owner), “Not owner”); require(owners.length > required, “Cannot remove owner”);
if (owners[i] == owner) {
owners[i] = owners[owners.length - 1];
owners.pop();
break;
emit OwnerRemoval(owner);
四、应用场景 1. 企业钱包 资金管理:
权限控制:
不同级别权限 大额交易需要更多确认 定期审查权限 角色分离 2. 项目资金 ICO/IDO资金:
众筹资金安全保管 需要团队多人同意才能使用 透明化管理 社区信任 DAO资金:
3. 个人高价值账户 大额资产:
冷钱包:
4. 智能合约管理 合约升级:
参数调整:
五、最佳实践 1. 配置建议 所有者选择:
确认数量:
2/3适合大多数场景 3/5适合重要资金 根据需求调整 2. 安全实践 私钥管理:
操作流程:
3. 使用建议 测试环境:
生产环境:
使用经过审计的合约 或使用Gnosis Safe 定期更新 监控异常 六、常见问题 1. 所有者丢失私钥 解决方案:
如果还有足够的所有者,可以继续操作 可以添加新所有者 移除丢失私钥的所有者 需要其他所有者同意 2. 确认数量不足 情况:
如果所有者数量减少 可能导致无法达到确认要求 需要调整配置 解决:
3. Gas费用 考虑因素:
多重签名交易需要更多Gas 每次确认都需要Gas 执行也需要Gas 成本较高 优化:
批量操作 合理设置Gas价格 使用Layer 2 优化合约代码 七、总结 多重签名是提高区块链账户安全性的重要机制,特别适合企业、项目和高价值账户。关键要点:
安全性:
需要多人协作 降低单点故障风险 防止私钥泄露 提高资金安全 适用场景:
最佳实践:
通过正确使用多重签名,可以大大提高资金安全性,适合各种需要高安全性的场景。建议使用经过审计的解决方案如Gnosis Safe,或自己实现时进行充分的安全审计。
本文标题: 什么是多重签名
本文作者: 狂欢马克思
发布时间: 2023年01月25日 00:00
最后更新: 2026年09月16日 05:40
原始链接: https://haoxiang.eu.org/446721c5/
版权声明: 本文著作权归作者所有,均采用CC BY-NC-SA 4.0 许可协议,转载请注明出处!