Encoding.sol 5.58 KB
Newer Older
1
// SPDX-License-Identifier: MIT
2
pragma solidity ^0.8.0;
3

4
import { Types } from "./Types.sol";
5 6 7 8 9 10 11 12 13
import { Hashing } from "./Hashing.sol";
import { RLPWriter } from "./rlp/RLPWriter.sol";

/**
 * @title Encoding
 * @notice Encoding handles Optimism's various different encoding schemes.
 */
library Encoding {
    /**
14
     * @notice RLP encodes the L2 transaction that would be generated when a given deposit is sent
15 16
     *         to the L2 system. Useful for searching for a deposit in the L2 system. The
     *         transaction is prefixed with 0x7e to identify its EIP-2718 type.
17
     *
18
     * @param _tx User deposit transaction to encode.
19 20
     *
     * @return RLP encoded L2 deposit transaction.
21
     */
22 23 24 25 26 27
    function encodeDepositTransaction(Types.UserDepositTransaction memory _tx)
        internal
        pure
        returns (bytes memory)
    {
        bytes32 source = Hashing.hashDepositSource(_tx.l1BlockHash, _tx.logIndex);
28
        bytes[] memory raw = new bytes[](8);
29
        raw[0] = RLPWriter.writeBytes(abi.encodePacked(source));
30 31 32 33 34
        raw[1] = RLPWriter.writeAddress(_tx.from);
        raw[2] = _tx.isCreation ? RLPWriter.writeBytes("") : RLPWriter.writeAddress(_tx.to);
        raw[3] = RLPWriter.writeUint(_tx.mint);
        raw[4] = RLPWriter.writeUint(_tx.value);
        raw[5] = RLPWriter.writeUint(uint256(_tx.gasLimit));
35 36
        raw[6] = RLPWriter.writeBool(false);
        raw[7] = RLPWriter.writeBytes(_tx.data);
37
        return abi.encodePacked(uint8(0x7e), RLPWriter.writeList(raw));
38 39 40
    }

    /**
41 42 43 44 45 46 47 48 49 50 51
     * @notice Encodes the cross domain message based on the version that is encoded into the
     *         message nonce.
     *
     * @param _nonce    Message nonce with version encoded into the first two bytes.
     * @param _sender   Address of the sender of the message.
     * @param _target   Address of the target of the message.
     * @param _value    ETH value to send to the target.
     * @param _gasLimit Gas limit to use for the message.
     * @param _data     Data to send with the message.
     *
     * @return Encoded cross domain message.
52
     */
53
    function encodeCrossDomainMessage(
54 55 56 57 58 59 60
        uint256 _nonce,
        address _sender,
        address _target,
        uint256 _value,
        uint256 _gasLimit,
        bytes memory _data
    ) internal pure returns (bytes memory) {
61
        (, uint16 version) = decodeVersionedNonce(_nonce);
62
        if (version == 0) {
63
            return encodeCrossDomainMessageV0(_target, _sender, _data, _nonce);
64
        } else if (version == 1) {
65 66 67
            return encodeCrossDomainMessageV1(_nonce, _sender, _target, _value, _gasLimit, _data);
        } else {
            revert("Encoding: unknown cross domain message version");
68 69 70 71
        }
    }

    /**
72 73 74 75 76 77 78 79
     * @notice Encodes a cross domain message based on the V0 (legacy) encoding.
     *
     * @param _target Address of the target of the message.
     * @param _sender Address of the sender of the message.
     * @param _data   Data to send with the message.
     * @param _nonce  Message nonce.
     *
     * @return Encoded cross domain message.
80
     */
81
    function encodeCrossDomainMessageV0(
82 83 84 85 86
        address _target,
        address _sender,
        bytes memory _data,
        uint256 _nonce
    ) internal pure returns (bytes memory) {
87 88 89 90 91 92 93 94
        return
            abi.encodeWithSignature(
                "relayMessage(address,address,bytes,uint256)",
                _target,
                _sender,
                _data,
                _nonce
            );
95 96 97
    }

    /**
98 99 100 101 102 103 104 105 106 107
     * @notice Encodes a cross domain message based on the V1 (current) encoding.
     *
     * @param _nonce    Message nonce.
     * @param _sender   Address of the sender of the message.
     * @param _target   Address of the target of the message.
     * @param _value    ETH value to send to the target.
     * @param _gasLimit Gas limit to use for the message.
     * @param _data     Data to send with the message.
     *
     * @return Encoded cross domain message.
108
     */
109
    function encodeCrossDomainMessageV1(
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        uint256 _nonce,
        address _sender,
        address _target,
        uint256 _value,
        uint256 _gasLimit,
        bytes memory _data
    ) internal pure returns (bytes memory) {
        return
            abi.encodeWithSignature(
                "relayMessage(uint256,address,address,uint256,uint256,bytes)",
                _nonce,
                _sender,
                _target,
                _value,
                _gasLimit,
                _data
            );
    }

    /**
130 131 132 133 134 135
     * @notice Adds a version number into the first two bytes of a message nonce.
     *
     * @param _nonce   Message nonce to encode into.
     * @param _version Version number to encode into the message nonce.
     *
     * @return Message nonce with version encoded into the first two bytes.
136
     */
137 138
    function encodeVersionedNonce(uint240 _nonce, uint16 _version) internal pure returns (uint256) {
        uint256 nonce;
139 140 141
        assembly {
            nonce := or(shl(240, _version), _nonce)
        }
142
        return nonce;
143 144 145
    }

    /**
146 147 148 149 150 151
     * @notice Pulls the version out of a version-encoded nonce.
     *
     * @param _nonce Message nonce with version encoded into the first two bytes.
     *
     * @return Nonce without encoded version.
     * @return Version of the message.
152
     */
153 154 155
    function decodeVersionedNonce(uint256 _nonce) internal pure returns (uint240, uint16) {
        uint240 nonce;
        uint16 version;
156
        assembly {
157
            nonce := and(_nonce, 0x0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
158 159
            version := shr(240, _nonce)
        }
160
        return (nonce, version);
161 162
    }
}