Skip to main content

Try it Live

Run RLP examples in the interactive playground

    Encodable Type

    Encodable is a union type accepting any input that can be encoded to RLP:

    Accepted Types

    Uint8Array - Raw byte arrays (encoded as RLP strings):
    BrandedRlp - RLP data structures:
    Arrays - Encoded as RLP lists:
    Nested Arrays - Arbitrary nesting supported:

    Usage Patterns

    Transaction Encoding

    Ethereum transactions use RLP encoding for signing and broadcasting:

    Block Encoding

    Block headers are RLP-encoded lists:

    Custom Schema

    Define typed structures with RLP encoding:

    Encoding Rules

    RLP encoding uses the first byte (prefix) to indicate data type and length:

    Single Byte (0x00-0x7f)

    Bytes with value < 0x80 encode as themselves:

    Short String (0-55 bytes)

    For byte arrays 0-55 bytes, prefix with 0x80 + length:

    Long String (56+ bytes)

    For byte arrays 56+ bytes, use long form: [0xb7 + length_of_length, ...length_bytes, ...bytes]

    Short List (< 56 bytes total)

    For lists with total payload < 56 bytes, prefix with 0xc0 + total_length:

    Long List (56+ bytes total)

    For lists with total payload >= 56 bytes, use long form: [0xf7 + length_of_length, ...length_bytes, ...encoded_items]

    Algorithm

    The encode method dispatches to specialized encoders:
    1. Uint8Array → Uses byte string encoding
    2. BrandedRlp (bytes) → Encodes as byte string
    3. BrandedRlp (list) → Encodes as list
    4. Array → Recursively encodes each element as list
    Use encode for general-purpose encoding. Use encodeBytes or encodeList when you know the specific type for slightly better performance.

    Performance

    Pre-sizing Buffers

    Calculate size before encoding for better performance:

    Tree-shaking

    Use specific encoders when type is known:

    Caching

    Cache encoded results when encoding the same data multiple times:
    Encoding is allocation-heavy for large data structures. Consider using WASM implementation for performance-critical operations.

    See Also