---
eip: 7405
label: ERC-7405
title: Portable Smart Contract Accounts
status: Draft
type: Standards Track
category: ERC
created: 2023-07-26
requires: 191, 1967
official: https://eips.ethereum.org/EIPS/eip-7405
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7405.md
canonical: https://eips.huatuo.xyz/zh/eip-7405/
lang: zh
tldr: 让用户借助一次性迁移密钥和时间锁，把基于代理的智能合约账户从一个钱包提供商的实现迁移到另一个提供商的实现，这很像导出 EOA 的私钥。
tags: [account-abstraction, wallet, proxy, migration]
---

# ERC-7405: Portable Smart Contract Accounts

> Migrating smart contract accounts at the Proxy (ERC-1967) layer.

**TL;DR:** 让用户借助一次性迁移密钥和时间锁，把基于代理的智能合约账户从一个钱包提供商的实现迁移到另一个提供商的实现，这很像导出 EOA 的私钥。

## 它解决什么问题？

对于外部拥有账户（EOA），你可以从一个钱包导出助记词，再导入另一个钱包。智能合约账户（SCA），例如 [ERC-4337](/eip-4337/) 钱包，则不是这样：每个钱包提供商都有自己的合约代码，因此你的账户地址实际上与部署它的提供商绑定。更换提供商意味着创建新账户并手动转移每一项资产。

ERC-7405 提出了一条在代理层工作的迁移路径。大多数 SCA 都是 [ERC-1967](/eip-1967/) 代理，因此如果能安全地把代理重新指向钱包 B 的实现，用户就可以在更换提供商的同时保留相同的地址和资产。

## 它如何运作

**迁移密钥。** 每次迁移，钱包 A 都会生成一个全新的随机私钥或助记词（"随机操作员"）并展示给用户。它的地址用来标识这次迁移。

**步骤 1：准备（钱包 A）。** 账户调用 `prepareAccountMigration(randomOperator, signature)`。这必须以账户自身的身份通过认证（例如经由 ERC-4337 EntryPoint）。它会检查操作员的签名，在共享的 `IERC7405Registry` 中记录 `{account, createTime, lockUntil}`，并锁定账户。

**锁定期。** 锁定期间，账户应拒绝资产转移、外部调用以及对其认证方式的更改。所有者可以用正常的账户认证调用 `cancelAccountMigration()` 来中止，这可以防范被盗或恶意的迁移。

**步骤 2：处理（钱包 B）。** 用户把迁移密钥导入钱包 B，钱包 B 调用 `handleAccountMigration(newImplementation, initData, signature)`。该函数被刻意设计为任何人都可以调用；它依赖迁移密钥的签名，并要求锁定期已过。它会运行钱包 A 的清理钩子，替换 ERC-1967 的实现槽，调用 `initData` 来设置钱包 B，删除注册表条目并发出 `AccountMigrated`。

**签名**是对 `abi.encode(chainID, selector, data)` 的 [ERC-191](/eip-191/) 签名。

**存储布局。** 为避免一个钱包的状态覆盖另一个钱包的状态，实现必须把所有状态保存在位于命名空间槽位（例如 `keccak256("foo_wallet_v1.config") - 1`）的 struct 中，而不是声明普通的状态变量。

## 示例代码

账户侧的接口：

```solidity
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

interface IERC7405 {
    event AccountMigrated(address oldImplementation, address newImplementation);

    // Called via the account's own auth (e.g. EntryPoint) on the old wallet
    function prepareAccountMigration(address randomOperator, bytes calldata signature) external;

    // Called via the account's own auth to abort during the lock period
    function cancelAccountMigration() external;

    // Callable by anyone holding a valid migration-key signature, after lockUntil
    function handleAccountMigration(
        address newImplementation,
        bytes calldata initData,
        bytes calldata signature
    ) external;
}

// Namespaced storage, as the spec recommends
library FooWalletStorage {
    bytes32 constant SLOT = bytes32(uint256(keccak256("foo_wallet_v1.config")) - 1);
    struct Layout { address owner; uint256 nonce; }
    function layout() internal pure returns (Layout storage l) {
        bytes32 s = SLOT;
        assembly { l.slot := s }
    }
}
```

## 需要注意

- 状态为 **Draft**，规范目前还没有参考实现。
- 注册表意在作为一个共享的单一合约；规范没有给出部署地址。
- 迁移密钥是持有者凭证：锁定期一过，任何持有它的人都能完成迁移。锁定期和对所有者的通知是主要的防御手段。
- 规范文本在一处把步骤 2 的函数叫作 `handleWalletMigration`，而在接口中叫作 `handleAccountMigration`；应实现的是接口中的名称。
- 命名空间存储的思路与 [ERC-7201](/eip-7201/) 密切相关。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7405
