ERC-5559 Cross-chain write deferral protocol (Cross Chain Write Deferral Protocol)
Stagnant · Names & metadata · created 2022-06-23
A standard that allows a smart contract to hand off data-change requests to an external handler, such as an L2 contract or an off-chain database, so that data like ENS addresses or identity information can be modified without the cost of on-chain storage.
Why it is needed
Without this standard, even when storing frequently changing data such as ENS domains or digital identity information on an L2 or off-chain database to reduce gas costs, each contract must use its own non-standard approach. Clients and wallets must know in advance where each protocol stores its data and how modification requests are made, so client-side code must be rebuilt every time the storage backend changes. In addition, each project must separately guard against the risk of requests being forged or misdirected to the wrong chain, especially mainnet, during the process of moving data off-chain or to an L2, resulting in inconsistent security levels across projects.
What to implement
event L2HandlerDefaultChainIdChanged(uint256 indexed previousChainId, uint256 indexed newChainId);: An event emitted when the default chain ID of the L2 handler changes.event L2HandlerContractAddressChanged(uint256 indexed chainId, address indexed previousContractAddress, address indexed newContractAddress);: An event emitted when the contract address of the L2 handler for a specific chain ID changes.event OffChainDatabaseHandlerURLChanged(string indexed previousUrl, string indexed newUrl);: An event emitted when the URL of the off-chain database handler changes.
In depth
Handler classification: L2 contracts and off-chain databases
This standard divides the external handlers to which a mutation request can be forwarded into two types. L2 contracts are EVM-compatible, so access can be managed using Ethereum-native attributes such as addresses. Off-chain databases are not an EVM environment, so additional parameters and a signature are required to verify the authenticity and validity of a request. By distinguishing and returning the handler type, a protocol can design different permission management and security levels suited to each storage location.
StorageHandledByL2: when deferred to an L2
When a client calls a function on the contract, the contract does not store the data and instead reverts with a StorageHandledByL2(chainId, contractAddress) error. The client directly selects the RPC corresponding to the chainId contained in this error and sends a new transaction to contractAddress using the original callData unchanged. For example, the setAddr(bytes32 node, address a) function does not process the request in its body but instead responds with revert StorageHandledByL2(10, _l2HandlerContractAddress), handing off processing to the L2 contract on chain ID 10.
StorageHandledByOffChainDatabase and EIP-712 signatures
When using off-chain storage, the contract reverts with a StorageHandledByOffChainDatabase(sender, url, data) error. sender is an EIP-712 domain definition (domainData: name, version, chainId, verifyingContract) and data is messageData(functionSelector, sender, parameter[] parameters, expirationTimestamp). The client uses this domainData and messageData to produce an EIP-712 typed data signature, then sends an HTTP POST request to url with sender, data, and signature in the JSON body. The gateway verifies the signature to confirm the authenticity and authorization of the request before processing it.
Double deferral: L1 > L2 > off-chain
In some cases a modification request may be deferred twice. If the L1 contract first reverts with StorageHandledByL2, the client sends a transaction with the original callData to that L2 contract, and if that L2 contract in turn reverts with StorageHandledByOffChainDatabase, the client finally sends an HTTP POST request containing an EIP-712 signature to the off-chain gateway.
Event emission requirements
When a handler's key variables change, the corresponding event MUST be emitted. For an L2 handler, changes to chainId and contractAddress must emit the L2HandlerDefaultChainIdChanged and L2HandlerContractAddressChanged events respectively, and a change to the off-chain database handler's url must emit the OffChainDatabaseHandlerURLChanged event. This is meant to increase transparency around administrative actions.
Design of passing call information via revert
This standard applies the technique EIP-3668 used for off-chain lookups to mutation requests as well. Without modifying the ABI or the EVM, revert can be used to convey the processing location and required parameters without changing the function signature, allowing the deferral feature to be added while keeping existing interfaces and infrastructure unchanged.
Backward compatibility
Existing contracts that do not use this standard are unaffected, and clients can add support for this standard to all contract calls with no additional overhead. However, contracts that require this standard will not work with clients that have not implemented it, and if such a client makes a call, the exception will simply be passed through to the user.
Proposal status
This ERC is currently in Stagnant status, a proposal that is no longer being discussed and has stalled. The specification may change in the future or may never be adopted as is.
Things to watch out for
- No re-delaying to mainnet: Change requests must not be delayed back to the Ethereum mainnet. Such attempts could become a hijacking vector that tricks users into signing and sending malicious transactions.
- Preserve original calldata when handing off to L2: When a transaction is delayed to an L2 system, the original calldata must be used as is. Otherwise there is a risk that the transaction's context could be maliciously altered.
- Fingerprinting attack risk: When reverting with StorageHandledByOffChainDatabase, since data includes msg.sender, sending the HTTP request can link the wallet address to the requester's IP address, allowing it to be tracked.
- Recommendation to offer an on-chain storage option: Contract developers should offer an option that lets users choose to store data directly on-chain on the network, so they can weigh the cost and risk themselves.
- Recommendation for allow/deny list hooks: Client libraries should provide hooks that can rewrite or block StorageHandledByOffChainDatabase calls, so domains can be easily managed with allow lists or deny lists.