RLP (Recursive Length Prefix)

4.1.0 · active · verified Thu Apr 09

RLP (Recursive Length Prefix) is a serialization format used extensively in Ethereum to encode arbitrarily nested arrays of binary data. The `rlp` Python library provides efficient functions for encoding Python objects (byte strings, integers, lists/tuples of these) into RLP byte strings and decoding them back. It is actively maintained by the Ethereum project. The current version is 4.1.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic RLP encoding and decoding of byte strings, integers, and nested lists. It highlights how the library handles different data types automatically. Note that all string inputs must be byte strings (`b'...'`).

import rlp

# Encode a simple byte string
encoded_string = rlp.encode(b'ethereum')
print(f"Encoded 'ethereum': {encoded_string}")

# Decode it back
decoded_string = rlp.decode(encoded_string)
print(f"Decoded back: {decoded_string}")

# Encode a list of byte strings and integers
data = [b'dog', b'cat', b'elephant', 0, 15, 12345]
encoded_list = rlp.encode(data)
print(f"\nEncoded list: {encoded_list}")

# Decode the list
decoded_list = rlp.decode(encoded_list)
print(f"Decoded list: {decoded_list}")

# Encoding nested lists
nested_data = [b'a', [b'b', b'c'], [b'd', [b'e', b'f']]]
encoded_nested = rlp.encode(nested_data)
print(f"\nEncoded nested: {encoded_nested}")

decoded_nested = rlp.decode(encoded_nested)
print(f"Decoded nested: {decoded_nested}")

view raw JSON →