X402.RLP (X402 v0.6.0)

Copy Markdown View Source

Minimal pure RLP (Recursive Length Prefix) encoding.

Provides exactly the encoding surface X402.Transaction needs to build EIP-1559 typed transactions: binaries, non-negative integers (encoded as their minimal big-endian byte representation, so 0 encodes as the empty string), and nested lists thereof. There is no decoder — the library never needs to parse RLP, and settlement tests decode raw transactions with their own reference decoder to prove the encoding.

Examples

iex> X402.RLP.encode("dog")
<<0x83, "dog">>

iex> X402.RLP.encode(["cat", "dog"])
<<0xC8, 0x83, "cat", 0x83, "dog">>

iex> X402.RLP.encode(0)
<<0x80>>

iex> X402.RLP.encode(1024)
<<0x82, 0x04, 0x00>>

Summary

Types

An RLP-encodable item: a binary, a non-negative integer, or a list of items.

Functions

RLP-encodes a binary, a non-negative integer, or a (nested) list of items.

Types

item()

@type item() :: binary() | non_neg_integer() | [item()]

An RLP-encodable item: a binary, a non-negative integer, or a list of items.

Functions

encode(item)

(since 0.6.0)
@spec encode(item()) :: binary()

RLP-encodes a binary, a non-negative integer, or a (nested) list of items.

Integers are encoded as their minimal big-endian byte representation per the Ethereum convention (0 becomes the empty byte string, so leading zero bytes never appear).

Examples

iex> X402.RLP.encode("")
<<0x80>>

iex> X402.RLP.encode(<<0x7F>>)
<<0x7F>>

iex> X402.RLP.encode(15)
<<0x0F>>

iex> X402.RLP.encode([])
<<0xC0>>

iex> X402.RLP.encode([[], [[]], [[], [[]]]])
<<0xC7, 0xC0, 0xC1, 0xC0, 0xC3, 0xC0, 0xC1, 0xC0>>