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

update rlp reader/writer

parent 27bf62a6
......@@ -23,7 +23,7 @@ library Lib_RLPReader {
LIST_ITEM
}
/***********
* Structs *
***********/
......@@ -32,12 +32,12 @@ library Lib_RLPReader {
uint256 length;
uint256 ptr;
}
/**********************
* Internal Functions *
**********************/
/**
* Converts bytes to a reference to memory position and length.
* @param _in Input bytes to convert.
......@@ -353,6 +353,11 @@ library Lib_RLPReader {
out := byte(0, mload(ptr))
}
require(
out == 0 || out == 1,
"Lib_RLPReader: Invalid RLP boolean value, must be 0 or 1"
);
return out != 0;
}
......@@ -479,7 +484,7 @@ library Lib_RLPReader {
// Short string.
uint256 strLen = prefix - 0x80;
require(
_in.length > strLen,
"Invalid RLP short string."
......
......@@ -2,9 +2,6 @@
pragma solidity >0.5.0 <0.8.0;
pragma experimental ABIEncoderV2;
/* Library Imports */
import { Lib_BytesUtils } from "./Lib_BytesUtils.sol";
/**
* @title Lib_RLPWriter
* @author Bakaoh (with modifications)
......@@ -18,7 +15,7 @@ library Lib_RLPWriter {
/**
* RLP encodes a byte string.
* @param _in The byte string to encode.
* @return _out The RLP encoded string in bytes.
* @return The RLP encoded string in bytes.
*/
function writeBytes(
bytes memory _in
......@@ -26,7 +23,7 @@ library Lib_RLPWriter {
internal
pure
returns (
bytes memory _out
bytes memory
)
{
bytes memory encoded;
......@@ -34,7 +31,7 @@ library Lib_RLPWriter {
if (_in.length == 1 && uint8(_in[0]) < 128) {
encoded = _in;
} else {
encoded = Lib_BytesUtils.concat(_writeLength(_in.length, 128), _in);
encoded = abi.encodePacked(_writeLength(_in.length, 128), _in);
}
return encoded;
......@@ -43,7 +40,7 @@ library Lib_RLPWriter {
/**
* RLP encodes a list of RLP encoded byte 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(
bytes[] memory _in
......@@ -51,17 +48,17 @@ library Lib_RLPWriter {
internal
pure
returns (
bytes memory _out
bytes memory
)
{
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.
* @param _in The string to encode.
* @return _out The RLP encoded string in bytes.
* @return The RLP encoded string in bytes.
*/
function writeString(
string memory _in
......@@ -69,7 +66,7 @@ library Lib_RLPWriter {
internal
pure
returns (
bytes memory _out
bytes memory
)
{
return writeBytes(bytes(_in));
......@@ -78,10 +75,27 @@ library Lib_RLPWriter {
/**
* RLP encodes an address.
* @param _in The address to encode.
* @return _out The RLP encoded address in bytes.
* @return The RLP encoded address in bytes.
*/
function writeAddress(
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
pure
......@@ -95,7 +109,7 @@ library Lib_RLPWriter {
/**
* RLP encodes a uint.
* @param _in The uint256 to encode.
* @return _out The RLP encoded uint256 in bytes.
* @return The RLP encoded uint256 in bytes.
*/
function writeUint(
uint256 _in
......@@ -103,7 +117,7 @@ library Lib_RLPWriter {
internal
pure
returns (
bytes memory _out
bytes memory
)
{
return writeBytes(_toBinary(_in));
......@@ -112,7 +126,7 @@ library Lib_RLPWriter {
/**
* RLP encodes a bool.
* @param _in The bool to encode.
* @return _out The RLP encoded bool in bytes.
* @return The RLP encoded bool in bytes.
*/
function writeBool(
bool _in
......@@ -120,7 +134,7 @@ library Lib_RLPWriter {
internal
pure
returns (
bytes memory _out
bytes memory
)
{
bytes memory encoded = new bytes(1);
......@@ -137,7 +151,7 @@ library Lib_RLPWriter {
* 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.
* @return _encoded RLP encoded bytes.
* @return RLP encoded bytes.
*/
function _writeLength(
uint256 _len,
......@@ -146,7 +160,7 @@ library Lib_RLPWriter {
private
pure
returns (
bytes memory _encoded
bytes memory
)
{
bytes memory encoded;
......@@ -176,7 +190,7 @@ library Lib_RLPWriter {
* 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.
* @return _binary RLP encoded bytes.
* @return RLP encoded bytes.
*/
function _toBinary(
uint256 _x
......@@ -184,7 +198,7 @@ library Lib_RLPWriter {
private
pure
returns (
bytes memory _binary
bytes memory
)
{
bytes memory b = abi.encodePacked(_x);
......@@ -243,7 +257,7 @@ library Lib_RLPWriter {
* 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.
* @return _flattened The flattened byte string.
* @return The flattened byte string.
*/
function _flatten(
bytes[] memory _list
......@@ -251,7 +265,7 @@ library Lib_RLPWriter {
private
pure
returns (
bytes memory _flattened
bytes memory
)
{
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