Skip to main content

Try it Live

Run RLP examples in the interactive playground

RLP Utilities

Helper methods for creating, measuring, flattening, and comparing RLP data structures.

Overview

Utility functions provide convenient ways to work with RLP data:
  • from - Create RLP data from various inputs
  • getEncodedLength - Calculate encoded size without encoding
  • flatten - Extract all bytes from nested structures
  • equals - Deep equality comparison

from

Create RLP data structure from various inputs with automatic type detection.

    Behavior

    The from method normalizes various input types into RLP data structures:
    1. Uint8Array → Creates bytes data
    2. BrandedRlp → Returns unchanged (idempotent)
    3. Array → Creates list data (doesn’t recursively convert items)
    Note: Arrays are not recursively converted:

    Use Cases

    Normalizing Input:
    Building Structures:

    getEncodedLength

    Calculate the byte length of RLP-encoded data without actually encoding it.

      Algorithm

      Calculates encoded size using RLP rules without allocating buffers: For Uint8Array:
      For Arrays (lists):

      Performance Benefits

      Measuring without encoding saves allocation and copying:
      Use Cases: Pre-sizing Buffers:
      Size Validation:
      Choosing Encoding Strategy:

      flatten

      Recursively extract all bytes data from nested lists (depth-first traversal).

        Algorithm

        Depth-first traversal that collects all bytes data:

        Use Cases

        Extract Transaction Fields:
        Count Total Bytes:
        Validate Structure:
        Map Over Bytes:

        equals

        Deep equality comparison for RLP data structures.

          Algorithm

          Recursive comparison with type checking:

          Use Cases

          Deduplication:
          Testing:
          Caching:
          Assertions:

          Utility Combinations

          Combine utilities for powerful operations: Measure Flattened Size:
          Compare After Normalization:
          Validate Then Flatten: