Try it Live
Run RLP examples in the interactive playground
RLP Decoding
Decode RLP-encoded bytes back to data structures with comprehensive validation and error handling.Overview
RLP decoding parses compact byte representations back into nested data structures. The decoder performs comprehensive validation to ensure canonical encoding and prevent malformed input attacks. Key Features:- Canonical validation - Rejects non-minimal encodings
- Depth limiting - Prevents stack overflow on deeply nested data
- Stream support - Decode multiple values from a byte stream
- Detailed errors - Clear error messages for debugging
decode
Decodes RLP-encoded bytes into data structures.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
Decoding Patterns
Extract Transaction Data
Decode transaction bytes and extract fields:Recursive Flattening
Flatten nested lists to extract all byte values:Stream Decoding
Decode multiple RLP values from a stream:Validate and Extract
Decode with validation and type checking: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 Considerations
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:Validation Overhead
Canonical validation adds minimal overhead but catches malformed inputs:Round-trip Encoding
Decode and re-encode produces identical bytes (for canonical input):Related
- Encoding - Encode data to RLP format
- Types - RLP data type system
- Algorithm - Specification details
- Usage Patterns - Real-world examples

