Try it Live
Run RLP examples in the interactive playground
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
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
Usage Patterns
Transaction Encoding
Ethereum transactions are RLP-encoded lists:Block Header Encoding
Block headers are RLP-encoded lists:Nested Data Structures
RLP handles arbitrary nesting:Merkle Proof
Encode Merkle proof as list of hashes:Performance
When to Use encodeList
UseencodeList instead of generic encode when:
- Known type - You know the input is a list
- Type-specific - Better tree-shaking
- Performance - Skips type dispatch overhead
Pre-sizing Buffers
Calculate total size before encoding:Avoiding Re-encoding
Cache encoded results when encoding the same data multiple times:See Also
- encode - Universal encoder
- encodeBytes - Encode byte string
- encodeArray - Encode with schema
- decode - Decode RLP bytes
- Algorithm - RLP specification

