多签钱包 Link to heading
多签钱包解决的核心问题:资产安全与风险分散 Link to heading
在传统的 EOA 钱包中,单一私钥控制所有资产,存在以下风险:
- 私钥丢失:一旦私钥丢失,资产永久无法找回
- 私钥泄露:私钥被盗,资产立即面临风险
- 单点故障:没有备份机制,容错性极低
- 权限过度集中:一个人掌控所有决策权
多签钱包通过以下机制解决这些问题:
- M/N 多重签名:需要 N 个签名者中的 M 个同意才能执行交易
- 风险分散:将控制权分散到多个参与者
- 容错机制:部分私钥丢失不影响钱包使用
- 治理机制:重要决策需要多方共识
多签钱包的通用架构 Link to heading
多签钱包 = 智能合约 + 多个所有者 + 签名阈值
核心组件:
- 所有者列表(Owners):有权签名的地址集合
- 签名阈值(Threshold):执行交易所需的最少签名数
- 交易队列(Transaction Queue):待执行的交易列表
- 确认机制(Confirmation):所有者对交易的签名确认
工作流程:
提交交易 → 收集签名 → 达到阈值 → 执行交易
多重签名架构 Link to heading
-
单一私钥风险高,丢失即资产全失
-
多重签名方案:需要 M 个签名中的 N 个才能执行交易(M/N 多签)
-
Gnosis Safe:最流行的多签钱包标准,支持 1/1 到 N/N 任意组合
-
社交恢复:通过朋友/家人帮助恢复钱包访问权限
智能合约钱包实现 Link to heading
-
智能合约钱包通过代码逻辑控制资产,提供比 EOA 更强大的功能
-
核心原理:使用
call
函数执行任意交易,实现可编程的资产管理
基础合约钱包实现
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title ContractWallet - 智能合约钱包
* @dev 一个简单的智能合约钱包实现,支持接收资金和执行任意交易
*/
contract ContractWallet {
// ============ 事件定义 ============
/**
* @dev 存款事件 - 当合约接收到资金时触发
*/
event Deposit(address indexed sender, uint amount, uint balance);
/**
* @dev 执行交易事件 - 当合约执行外部交易时触发
*/
event ExecuteTransaction(
address indexed to,
uint value,
bytes data
);
// ============ 状态变量 ============
/**
* @dev 钱包所有者地址
*/
address public owner;
// ============ 修饰符 ============
/**
* @dev 只有所有者可以调用的修饰符
*/
modifier onlyOwner() {
require(owner == msg.sender, "not owner");
_;
}
// ============ 构造函数 ============
/**
* @dev 构造函数 - 部署时设置钱包所有者
*/
constructor(address _owner) {
owner = _owner;
}
// ============ 接收函数 ============
/**
* @dev receive 函数 - 接收 ETH 转账
*/
receive() external payable {
emit Deposit(msg.sender, msg.value, address(this).balance);
}
// ============ 核心功能函数 ============
/**
* @dev 执行任意交易 - 智能合约钱包的核心功能
* @param _to 目标地址(可以是 EOA 地址或合约地址)
* @param _value 要发送的 ETH 数量(单位:wei)
* @param _data 调用数据(ABI 编码的函数调用数据)
*/
function executeTransaction(
address _to,
uint _value,
bytes memory _data
) public onlyOwner {
// 使用 call 执行低级调用
(bool success, ) = _to.call{value: _value}(_data);
// 检查调用是否成功
require(success, "tx failed");
// 触发执行交易事件
emit ExecuteTransaction(_to, _value, _data);
}
}
多重签名钱包实现
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title MultiSigWallet - 多重签名钱包
* @dev 需要多个所有者签名才能执行交易的智能合约钱包
*/
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(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() 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
);
}
}
使用示例
import { ethers } from "ethers";
async function deployAndUseMultiSig() {
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");
const [owner1, owner2, owner3] = await provider.listAccounts();
// 1. 部署多签钱包(3个所有者,需要2个签名)
const MultiSigWallet = await ethers.getContractFactory("MultiSigWallet");
const multiSig = await MultiSigWallet.deploy(
[owner1, owner2, owner3], // 所有者列表
2 // 需要2个签名
);
console.log("多签钱包地址:", multiSig.address);
// 2. 向钱包存入资金
await owner1.sendTransaction({
to: multiSig.address,
value: ethers.utils.parseEther("1.0")
});
// 3. 提交交易(转账0.5 ETH)
const tx = await multiSig.connect(owner1).submitTransaction(
"0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6", // 接收地址
ethers.utils.parseEther("0.5"), // 金额
"0x" // 数据
);
const receipt = await tx.wait();
const txIndex = 0; // 第一笔交易
// 4. 第二个所有者确认交易
await multiSig.connect(owner2).confirmTransaction(txIndex);
// 5. 执行交易(已有2个确认)
await multiSig.connect(owner1).executeTransaction(txIndex);
console.log("多签交易执行成功");
}
// 使用 ERC20 代币转账示例
async function multiSigERC20Transfer() {
const erc20 = new ethers.Contract(tokenAddress, erc20ABI, provider);
// 编码 transfer 函数调用
const transferData = erc20.interface.encodeFunctionData("transfer", [
recipientAddress,
ethers.utils.parseUnits("100", 18) // 100 tokens
]);
// 提交到多签钱包
await multiSig.submitTransaction(
tokenAddress, // ERC20 合约地址
0, // 不发送 ETH
transferData // 调用数据
);
}
账户抽象(Account Abstraction) Link to heading
-
EIP-4337:统一 EOA 和 CA 的用户体验,让智能合约钱包像 EOA 一样易用
-
EIP-7702:临时将 EOA 转换为智能合约账户,享受 CA 的高级功能
-
Paymaster:第三方代付 Gas 费,实现真正的 gasless 体验
-
Bundler:批量处理用户操作,提高网络效率
小结 Link to heading
- CA 账户通过智能合约代码控制资产,提供比 EOA 更强大和灵活的功能
- 多重签名、社交恢复等机制大大提升了资产安全性
- Account Abstraction 正在统一 EOA 和 CA 的用户体验
- 智能合约钱包是 Web3 用户体验升级的重要方向