Try it Live
Run RLP examples in the interactive playground
Decoding Algorithm
RLP decoder uses the first byte (prefix) to determine data type and length:Prefix Ranges
Single Byte (0x00-0x7f)
Bytes with value < 0x80 encode as themselves:Short String (0x80-0xb7)
Length encoded in prefix:length = prefix - 0x80
Long String (0xb8-0xbf)
Length-of-length encoding:lengthOfLength = prefix - 0xb7
Short List (0xc0-0xf7)
Total payload length in prefix:length = prefix - 0xc0
Long List (0xf8-0xff)
Length-of-length encoding:lengthOfLength = prefix - 0xf7
Usage Patterns
Extract Transaction Data
Decode transaction bytes and extract fields:Stream Decoding
Decode multiple RLP values from a stream:Validate and Extract
Decode with validation and type checking:Recursive Flattening
Flatten nested lists to extract all byte values:Canonical Validation
RLP decoder enforces canonical encoding rules to prevent malleability:Non-canonical Single Byte
Single bytes < 0x80 must not have a length prefix:Non-canonical Short Form
Strings < 56 bytes must use short form:Leading Zeros
Length encodings must not have leading zeros:Length Mismatches
Declared length must match actual data:Error Handling
Comprehensive error handling for malformed input:Error Types Reference
| Error | Cause | Fix |
|---|---|---|
InputTooShort | Not enough bytes for declared length | Provide complete data |
InvalidRemainder | Extra bytes after value (non-stream) | Use stream=true or trim input |
NonCanonicalSize | Non-minimal length encoding | Use canonical encoding |
LeadingZeros | Length has leading zero bytes | Remove leading zeros from length |
InvalidLength | List payload length mismatch | Fix list item encodings |
RecursionDepthExceeded | Nested > 32 levels deep | Reduce nesting depth |
UnexpectedInput | Invalid prefix or format | Check input is valid RLP |
Performance
Depth Limiting
Maximum recursion depth is 32 to prevent stack overflow:Stream Mode Efficiency
Use stream mode to avoid re-parsing when decoding multiple values:Round-trip Encoding
Decode and re-encode produces identical bytes (for canonical input):See Also
- encode - Encode data to RLP format
- decodeArray - Decode with schema
- decodeObject - Decode to object
- validate - Validate RLP structure
- Algorithm - RLP specification

