creation_sourcecode: {{
    "language": "Solidity",
    "sources": {
      "contracts/lib/Lib_RLPReader.sol": {
        "content": "// SPDX-License-Identifier: Apache-2.0\n\n/*\n * @author Hamdi Allam hamdi.allam97@gmail.com\n * Please reach out with any questions or concerns\n */\npragma solidity >=0.5.10 <0.9.0;\n\nlibrary Lib_RLPReader {\n    uint8 constant STRING_SHORT_START = 0x80;\n    uint8 constant STRING_LONG_START = 0xb8;\n    uint8 constant LIST_SHORT_START = 0xc0;\n    uint8 constant LIST_LONG_START = 0xf8;\n    uint8 constant WORD_SIZE = 32;\n\n    struct RLPItem {\n        uint256 len;\n        uint256 memPtr;\n    }\n\n    struct Iterator {\n        RLPItem item; // Item that's being iterated over.\n        uint256 nextPtr; // Position of the next item in the list.\n    }\n\n    /*\n     * @dev Returns the next element in the iteration. Reverts if it has not next element.\n     * @param self The iterator.\n     * @return The next element in the iteration.\n     */\n    function next(Iterator memory self) internal pure returns (RLPItem memory) {\n        require(hasNext(self));\n\n        uint256 ptr = self.nextPtr;\n        uint256 itemLength = _itemLength(ptr);\n        self.nextPtr = ptr + itemLength;\n\n        return RLPItem(itemLength, ptr);\n    }\n\n    /*\n     * @dev Returns true if the iteration has more elements.\n     * @param self The iterator.\n     * @return true if the iteration has more elements.\n     */\n    function hasNext(Iterator memory self) internal pure returns (bool) {\n        RLPItem memory item = self.item;\n        return self.nextPtr < item.memPtr + item.len;\n    }\n\n    /*\n     * @param item RLP encoded bytes\n     */\n    function toRlpItem(\n        bytes memory item\n    ) internal pure returns (RLPItem memory) {\n        uint256 memPtr;\n        assembly {\n            memPtr := add(item, 0x20)\n        }\n\n        return RLPItem(item.length, memPtr);\n    }\n\n    /*\n     * @dev Create an iterator. Reverts if item is not a list.\n     * @param self The RLP item.\n     * @return An 'Iterator' over the item.\n     */\n    function iterator(\n        RLPItem memory self\n    ) internal pure returns (Iterator memory) {\n        require(isList(self));\n\n        uint256 ptr = self.memPtr + _payloadOffset(self.memPtr);\n        return Iterator(self, ptr);\n    }\n\n    /*\n     * @param the RLP item.\n     */\n    function rlpLen(RLPItem memory item) internal pure returns (uint256) {\n        return item.len;\n    }\n\n    /*\n     * @param the RLP item.\n     * @return (memPtr, len) pair: location of the item's payload in memory.\n     */\n    function payloadLocation(\n        RLPItem memory item\n    ) internal pure returns (uint256, uint256) {\n        uint256 offset = _payloadOffset(item.memPtr);\n        uint256 memPtr = item.memPtr + offset;\n        uint256 len = item.len - offset; // data length\n        return (memPtr, len);\n    }\n\n    /*\n     * @param the RLP item.\n     */\n    function payloadLen(RLPItem memory item) internal pure returns (uint256) {\n        (, uint256 len) = payloadLocation(item);\n        return len;\n    }\n\n    /*\n     * @param the RLP item containing the encoded list.\n     */\n    function toList(\n        RLPItem memory item\n    ) internal pure returns (RLPItem[] memory) {\n        require(isList(item));\n\n        uint256 items = numItems(item);\n        RLPItem[] memory result = new RLPItem[](items);\n\n        uint256 memPtr = item.memPtr + _payloadOffset(item.memPtr);\n        uint256 dataLen;\n        for (uint256 i = 0; i < items; i++) {\n            dataLen = _itemLength(memPtr);\n            result[i] = RLPItem(dataLen, memPtr);\n            memPtr = memPtr + dataLen;\n        }\n\n        return result;\n    }\n\n    // @return indicator whether encoded payload is a list. negate this function call for isData.\n    function isList(RLPItem memory item) internal pure returns (bool) {\n        if (item.len == 0) return false;\n\n        uint8 byte0;\n        uint256 memPtr = item.memPtr;\n        assembly {\n            byte0 := byte(0, mload(memPtr))\n        }\n\n        if (byte0 < LIST_SHORT_START) return false;\n        return true;\n    }\n\n    /*\n     * @dev A cheaper version of keccak256(toRlpBytes(item)) that avoids copying memory.\n     * @return keccak256 hash of RLP encoded bytes.\n     */\n    function rlpBytesKeccak256(\n        RLPItem memory item\n    ) internal pure returns (bytes32) {\n        uint256 ptr = item.memPtr;\n        uint256 len = item.len;\n        bytes32 result;\n        assembly {\n            result := keccak256(ptr, len)\n        }\n        return result;\n    }\n\n    /*\n     * @dev A cheaper version of keccak256(toBytes(item)) that avoids copying memory.\n     * @return keccak256 hash of the item payload.\n     */\n    function payloadKeccak256(\n        RLPItem memory item\n    ) internal pure returns (bytes32) {\n        (uint256 memPtr, uint256 len) = payloadLocation(item);\n        bytes32 result;\n        assembly {\n            result := keccak256(memPtr, len)\n        }\n        return result;\n    }\n\n    /** RLPItem conversions into data types **/\n\n    // @returns raw rlp encoding in bytes\n    function toRlpBytes(\n        RLPItem memory item\n    ) internal pure returns (bytes memory) {\n        bytes memory result = new bytes(item.len);\n        if (result.length == 0) return result;\n\n        uint256 ptr;\n        assembly {\n            ptr := add(0x20, result)\n        }\n\n        copy(item.memPtr, ptr, item.len);\n        return result;\n    }\n\n    // any non-zero byte except \"0x80\" is considered true\n    function toBoolean(RLPItem memory item) internal pure returns (bool) {\n        require(item.len == 1);\n        uint256 result;\n        uint256 memPtr = item.memPtr;\n        assembly {\n            result := byte(0, mload(memPtr))\n        }\n\n        // SEE Github Issue #5.\n        // Summary: Most commonly used RLP libraries (i.e Geth) will encode\n        // \"0\" as \"0x80\" instead of as \"0\". We handle this edge case explicitly\n        // here.\n        if (result == 0 || result == STRING_SHORT_START) {\n            return false;\n        } else {\n            return true;\n        }\n    }\n\n    function toAddress(RLPItem memory item) internal pure returns (address) {\n        // 1 byte for the length prefix\n        require(item.len == 21);\n\n        return address(uint160(toUint(item)));\n    }\n\n    function toUint(RLPItem memory item) internal pure returns (uint256) {\n        require(item.len > 0 && item.len <= 33);\n\n        (uint256 memPtr, uint256 len) = payloadLocation(item);\n\n        uint256 result;\n        assembly {\n            result := mload(memPtr)\n\n            // shift to the correct location if neccesary\n            if lt(len, 32) {\n                result := div(result, exp(256, sub(32, len)))\n            }\n        }\n\n        return result;\n    }\n\n    // enforces 32 byte length\n    function toUintStrict(RLPItem memory item) internal pure returns (uint256) {\n        // one byte prefix\n        require(item.len == 33);\n\n        uint256 result;\n        uint256 memPtr = item.memPtr + 1;\n        assembly {\n            result := mload(memPtr)\n        }\n\n        return result;\n    }\n\n    function toBytes(RLPItem memory item) internal pure returns (bytes memory) {\n        require(item.len > 0);\n\n        (uint256 memPtr, uint256 len) = payloadLocation(item);\n        bytes memory result = new bytes(len);\n\n        uint256 destPtr;\n        assembly {\n            destPtr := add(0x20, result)\n        }\n\n        copy(memPtr, destPtr, len);\n        return result;\n    }\n\n    /*\n     * Private Helpers\n     */\n\n    // @return number of payload items inside an encoded list.\n    function numItems(RLPItem memory item) private pure returns (uint256) {\n        if (item.len == 0) return 0;\n\n        uint256 count = 0;\n        uint256 currPtr = item.memPtr + _payloadOffset(item.memPtr);\n        uint256 endPtr = item.memPtr + item.len;\n        while (currPtr < endPtr) {\n            currPtr = currPtr + _itemLength(currPtr); // skip over an item\n            count++;\n        }\n\n        return count;\n    }\n\n    // @return entire rlp item byte length\n    function _itemLength(uint256 memPtr) private pure returns (uint256) {\n        uint256 itemLen;\n        uint256 byte0;\n        assembly {\n            byte0 := byte(0, mload(memPtr))\n        }\n\n        if (byte0 < STRING_SHORT_START) {\n            itemLen = 1;\n        } else if (byte0 < STRING_LONG_START) {\n            itemLen = byte0 - STRING_SHORT_START + 1;\n        } else if (byte0 < LIST_SHORT_START) {\n            assembly {\n                let byteLen := sub(byte0, 0xb7) // # of bytes the actual length is\n                memPtr := add(memPtr, 1) // skip over the first byte\n\n                /* 32 byte word size */\n                let dataLen := div(mload(memPtr), exp(256, sub(32, byteLen))) // right shifting to get the len\n                itemLen := add(dataLen, add(byteLen, 1))\n            }\n        } else if (byte0 < LIST_LONG_START) {\n            itemLen = byte0 - LIST_SHORT_START + 1;\n        } else {\n            assembly {\n                let byteLen := sub(byte0, 0xf7)\n                memPtr := add(memPtr, 1)\n\n                let dataLen := div(mload(memPtr), exp(256, sub(32, byteLen))) // right shifting to the correct length\n                itemLen := add(dataLen, add(byteLen, 1))\n            }\n        }\n\n        return itemLen;\n    }\n\n    // @return number of bytes until the data\n    function _payloadOffset(uint256 memPtr) private pure returns (uint256) {\n        uint256 byte0;\n        assembly {\n            byte0 := byte(0, mload(memPtr))\n        }\n\n        if (byte0 < STRING_SHORT_START) {\n            return 0;\n        } else if (\n            byte0 < STRING_LONG_START ||\n            (byte0 >= LIST_SHORT_START && byte0 < LIST_LONG_START)\n        ) {\n            return 1;\n        } else if (byte0 < LIST_SHORT_START) {\n            // being explicit\n            return byte0 - (STRING_LONG_START - 1) + 1;\n        } else {\n            return byte0 - (LIST_LONG_START - 1) + 1;\n        }\n    }\n\n    /*\n     * @param src Pointer to source\n     * @param dest Pointer to destination\n     * @param len Amount of memory to copy from the source\n     */\n    function copy(uint256 src, uint256 dest, uint256 len) private pure {\n        if (len == 0) return;\n\n        // copy as many word sizes as possible\n        for (; len >= WORD_SIZE; len -= WORD_SIZE) {\n            assembly {\n                mstore(dest, mload(src))\n            }\n\n            src += WORD_SIZE;\n            dest += WORD_SIZE;\n        }\n\n        if (len > 0) {\n            // left over bytes. Mask is used to remove unwanted bytes from the word\n            uint256 mask = 256 ** (WORD_SIZE - len) - 1;\n            assembly {\n                let srcpart := and(mload(src), not(mask)) // zero out src\n                let destpart := and(mload(dest), mask) // retrieve the bytes\n                mstore(dest, or(destpart, srcpart))\n            }\n        }\n    }\n}\n"
      },
      "contracts/RLPReader.sol": {
        "content": "// SPDX-License-Identifier: Apache-2.0\npragma solidity >=0.5.10 <0.9.0;\n\nimport \"./lib/Lib_RLPReader.sol\";\n\n/// @title RLPReader\n/// @author LightLink Hummingbird\n/// @custom:version v1.1.0-beta\n/// @notice A contract that reads RLP encoded data.\ncontract RLPReader {\n    using Lib_RLPReader for bytes;\n    using Lib_RLPReader for uint;\n    using Lib_RLPReader for Lib_RLPReader.RLPItem;\n    using Lib_RLPReader for Lib_RLPReader.Iterator;\n\n    /// @notice Returns the block header from the RLP encoded data.\n    /// @param rlpHeader - The RLP encoded block header.\n    /// @return parentHash - The parent hash.\n    /// @return sha3Uncles - The sha3 uncles.\n    /// @return coinbase - The coinbase address.\n    /// @return stateRoot - The state root.\n    /// @return transactionsRoot - The transactions root.\n    /// @return receiptsRoot - The receipts root.\n    /// @return difficulty - The difficulty.\n    /// @return number - The block number.\n    /// @return gasLimit - The gas limit.\n    /// @return gasUsed - The gas used.\n    /// @return timestamp - The timestamp.\n    /// @return nonce - The nonce.\n    function toBlockHeader(\n        bytes memory rlpHeader\n    )\n        public\n        pure\n        returns (\n            bytes32 parentHash,\n            bytes32 sha3Uncles,\n            address coinbase,\n            bytes32 stateRoot,\n            bytes32 transactionsRoot,\n            bytes32 receiptsRoot,\n            uint difficulty,\n            uint number,\n            uint gasLimit,\n            uint gasUsed,\n            uint timestamp,\n            uint nonce\n        )\n    {\n        Lib_RLPReader.Iterator memory it = rlpHeader.toRlpItem().iterator();\n        uint idx;\n        while (it.hasNext()) {\n            if (idx == 0) parentHash = bytes32(it.next().toUint());\n            else if (idx == 1) sha3Uncles = bytes32(it.next().toUint());\n            else if (idx == 2) coinbase = it.next().toAddress();\n            else if (idx == 3) stateRoot = bytes32(it.next().toUint());\n            else if (idx == 4) transactionsRoot = bytes32(it.next().toUint());\n            else if (idx == 5) receiptsRoot = bytes32(it.next().toUint());\n            else if (idx == 7) difficulty = it.next().toUint();\n            else if (idx == 8) number = it.next().toUint();\n            else if (idx == 9) gasLimit = it.next().toUint();\n            else if (idx == 10) gasUsed = it.next().toUint();\n            else if (idx == 11) timestamp = it.next().toUint();\n            else if (idx == 14) nonce = it.next().toUint();\n            else it.next();\n\n            idx++;\n        }\n    }\n\n    /// @notice Returns the legacy transaction from the RLP encoded data.\n    /// @param rlpTx - The RLP encoded transaction.\n    /// @return nonce - The nonce.\n    /// @return gasPrice - The gas price.\n    /// @return gasLimit - The gas limit.\n    /// @return to - The recipient address.\n    /// @return value - The value.\n    /// @return data - The data.\n    /// @return v - The v value.\n    /// @return r - The r value.\n    /// @return s - The s value.\n    function toLegacyTx(\n        bytes memory rlpTx\n    )\n        public\n        pure\n        returns (\n            uint nonce,\n            uint gasPrice,\n            uint gasLimit,\n            address to,\n            uint value,\n            bytes memory data,\n            uint v,\n            uint r,\n            uint s\n        )\n    {\n        Lib_RLPReader.Iterator memory it = rlpTx.toRlpItem().iterator();\n        uint idx;\n        while (it.hasNext()) {\n            if (idx == 0) nonce = it.next().toUint();\n            else if (idx == 1) gasPrice = it.next().toUint();\n            else if (idx == 2) gasLimit = it.next().toUint();\n            else if (idx == 3) to = it.next().toAddress();\n            else if (idx == 4) value = it.next().toUint();\n            else if (idx == 5) data = it.next().toBytes();\n            else if (idx == 6) v = it.next().toUint();\n            else if (idx == 7) r = it.next().toUint();\n            else if (idx == 8) s = it.next().toUint();\n            else it.next();\n            idx++;\n        }\n        return (nonce, gasPrice, gasLimit, to, value, data, v, r, s);\n    }\n\n    /// @notice Returns the deposit transaction from the RLP encoded data.\n    /// @param rlpTx - The RLP encoded transaction.\n    /// @return chainId - The chain ID.\n    /// @return nonce - The nonce.\n    /// @return gasPrice - The gas price.\n    /// @return gasLimit - The gas limit.\n    /// @return to - The recipient address.\n    /// @return value - The value.\n    /// @return data - The data.\n    /// @return v - The v value.\n    /// @return r - The r value.\n    /// @return s - The s value.\n    function toDepositTx(\n        bytes memory rlpTx\n    )\n        public\n        pure\n        returns (\n            uint chainId,\n            uint nonce,\n            uint gasPrice,\n            uint gasLimit,\n            address to,\n            uint value,\n            bytes memory data,\n            uint v,\n            uint r,\n            uint s\n        )\n    {\n        Lib_RLPReader.Iterator memory it = rlpTx.toRlpItem().iterator();\n        uint idx;\n        while (it.hasNext()) {\n            if (idx == 0) chainId = it.next().toUint();\n            else if (idx == 1) nonce = it.next().toUint();\n            else if (idx == 2) gasPrice = it.next().toUint();\n            else if (idx == 3) gasLimit = it.next().toUint();\n            else if (idx == 4) to = it.next().toAddress();\n            else if (idx == 5) value = it.next().toUint();\n            else if (idx == 6) data = it.next().toBytes();\n            else if (idx == 7) v = it.next().toUint();\n            else if (idx == 8) r = it.next().toUint();\n            else if (idx == 9) s = it.next().toUint();\n            else it.next();\n\n            idx++;\n        }\n    }\n}\n"
      }
    },
    "settings": {
      "optimizer": {
        "enabled": true,
        "runs": 200
      },
      "viaIR": true,
      "evmVersion": "paris",
      "outputSelection": {
        "*": {
          "*": [
            "evm.bytecode",
            "evm.deployedBytecode",
            "devdoc",
            "userdoc",
            "metadata",
            "abi"
          ]
        }
      },
      "libraries": {}
    }
  }}