eth-rlp

2.2.0 · active · verified Thu Apr 09

eth-rlp provides RLP (Recursive Length Prefix) serialization definitions for common Ethereum objects in Python. It's used to encode arbitrarily nested arrays of binary data into a space-efficient format, crucial for data transfer between Ethereum nodes and object serialization within the Ethereum execution layer. The library is currently at version 2.2.0 and receives updates to align with Ethereum protocol specifications.

Warnings

Install

Imports

Quickstart

This example demonstrates defining an RLP schema using a dataclass, then encoding a Python object into RLP bytes and decoding it back. It utilizes `ethereum_types.numeric.Uint` for integer types.

from dataclasses import dataclass
from ethereum_rlp import encode, decode_to
from ethereum_types.numeric import Uint
from typing import List

@dataclass
class Stuff:
    toggle: bool
    number: Uint
    sequence: List["Stuff"]

# Encode an object
original_stuff = Stuff(toggle=True, number=Uint(3), sequence=[])
encoded_data = encode(original_stuff)
print(f"Encoded: {encoded_data.hex()}")

# Decode back to the object type
decoded_stuff = decode_to(Stuff, encoded_data)
print(f"Decoded number: {decoded_stuff.number}")
assert decoded_stuff.number == Uint(3)

view raw JSON →