---
title: "RLP"
description: "Ethereum's Recursive Length Prefix. Bytes and nested lists in and out and decoded as strictly as geth does it"
canonical_url: "https://encodings.agntn.dev/guide/rlp"
---
# RLP

> Ethereum's Recursive Length Prefix. Bytes and nested lists in and out and decoded as strictly as geth does it

## Bytes and lists, nothing else

RLP isn't text. It's how Ethereum writes a transaction, a block header, a trie node. Every value is bytes or a list of values, and each one carries its length up front. That's the whole format. No types, no field names, no mercy.

```ts
import { hex, rlp } from "@agntn/encodings";

hex.encode(rlp.encode([new TextEncoder().encode("dog"), 1000n, []]));
// "c883646f678203e8c0"
rlp.decode(hex.decode("c88363617483646f67"));
// [Uint8Array [0x63, 0x61, 0x74], Uint8Array [0x64, 0x6f, 0x67]]
```

`encode` takes a `Uint8Array`, a `bigint` or a list of those, nested as deep as you like. A `bigint` goes big-endian with no leading zero, so `0n` is no bytes at all and comes out as `80`. `decode` hands back bytes and arrays. Was `03e8` a number or two bytes of text? RLP never says, so that call is yours.

## Why does decode refuse my bytes?

Because one value gets exactly one encoding. geth holds every node to that, and so does `decode`:

- a length written with a leading zero byte,
- a long length header for a payload under 56 bytes,
- one byte under `0x80` wrapped in a header it doesn't need,
- an item that runs past the end of its list,
- anything left over after the item.

```ts
rlp.decode(hex.decode("8100"));
// DecodeError: rlp: header at index 0 wraps a single byte under 0x80
```

Each refusal is a `DecodeError` with the byte `index`. Lenient sounds friendlier. It isn't. A lenient decoder would happily give you a digest for a transaction no node accepts.

## A signed transaction, taken apart

Here's a real type 2 transfer from mainnet. Drop the `0x02` type byte and decode the rest. Twelve fields come out, and the last three are the signature. Encode the first nine again behind `0x02`, hash it, and you hold the digest the sender signed.

```ts
import { keccak256 } from "@agntn/hashes/keccak";
import { hex, rlp, type RlpItem } from "@agntn/encodings";

const raw = hex.decode(
  "02f87301068405f5e100850299e45aba82520894f755aaace22fcfdcd4e300a3f26249000d95f1b28803b4860307b3664f80c080a03945159237a25c17a158194a90cf98a8cac028616751fc9b318a9aa2cdfd2beea05195431bd190114cb0562ca747c5784b0833369520916ee35ba8d2a78147d662",
);
const fields = rlp.decode(raw.subarray(1)) as RlpItem[];
hex.encode(keccak256(Uint8Array.of(2, ...rlp.encode(fields.slice(0, 9)))));
// "9b5cfa17969993d9a389747871fd5a80e82d829b276fa8245492806182b84ea6"
```

Feed that digest and the signature to `keys_message_recover` from [@agntn/keys](https://github.com/agntn/keys) and out comes the sender's public key. It was sitting in the transaction all along.

Deeply nested input is fine too. Neither direction recurses, so a list a hundred thousand levels deep costs memory, not your call stack.

## Where it lives

RLP gives back lists, and every encoding in the registry gives back bytes. So `rlp` sits outside it, like the [code pages](https://encodings.agntn.dev/guide/code-pages), and `identify` never guesses it. The subpath is `@agntn/encodings/rlp`. No CLI command and no agent tool yet, just the two functions.


## Sitemap

See the full [sitemap](https://encodings.agntn.dev/sitemap.md) for all pages.
