ERC-725 Universal Data Store and Executable Account Standard (General data key/value store and execution)
Draft · Accounts & wallets · created 2017-10-02
A standard that adds a freely readable and writable key/value data store for smart contracts (ERC725Y) and a universal execution function (ERC725X) that can call other addresses and deploy contracts.
Why it is needed
For an ordinary smart contract, the kinds of data it can store and the actions it can perform are fixed in code at deployment time. If you later want to attach a new kind of information, you have to redeploy the contract, and if you want the contract to act like an account calling arbitrary other contracts, you have to write a dedicated function for each case. If every contract implements these features differently, wallets and apps have no common way to read data or request execution. This standard grows out of the motivation to build a smart contract account system that can be used for a long time, and solves the problem by standardizing two common building blocks: data storage and execution. The two blocks can be used together to build an account, or ERC725Y alone can be used to extend the metadata of an NFT or token.
What to implement
function execute(uint256 operationType, address target, uint256 value, bytes memory data) external payable returns(bytes memory): A universal execution function that only the owner can call. Depending on the operation type, it performs a call to another contract, a coin transfer, or a contract deployment, and returns the result.function executeBatch(uint256[] memory operationsType, address[] memory targets, uint256[] memory values, bytes[] memory datas) external payable returns(bytes[] memory): Processes multiple executions at once. The arrays passed in must all be the same length, and the result of each one is returned as an array.event Executed(uint256 indexed operationType, address indexed target, uint256 indexed value, bytes4 data);: An event that must be emitted when a call is executed with operation type 0, 3, or 4.event ContractCreated(uint256 indexed operationType, address indexed contractAddress, uint256 indexed value, bytes32 salt);: An event that must be emitted when a new contract is deployed with operation type 1 or 2.function getData(bytes32 dataKey) external view returns(bytes memory): Reads back the value stored under a given data key. A view function that anyone can call.function getDataBatch(bytes32[] memory dataKeys) external view returns(bytes[] memory): Reads the values of multiple data keys at once and returns them as an array.function setData(bytes32 dataKey, bytes memory dataValue) external: Stores a byte value under a single data key. Only the owner can call this.function setDataBatch(bytes32[] memory dataKeys, bytes[] memory dataValues) external: Stores values under multiple data keys at once. The two arrays must be the same length, and only the owner can call this.event DataChanged(bytes32 indexed dataKey, bytes dataValue): An event that must be emitted whenever a value is successfully stored under a data key.
In depth
Two sub-standards and the ownership structure
This standard consists of two sub-standards, the universal key/value store ERC725Y and the universal execution function ERC725X, which can be used together or separately. The account version is standardized separately as LSP0-ERC725Account. The contract is controlled by a single owner, who can be either a contract or an externally owned account. It requires ERC-173 and should implement an owner() view function, a transferOwnership(address newOwner) function, and an OwnershipTransferred event. Both sub-standards must implement ERC-165's supportsInterface, with interface IDs of 0x7545acac for ERC725X and 0x629aa694 for ERC725Y.
execute and the five operation types
execute (function selector 0x44c028fe) handles calling another contract, transferring native tokens, and deploying a new contract through a single function. The operationType is 0 for CALL, 1 for CREATE, 2 for CREATE2, 3 for STATICCALL, and 4 for DELEGATECALL, with more possibly added in the future. The source text explicitly calls type 4 a potentially dangerous operation type. Only the current owner may call it, and it must revert if execution or deployment fails. For deployment (types 1, 2), target should be set to address(0), and for STATICCALL and DELEGATECALL (types 3, 4), value should be set to 0. The return value is either the call's return bytes or the deployed contract's address.
Format of the data parameter
The contents of the data field vary by operation type. For CALL, STATICCALL, and DELEGATECALL, it is arbitrary bytes or an abi-encoded function call. For CREATE, it is the creation bytecode of the contract to deploy followed by the abi-encoded constructor arguments. For CREATE2, it is the creation bytecode followed by the abi-encoded constructor arguments and then a bytes32 salt. In other words, data = creation code + constructor arguments + salt. For the detailed behavior of CREATE2, it directs readers to EIP-1014.
The ERC725Y key/value store
The store is a structure mapping bytes32 data keys to byte values. It is read with getData (0x54f6127f) and written with setData (0x7f23690c). Only the current owner may write, and a DataChanged event must be emitted on success. Data keys can be defined freely or via a standard, and a common approach is to use the hash of a word. For example, keccak256('ERCXXXMyNewKeyType') becomes 0x6935a24ea384927f250ee0b954ed498cd9203fc5d2bf95c735e52e6ca675e047. LSP2-ERC725JSONSchema is a standard that more explicitly defines key types, value types, and encoding. Stored data can be read and written not only by off-chain apps but also by other contracts, allowing other protocols to interpret this data in new ways and react to it.
Batch functions and removal of overloading
executeBatch (0x31858452), getDataBatch (0xdedff9c6), and setDataBatch (0x97902421) are functions that process multiple items at once to save gas, and their array parameters must all be the same length. executeBatch emits an Executed or ContractCreated event for each call it processes. Since ERC725v2 in 2018/19, the contract has remained compatible with the current version, with the main changes being the interface ID and event parameters, plus the addition of batch functions. Since 2023, function overloading has been removed; although Solidity supports overloading, it is not widely supported in other blockchain languages, so for language independence, functions taking arrays were instead given names with a Batch suffix.
Why universal storage was chosen
The universal key/value storage approach was chosen to allow upgrades over time. Stored values can be changed later, and other smart contract protocols can interpret this data in new ways and react differently when interacting with the ERC725 contract. Both single-key and multi-key lookups are provided to keep gas costs minimal for both use cases. The reference implementation is provided as ERC725.sol.
A proposal still in Draft status
This document was created in October 2017 and is currently in Draft status. It should be kept in mind that this is a proposal that has not been finalized and its contents may change.
Things to watch out for
- Reentrancy and other call-chain attacks: Because this contract allows universal execution, special care is needed to guard against reentrancy attacks and other call-chain attacks.
- The danger of delegatecall (operation type 4): Using delegatecall with operation type 4 allows the called contract to change the state of the calling contract, including arbitrarily changing the owner variable and the ERC725Y data store entries. It should be considered that harmful state-changing operations, including a call to selfdestruct, are also possible.