Lib_RLPWriter.sol 6.69 KB
Newer Older
George Hotz's avatar
George Hotz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// SPDX-License-Identifier: MIT
pragma solidity >0.5.0 <0.8.0;
pragma experimental ABIEncoderV2;

/**
 * @title Lib_RLPWriter
 * @author Bakaoh (with modifications)
 */
library Lib_RLPWriter {

    /**********************
     * Internal Functions *
     **********************/

    /**
     * RLP encodes a byte string.
     * @param _in The byte string to encode.
George Hotz's avatar
George Hotz committed
18
     * @return The RLP encoded string in bytes.
George Hotz's avatar
George Hotz committed
19 20 21 22 23 24 25
     */
    function writeBytes(
        bytes memory _in
    )
        internal
        pure
        returns (
George Hotz's avatar
George Hotz committed
26
            bytes memory
George Hotz's avatar
George Hotz committed
27 28 29 30 31 32 33
        )
    {
        bytes memory encoded;

        if (_in.length == 1 && uint8(_in[0]) < 128) {
            encoded = _in;
        } else {
George Hotz's avatar
George Hotz committed
34
            encoded = abi.encodePacked(_writeLength(_in.length, 128), _in);
George Hotz's avatar
George Hotz committed
35 36 37 38 39 40 41 42
        }

        return encoded;
    }

    /**
     * RLP encodes a list of RLP encoded byte byte strings.
     * @param _in The list of RLP encoded byte strings.
George Hotz's avatar
George Hotz committed
43
     * @return The RLP encoded list of items in bytes.
George Hotz's avatar
George Hotz committed
44 45 46 47 48 49 50
     */
    function writeList(
        bytes[] memory _in
    )
        internal
        pure
        returns (
George Hotz's avatar
George Hotz committed
51
            bytes memory
George Hotz's avatar
George Hotz committed
52 53 54
        )
    {
        bytes memory list = _flatten(_in);
George Hotz's avatar
George Hotz committed
55
        return abi.encodePacked(_writeLength(list.length, 192), list);
George Hotz's avatar
George Hotz committed
56 57 58 59 60
    }

    /**
     * RLP encodes a string.
     * @param _in The string to encode.
George Hotz's avatar
George Hotz committed
61
     * @return The RLP encoded string in bytes.
George Hotz's avatar
George Hotz committed
62 63 64 65 66 67 68
     */
    function writeString(
        string memory _in
    )
        internal
        pure
        returns (
George Hotz's avatar
George Hotz committed
69
            bytes memory
George Hotz's avatar
George Hotz committed
70 71 72 73 74 75 76 77
        )
    {
        return writeBytes(bytes(_in));
    }

    /**
     * RLP encodes an address.
     * @param _in The address to encode.
George Hotz's avatar
George Hotz committed
78
     * @return The RLP encoded address in bytes.
George Hotz's avatar
George Hotz committed
79 80 81
     */
    function writeAddress(
        address _in
George Hotz's avatar
George Hotz committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    )
        internal
        pure
        returns (
            bytes memory
        )
    {
        return writeBytes(abi.encodePacked(_in));
    }

    /**
     * RLP encodes a bytes32 value.
     * @param _in The bytes32 to encode.
     * @return _out The RLP encoded bytes32 in bytes.
     */
    function writeBytes32(
        bytes32 _in
George Hotz's avatar
George Hotz committed
99 100 101 102 103 104 105 106 107 108 109 110 111
    )
        internal
        pure
        returns (
            bytes memory _out
        )
    {
        return writeBytes(abi.encodePacked(_in));
    }

    /**
     * RLP encodes a uint.
     * @param _in The uint256 to encode.
George Hotz's avatar
George Hotz committed
112
     * @return The RLP encoded uint256 in bytes.
George Hotz's avatar
George Hotz committed
113 114 115 116 117 118 119
     */
    function writeUint(
        uint256 _in
    )
        internal
        pure
        returns (
George Hotz's avatar
George Hotz committed
120
            bytes memory
George Hotz's avatar
George Hotz committed
121 122 123 124 125 126 127 128
        )
    {
        return writeBytes(_toBinary(_in));
    }

    /**
     * RLP encodes a bool.
     * @param _in The bool to encode.
George Hotz's avatar
George Hotz committed
129
     * @return The RLP encoded bool in bytes.
George Hotz's avatar
George Hotz committed
130 131 132 133 134 135 136
     */
    function writeBool(
        bool _in
    )
        internal
        pure
        returns (
George Hotz's avatar
George Hotz committed
137
            bytes memory
George Hotz's avatar
George Hotz committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        )
    {
        bytes memory encoded = new bytes(1);
        encoded[0] = (_in ? bytes1(0x01) : bytes1(0x80));
        return encoded;
    }


    /*********************
     * Private Functions *
     *********************/

    /**
     * Encode the first byte, followed by the `len` in binary form if `length` is more than 55.
     * @param _len The length of the string or the payload.
     * @param _offset 128 if item is string, 192 if item is list.
George Hotz's avatar
George Hotz committed
154
     * @return RLP encoded bytes.
George Hotz's avatar
George Hotz committed
155 156 157 158 159 160 161 162
     */
    function _writeLength(
        uint256 _len,
        uint256 _offset
    )
        private
        pure
        returns (
George Hotz's avatar
George Hotz committed
163
            bytes memory
George Hotz's avatar
George Hotz committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        )
    {
        bytes memory encoded;

        if (_len < 56) {
            encoded = new bytes(1);
            encoded[0] = byte(uint8(_len) + uint8(_offset));
        } else {
            uint256 lenLen;
            uint256 i = 1;
            while (_len / i != 0) {
                lenLen++;
                i *= 256;
            }

            encoded = new bytes(lenLen + 1);
            encoded[0] = byte(uint8(lenLen) + uint8(_offset) + 55);
            for(i = 1; i <= lenLen; i++) {
                encoded[i] = byte(uint8((_len / (256**(lenLen-i))) % 256));
            }
        }

        return encoded;
    }

    /**
     * Encode integer in big endian binary form with no leading zeroes.
     * @notice TODO: This should be optimized with assembly to save gas costs.
     * @param _x The integer to encode.
George Hotz's avatar
George Hotz committed
193
     * @return RLP encoded bytes.
George Hotz's avatar
George Hotz committed
194 195 196 197 198 199 200
     */
    function _toBinary(
        uint256 _x
    )
        private
        pure
        returns (
George Hotz's avatar
George Hotz committed
201
            bytes memory
George Hotz's avatar
George Hotz committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        )
    {
        bytes memory b = abi.encodePacked(_x);

        uint256 i = 0;
        for (; i < 32; i++) {
            if (b[i] != 0) {
                break;
            }
        }

        bytes memory res = new bytes(32 - i);
        for (uint256 j = 0; j < res.length; j++) {
            res[j] = b[i++];
        }

        return res;
    }

    /**
     * Copies a piece of memory to another location.
     * @notice From: https://github.com/Arachnid/solidity-stringutils/blob/master/src/strings.sol.
     * @param _dest Destination location.
     * @param _src Source location.
     * @param _len Length of memory to copy.
     */
    function _memcpy(
        uint256 _dest,
        uint256 _src,
        uint256 _len
    )
        private
        pure
    {
        uint256 dest = _dest;
        uint256 src = _src;
        uint256 len = _len;

        for(; len >= 32; len -= 32) {
            assembly {
                mstore(dest, mload(src))
            }
            dest += 32;
            src += 32;
        }

        uint256 mask = 256 ** (32 - len) - 1;
        assembly {
            let srcpart := and(mload(src), not(mask))
            let destpart := and(mload(dest), mask)
            mstore(dest, or(destpart, srcpart))
        }
    }

    /**
     * Flattens a list of byte strings into one byte string.
     * @notice From: https://github.com/sammayo/solidity-rlp-encoder/blob/master/RLPEncode.sol.
     * @param _list List of byte strings to flatten.
George Hotz's avatar
George Hotz committed
260
     * @return The flattened byte string.
George Hotz's avatar
George Hotz committed
261 262 263 264 265 266 267
     */
    function _flatten(
        bytes[] memory _list
    )
        private
        pure
        returns (
George Hotz's avatar
George Hotz committed
268
            bytes memory
George Hotz's avatar
George Hotz committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        )
    {
        if (_list.length == 0) {
            return new bytes(0);
        }

        uint256 len;
        uint256 i = 0;
        for (; i < _list.length; i++) {
            len += _list[i].length;
        }

        bytes memory flattened = new bytes(len);
        uint256 flattenedPtr;
        assembly { flattenedPtr := add(flattened, 0x20) }

        for(i = 0; i < _list.length; i++) {
            bytes memory item = _list[i];

            uint256 listPtr;
            assembly { listPtr := add(item, 0x20)}

            _memcpy(flattenedPtr, listPtr, item.length);
            flattenedPtr += _list[i].length;
        }

        return flattened;
    }
}