Recursive Length Prefix (RLP)
Overview
The Recursive Length Prefix algorithm is a serialization method used extensively in the Ethereum protocol. RLP is a standard that aims to define a space-efficient approach to packaging and transferring arbitrary data.
RLP is commonly used for use cases on Ethereum Applications such as:
- serializing Transaction Envelopes to be submitted to the network,
- serializing EIP-7702 Authorization tuples to be signed over,
- serializing components to derive a
CREATE
Contract Address.
Examples
Encoding
We can serialize arbitrary data into RLP-encoded format using the Rlp.fromHex
or Rlp.fromBytes
functions.
import { Hex, Rlp } from 'ox'
const rlp = Rlp.fromHex([
Hex.fromString('hello'),
Hex.fromNumber(1337),
[Hex.fromString('foo'), Hex.fromString('bar')],
])
0xd28568656c6c6f820539c883666f6f83626172
Decoding
We can deserialize RLP-encoded data into its original format using the Rlp.toHex
or Rlp.toBytes
functions.
import { Hex, Rlp } from 'ox'
const rlp = Rlp.fromHex([
Hex.fromString('hello'),
Hex.fromNumber(1337),
[Hex.fromString('foo'), Hex.fromString('bar')],
])
const values = Rlp.toHex(rlp)
[Hex.fromString('hello'), Hex.fromNumber(1337), [Hex.fromString('foo'), Hex.fromString('bar')]]
Related Modules
Module | Description |
---|---|
Rlp | Utility functions for RLP encoding & decoding. |