diff --git a/EIPS/eip-820.md b/EIPS/eip-820.md index 1cc8eff5..3dda93b8 100644 --- a/EIPS/eip-820.md +++ b/EIPS/eip-820.md @@ -1,6 +1,6 @@ --- eip: 820 -title: Pseudo-introspection registry contract +title: Pseudo-introspection Registry Contract author: Jordi Baylina , Jacques Dafflon discussions-to: https://github.com/ethereum/EIPs/issues/820 status: Draft @@ -11,165 +11,200 @@ created: 2018-01-05 ## Simple Summary -This standard defines a universal registry smart contract where any address (contract or regular account) can register which interface it implements and which smart contract is responsible for its implementation. +This standard defines a universal registry smart contract where any address (contract or regular account) can register which interface it supports and which smart contract is responsible for its implementation. -This standard keeps backwards compatibility with [EIP-165](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md) +This standard keeps backwards compatibility with [ERC165]. ## Abstract -This standard attempts to define a registry where smart contracts and regular accounts can publish which functionalities they implement. +This standard attempts to define a registry where smart contracts and regular accounts can publish which functionalities they implement—either directly or through a proxy contract . The rest of the world can query this registry to ask if a specific address implements a given interface and which smart contract handles its implementation. -This registry can be deployed on any chain and will share the exact same address. +This registry MAY be deployed on any chain and will share the exact same address. -Interfaces where the last 28 bytes are `0` are considered [EIP-165](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md) interfaces, and this registry -will forward the call to the contract to see if they implement that interface. +Interfaces with zeroes (`0`) as the the last 28 bytes are considered [ERC165] interfaces, and this registry SHALL forward the call to the contract to see if it implements the interface. -This contract will act also as an [EIP-165](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md) cache in order to safe gas. +This contract also acts as an [ERC165] cache in order to reduce gas consumption. ## Motivation -There has been different approaches to define pseudo-introspection in the Ethereum. The first is [EIP-165](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md) which has the problem that it is not available for regular accounts to use. The second approach is EIP-672 which uses reverseENS. Using reverseENS, has two issues. First, it is unnecessarily complex, and second, ENS is still a centralized contract controlled by a multisig. This multisig, theoretically would be able to modify the system. +There has been different approaches to define pseudo-introspection in Ethereum. The first is [ERC165] which has the limitation that it cannot be used by regular accounts. The second attempt is [ERC672] which uses reverseENS. Using reverseENS has two issues. First, it is unnecessarily complex, and second, ENS is still a centralized contract controlled by a multisig. This multisig, theoretically would be able to modify the system. -This standard is much simpler than [EIP-672](https://github.com/ethereum/EIPs/issues/672) and it is absolutely decentralized. +This standard is much simpler than [ERC672] and it is fully decentralized. -This standard also solves the problem of having different addresses for different chains. +This standard also provides a unique address for all chains. Thus solving the problem of resolving the correct registry address for different chains. ## Specification -### The smart contract +### [ERC820] Registry Smart Contract -```solidity +> This is an exact copy of the code of the [ERC820 registry smart contract]. + +``` solidity pragma solidity 0.4.24; +// IV is a simply value to have a "nice" address for the registry, starting with `0x820`. +// IV: 12302 +/// @dev The interface a contract MUST implement if it is the implementer of +/// some interface for any address other than itself. interface ERC820ImplementerInterface { - /// @notice Contracts that implement an interferce in behalf of another contract must return true - /// @param addr Address that the contract woll implement the interface in behalf of - /// @param interfaceHash keccak256 of the name of the interface - /// @return ERC820_ACCEPT_MAGIC if the contract can implement the interface represented by - /// `ìnterfaceHash` in behalf of `addr` - function canImplementInterfaceForAddress(address addr, bytes32 interfaceHash) view public returns(bytes32); + /// @notice Indicates whether the contract implements the interface `interfaceHash` for the address `addr` or not. + /// @param addr Address for which the contract will implement the interface + /// @param interfaceHash keccak256 hash of the name of the interface + /// @return ERC820_ACCEPT_MAGIC only if the contract implements `ìnterfaceHash` for the address `addr`. + function canImplementInterfaceForAddress(address addr, bytes32 interfaceHash) public view returns(bytes32); } +/// @title ERC820 Pseudo-introspection Registry Contract +/// @author Jordi Baylina and Jacques Dafflon +/// @notice This contract is the official implementation of the ERC820 Registry. +/// @notice For more details, see https://eips.ethereum.org/EIPS/eip-820 contract ERC820Registry { - bytes4 constant InvalidID = 0xffffffff; + /// @notice ERC165 Invalid ID. + bytes4 constant INVALID_ID = 0xffffffff; + /// @notice Method ID for the ERC165 supportsInterface method (= `bytes4(keccak256('supportsInterface(bytes4)'))`). bytes4 constant ERC165ID = 0x01ffc9a7; - bytes32 constant ERC820_ACCEPT_MAGIC = keccak256("ERC820_ACCEPT_MAGIC"); + /// @notice Magic value which is returned if a contrct implements an interface on behalf of some other address. + bytes32 constant ERC820_ACCEPT_MAGIC = keccak256(abi.encodePacked("ERC820_ACCEPT_MAGIC")); mapping (address => mapping(bytes32 => address)) interfaces; mapping (address => address) managers; - mapping (address => mapping(bytes4 => bool)) erc165Cache; + mapping (address => mapping(bytes4 => bool)) erc165Cached; + /// @notice Indicates a contract is the `implementer` of `interfaceHash` for `addr`. event InterfaceImplementerSet(address indexed addr, bytes32 indexed interfaceHash, address indexed implementer); + /// @notice Indicates `newManager` is the address of the new manager for `addr`. event ManagerChanged(address indexed addr, address indexed newManager); - /// @notice Query the hash of an interface given a name - /// @param interfaceName Name of the interfce - function interfaceHash(string interfaceName) public pure returns(bytes32) { - return keccak256(interfaceName); + /// @notice Query if an address implements an interface and through which contract. + /// @param _addr Address being queried for the implementer of an interface. + /// (If `_addr == 0` then `msg.sender` is assumed.) + /// @param _interfaceHash keccak256 hash of the name of the interface as a string. + /// E.g., `web3.utils.keccak256('ERC777Token')`. + /// @return The address of the contract which implements the interface `_interfaceHash` for `_addr` + /// or `0x0` if `_addr` did not register an implementer for this interface. + function getInterfaceImplementer(address _addr, bytes32 _interfaceHash) external view returns (address) { + address addr = _addr == 0 ? msg.sender : _addr; + if (isERC165Interface(_interfaceHash)) { + bytes4 erc165InterfaceHash = bytes4(_interfaceHash); + return implementsERC165Interface(addr, erc165InterfaceHash) ? addr : 0; + } + return interfaces[addr][_interfaceHash]; } - /// @notice GetManager - function getManager(address addr) public view returns(address) { + /// @notice Sets the contract which implements a specific interface for an address. + /// Only the manager defined for that address can set it. + /// (Each address is the manager for itself until it sets a new manager.) + /// @param _addr Address to define the interface for. (If `_addr == 0` then `msg.sender` is assumed.) + /// @param _interfaceHash keccak256 hash of the name of the interface as a string. + /// For example, `web3.utils.keccak256('ERC777TokensRecipient')` for the `ERC777TokensRecipient` interface. + function setInterfaceImplementer(address _addr, bytes32 _interfaceHash, address _implementer) external { + address addr = _addr == 0 ? msg.sender : _addr; + require(getManager(addr) == msg.sender, "Not the manager"); + + require(!isERC165Interface(_interfaceHash), "Must not be a ERC165 hash"); + if (_implementer != 0 && _implementer != msg.sender) { + require( + ERC820ImplementerInterface(_implementer) + .canImplementInterfaceForAddress(addr, _interfaceHash) == ERC820_ACCEPT_MAGIC, + "Does not implement the interface" + ); + } + interfaces[addr][_interfaceHash] = _implementer; + emit InterfaceImplementerSet(addr, _interfaceHash, _implementer); + } + + /// @notice Sets the `_newManager` as manager for the `_addr` address. + /// The new manager will be able to call `setInterfaceImplementer` for `_addr`. + /// @param _addr Address for which to set the new manager. (If `_addr == 0` then `msg.sender` is assumed.) + /// @param _newManager Address of the new manager for `addr`. (Pass `0x0` to reset the manager to `_addr` itself.) + function setManager(address _addr, address _newManager) external { + address addr = _addr == 0 ? msg.sender : _addr; + require(getManager(addr) == msg.sender, "Not the manager"); + managers[addr] = _newManager == addr ? 0 : _newManager; + emit ManagerChanged(addr, _newManager); + } + + /// @notice Get the manager of an address. + /// @param _addr Address for which to return the manager. + /// @return Address of the manager for a given address. + function getManager(address _addr) public view returns(address) { // By default the manager of an address is the same address - if (managers[addr] == 0) { - return addr; + if (managers[_addr] == 0) { + return _addr; } else { - return managers[addr]; + return managers[_addr]; } } - /// @notice Sets an external `manager` that will be able to call `setInterfaceImplementer()` - /// on behalf of the address. - /// @param _addr Address that you are defining the manager for. (0x0 if is msg.sender) - /// @param newManager The address of the manager for the `addr` that will replace - /// the old one. Set to 0x0 if you want to remove the manager. - function setManager(address _addr, address newManager) public { - address addr = _addr == 0 ? msg.sender : _addr; - require(getManager(addr) == msg.sender); - managers[addr] = newManager == addr ? 0 : newManager; - ManagerChanged(addr, newManager); + /// @notice Compute the keccak256 hash of an interface given its name. + /// @param _interfaceName Name of the interface. + /// @return The keccak256 hash of an interface name. + function interfaceHash(string _interfaceName) public pure returns(bytes32) { + return keccak256(abi.encodePacked(_interfaceName)); } - /// @notice Query if an address implements an interface and thru which contract - /// @param _addr Address that is being queried for the implementation of an interface - /// (if _addr == 0 them `msg.sender` is assumed) - /// @param iHash SHA3 of the name of the interface as a string - /// Example `web3.utils.sha3('ERC777Token`')` - /// @return The address of the contract that implements a specific interface - /// or 0x0 if `addr` does not implement this interface - function getInterfaceImplementer(address _addr, bytes32 iHash) constant public returns (address) { - address addr = _addr == 0 ? msg.sender : _addr; - if (isERC165Interface(iHash)) { - bytes4 i165Hash = bytes4(iHash); - return erc165InterfaceSupported(addr, i165Hash) ? addr : 0; - } - return interfaces[addr][iHash]; - } + /* --- ERC165 Related Functions --- */ - /// @notice Sets the contract that will handle a specific interface; only - /// a `manager` defined for that address can set it. - /// ( Each address is the manager for itself until a new manager is defined) - /// @param _addr Address that you want to define the interface for - /// (if _addr == 0 them `msg.sender` is assumed) - /// @param iHash SHA3 of the name of the interface as a string - /// For example `web3.utils.sha3('Ierc777')` for the Ierc777 - function setInterfaceImplementer(address _addr, bytes32 iHash, address implementer) public { - address addr = _addr == 0 ? msg.sender : _addr; - require(getManager(addr) == msg.sender); - - require(!isERC165Interface(iHash)); - if ((implementer != 0) && (implementer!=msg.sender)) { - require(ERC820ImplementerInterface(implementer).canImplementInterfaceForAddress(addr, iHash) - == ERC820_ACCEPT_MAGIC); - } - interfaces[addr][iHash] = implementer; - InterfaceImplementerSet(addr, iHash, implementer); - } - - -/// ERC165 Specific - - function isERC165Interface(bytes32 iHash) internal pure returns (bool) { - return iHash & 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0; - } - - function erc165InterfaceSupported(address _contract, bytes4 _interfaceId) constant public returns (bool) { - if (!erc165Cache[_contract][_interfaceId]) { - erc165UpdateCache(_contract, _interfaceId); + /// @notice Checks whether a contract implements an ERC165 interface or not. + /// The result is cached. If the cache is out of date, it must be updated by calling `updateERC165Cache`. + /// @param _contract Address of the contract to check. + /// @param _interfaceId ERC165 interface to check. + /// @return `true` if `_contract` implements `_interfaceId`, false otherwise. + /// @dev This function may modify the state when updating the cache. However, this function must have the `view` + /// modifier since `getInterfaceImplementer` also calls it. If called from within a transaction, the ERC165 cache + /// is updated. + function implementsERC165Interface(address _contract, bytes4 _interfaceId) public view returns (bool) { + if (!erc165Cached[_contract][_interfaceId]) { + updateERC165Cache(_contract, _interfaceId); } return interfaces[_contract][_interfaceId] != 0; } - function erc165UpdateCache(address _contract, bytes4 _interfaceId) public { - interfaces[_contract][_interfaceId] = - erc165InterfaceSupported_NoCache(_contract, _interfaceId) ? _contract : 0; - erc165Cache[_contract][_interfaceId] = true; + /// @notice Updates the cache with whether contract implements an ERC165 interface or not. + /// @param _contract Address of the contract for which to update the cache. + /// @param _interfaceId ERC165 interface for which to update the cache. + function updateERC165Cache(address _contract, bytes4 _interfaceId) public { + interfaces[_contract][_interfaceId] = implementsERC165InterfaceNoCache(_contract, _interfaceId) ? _contract : 0; + erc165Cached[_contract][_interfaceId] = true; } - function erc165InterfaceSupported_NoCache(address _contract, bytes4 _interfaceId) public constant returns (bool) { + /// @notice Checks whether a contract implements an ERC165 interface or not without using nor updating the cache. + /// @param _contract Address of the contract to check. + /// @param _interfaceId ERC165 interface to check. + /// @return `true` if `_contract` implements `_interfaceId`, false otherwise. + function implementsERC165InterfaceNoCache(address _contract, bytes4 _interfaceId) public view returns (bool) { uint256 success; uint256 result; (success, result) = noThrowCall(_contract, ERC165ID); - if ((success==0)||(result==0)) { + if (success == 0 || result == 0) { return false; } - (success, result) = noThrowCall(_contract, InvalidID); - if ((success==0)||(result!=0)) { + (success, result) = noThrowCall(_contract, INVALID_ID); + if (success == 0 || result != 0) { return false; } (success, result) = noThrowCall(_contract, _interfaceId); - if ((success==1)&&(result==1)) { + if (success == 1 && result == 1) { return true; } return false; } - function noThrowCall(address _contract, bytes4 _interfaceId) constant internal returns (uint256 success, uint256 result) { + /// @notice Checks whether the hash is a ERC165 interface (ending with 28 zeroes) or not. + /// @param _interfaceHash The hash to check. + /// @return `true` if the hash is a ERC165 interface (ending with 28 zeroes), `false` otherwise. + function isERC165Interface(bytes32 _interfaceHash) internal pure returns (bool) { + return _interfaceHash & 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0; + } + + function noThrowCall(address _contract, bytes4 _interfaceId) + internal view returns (uint256 success, uint256 result) + { bytes4 erc165ID = ERC165ID; assembly { @@ -178,99 +213,537 @@ contract ERC820Registry { mstore(add(x, 0x04), _interfaceId) // Place first argument directly next to signature success := staticcall( - 30000, // 30k gas - _contract, // To addr - x, // Inputs are stored at location x - 0x08, // Inputs are 8 bytes long - x, // Store output over input (saves space) - 0x20) // Outputs are 32 bytes long + 30000, // 30k gas + _contract, // To addr + x, // Inputs are stored at location x + 0x08, // Inputs are 8 bytes long + x, // Store output over input (saves space) + 0x20 // Outputs are 32 bytes long + ) result := mload(x) // Load the result } } } -``` - -### Raw transaction for deploying the smart contract on any chain ``` -0xf908b78085174876e800830c35008080b90864608060405234801561001057600080fd5b50610844806100206000396000f30060806040526004361061008d5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166329965a1d81146100925780633d584063146100bf578063571a1f66146100fc5780635df8122f1461012a57806365ba36c11461015157806390e47957146101bc578063aabbb8ca146101fe578063ddc23ddd14610222575b600080fd5b34801561009e57600080fd5b506100bd600160a060020a036004358116906024359060443516610250565b005b3480156100cb57600080fd5b506100e0600160a060020a0360043516610402565b60408051600160a060020a039092168252519081900360200190f35b34801561010857600080fd5b506100bd600160a060020a0360043516600160e060020a03196024351661044e565b34801561013657600080fd5b506100bd600160a060020a03600435811690602435166104d8565b34801561015d57600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526101aa9436949293602493928401919081908401838280828437509497506105a09650505050505050565b60408051918252519081900360200190f35b3480156101c857600080fd5b506101ea600160a060020a0360043516600160e060020a031960243516610604565b604080519115158252519081900360200190f35b34801561020a57600080fd5b506100e0600160a060020a036004351660243561067a565b34801561022e57600080fd5b506101ea600160a060020a0360043516600160e060020a0319602435166106f4565b6000600160a060020a038416156102675783610269565b335b90503361027582610402565b600160a060020a03161461028857600080fd5b610291836107a9565b1561029b57600080fd5b600160a060020a038216158015906102bc5750600160a060020a0382163314155b1561039157604080517f4552433832305f4143434550545f4d4147494300000000000000000000000000815281519081900360130181207ff0083250000000000000000000000000000000000000000000000000000000008252600160a060020a038481166004840152602483018790529251909285169163f00832509160448083019260209291908290030181600087803b15801561035b57600080fd5b505af115801561036f573d6000803e3d6000fd5b505050506040513d602081101561038557600080fd5b50511461039157600080fd5b600160a060020a03818116600081815260208181526040808320888452909152808220805473ffffffffffffffffffffffffffffffffffffffff19169487169485179055518692917f93baa6efbd2244243bfee6ce4cfdd1d04fc4c0e9a786abd3a41313bd352db15391a450505050565b600160a060020a03808216600090815260016020526040812054909116151561042c575080610449565b50600160a060020a03808216600090815260016020526040902054165b919050565b61045882826106f4565b610463576000610465565b815b600160a060020a03928316600081815260208181526040808320600160e060020a031996909616808452958252808320805473ffffffffffffffffffffffffffffffffffffffff19169590971694909417909555908152600284528181209281529190925220805460ff19166001179055565b6000600160a060020a038316156104ef57826104f1565b335b9050336104fd82610402565b600160a060020a03161461051057600080fd5b80600160a060020a031682600160a060020a03161461052f5781610532565b60005b600160a060020a03828116600081815260016020526040808220805473ffffffffffffffffffffffffffffffffffffffff19169585169590951790945592519185169290917f605c2dbf762e5f7d60a546d42e7205dcb1b011ebc62a61736a57c9089d3a43509190a3505050565b6000816040518082805190602001908083835b602083106105d25780518252601f1990920191602091820191016105b3565b5181516020939093036101000a6000190180199091169216919091179052604051920182900390912095945050505050565b600160a060020a0382166000908152600260209081526040808320600160e060020a03198516845290915281205460ff16151561064557610645838361044e565b50600160a060020a03918216600090815260208181526040808320600160e060020a0319949094168352929052205416151590565b60008080600160a060020a038516156106935784610695565b335b91506106a0846107a9565b156106c55750826106b18282610604565b6106bc5760006106be565b815b92506106ec565b600160a060020a038083166000908152602081815260408083208884529091529020541692505b505092915050565b60008080610722857f01ffc9a7000000000000000000000000000000000000000000000000000000006107cb565b9092509050811580610732575080155b1561074057600092506106ec565b61075285600160e060020a03196107cb565b909250905081158061076357508015155b1561077157600092506106ec565b61077b85856107cb565b90925090506001821480156107905750806001145b1561079e57600192506106ec565b506000949350505050565b7bffffffffffffffffffffffffffffffffffffffffffffffffffffffff161590565b6040517f01ffc9a7000000000000000000000000000000000000000000000000000000008082526004820183905260009182919060208160088189617530fa9051909690955093505050505600a165627a7a72305820ba6e246cbdcaf97334eb3dd1ac11e6490103d5ead3f963b5f312a729e88cf9db00291ba079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798a00aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + +### Deployment Transaction + +Below is the raw transaction which MUST be used to deploy the smart contract on any chain. + +``` +0xf90ab18085174876e800830c35008080b90a5e608060405234801561001057600080fd5b50610a3e806100206000396000f30060806040526004361061008d5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166329965a1d81146100925780633d584063146100bf5780635df8122f146100fc57806365ba36c114610123578063a41e7d511461018e578063aabbb8ca146101bc578063b7056765146101e0578063f712f3e814610222575b600080fd5b34801561009e57600080fd5b506100bd600160a060020a036004358116906024359060443516610250565b005b3480156100cb57600080fd5b506100e0600160a060020a036004351661054b565b60408051600160a060020a039092168252519081900360200190f35b34801561010857600080fd5b506100bd600160a060020a0360043581169060243516610597565b34801561012f57600080fd5b506040805160206004803580820135601f810184900484028501840190955284845261017c9436949293602493928401919081908401838280828437509497506106aa9650505050505050565b60408051918252519081900360200190f35b34801561019a57600080fd5b506100bd600160a060020a0360043516600160e060020a031960243516610774565b3480156101c857600080fd5b506100e0600160a060020a03600435166024356107fe565b3480156101ec57600080fd5b5061020e600160a060020a0360043516600160e060020a031960243516610878565b604080519115158252519081900360200190f35b34801561022e57600080fd5b5061020e600160a060020a0360043516600160e060020a03196024351661092d565b6000600160a060020a038416156102675783610269565b335b9050336102758261054b565b600160a060020a0316146102d3576040805160e560020a62461bcd02815260206004820152600f60248201527f4e6f7420746865206d616e616765720000000000000000000000000000000000604482015290519081900360640190fd5b6102dc836109a3565b15610331576040805160e560020a62461bcd02815260206004820152601960248201527f4d757374206e6f74206265206120455243313635206861736800000000000000604482015290519081900360640190fd5b600160a060020a038216158015906103525750600160a060020a0382163314155b156104da5760405160200180807f4552433832305f4143434550545f4d414749430000000000000000000000000081525060130190506040516020818303038152906040526040518082805190602001908083835b602083106103c65780518252601f1990920191602091820191016103a7565b51815160209384036101000a6000190180199092169116179052604080519290940182900382207ff0083250000000000000000000000000000000000000000000000000000000008352600160a060020a038881166004850152602484018b90529451909650938816945063f0083250936044808401945091929091908290030181600087803b15801561045957600080fd5b505af115801561046d573d6000803e3d6000fd5b505050506040513d602081101561048357600080fd5b5051146104da576040805160e560020a62461bcd02815260206004820181905260248201527f446f6573206e6f7420696d706c656d656e742074686520696e74657266616365604482015290519081900360640190fd5b600160a060020a03818116600081815260208181526040808320888452909152808220805473ffffffffffffffffffffffffffffffffffffffff19169487169485179055518692917f93baa6efbd2244243bfee6ce4cfdd1d04fc4c0e9a786abd3a41313bd352db15391a450505050565b600160a060020a038082166000908152600160205260408120549091161515610575575080610592565b50600160a060020a03808216600090815260016020526040902054165b919050565b6000600160a060020a038316156105ae57826105b0565b335b9050336105bc8261054b565b600160a060020a03161461061a576040805160e560020a62461bcd02815260206004820152600f60248201527f4e6f7420746865206d616e616765720000000000000000000000000000000000604482015290519081900360640190fd5b80600160a060020a031682600160a060020a031614610639578161063c565b60005b600160a060020a03828116600081815260016020526040808220805473ffffffffffffffffffffffffffffffffffffffff19169585169590951790945592519185169290917f605c2dbf762e5f7d60a546d42e7205dcb1b011ebc62a61736a57c9089d3a43509190a3505050565b6000816040516020018082805190602001908083835b602083106106df5780518252601f1990920191602091820191016106c0565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040516020818303038152906040526040518082805190602001908083835b602083106107425780518252601f199092019160209182019101610723565b5181516020939093036101000a6000190180199091169216919091179052604051920182900390912095945050505050565b61077e8282610878565b61078957600061078b565b815b600160a060020a03928316600081815260208181526040808320600160e060020a031996909616808452958252808320805473ffffffffffffffffffffffffffffffffffffffff19169590971694909417909555908152600284528181209281529190925220805460ff19166001179055565b60008080600160a060020a038516156108175784610819565b335b9150610824846109a3565b15610849575082610835828261092d565b610840576000610842565b815b9250610870565b600160a060020a038083166000908152602081815260408083208884529091529020541692505b505092915050565b600080806108a6857f01ffc9a7000000000000000000000000000000000000000000000000000000006109c5565b90925090508115806108b6575080155b156108c45760009250610870565b6108d685600160e060020a03196109c5565b90925090508115806108e757508015155b156108f55760009250610870565b6108ff85856109c5565b90925090506001821480156109145750806001145b156109225760019250610870565b506000949350505050565b600160a060020a0382166000908152600260209081526040808320600160e060020a03198516845290915281205460ff16151561096e5761096e8383610774565b50600160a060020a03918216600090815260208181526040808320600160e060020a0319949094168352929052205416151590565b7bffffffffffffffffffffffffffffffffffffffffffffffffffffffff161590565b6040517f01ffc9a7000000000000000000000000000000000000000000000000000000008082526004820183905260009182919060208160088189617530fa9051909690955093505050505600a165627a7a723058209f4f0ded519f65ffc31c58c2e588b1713ee5a922e97928e2ccbd91e39128037700291ba079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798a00aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ``` -You can see the string of `a`s at the end of the transaction. This is the `s` of the signature, meaning that its a deterministic by hand forced signature. +The string of `a`'s at the end of the transaction is the `s` of the signature. From this pattern, one can clearly deduce that it is a deterministic signature generated by a human. -### Deployment method +### Deployment Method -This contract is going to be deployed using the Nick's Method. +This contract is going to be deployed using [Nick]'s Method. This method works as follows: -This method works as follows: +1. Generate a transaction which deploys the contract from a new random account. + - This transaction MUST NOT use [ERC155] in order to work on any chain. + - This transaction MUST have a relatively high gas price in order to be deployed on any chain. In this case, it is going to be 100 Gwei. + +2. Set the `v`, `r`, `s` of the transaction signature to the following values: + + ``` + v: 27 + r: 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 + s: 0x0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ``` + +This nice `s` value is a "random number" generated deterministically by a human. + +3. We recover the sender of this transaction. Thus we obtain an account that can broadcast that transaction, but we also have the warranty that nobody knows the private key of that account. -1. Generate a transaction that deploys the contract from a new random account. This transaction must not use [EIP-155](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md) so it can work on any chain. This transaction needs to also have a relatively high gas price in order to be deployed in any chain. In this case, it's going to be 100Gwei. -2. Set the `v`, `r`, `s` of the transaction signature to the following values: -` - v: 27` -` - r: 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798` -` - s: 0x0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa` -This nice `s` value is a random number generated deterministically by a human. -3. We recover the sender of this transaction. We will have an account that can broadcast that transaction, but we also have the waranty that nobody knows the private key of that account. 4. Send Ether to this deployment account. + 5. Broadcast the transaction. -This operation can be done in any chain, guaranteed that the contract address is going to always be the same and nobody will be able to mess up that address with a different contract. +This operation can be done in any chain, guaranteeing that the contract address is going to always be the same and nobody will be able to mess up that address with a different contract. -### Special registry deployment account +### Special Registry Deployment Account ``` -0xe515e6d0e6b09a60e3cb50c6a8d51d3009ad18af +0x6c3FB9d29823aB78F93d46C8bF40Abf7a70ED9be ``` -This account is generated by reverse engineering it from it's signature for the transaction, in this way no one knows the private key, but it is known that it's the valid signer of the deployment transaction. +This account is generated by reverse engineering it from its signature for the transaction. This way no one knows the private key, but it is known that it is the valid signer of the deployment transaction. ### Deployed contract ``` -0xbe78655dff872d22b95ae366fb3477d977328ade +0x820A6e5561A66f60c6546a74001db369bbFf238D ``` -The contract will have this address for every chain it is deployed to. +The contract has the address above for every chain it is deployed to. +
+Raw metadata of ./contracts/ERC820Registry.sol +
{
+  "compiler": {
+    "version": "0.4.24+commit.e67f0147"
+  },
+  "language": "Solidity",
+  "output": {
+    "abi": [
+      {
+        "constant": false,
+        "inputs": [
+          {
+            "name": "_addr",
+            "type": "address"
+          },
+          {
+            "name": "_interfaceHash",
+            "type": "bytes32"
+          },
+          {
+            "name": "_implementer",
+            "type": "address"
+          }
+        ],
+        "name": "setInterfaceImplementer",
+        "outputs": [],
+        "payable": false,
+        "stateMutability": "nonpayable",
+        "type": "function"
+      },
+      {
+        "constant": true,
+        "inputs": [
+          {
+            "name": "_addr",
+            "type": "address"
+          }
+        ],
+        "name": "getManager",
+        "outputs": [
+          {
+            "name": "",
+            "type": "address"
+          }
+        ],
+        "payable": false,
+        "stateMutability": "view",
+        "type": "function"
+      },
+      {
+        "constant": false,
+        "inputs": [
+          {
+            "name": "_addr",
+            "type": "address"
+          },
+          {
+            "name": "_newManager",
+            "type": "address"
+          }
+        ],
+        "name": "setManager",
+        "outputs": [],
+        "payable": false,
+        "stateMutability": "nonpayable",
+        "type": "function"
+      },
+      {
+        "constant": true,
+        "inputs": [
+          {
+            "name": "_interfaceName",
+            "type": "string"
+          }
+        ],
+        "name": "interfaceHash",
+        "outputs": [
+          {
+            "name": "",
+            "type": "bytes32"
+          }
+        ],
+        "payable": false,
+        "stateMutability": "pure",
+        "type": "function"
+      },
+      {
+        "constant": false,
+        "inputs": [
+          {
+            "name": "_contract",
+            "type": "address"
+          },
+          {
+            "name": "_interfaceId",
+            "type": "bytes4"
+          }
+        ],
+        "name": "updateERC165Cache",
+        "outputs": [],
+        "payable": false,
+        "stateMutability": "nonpayable",
+        "type": "function"
+      },
+      {
+        "constant": true,
+        "inputs": [
+          {
+            "name": "_addr",
+            "type": "address"
+          },
+          {
+            "name": "_interfaceHash",
+            "type": "bytes32"
+          }
+        ],
+        "name": "getInterfaceImplementer",
+        "outputs": [
+          {
+            "name": "",
+            "type": "address"
+          }
+        ],
+        "payable": false,
+        "stateMutability": "view",
+        "type": "function"
+      },
+      {
+        "constant": true,
+        "inputs": [
+          {
+            "name": "_contract",
+            "type": "address"
+          },
+          {
+            "name": "_interfaceId",
+            "type": "bytes4"
+          }
+        ],
+        "name": "implementsERC165InterfaceNoCache",
+        "outputs": [
+          {
+            "name": "",
+            "type": "bool"
+          }
+        ],
+        "payable": false,
+        "stateMutability": "view",
+        "type": "function"
+      },
+      {
+        "constant": true,
+        "inputs": [
+          {
+            "name": "_contract",
+            "type": "address"
+          },
+          {
+            "name": "_interfaceId",
+            "type": "bytes4"
+          }
+        ],
+        "name": "implementsERC165Interface",
+        "outputs": [
+          {
+            "name": "",
+            "type": "bool"
+          }
+        ],
+        "payable": false,
+        "stateMutability": "view",
+        "type": "function"
+      },
+      {
+        "anonymous": false,
+        "inputs": [
+          {
+            "indexed": true,
+            "name": "addr",
+            "type": "address"
+          },
+          {
+            "indexed": true,
+            "name": "interfaceHash",
+            "type": "bytes32"
+          },
+          {
+            "indexed": true,
+            "name": "implementer",
+            "type": "address"
+          }
+        ],
+        "name": "InterfaceImplementerSet",
+        "type": "event"
+      },
+      {
+        "anonymous": false,
+        "inputs": [
+          {
+            "indexed": true,
+            "name": "addr",
+            "type": "address"
+          },
+          {
+            "indexed": true,
+            "name": "newManager",
+            "type": "address"
+          }
+        ],
+        "name": "ManagerChanged",
+        "type": "event"
+      }
+    ],
+    "devdoc": {
+      "author": "Jordi Baylina and Jacques Dafflon",
+      "methods": {
+        "getInterfaceImplementer(address,bytes32)": {
+          "params": {
+            "_addr": "Address being queried for the implementer of an interface. (If `_addr == 0` then `msg.sender` is assumed.)",
+            "_interfaceHash": "keccak256 hash of the name of the interface as a string. E.g., `web3.utils.keccak256('ERC777Token')`."
+          },
+          "return": "The address of the contract which implements the interface `_interfaceHash` for `_addr` or `0x0` if `_addr` did not register an implementer for this interface."
+        },
+        "getManager(address)": {
+          "params": {
+            "_addr": "Address for which to return the manager."
+          },
+          "return": "Address of the manager for a given address."
+        },
+        "implementsERC165Interface(address,bytes4)": {
+          "details": "This function may modify the state when updating the cache. However, this function must have the `view` modifier since `getInterfaceImplementer` also calls it. If called from within a transaction, the ERC165 cache is updated.",
+          "params": {
+            "_contract": "Address of the contract to check.",
+            "_interfaceId": "ERC165 interface to check."
+          },
+          "return": "`true` if `_contract` implements `_interfaceId`, false otherwise."
+        },
+        "implementsERC165InterfaceNoCache(address,bytes4)": {
+          "params": {
+            "_contract": "Address of the contract to check.",
+            "_interfaceId": "ERC165 interface to check."
+          },
+          "return": "`true` if `_contract` implements `_interfaceId`, false otherwise."
+        },
+        "interfaceHash(string)": {
+          "params": {
+            "_interfaceName": "Name of the interface."
+          },
+          "return": "The keccak256 hash of an interface name."
+        },
+        "setInterfaceImplementer(address,bytes32,address)": {
+          "params": {
+            "_addr": "Address to define the interface for. (If `_addr == 0` then `msg.sender` is assumed.)",
+            "_interfaceHash": "keccak256 hash of the name of the interface as a string. For example, `web3.utils.keccak256('ERC777TokensRecipient')` for the `ERC777TokensRecipient` interface."
+          }
+        },
+        "setManager(address,address)": {
+          "params": {
+            "_addr": "Address for which to set the new manager. (If `_addr == 0` then `msg.sender` is assumed.)",
+            "_newManager": "Address of the new manager for `addr`. (Pass `0x0` to reset the manager to `_addr` itself.)"
+          }
+        },
+        "updateERC165Cache(address,bytes4)": {
+          "params": {
+            "_contract": "Address of the contract for which to update the cache.",
+            "_interfaceId": "ERC165 interface for which to update the cache."
+          }
+        }
+      },
+      "title": "ERC820 Pseudo-introspection Registry Contract"
+    },
+    "userdoc": {
+      "methods": {
+        "getInterfaceImplementer(address,bytes32)": {
+          "notice": "Query if an address implements an interface and through which contract."
+        },
+        "getManager(address)": {
+          "notice": "Get the manager of an address."
+        },
+        "implementsERC165Interface(address,bytes4)": {
+          "notice": "Checks whether a contract implements an ERC165 interface or not. The result is cached. If the cache is out of date, it must be updated by calling `updateERC165Cache`."
+        },
+        "implementsERC165InterfaceNoCache(address,bytes4)": {
+          "notice": "Checks whether a contract implements an ERC165 interface or not without using nor updating the cache."
+        },
+        "interfaceHash(string)": {
+          "notice": "Compute the keccak256 hash of an interface given its name."
+        },
+        "setInterfaceImplementer(address,bytes32,address)": {
+          "notice": "Sets the contract which implements a specific interface for an address. Only the manager defined for that address can set it. (Each address is the manager for itself until it sets a new manager.)"
+        },
+        "setManager(address,address)": {
+          "notice": "Sets the `_newManager` as manager for the `_addr` address. The new manager will be able to call `setInterfaceImplementer` for `_addr`."
+        },
+        "updateERC165Cache(address,bytes4)": {
+          "notice": "Updates the cache with whether contract implements an ERC165 interface or not."
+        }
+      }
+    }
+  },
+  "settings": {
+    "compilationTarget": {
+      "./contracts/ERC820Registry.sol": "ERC820Registry"
+    },
+    "evmVersion": "byzantium",
+    "libraries": {},
+    "optimizer": {
+      "enabled": true,
+      "runs": 200
+    },
+    "remappings": []
+  },
+  "sources": {
+    "./contracts/ERC820Registry.sol": {
+      "content": "pragma solidity 0.4.24;\n// IV is a simply value to have a \"nice\" address for the registry, starting with `0x820`.\n// IV: 12302\n\n/// @dev The interface a contract MUST implement if it is the implementer of\n/// some interface for any address other than itself.\ninterface ERC820ImplementerInterface {\n    /// @notice Indicates whether the contract implements the interface `interfaceHash` for the address `addr` or not.\n    /// @param addr Address for which the contract will implement the interface\n    /// @param interfaceHash keccak256 hash of the name of the interface\n    /// @return ERC820_ACCEPT_MAGIC only if the contract implements `ìnterfaceHash` for the address `addr`.\n    function canImplementInterfaceForAddress(address addr, bytes32 interfaceHash) public view returns(bytes32);\n}\n\n/// @title ERC820 Pseudo-introspection Registry Contract\n/// @author Jordi Baylina and Jacques Dafflon\n/// @notice This contract is the official implementation of the ERC820 Registry.\n/// @notice For more details, see https://eips.ethereum.org/EIPS/eip-820\ncontract ERC820Registry {\n    /// @notice ERC165 Invalid ID.\n    bytes4 constant INVALID_ID = 0xffffffff;\n    /// @notice Method ID for the ERC165 supportsInterface method (= `bytes4(keccak256('supportsInterface(bytes4)'))`).\n    bytes4 constant ERC165ID = 0x01ffc9a7;\n    /// @notice Magic value which is returned if a contrct implements an interface on behalf of some other address.\n    bytes32 constant ERC820_ACCEPT_MAGIC = keccak256(abi.encodePacked(\"ERC820_ACCEPT_MAGIC\"));\n\n    mapping (address => mapping(bytes32 => address)) interfaces;\n    mapping (address => address) managers;\n    mapping (address => mapping(bytes4 => bool)) erc165Cached;\n\n    /// @notice Indicates a contract is the `implementer` of `interfaceHash` for `addr`.\n    event InterfaceImplementerSet(address indexed addr, bytes32 indexed interfaceHash, address indexed implementer);\n    /// @notice Indicates `newManager` is the address of the new manager for `addr`.\n    event ManagerChanged(address indexed addr, address indexed newManager);\n\n    /// @notice Query if an address implements an interface and through which contract.\n    /// @param _addr Address being queried for the implementer of an interface.\n    /// (If `_addr == 0` then `msg.sender` is assumed.)\n    /// @param _interfaceHash keccak256 hash of the name of the interface as a string.\n    /// E.g., `web3.utils.keccak256('ERC777Token')`.\n    /// @return The address of the contract which implements the interface `_interfaceHash` for `_addr`\n    /// or `0x0` if `_addr` did not register an implementer for this interface.\n    function getInterfaceImplementer(address _addr, bytes32 _interfaceHash) external view returns (address) {\n        address addr = _addr == 0 ? msg.sender : _addr;\n        if (isERC165Interface(_interfaceHash)) {\n            bytes4 erc165InterfaceHash = bytes4(_interfaceHash);\n            return implementsERC165Interface(addr, erc165InterfaceHash) ? addr : 0;\n        }\n        return interfaces[addr][_interfaceHash];\n    }\n\n    /// @notice Sets the contract which implements a specific interface for an address.\n    /// Only the manager defined for that address can set it.\n    /// (Each address is the manager for itself until it sets a new manager.)\n    /// @param _addr Address to define the interface for. (If `_addr == 0` then `msg.sender` is assumed.)\n    /// @param _interfaceHash keccak256 hash of the name of the interface as a string.\n    /// For example, `web3.utils.keccak256('ERC777TokensRecipient')` for the `ERC777TokensRecipient` interface.\n    function setInterfaceImplementer(address _addr, bytes32 _interfaceHash, address _implementer) external {\n        address addr = _addr == 0 ? msg.sender : _addr;\n        require(getManager(addr) == msg.sender, \"Not the manager\");\n\n        require(!isERC165Interface(_interfaceHash), \"Must not be a ERC165 hash\");\n        if (_implementer != 0 && _implementer != msg.sender) {\n            require(\n                ERC820ImplementerInterface(_implementer)\n                    .canImplementInterfaceForAddress(addr, _interfaceHash) == ERC820_ACCEPT_MAGIC,\n                \"Does not implement the interface\"\n            );\n        }\n        interfaces[addr][_interfaceHash] = _implementer;\n        emit InterfaceImplementerSet(addr, _interfaceHash, _implementer);\n    }\n\n    /// @notice Sets the `_newManager` as manager for the `_addr` address.\n    /// The new manager will be able to call `setInterfaceImplementer` for `_addr`.\n    /// @param _addr Address for which to set the new manager. (If `_addr == 0` then `msg.sender` is assumed.)\n    /// @param _newManager Address of the new manager for `addr`. (Pass `0x0` to reset the manager to `_addr` itself.)\n    function setManager(address _addr, address _newManager) external {\n        address addr = _addr == 0 ? msg.sender : _addr;\n        require(getManager(addr) == msg.sender, \"Not the manager\");\n        managers[addr] = _newManager == addr ? 0 : _newManager;\n        emit ManagerChanged(addr, _newManager);\n    }\n\n    /// @notice Get the manager of an address.\n    /// @param _addr Address for which to return the manager.\n    /// @return Address of the manager for a given address.\n    function getManager(address _addr) public view returns(address) {\n        // By default the manager of an address is the same address\n        if (managers[_addr] == 0) {\n            return _addr;\n        } else {\n            return managers[_addr];\n        }\n    }\n\n    /// @notice Compute the keccak256 hash of an interface given its name.\n    /// @param _interfaceName Name of the interface.\n    /// @return The keccak256 hash of an interface name.\n    function interfaceHash(string _interfaceName) public pure returns(bytes32) {\n        return keccak256(abi.encodePacked(_interfaceName));\n    }\n\n    /* --- ERC165 Related Functions --- */\n\n    /// @notice Checks whether a contract implements an ERC165 interface or not.\n    /// The result is cached. If the cache is out of date, it must be updated by calling `updateERC165Cache`.\n    /// @param _contract Address of the contract to check.\n    /// @param _interfaceId ERC165 interface to check.\n    /// @return `true` if `_contract` implements `_interfaceId`, false otherwise.\n    /// @dev This function may modify the state when updating the cache. However, this function must have the `view`\n    /// modifier since `getInterfaceImplementer` also calls it. If called from within a transaction, the ERC165 cache\n    /// is updated.\n    function implementsERC165Interface(address _contract, bytes4 _interfaceId) public view returns (bool) {\n        if (!erc165Cached[_contract][_interfaceId]) {\n            updateERC165Cache(_contract, _interfaceId);\n        }\n        return interfaces[_contract][_interfaceId] != 0;\n    }\n\n    /// @notice Updates the cache with whether contract implements an ERC165 interface or not.\n    /// @param _contract Address of the contract for which to update the cache.\n    /// @param _interfaceId ERC165 interface for which to update the cache.\n    function updateERC165Cache(address _contract, bytes4 _interfaceId) public {\n        interfaces[_contract][_interfaceId] = implementsERC165InterfaceNoCache(_contract, _interfaceId) ? _contract : 0;\n        erc165Cached[_contract][_interfaceId] = true;\n    }\n\n    /// @notice Checks whether a contract implements an ERC165 interface or not without using nor updating the cache.\n    /// @param _contract Address of the contract to check.\n    /// @param _interfaceId ERC165 interface to check.\n    /// @return `true` if `_contract` implements `_interfaceId`, false otherwise.\n    function implementsERC165InterfaceNoCache(address _contract, bytes4 _interfaceId) public view returns (bool) {\n        uint256 success;\n        uint256 result;\n\n        (success, result) = noThrowCall(_contract, ERC165ID);\n        if (success == 0 || result == 0) {\n            return false;\n        }\n\n        (success, result) = noThrowCall(_contract, INVALID_ID);\n        if (success == 0 || result != 0) {\n            return false;\n        }\n\n        (success, result) = noThrowCall(_contract, _interfaceId);\n        if (success == 1 && result == 1) {\n            return true;\n        }\n        return false;\n    }\n\n    /// @notice Checks whether the hash is a ERC165 interface (ending with 28 zeroes) or not.\n    /// @param _interfaceHash The hash to check.\n    /// @return `true` if the hash is a ERC165 interface (ending with 28 zeroes), `false` otherwise.\n    function isERC165Interface(bytes32 _interfaceHash) internal pure returns (bool) {\n        return _interfaceHash & 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0;\n    }\n\n    function noThrowCall(address _contract, bytes4 _interfaceId)\n        internal view returns (uint256 success, uint256 result)\n    {\n        bytes4 erc165ID = ERC165ID;\n\n        assembly {\n                let x := mload(0x40)               // Find empty storage location using \"free memory pointer\"\n                mstore(x, erc165ID)                // Place signature at begining of empty storage\n                mstore(add(x, 0x04), _interfaceId) // Place first argument directly next to signature\n\n                success := staticcall(\n                    30000,                         // 30k gas\n                    _contract,                     // To addr\n                    x,                             // Inputs are stored at location x\n                    0x08,                          // Inputs are 8 bytes long\n                    x,                             // Store output over input (saves space)\n                    0x20                           // Outputs are 32 bytes long\n                )\n\n                result := mload(x)                 // Load the result\n        }\n    }\n}\n",
+      "keccak256": "0xb7b2e57b5db209f6d6c10c9172abe541be3010bfb194e8fadb460fd58608a9ee"
+    }
+  },
+  "version": 1
+}
+
### Interface name -Your interface name is hashed and sent to `getInterfaceImplementer()`. If you are writing a standard, it is best practice to explicitly state the interface name and link to this published EIP-820 so that other people don't have to come here to look up these rules. +Any interface name is hashed using `keccak256` and sent to `getInterfaceImplementer()`. -#### If it's an approved EIP +If the interface is part of a standard, it is best practice to explicitly state the interface name and link to this published [ERC820] such that other people don't have to come here to look up these rules. -The interface is named like `ERC###XXXXX`. The meaning of this interface is defined in the EIP specified. And XXXXX should be the name of the interface camelCase. +#### **Approved ERCs** + +If the interface is part of an approved ERC, it MUST be named `ERC###XXXXX` where `###` is the number of the ERC and XXXXX should be the name of the interface in CamelCase. The meaning of this interface SHOULD be defined in the specified ERC. Examples: -`sha3("ERC20Token")` -`sha3("ERC777Token")` -`sha3("ERC777TokensRecipient")` -`sha3("ERC777TokensSender")` +- `keccak256("ERC20Token")` +- `keccak256("ERC777Token")` +- `keccak256("ERC777TokensSender")` +- `keccak256("ERC777TokensRecipient")` -#### [EIP-165](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md) compatible interfaces +#### **[ERC165] Compatible Interfaces** -Interfaces where the last 28bytes are 0, then this will be considered an [EIP-165](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md) interface. +Any interface where the last 28 bytes are zeroes (`0`) SHALL be considered an [ERC165] interface. -#### Private user defined interface +#### **Private User-defined Interfaces** -This scheme is extensible. If you want to make up your own interface name and raise awareness to get other people to implement it and then check for those implementations, great! Have fun, but please do not conflict with the reserved designations above. +This scheme is extensible. You MAY make up your own interface name and raise awareness to get other people to implement it and then check for those implementations. Have fun but please, you MUST not conflict with the reserved designations above. + +### Set An Interface For An Address + +For any address to set a contract as the interface implementation, it must call the following function of the [ERC820] registry: + +``` solidity +function setInterfaceImplementer(address _addr, bytes32 _interfaceHash, address _implementer) public +``` + +Sets the contract that will handle a specific interface. + +Only a `manager` defined for that address can set it. (Each address is the manager for itself until a new manager is defined) + +*NOTE*: If `_addr` and `_implementer` are two different addresses, then: + +- The `_implementer` MUST implement the `ERC820ImplementerInterface` (detailed below). +- Calling `canImplementInterfaceForAddress` on `_implementer` with the given `_addr` and `_interfaceHash` MUST return the `ERC820_ACCEPT_MAGIC` value. + +*NOTE*: The `_interfaceHash` MUST NOT be an [ERC165] interface—it MUST NOT end with 28 zeroes (`0`). + +> **parameters** +> `_addr` Address to define the interface for (if `_addr == 0` them `msg.sender` is assumed) +> `_interfaceHash` `keccak256` hash of the name of the interface as a string, for example `web3.utils.keccak256('ERC777TokensRecipient')` for the ERC777TokensRecipient interface. + +### Get An Implementation Of An Interface For An Address + +Anyone MAY query the [ERC820] Registry to obtain the address of a contract implementing an interface on behalf of some address using the `getInterfaceImplementer` function. + +``` solidity +function getInterfaceImplementer(address _addr, bytes32 _interfaceHash) public view returns (address) +``` + +Query if an address implements an interface and through which contract. + +*NOTE*: If the last 28 bytes of the `_interfaceHash` are zeroes (`0`), then the first 4 bytes are considered an [ERC165] interface and the registry SHALL forward the call to the contract at `_addr` to see if it implements the [ERC165] interface (the first 4 bytes of `_interfaceHash`). The registry SHALL also cache [ERC165] queries in order to reduce gas consumption. Anyone MAY call the `erc165UpdateCache` function to update whether a contract implements an interface or not. + +> **parameters** +> `_addr` Address being queried for the implementer of an interface. (If `_addr == 0` them `msg.sender` is assumed.) +> `_interfaceHash` keccak256 hash of the name of the interface as a string. E.g. `web3.utils.keccak256('ERC777Token')` +> **returns:** The address of the contract which implements the interface `_interfaceHash` for `_addr` or `0x0` if `_addr` did not registeran implemeter for this interface. + + +### Interface Implementation (`ERC820ImplementerInterface`) + +``` solidity +interface ERC820ImplementerInterface { + /// @notice Indicates whether the contract implements the interface `interfaceHash` for the address `addr`. + /// @param addr Address for which the contract will implement the interface + /// @param interfaceHash keccak256 hash of the name of the interface + /// @return ERC820_ACCEPT_MAGIC only if the contract implements `ìnterfaceHash` for the address `addr`. + function canImplementInterfaceForAddress(address addr, bytes32 interfaceHash) public view returns(bytes32); +} +``` + +Any contract being registered as the implementation of an interface for a given address MUST implement said interface. In addition if it implements an interface on behalf of a different address, the contract MUST implement the `ERC820ImplementerInterface` shown above. + +``` solidity +function canImplementInterfaceForAddress(address addr, bytes32 interfaceHash) view public returns(bytes32); +``` + +Indicates whether a contract implements an interface (`interfaceHash`) for a given address (`addr`). + +If a contract implements the interface (`interfaceHash`) for a given address (`addr`), it MUST return `ERC820_ACCEPT_MAGIC` when called with the `addr` and the `interfaceHash`. If it does not implement the `interfaceHash` for a given address (`addr`), it MUST NOT return `ERC820_ACCEPT_MAGIC`. + +> **parameters** +> `addr`: Address for which the interface is implemented +> `interfaceHash`: Hash of the interface which is implemented + +The special value `ERC820_ACCEPT_MAGIC` is defined as the `keccka256` hash of the string `"ERC820_ACCEPT_MAGIC"`. + +``` solidity +bytes32 constant ERC820_ACCEPT_MAGIC = keccak256("ERC820_ACCEPT_MAGIC"); +``` + +> The reason to return `ERC820_ACCEPT_MAGIC` instead of a boolean is to prevent cases where a contract fails to implement the `canImplementInterfaceForAddress` but implements a fallback function which does not throw. In this case, since `canImplementInterfaceForAddress` does not exist, the fallback function is called instead, executed without throwing and returns `1`. Thus making it appear as if `canImplementInterfaceForAddress` returned `true`. + +### Manager + +The manager of an address (regular account or a contract) is the only entity allowed to register implementations of interfaces for the address. By default any address is its own manager. + +The manager can transfer its role to another address by calling `setManager` with the address for which to transfer the manager and the address of the new manager. + +``` solidity +function setManager(address _addr, address _newManager) public +``` + +Sets the `_newManager` as manager for the `_addr` address. + +The new manager will be able to call `setInterfaceImplementer` for `_addr`. + +If `_addr` is `0x0`, `msg.sender` is assumed for `_addr`. +If `_newManager` is `0x0`, the manager is reset to `_addr` itself as the manager. + +> **parameters** +> `_addr` Address for which to set the new manager. (Pass `0x0` to use `msg.sender` as the address.) +> `_newManager` The address of the new manager for `_addr`. (Pass `0x0` to reset the manager to `_addr`.) ## Backwards Compatibility -This standard is backwards compatible with [EIP-165](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md), as both methods can be implemented without conflicting one each other. +This standard is backwards compatible with [ERC165], as both methods MAY be implemented without conflicting with each other. ## Test Cases -Please, check the repository https://github.com/jbaylina/eip820 for the full test suit. +Please check the [jbaylina/eip820] repository for the full test suite. ## Implementation -The implementation can be found in this repo: https://github.com/jbaylina/eip820 +The implementation can be found in this repo: [jbaylina/eip820]. ## Copyright -Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/). +Copyright and related rights waived via [CC0]. + +[ERC155]: https://eips.ethereum.org/EIPS/eip-155 +[ERC165]: https://eips.ethereum.org/EIPS/eip-165 +[ERC672]: https://github.com/ethereum/EIPs/issues/672 +[ERC820]: https://eips.ethereum.org/EIPS/eip-820 +[ERC820 registry smart contract]: https://github.com/jbaylina/eip820/blob/master/contracts/ERC820Registry.sol +[jbaylina/eip820]: https://github.com/jbaylina/eip820 +[CC0]: https://creativecommons.org/publicdomain/zero/1.0/ +[Nick]: https://github.com/Arachnid/