EIPs Explained

ERC-5216 · Last Call(最后征求意见) · ERC · 2022-07-11

ERC-5216: ERC-1155 Allowance Extension

为 ERC-1155 增加 ERC-20 风格的 approve(operator, id, amount) 和 allowance(),让你可以只允许市场转移某个 token id 的有限数量,而不是全部代币。

它解决什么问题?

ERC-1155 只有一种授权机制:setApprovalForAll(operator, true),它让操作者可以转移你在该合约中持有的所有代币,无论 id 是什么、数量多少。要在市场上架一件物品,你必须把整个合集的无限访问权限交给市场。一旦出现漏洞或操作者被攻破,你的全部资产都可能被转空。

ERC-5216 借用了 ERC-20 的额度模型,让所有者只授权所需的部分:某个 id 的指定 amount。

它如何运作

合约在 ERC-1155 之上实现 IERC5216:

event Approval(address indexed account, address indexed operator, uint256 id, uint256 amount);
function approve(address operator, uint256 id, uint256 amount) external;
function allowance(address account, address operator, uint256 id) external view returns (uint256);
  • approve 为某一个 id 设置(而非累加)操作者的额度并触发 Approval。操作者不能是调用者本人。
  • 当调用者对该 id 拥有足够额度时,safeTransferFrom 必须成功,并且必须扣减已转移的数量;通过 setApprovalForAll 获得授权的调用者除外(这仍然意味着无限额度)。
  • safeBatchTransferFrom 必须检查批次中每个 id 的额度。
  • supportsInterface(0x1be07d74) 返回 true(ERC-165)。

示例代码

基于 OpenZeppelin 的 ERC-1155(v5)实现的核心逻辑,仅作演示:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

contract ERC1155Allowance is ERC1155 {
    event Approval(address indexed account, address indexed operator, uint256 id, uint256 amount);

    mapping(address => mapping(address => mapping(uint256 => uint256))) private _allowances;

    constructor() ERC1155("https://example.invalid/{id}.json") {}

    function approve(address operator, uint256 id, uint256 amount) external {
        require(operator != msg.sender, "self approval");
        _allowances[msg.sender][operator][id] = amount;
        emit Approval(msg.sender, operator, id, amount);
    }

    function allowance(address account, address operator, uint256 id) public view returns (uint256) {
        return _allowances[account][operator][id];
    }

    function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes memory data)
        public override
    {
        if (from != msg.sender && !isApprovedForAll(from, msg.sender)) {
            uint256 a = _allowances[from][msg.sender][id];
            require(a >= value, "insufficient allowance");
            _allowances[from][msg.sender][id] = a - value;
        }
        _safeTransferFrom(from, to, id, value, data);
    }

    function supportsInterface(bytes4 iid) public view override returns (bool) {
        return iid == 0x1be07d74 || super.supportsInterface(iid);
    }
    // safeBatchTransferFrom must apply the same check per id (omitted)
}

需要注意

  • 状态:Last Call(截止日期在 2022 年,从未进入 Final)。该接口可能仍被视为尚未定型。
  • 只有当市场真正请求按 id 授权额度时才有好处;许多市场仍然要求 setApprovalForAll。
  • 与 ERC-20 一样,把非零额度改为另一个非零值可能被抢跑。请先设为 0,并撤销未使用的额度。
  • setApprovalForAll 仍然凌驾于一切之上。审计操作者能转移什么时,请检查 isApprovedForAll。

#erc-1155 #approvals #allowance #nft #security

详细信息

作者
Iván Mañús (@ivanmmurciaua), Juan Carlos Cantó (@EscuelaCryptoES)
状态
Last Call(最后征求意见)
类型
Standards Track / ERC
创建日期
2022-07-11
依赖的提案
被以下提案依赖

机器可读数据: Markdown · JSON