Currently support for solidity v0.5.0
Please raise issues for bugs, and solidity updates. I will be monitoring the solidity changelogs and updating this package accordingly
npm install solidity-rlpin the project directory. Make sure to install through npm for prompt updates!import "solidity-rlp/contracts/RLPReader.sol"in the desired smart contract.
See the example smart contract below
The reader contract provides an interface to first take RLP encoded bytes and convert them into
an internal data structure, RLPItem through the function, toRlpItem(bytes). This data structure can then be
destructured into the desired data types.
Transformations(All take an RLPItem as an arg):
isList(RLPItem) bool: inidicator if the encoded data is a listtoList(RLPItem) RLPItem[]: returns a list of RLPItemstoBytes(RLPItem) bytes: returns the payload in bytestoAddress(RLPItem) address: returns the encoded address. Must be exactly 20 bytes.toUint(RLPItem) uint: returns the encoded uint. Enforced data is capped to 32 bytes.toUintStrict(RLPItem) uint: returns the encoded uint. Encoded data must be padded to 32 bytes.toBoolean(RLPItem) bool: returns the encoded booleantoRlpBytes(RLPItem) bytes: returns the raw rlp encoded byte formrlpLen(RLPItem) uint: returns the byte length of the rlp itempayloadLen(RLPItem) uint: returns the byte length of the data payload
Note: The reader contract only provides only these conversion functions. All other solidity data types can be derived from
this base. For example, a bytes32 encoded data type is equivalent to bytes32(toUint(RLPItem)). Start with a uint and convert from there.
A string can be retrieved by string(toBytes(RLPItem)). See example for a sample smart contract.
import "solidity-rlp/contracts/RLPReader.sol"
contract SomeContract {
// optional way to attach library functions to these data types.
using RLPReader for RLPReader.RLPItem;
using RLPReader for bytes;
// lets assume that rlpBytes is an encoding of [[1, "nested"], 2, 0x<Address>]
function someFunctionThatTakesAnEncodedItem(bytes memory rlpBytes) public {
RLPReader.RLPItem[] memory ls = rlpBytes.toRlpItem().toList(); // must convert to an rlpItem first!
RLPReader.RLPItem memory item = ls[0] // the encoding of [1, "nested"].
item.toList()[0].toUint() // 1
string(item.toList()[1].toBytes) // "nested"
ls[1].toUint() // 2
ls[2].toAddress() // 0x<Address>
}
}git clone https://github.com/hamdiallam/solidity-rlp && cd solidity-rlpnpm installnpm install -g truffle ganache-cliinstalled globally for the dev envirnomentganache-clirun in a background process or seperate terminal window.truffle compile && truffle test