---
eip: 7377
label: EIP-7377
title: Migration Transaction
status: Stagnant
type: Standards Track
category: Core
created: 2023-07-21
requires: 170, 1559, 2200, 2718
official: https://eips.ethereum.org/EIPS/eip-7377
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7377.md
canonical: https://eips.huatuo.xyz/zh/eip-7377/
lang: zh
tldr: 一种拟议的一次性交易类型，可把现有 EOA 原地变成智能合约钱包，同时保留其地址和资产。
tags: [account-abstraction, transactions, eoa, core]
---

# EIP-7377: Migration Transaction

> Allow EOAs to send a one-time transaction which deploys code at their account.

**TL;DR:** 一种拟议的一次性交易类型，可把现有 EOA 原地变成智能合约钱包，同时保留其地址和资产。

## 它解决什么问题？

智能合约钱包提供恢复、批量操作、支出限额等功能，但大多数用户仍把资产放在普通 EOA 中。迁移到新的合约地址意味着要逐个转移每一种代币、NFT 和仓位，既昂贵又繁琐。许多用户从不迁移。

EIP-7377 提出了一条协议层面的捷径：让 EOA 发送一笔特殊交易，把合约代码安装到**它自己的地址**上，这样账户就变成了智能合约，而无需转移任何东西。

## 它如何运作

一种新的 [EIP-2718](/eip-2718/) 交易类型 `0x04`：

```
0x04 || rlp([chainId, nonce, maxFeePerGas, maxPriorityFeePerGas, gasLimit,
             codeAddr, storage, data, value, accessList, yParity, r, s])
```

- 没有 `to` 字段。取而代之的是，`codeAddr` 指向一个已部署的合约，其代码会被复制到发送者的账户上。指向已有代码，可以避免一遍又一遍地把同样的钱包字节码放进 calldata。
- `storage` 是一组写入发送者存储的 `(slot, value)` 对，例如用来设置钱包所有者。
- 之后，该交易会以 `data` 和 `value` 对发送者（现在已是合约）的账户发起一次普通的 [EIP-1559](/eip-1559/) 风格调用，因此可以执行任意初始化。
- `codeAddr` 处的代码必须非空，并且小于 24,576 字节的上限。
- 固有 gas 是通常的 1559 公式再加上**每个 storage 元组 20,000 gas**。部署不额外收费，因为客户端对相同的代码只存储一份。
- 执行期间，`tx.origin` 被设置为 `keccak256(sender)[0..20]` 而不是发送者本身，因此现有的 `msg.sender == tx.origin`"这是 EOA 吗？"检查仍然表现如常。

由于 [EIP-3607](/eip-3607/)（有代码的账户不能发送交易），迁移只能发生一次。

## 示例代码

客户端处理该交易时所做操作的伪代码：

```python
def apply_migration_tx(state, tx, sender):
    code = state[tx.codeAddr].code
    assert 0 < len(code) < 24576
    state[sender].code = code                 # install wallet code at the EOA
    for slot, value in tx.storage:
        state[sender].storage[slot] = value   # e.g. owner, guardians
    origin = keccak256(sender)[:20]           # rewritten tx.origin
    return evm_call(caller=sender, to=sender, origin=origin,
                    data=tx.data, value=tx.value)
```

## 需要注意

- 状态为 **Stagnant**。它没有被安排进任何网络升级。
- 其根本目标，即让 EOA 拥有智能账户功能，已在 Pectra 中由 [EIP-7702](/eip-7702/) 解决；7702 让 EOA *委托*给合约代码，而不是永久替换它的代码。
- 迁移之后私钥仍然存在。[ERC-2612](/eip-2612/) `permit` 之类基于签名的方案仍然可以用它签名，因此泄露或丢失私钥依然危险。迁移也是按链进行的，所以其他链上的同一地址仍然是普通 EOA。
- 钱包需要极其谨慎地对待这种交易类型，因为盲签的迁移可能把账户交给攻击者的代码。


---

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