---
eip: 7851
label: EIP-7851
title: Code-Controlled EOA Delegation
status: Draft
type: Standards Track
category: Core
created: 2024-12-27
requires: 7702
official: https://eips.ethereum.org/EIPS/eip-7851
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7851.md
canonical: https://eips.huatuo.xyz/zh/eip-7851/
lang: zh
tldr: 扩展 EIP-7702，新增 SETSELFDELEGATE opcode，使钱包代码能够更改自身的委托，并永久禁用该账户原来的私钥。
tags: [eip-7702, account-abstraction, eoa, opcode, security]
---

# EIP-7851: Code-Controlled EOA Delegation

> Allow EIP-7702 delegated EOAs to update delegation through code while permanently disabling residual ECDSA authority.

**TL;DR:** 扩展 EIP-7702，新增 SETSELFDELEGATE opcode，使钱包代码能够更改自身的委托，并永久禁用该账户原来的私钥。

## 它解决什么问题？

[EIP-7702](/eip-7702/) 允许 EOA 通过委托标识符 `0xef0100 || delegate_address` 指向智能合约代码。账户随后可以使用通行密钥（passkey）、多签、社交恢复或模块。但原来的 secp256k1 私钥从未失去效力：它仍然可以签署交易并重新委托账户。如果该密钥泄露，智能钱包的保护就会被绕过。

EIP-7851 让钱包代码自己接管委托的生命周期，并在此过程中永久关闭原来的密钥。

## 它如何运作

- **新标识符 `0xef0101 || delegate_address`** 表示"禁用 ECDSA 的委托"。代码执行时对它的处理与 `0xef0100` 完全相同。
- **`SETSELFDELEGATE(delegate_address)`** opcode（字节待定，9500 gas）。它只作用于当前执行上下文的地址（即正在运行该代码的账户）：
  1. 如果 `delegate_address` 为零：压入 `0`，不做任何更改；
  2. 如果账户的原始代码恰好是 23 字节，且以 `0xef0100` 或 `0xef0101` 开头：将其设置为 `0xef0101 || delegate_address`，压入 `1`；
  3. 否则：压入 `0`，不做任何更改。
  在静态上下文中它会异常终止，而 revert 会回滚该更改。
- **验证规则。** 账户一旦拥有 `0xef0101` 代码，任何由其 ECDSA 密钥签署的交易都是无效的，mempool 必须拒绝；它签署的任何 EIP-7702 授权也会被跳过。

因此，第一次成功调用就会永久禁用 ECDSA 权限；之后的调用可以切换到新的委托实现，但密钥再也不会恢复。其 gas 成本是 EIP-7702 中每个授权的基础成本减去用于签名恢复的 3000 gas。

## 示例代码

钱包代码在自身授权之后暴露该操作（使用一个假设的 opcode 的 Solidity；目前还没有编译器支持）：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract Wallet {
    function upgradeAndLockKey(address newImpl) external {
        require(msg.sender == address(this), "only self");     // e.g. via a 7821 batch
        _requireUserConfirmed();                                // passkey / multisig check
        bool ok;
        assembly {
            // ok := setselfdelegate(newImpl)   // SETSELFDELEGATE, opcode byte TBD
        }
        require(ok, "not a 7702 delegated account");
    }

    function _requireUserConfirmed() internal view { /* wallet-specific */ }
}
```

账户代码前后对比：

```text
before: 0xef0100 || <old delegate>   ; key can still sign txs
after:  0xef0101 || <new delegate>   ; key can never sign protocol txs again
```

## 需要注意

- **状态：Draft。** opcode 字节尚未分配。
- 这一切换是**不可逆的**。钱包只应在用户明确确认之后才暴露该功能，并将其视为最高权限。
- 它只阻止密钥在协议层面的使用。自己验证 ECDSA 签名的合约（例如代币的 `permit`）仍会接受该密钥的签名。配套提案 [EIP-8151](/eip-8151/) 处理的是 `ecrecover`。
- 现有的 `0xef0100` 委托不受影响。


---

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