Try it Live
Run RLP examples in the interactive playground
RLP Encoding
Methods for encoding bytes, lists, and nested structures into RLP format.Overview
RLP encoding converts arbitrary nested data structures into a compact byte representation. The encoder handles three main cases:- Bytes - Raw byte arrays (strings in RLP terminology)
- Lists - Arrays of encodable items
- Nested - Lists containing other lists
RLP Encoding Tree Diagram
RLP encodes nested structures hierarchically:RLP Encoding Algorithm
-
Determine Data Type
- Is input a single byte < 0x80? Encode as-is
- Is input bytes? Encode with length prefix
- Is input array? Encode each element recursively
-
Encode Bytes
- If length = 1 and byte < 0x80: no prefix
- If length ≤ 55: prefix = 0x80 + length
- If length > 55: prefix = 0xb7 + (length of length) + length
-
Encode Lists
- Recursively encode each element
- Concatenate all encoded elements
- Calculate total payload length
- If length ≤ 55: prefix = 0xc0 + length
- If length > 55: prefix = 0xf7 + (length of length) + length
-
Return RLP Bytes
- Prefix + (length if needed) + data
encode
General-purpose encoding method that accepts bytes, lists, or RLP data structures.Signature
data: Encodable- Data to encode (Uint8Array, RlpData, or array)
Uint8Array- RLP-encoded bytes
Error('UnexpectedInput')- Invalid encodable data type
Usage
Algorithm
Theencode method dispatches to specialized encoders based on input type:
- Uint8Array → Uses
encodeBytesfor string encoding - BrandedRlp (bytes) → Uses
encodeByteson value - BrandedRlp (list) → Uses
encodeListon value - Array → Uses
encodeListfor list encoding
encodeBytes
Encodes a byte array according to RLP string rules.Signature
bytes: Uint8Array- Byte array to encode
Uint8Array- RLP-encoded bytes
Usage
String Encoding Rules
RLP string encoding has three cases based on byte length:1. Single Byte < 0x80
For a single byte with value less than 0x80 (128), the byte encodes as itself with no prefix:2. Short String (0-55 bytes)
For strings of 0-55 bytes, prefix with0x80 + length:
3. Long String (56+ bytes)
For strings of 56+ bytes, use long form:[0xb7 + length_of_length, ...length_bytes, ...bytes]
encodeList
Encodes a list of RLP-encodable items.Signature
items: Encodable[]- Array of items to encode
Uint8Array- RLP-encoded list
Usage
List Encoding Rules
RLP list encoding has two cases based on total payload length:1. Short List (< 56 bytes total)
For lists with total payload < 56 bytes, prefix with0xc0 + total_length:
2. Long List (56+ bytes total)
For lists with total payload >= 56 bytes, use long form:[0xf7 + length_of_length, ...length_bytes, ...encoded_items]
Algorithm Details
TheencodeList implementation:
- Encode each item using
encode()(dispatches to appropriate encoder) - Calculate total length by summing encoded item lengths
- Choose encoding based on total length:
- < 56 bytes: Short form with single prefix byte
- >= 56 bytes: Long form with length-of-length encoding
- Concatenate prefix + encoded items into result buffer
Encoding Examples
Transaction Encoding
Ethereum transactions use RLP encoding for signing and broadcasting:Block Header Encoding
Block headers are RLP-encoded lists:Nested Data Structures
RLP handles arbitrary nesting:Performance Considerations
Pre-sizing Buffers
For better performance when encoding many items, pre-calculate total size:Performance
Use specific encoders when type is known for better performance:Avoiding Re-encoding
Cache encoded results when encoding the same data multiple times:Related
- Decoding - Decode RLP-encoded bytes
- Algorithm - RLP specification details
- WASM - High-performance WASM encoder
- Usage Patterns - Real-world examples

