npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

solidity-rlp

v2.0.8

Published

solidity rlp encoder/decoder

Downloads

939

Readme

RLP decoder/reader

Build Status Currently supports solidity v0.6.0

Please raise issues for bugs, and solidity updates. I will be monitoring the solidity changelogs and updating this package accordingly

Installation

  1. npm install solidity-rlp in the project directory. Make sure to install through npm for prompt updates!
  2. import "solidity-rlp/contracts/RLPReader.sol" in the desired smart contract.

See the example smart contract below

Usage

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):

  1. isList(RLPItem) bool : inidicator if the encoded data is a list
  2. toList(RLPItem) RLPItem[] : returns a list of RLPItems
  3. toBytes(RLPItem) bytes : returns the payload in bytes
  4. toAddress(RLPItem) address : returns the encoded address. Must be exactly 20 bytes.
  5. toUint(RLPItem) uint : returns the encoded uint. Enforced data is capped to 32 bytes.
  6. toUintStrict(RLPItem) uint : returns the encoded uint. Encoded data must be padded to 32 bytes.
  7. toBoolean(RLPItem) bool : returns the encoded boolean
  8. toRlpBytes(RLPItem) bytes : returns the raw rlp encoded byte form
  9. rlpLen(RLPItem) uint : returns the byte length of the rlp item
  10. payloadLocation(RLPItem) (uint memPtr, uint len) : returns the memory pointer and byte length of the data payload
  11. payloadLen(RLPItem) uint : returns the byte length of the data payload; an alias to payloadLocation(item)[1]

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.

Iteration functions:

  1. iterator(RLPItem) Iterator : returns an Iterator over the RLPItem. RLPItem must be an encoded list
  2. hasNext(Iterator) bool : indicator if there is another item to iterate on
  3. next(Iterator) RLPItem : returns the next RLPItem in the iterator

Utility functions:

  1. rlpBytesKeccak256(RLPItem) bytes32 : returns keccak256 hash of RLP encoded bytes. A cheap version of keccak256(toRlpBytes(RLPItem)) that avoids copying memory.
  2. payloadKeccak256(RLPItem) bytes32 : returns keccak256 hash of the item payload. A cheap version of keccak256(toBytes(RLPItem)) that avoids copying memory.

Example

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 RLPReader.Iterator;
    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>
    }

    // lets assume rlpBytes is an encoding of [["sublist"]]
    function someFunctionThatDemonstratesIterators(bytes memory rlpBytes) public {
        RLPReader.Iterator memory iter = rlpBytes.toRlpItem().iterator();
        RLPReader.Iterator memory subIter = iter.next().iterator();

        // iter.hasNext() == false
        // string(subIter.next().toBytes()) == "sublist"
        // subIter.hasNext() == false
    }
}

Tests

  1. git clone https://github.com/hamdiallam/solidity-rlp && cd solidity-rlp
  2. npm install
  3. npm install -g truffle ganache-cli installed globally for the dev envirnoment
  4. ganache-cli run in a background process or seperate terminal window.
  5. truffle compile && truffle test

Audits

2021-05: MixBytes

Version 2.0.5 of this library (commit a283779) was audited by MixBytes as part of the Lido stETH price oracle audit.

Summary:

  • Total Issues: 1 (1 Fixed)
  • Critical Issues: 0 (0 Fixed)
  • Major Issues: 0 (0 Fixed)
  • Warning Issues: 0 (0 No issue)
  • Comment Risk Issues: 1 (1 Fixed)

See the full report for details.