Pure-PHP RLP (Recursive Length Prefix) encoder and decoder — Ethereum's canonical serialization for transactions, blocks and the state trie.
amashukov/rlp-php is a pure-PHP implementation of RLP (Recursive Length Prefix), the canonical serialization format used throughout Ethereum and the EVM for transactions, blocks and the Merkle-Patricia state trie. It encodes and decodes nested byte-string structures and minimal big-endian integers, and enforces strict RLP canonicality on decode to defend against malleable encodings.
The package is a leaf primitive — zero composer dependencies, just ext-gmp for big-integer encoding helpers.
ext-gmp only.@PER-CS formatted, CI-tested.composer require amashukov/rlp-php
use Amashukov\Rlp\Rlp;
// Empty string.
Rlp::encode(''); // 0x80
// Single byte < 0x80 is its own encoding.
Rlp::encode("\x7f"); // 0x7f
// Short string (≤ 55 bytes).
Rlp::encode('dog'); // 0x83 'd' 'o' 'g'
// Long string (> 55 bytes).
Rlp::encode(str_repeat('a', 56)); // 0xb8 0x38 'a' × 56
// Empty list.
Rlp::encode([]); // 0xc0
// Flat list of byte-strings.
Rlp::encode(['cat', 'dog']); // 0xc8 0x83 'cat' 0x83 'dog'
// Nested list.
Rlp::encode([[], [[]], [[], [[]]]]); // 0xc7 0xc0 0xc1 0xc0 0xc3 0xc0 0xc1 0xc0
Rlp::encodeInt(0); // 0x80 (empty bytes)
Rlp::encodeInt(1); // 0x01
Rlp::encodeInt(127); // 0x7f
Rlp::encodeInt(128); // 0x81 0x80
Rlp::encodeInt(1024); // 0x82 0x04 0x00
Rlp::encodeInt('1000000000000000000'); // big-int via decimal string (1 ETH in wei)
Rlp::decode("\x80"); // ''
Rlp::decode("\x83dog"); // 'dog'
Rlp::decode("\xc8\x83cat\x83dog"); // ['cat', 'dog']
Rlp::decode("\xc7\xc0\xc1\xc0\xc3\xc0\xc1\xc0"); // [[], [[]], [[], [[]]]]
Leaves are always returned as raw byte strings; integers are the caller's interpretation step.
For parsing concatenated RLP items (e.g. a stream of transactions), use decodeStream:
$stream = Rlp::encode('first') . Rlp::encode('second');
[$first, $consumed] = Rlp::decodeStream($stream, 0);
[$second, $consumed2] = Rlp::decodeStream($stream, $consumed);
// $first = 'first', $second = 'second'
decode() enforces RLP canonicality. The following inputs are rejected with InvalidArgumentException:
0x81 0x00).0xb8… prefix).0xf8… prefix).0xb9 0x00 0x38 …).These checks defend downstream consumers against malleable transaction encodings and trie tampering.
ext-gmpNo composer dependencies.
Part of a modular pure-PHP blockchain toolkit:
| Package | Purpose |
|---|---|
| amashukov/keccak-php | Keccak-256 / SHA-3 / SHAKE hashing |
| amashukov/secp256k1-php | secp256k1 ECDSA sign / verify / recover |
| amashukov/rlp-php | Ethereum RLP encode / decode |
| amashukov/ton-cell-php | TON TLB Cell / Builder / Slice / BOC |
| amashukov/eip1559-tx-signer-php | EIP-1559 transaction signer |
| amashukov/abi-encoder-php | Ethereum ABI encoder |
| amashukov/eth-rpc-client-php | Ethereum JSON-RPC client |
| amashukov/eth-php | EVM umbrella package |
@PER-CS ruleset.MIT License.
How can I help you explore Laravel packages today?