Skip to main content

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 with 0xc0 + 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]
    Lists can contain other lists recursively. Each nested list is encoded using the same rules, then the encoded bytes become part of the parent list’s payload.

    Algorithm

    The encodeList implementation:
    1. Encode each item using encode() (dispatches to appropriate encoder)
    2. Calculate total length by summing encoded item lengths
    3. Choose encoding based on total length:
      • < 56 bytes: Short form with single prefix byte
      • >= 56 bytes: Long form with length-of-length encoding
    4. 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

    Use encodeList instead of generic encode when:
    1. Known type - You know the input is a list
    2. Type-specific - Better tree-shaking
    3. 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:
    List encoding is allocation-heavy for large lists. Consider using WASM implementation for performance-critical operations.

    See Also