Commit 2278662b authored by George Hotz's avatar George Hotz

update rlp reader/writer

parent 27bf62a6
...@@ -23,7 +23,7 @@ library Lib_RLPReader { ...@@ -23,7 +23,7 @@ library Lib_RLPReader {
LIST_ITEM LIST_ITEM
} }
/*********** /***********
* Structs * * Structs *
***********/ ***********/
...@@ -32,12 +32,12 @@ library Lib_RLPReader { ...@@ -32,12 +32,12 @@ library Lib_RLPReader {
uint256 length; uint256 length;
uint256 ptr; uint256 ptr;
} }
/********************** /**********************
* Internal Functions * * Internal Functions *
**********************/ **********************/
/** /**
* Converts bytes to a reference to memory position and length. * Converts bytes to a reference to memory position and length.
* @param _in Input bytes to convert. * @param _in Input bytes to convert.
...@@ -353,6 +353,11 @@ library Lib_RLPReader { ...@@ -353,6 +353,11 @@ library Lib_RLPReader {
out := byte(0, mload(ptr)) out := byte(0, mload(ptr))
} }
require(
out == 0 || out == 1,
"Lib_RLPReader: Invalid RLP boolean value, must be 0 or 1"
);
return out != 0; return out != 0;
} }
...@@ -479,7 +484,7 @@ library Lib_RLPReader { ...@@ -479,7 +484,7 @@ library Lib_RLPReader {
// Short string. // Short string.
uint256 strLen = prefix - 0x80; uint256 strLen = prefix - 0x80;
require( require(
_in.length > strLen, _in.length > strLen,
"Invalid RLP short string." "Invalid RLP short string."
......
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
pragma solidity >0.5.0 <0.8.0; pragma solidity >0.5.0 <0.8.0;
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
/* Library Imports */
import { Lib_BytesUtils } from "./Lib_BytesUtils.sol";
/** /**
* @title Lib_RLPWriter * @title Lib_RLPWriter
* @author Bakaoh (with modifications) * @author Bakaoh (with modifications)
...@@ -18,7 +15,7 @@ library Lib_RLPWriter { ...@@ -18,7 +15,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes a byte string. * RLP encodes a byte string.
* @param _in The byte string to encode. * @param _in The byte string to encode.
* @return _out The RLP encoded string in bytes. * @return The RLP encoded string in bytes.
*/ */
function writeBytes( function writeBytes(
bytes memory _in bytes memory _in
...@@ -26,7 +23,7 @@ library Lib_RLPWriter { ...@@ -26,7 +23,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
bytes memory encoded; bytes memory encoded;
...@@ -34,7 +31,7 @@ library Lib_RLPWriter { ...@@ -34,7 +31,7 @@ library Lib_RLPWriter {
if (_in.length == 1 && uint8(_in[0]) < 128) { if (_in.length == 1 && uint8(_in[0]) < 128) {
encoded = _in; encoded = _in;
} else { } else {
encoded = Lib_BytesUtils.concat(_writeLength(_in.length, 128), _in); encoded = abi.encodePacked(_writeLength(_in.length, 128), _in);
} }
return encoded; return encoded;
...@@ -43,7 +40,7 @@ library Lib_RLPWriter { ...@@ -43,7 +40,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes a list of RLP encoded byte byte strings. * RLP encodes a list of RLP encoded byte byte strings.
* @param _in The list of RLP encoded byte strings. * @param _in The list of RLP encoded byte strings.
* @return _out The RLP encoded list of items in bytes. * @return The RLP encoded list of items in bytes.
*/ */
function writeList( function writeList(
bytes[] memory _in bytes[] memory _in
...@@ -51,17 +48,17 @@ library Lib_RLPWriter { ...@@ -51,17 +48,17 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
bytes memory list = _flatten(_in); bytes memory list = _flatten(_in);
return Lib_BytesUtils.concat(_writeLength(list.length, 192), list); return abi.encodePacked(_writeLength(list.length, 192), list);
} }
/** /**
* RLP encodes a string. * RLP encodes a string.
* @param _in The string to encode. * @param _in The string to encode.
* @return _out The RLP encoded string in bytes. * @return The RLP encoded string in bytes.
*/ */
function writeString( function writeString(
string memory _in string memory _in
...@@ -69,7 +66,7 @@ library Lib_RLPWriter { ...@@ -69,7 +66,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
return writeBytes(bytes(_in)); return writeBytes(bytes(_in));
...@@ -78,10 +75,27 @@ library Lib_RLPWriter { ...@@ -78,10 +75,27 @@ library Lib_RLPWriter {
/** /**
* RLP encodes an address. * RLP encodes an address.
* @param _in The address to encode. * @param _in The address to encode.
* @return _out The RLP encoded address in bytes. * @return The RLP encoded address in bytes.
*/ */
function writeAddress( function writeAddress(
address _in address _in
)
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
) )
internal internal
pure pure
...@@ -95,7 +109,7 @@ library Lib_RLPWriter { ...@@ -95,7 +109,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes a uint. * RLP encodes a uint.
* @param _in The uint256 to encode. * @param _in The uint256 to encode.
* @return _out The RLP encoded uint256 in bytes. * @return The RLP encoded uint256 in bytes.
*/ */
function writeUint( function writeUint(
uint256 _in uint256 _in
...@@ -103,7 +117,7 @@ library Lib_RLPWriter { ...@@ -103,7 +117,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
return writeBytes(_toBinary(_in)); return writeBytes(_toBinary(_in));
...@@ -112,7 +126,7 @@ library Lib_RLPWriter { ...@@ -112,7 +126,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes a bool. * RLP encodes a bool.
* @param _in The bool to encode. * @param _in The bool to encode.
* @return _out The RLP encoded bool in bytes. * @return The RLP encoded bool in bytes.
*/ */
function writeBool( function writeBool(
bool _in bool _in
...@@ -120,7 +134,7 @@ library Lib_RLPWriter { ...@@ -120,7 +134,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
bytes memory encoded = new bytes(1); bytes memory encoded = new bytes(1);
...@@ -137,7 +151,7 @@ library Lib_RLPWriter { ...@@ -137,7 +151,7 @@ library Lib_RLPWriter {
* Encode the first byte, followed by the `len` in binary form if `length` is more than 55. * 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 _len The length of the string or the payload.
* @param _offset 128 if item is string, 192 if item is list. * @param _offset 128 if item is string, 192 if item is list.
* @return _encoded RLP encoded bytes. * @return RLP encoded bytes.
*/ */
function _writeLength( function _writeLength(
uint256 _len, uint256 _len,
...@@ -146,7 +160,7 @@ library Lib_RLPWriter { ...@@ -146,7 +160,7 @@ library Lib_RLPWriter {
private private
pure pure
returns ( returns (
bytes memory _encoded bytes memory
) )
{ {
bytes memory encoded; bytes memory encoded;
...@@ -176,7 +190,7 @@ library Lib_RLPWriter { ...@@ -176,7 +190,7 @@ library Lib_RLPWriter {
* Encode integer in big endian binary form with no leading zeroes. * Encode integer in big endian binary form with no leading zeroes.
* @notice TODO: This should be optimized with assembly to save gas costs. * @notice TODO: This should be optimized with assembly to save gas costs.
* @param _x The integer to encode. * @param _x The integer to encode.
* @return _binary RLP encoded bytes. * @return RLP encoded bytes.
*/ */
function _toBinary( function _toBinary(
uint256 _x uint256 _x
...@@ -184,7 +198,7 @@ library Lib_RLPWriter { ...@@ -184,7 +198,7 @@ library Lib_RLPWriter {
private private
pure pure
returns ( returns (
bytes memory _binary bytes memory
) )
{ {
bytes memory b = abi.encodePacked(_x); bytes memory b = abi.encodePacked(_x);
...@@ -243,7 +257,7 @@ library Lib_RLPWriter { ...@@ -243,7 +257,7 @@ library Lib_RLPWriter {
* Flattens a list of byte strings into one byte string. * Flattens a list of byte strings into one byte string.
* @notice From: https://github.com/sammayo/solidity-rlp-encoder/blob/master/RLPEncode.sol. * @notice From: https://github.com/sammayo/solidity-rlp-encoder/blob/master/RLPEncode.sol.
* @param _list List of byte strings to flatten. * @param _list List of byte strings to flatten.
* @return _flattened The flattened byte string. * @return The flattened byte string.
*/ */
function _flatten( function _flatten(
bytes[] memory _list bytes[] memory _list
...@@ -251,7 +265,7 @@ library Lib_RLPWriter { ...@@ -251,7 +265,7 @@ library Lib_RLPWriter {
private private
pure pure
returns ( returns (
bytes memory _flattened bytes memory
) )
{ {
if (_list.length == 0) { if (_list.length == 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment