File size: 13,952 Bytes
f998fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{
  "language": "Solidity",
  "settings": {
    "evmVersion": "london",
    "libraries": {},
    "metadata": {
      "bytecodeHash": "ipfs",
      "useLiteralContent": true
    },
    "optimizer": {
      "enabled": true,
      "runs": 800
    },
    "remappings": [],
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode",
          "evm.deployedBytecode",
          "devdoc",
          "userdoc",
          "metadata",
          "abi"
        ]
      }
    }
  },
  "sources": {
    "contracts/interfaces/IERC165.sol": {
      "content": "// SPDX-License-Identifier: GPL-3.0\npragma solidity 0.8.16;\n\n/**\n * @title IERC165\n * @notice Support of ERC165.\n */\ninterface IERC165 {\n    /**\n     * @notice Query if a contract implements an interface\n     *\n     * @param interfaceID The interface identifier, as specified in ERC-165\n     *\n     * @dev Interface identification is specified in ERC-165. This function\n     * uses less than 30,000 gas.\n     *\n     * @return `true` if the contract implements `interfaceID` and\n     * interfaceID` is not 0xffffffff, `false` otherwise\n     */\n    function supportsInterface(bytes4 interfaceID) external view returns (bool);\n}\n"
    },
    "contracts/interfaces/ILaserFactory.sol": {
      "content": "// SPDX-License-Identifier: LGPL-3.0-only\npragma solidity 0.8.16;\n\nimport \"../proxies/LaserProxy.sol\";\n\n/**\n * @title  LaserFactory\n *\n * @notice Factory that creates new Laser proxies, and has helper methods.\n *\n * @dev    This interface has all events, errors, and external function for LaserFactory.\n */\ninterface ILaserFactory {\n    /*//////////////////////////////////////////////////////////////\n                                 EVENTS\n    //////////////////////////////////////////////////////////////*/\n\n    event LaserCreated(address laser);\n\n    /*//////////////////////////////////////////////////////////////\n                                 ERRORS\n    //////////////////////////////////////////////////////////////*/\n\n    error LF__constructor__invalidSingleton();\n\n    error LF__createProxy__creationFailed();\n\n    error LF__deployProxy__create2Failed();\n\n    /*//////////////////////////////////////////////////////////////\n                                 STATE\n    //////////////////////////////////////////////////////////////*/\n\n    function singleton() external view returns (address);\n\n    /*//////////////////////////////////////////////////////////////\n                                EXTERNAL\n    //////////////////////////////////////////////////////////////*/\n\n    /**\n     * @dev Allows to create new proxy contact and execute a message call to the new proxy within one transaction.\n     *\n     * @param initializer   Payload for message call sent to new proxy contract.\n     * @param saltNonce     Nonce that will be used to generate the salt to calculate the address of the new proxy contract.\n     */\n    function createProxy(bytes memory initializer, uint256 saltNonce) external returns (LaserProxy proxy);\n\n    /**\n     * @dev Precomputes the address of a proxy that is created through 'create2'.\n     */\n    function preComputeAddress(bytes memory initializer, uint256 saltNonce) external view returns (address);\n\n    /**\n     * @dev Allows to retrieve the runtime code of a deployed Proxy. This can be used to check that the expected Proxy was deployed.\n     */\n    function proxyRuntimeCode() external pure returns (bytes memory);\n\n    /**\n     *  @dev Allows to retrieve the creation code used for the Proxy deployment. With this it is easily possible to calculate predicted address.\n     */\n    function proxyCreationCode() external pure returns (bytes memory);\n}\n"
    },
    "contracts/interfaces/ILaserWallet.sol": {
      "content": "// SPDX-License-Identifier: GPL-3.0\npragma solidity 0.8.16;\n\nstruct Transaction {\n    address to;\n    uint256 value;\n    bytes callData;\n    uint256 nonce;\n    bytes signatures;\n}\n\n/**\n * @title  ILaserWallet\n *\n * @author Rodrigo Herrera I.\n *\n * @notice Laser is a secure smart contract wallet (vault) made for the Ethereum Virtual Machine.\n *\n * @dev    This interface has all events, errors, and external function for LaserWallet.\n */\ninterface ILaserWallet {\n    /*//////////////////////////////////////////////////////////////\n                                 EVENTS\n    //////////////////////////////////////////////////////////////*/\n\n    event ExecSuccess(address to, uint256 value, uint256 nonce, bytes4 funcSig);\n\n    /*//////////////////////////////////////////////////////////////\n                                 ERRORS\n    //////////////////////////////////////////////////////////////*/\n\n    error LW__init__notOwner();\n\n    error LW__exec__invalidNonce();\n\n    error LW__exec__walletLocked();\n\n    error LW__exec__invalidSignatureLength();\n\n    error LW__exec__invalidSignature();\n\n    error LW__exec__callFailed();\n\n    error LW__recovery__invalidNonce();\n\n    error LW__recovery__invalidSignatureLength();\n\n    error LW__recovery__duplicateSigner();\n\n    error LW__recoveryLock__invalidSignature();\n\n    error LW__recoveryUnlock__time();\n\n    error LW__recoveryUnlock__invalidSignature();\n\n    error LW__recoveryRecover__walletLocked();\n\n    error LW__recoveryRecover__invalidSignature();\n\n    error LW__recovery__invalidOperation();\n\n    error LW__recovery__callFailed();\n\n    error LaserWallet__invalidSignature();\n\n    /*//////////////////////////////////////////////////////////////\n                                EXTERNAL\n    //////////////////////////////////////////////////////////////*/\n\n    /**\n     * @notice Setup function, sets initial storage of the wallet.\n     *         It can't be called after initialization.\n     *\n     * @param _owner           The owner of the wallet.\n     * @param _guardians       Array of guardians.\n     * @param _recoveryOwners  Array of recovery owners.\n     * @param ownerSignature   Signature of the owner that validates the correctness of the address.\n     */\n    function init(\n        address _owner,\n        address[] calldata _guardians,\n        address[] calldata _recoveryOwners,\n        bytes calldata ownerSignature\n    ) external;\n\n    /**\n     * @notice Executes a generic transaction.\n     *         The transaction is required to be signed by the owner + recovery owner or owner + guardian\n     *         while the wallet is not locked.\n     *\n     * @param to         Destination address.\n     * @param value      Amount in WEI to transfer.\n     * @param callData   Data payload to send.\n     * @param _nonce     Anti-replay number.\n     * @param signatures Signatures of the hash of the transaction.\n     */\n    function exec(\n        address to,\n        uint256 value,\n        bytes calldata callData,\n        uint256 _nonce,\n        bytes calldata signatures\n    ) external returns (bool success);\n\n    /**\n     * @notice Executes a batch of transactions.\n     *\n     * @param transactions An array of Laser transactions.\n     */\n    function multiCall(Transaction[] calldata transactions) external;\n\n    /**\n     * @notice Triggers the recovery mechanism.\n     *\n     * @param callData   Data payload, can only be either lock(), unlock() or recover().\n     * @param signatures Signatures of the hash of the transaction.\n     */\n    function recovery(\n        uint256 _nonce,\n        bytes calldata callData,\n        bytes calldata signatures\n    ) external;\n\n    /**\n     * @notice Returns the hash to be signed to execute a transaction.\n     */\n    function operationHash(\n        address to,\n        uint256 value,\n        bytes calldata callData,\n        uint256 _nonce\n    ) external view returns (bytes32);\n\n    /**\n     * @notice Should return whether the signature provided is valid for the provided hash.\n     *\n     * @param hash      Hash of the data to be signed.\n     * @param signature Signature byte array associated with hash.\n     *\n     * MUST return the bytes4 magic value 0x1626ba7e when function passes.\n     * MUST NOT modify state (using STATICCALL for solc < 0.5, view modifier for solc > 0.5)\n     * MUST allow external calls\n     *\n     * @return Magic value.\n     */\n    function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bytes4);\n\n    /**\n     * @return chainId The chain id of this.\n     */\n    function getChainId() external view returns (uint256 chainId);\n\n    /**\n     * @notice Domain separator for this wallet.\n     */\n    function domainSeparator() external view returns (bytes32);\n}\n"
    },
    "contracts/proxies/LaserFactory.sol": {
      "content": "// SPDX-License-Identifier: LGPL-3.0-only\npragma solidity 0.8.16;\n\nimport \"../interfaces/IERC165.sol\";\nimport \"../interfaces/ILaserFactory.sol\";\nimport \"../interfaces/ILaserWallet.sol\";\n\n/**\n * @title LaserFactory\n *\n * @notice Factory that creates new Laser proxies, and has helper methods.\n */\ncontract LaserFactory is ILaserFactory {\n    address public immutable singleton;\n\n    /**\n     * @param _singleton Base contract.\n     */\n    constructor(address _singleton) {\n        // Laser Wallet contract: bytes4(keccak256(\"I_AM_LASER\"))\n        if (!IERC165(_singleton).supportsInterface(0xae029e0b)) {\n            revert LF__constructor__invalidSingleton();\n        }\n        singleton = _singleton;\n    }\n\n    /**\n     * @dev Allows to create new proxy contact and execute a message call to the new proxy within one transaction.\n     *\n     * @param initializer   Payload for message call sent to new proxy contract.\n     * @param saltNonce     Nonce that will be used to generate the salt to calculate the address of the new proxy contract.\n     */\n    function createProxy(bytes memory initializer, uint256 saltNonce) external returns (LaserProxy proxy) {\n        proxy = deployProxy(initializer, saltNonce);\n\n        bool success;\n        assembly {\n            // We initialize the wallet in a single call.\n            success := call(gas(), proxy, 0, add(initializer, 0x20), mload(initializer), 0, 0)\n        }\n\n        if (!success) revert LF__createProxy__creationFailed();\n\n        emit LaserCreated(address(proxy));\n    }\n\n    /**\n     * @dev Precomputes the address of a proxy that is created through 'create2'.\n     */\n    function preComputeAddress(bytes memory initializer, uint256 saltNonce) external view returns (address) {\n        bytes memory creationCode = proxyCreationCode();\n        bytes memory data = abi.encodePacked(creationCode, uint256(uint160(singleton)));\n\n        bytes32 salt = keccak256(abi.encodePacked(keccak256(initializer), saltNonce));\n\n        bytes32 hash = keccak256(abi.encodePacked(bytes1(0xff), address(this), salt, keccak256(data)));\n\n        return address(uint160(uint256(hash)));\n    }\n\n    /**\n     * @dev Allows to retrieve the runtime code of a deployed Proxy. This can be used to check that the expected Proxy was deployed.\n     */\n    function proxyRuntimeCode() external pure returns (bytes memory) {\n        return type(LaserProxy).runtimeCode;\n    }\n\n    /**\n     *  @dev Allows to retrieve the creation code used for the Proxy deployment. With this it is easily possible to calculate predicted address.\n     */\n    function proxyCreationCode() public pure returns (bytes memory) {\n        return type(LaserProxy).creationCode;\n    }\n\n    /**\n     * @notice Allows to create new proxy contact using CREATE2 but it doesn't run the initializer.\n     *         This method is only meant as an utility to be called from other methods.\n     *\n     * @param initializer Payload for message call sent to new proxy contract.\n     * @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract.\n     */\n    function deployProxy(bytes memory initializer, uint256 saltNonce) internal returns (LaserProxy proxy) {\n        // If the initializer changes the proxy address should change too. Hashing the initializer data is cheaper than just concatinating it\n        bytes32 salt = keccak256(abi.encodePacked(keccak256(initializer), saltNonce));\n\n        bytes memory deploymentData = abi.encodePacked(type(LaserProxy).creationCode, uint256(uint160(singleton)));\n\n        assembly {\n            proxy := create2(0x0, add(0x20, deploymentData), mload(deploymentData), salt)\n        }\n\n        if (address(proxy) == address(0)) revert LF__deployProxy__create2Failed();\n    }\n}\n"
    },
    "contracts/proxies/LaserProxy.sol": {
      "content": "// SPDX-License-Identifier: LGPL-3.0-only\npragma solidity 0.8.16;\n\n/**\n * @title LaserProxy\n *\n * @notice Proxy contract that delegates all calls to a master copy.\n */\ncontract LaserProxy {\n    // The singleton always needs to be at storage slot 0.\n    address internal singleton;\n\n    /**\n     * @param _singleton Singleton address.\n     */\n    constructor(address _singleton) {\n        // The proxy creation is done through the LaserProxyFactory.\n        // The singleton is created at the factory's creation, so there is no need to do checks here.\n        singleton = _singleton;\n    }\n\n    /**\n     * @dev Fallback function forwards all transactions and returns all received return data.\n     */\n    fallback() external payable {\n        address _singleton = singleton;\n        assembly {\n            calldatacopy(0, 0, calldatasize())\n            let success := delegatecall(gas(), _singleton, 0, calldatasize(), 0, 0)\n            returndatacopy(0, 0, returndatasize())\n            if eq(success, 0) {\n                revert(0, returndatasize())\n            }\n            return(0, returndatasize())\n        }\n    }\n}\n"
    }
  }
}