ERC6551Registry.sol
The canonical ERC-6551 registry used to derive and deploy Seat accounts.
58 lines2.2 KBSolidity
| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.24; |
| 3 | |
| 4 | import {Create2} from "@openzeppelin/contracts/utils/Create2.sol"; |
| 5 | import {IERC6551Registry} from "./interfaces/IK401Interfaces.sol"; |
| 6 | |
| 7 | /** |
| 8 | * @title ERC6551Registry |
| 9 | * @notice Canonical-layout ERC-6551 registry. Deploys ERC-1167 proxies whose runtime |
| 10 | * code carries `abi.encode(salt, chainId, tokenContract, tokenId)` as a footer, |
| 11 | * exactly as the reference registry at 0x000000006551c19487814612e58FE06813775758. |
| 12 | * |
| 13 | * @dev We ship our own instance so the suite (and any chain without the canonical |
| 14 | * deployment) is self-contained. The address derivation is identical, so an account |
| 15 | * created here is byte-compatible with any ERC-6551 tooling. |
| 16 | */ |
| 17 | contract ERC6551Registry is IERC6551Registry { |
| 18 | function _creationCode(address implementation, bytes32 salt, uint256 chainId, address tokenContract, uint256 tokenId) |
| 19 | internal |
| 20 | pure |
| 21 | returns (bytes memory) |
| 22 | { |
| 23 | return abi.encodePacked( |
| 24 | hex"3d60ad80600a3d3981f3363d3d373d3d3d363d73", |
| 25 | implementation, |
| 26 | hex"5af43d82803e903d91602b57fd5bf3", |
| 27 | abi.encode(salt, chainId, tokenContract, tokenId) |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | function createAccount( |
| 32 | address implementation, |
| 33 | bytes32 salt, |
| 34 | uint256 chainId, |
| 35 | address tokenContract, |
| 36 | uint256 tokenId |
| 37 | ) external returns (address) { |
| 38 | bytes memory code = _creationCode(implementation, salt, chainId, tokenContract, tokenId); |
| 39 | address predicted = Create2.computeAddress(salt, keccak256(code), address(this)); |
| 40 | if (predicted.code.length != 0) return predicted; |
| 41 | |
| 42 | address deployed = Create2.deploy(0, salt, code); |
| 43 | if (deployed != predicted) revert AccountCreationFailed(); |
| 44 | |
| 45 | emit ERC6551AccountCreated(deployed, implementation, salt, chainId, tokenContract, tokenId); |
| 46 | return deployed; |
| 47 | } |
| 48 | |
| 49 | function account(address implementation, bytes32 salt, uint256 chainId, address tokenContract, uint256 tokenId) |
| 50 | external |
| 51 | view |
| 52 | returns (address) |
| 53 | { |
| 54 | bytes memory code = _creationCode(implementation, salt, chainId, tokenContract, tokenId); |
| 55 | return Create2.computeAddress(salt, keccak256(code), address(this)); |
| 56 | } |
| 57 | } |
| 58 |
Click any line number to deep-link to it — the target line highlights on load.