Skip to main content

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
All encoding operations are deterministic and canonical, ensuring the same input always produces identical output.

RLP Encoding Tree Diagram

RLP encodes nested structures hierarchically:

RLP Encoding Algorithm

encode

General-purpose encoding method that accepts bytes, lists, or RLP data structures.

Signature

Parameters:
  • data: Encodable - Data to encode (Uint8Array, RlpData, or array)
Returns:
  • Uint8Array - RLP-encoded bytes
Throws:
  • Error('UnexpectedInput') - Invalid encodable data type
Source: encode.js:38-59

Usage

Algorithm

The encode method dispatches to specialized encoders based on input type:
  1. Uint8Array → Uses encodeBytes for string encoding
  2. BrandedRlp (bytes) → Uses encodeBytes on value
  3. BrandedRlp (list) → Uses encodeList on value
  4. Array → Uses encodeList for list encoding
This automatic dispatch simplifies encoding of complex structures:

encodeBytes

Encodes a byte array according to RLP string rules.

Signature

Parameters:
  • bytes: Uint8Array - Byte array to encode
Returns:
  • Uint8Array - RLP-encoded bytes
Source: encodeBytes.js:32-53

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 with 0x80 + 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

Parameters:
  • items: Encodable[] - Array of items to encode
Returns:
  • Uint8Array - RLP-encoded list
Source: encodeList.js:33-63

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 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]

Algorithm Details

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

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: