For a single byte with value less than 0x80 (128), the byte encodes as itself with no prefix:
Copy
Ask AI
import { Rlp } from 'tevm'// Byte value 0x7f (127)const input = new Uint8Array([0x7f])const encoded = Rlp.encodeBytes(input)// => Uint8Array([0x7f])// Byte value 0x00const zero = new Uint8Array([0x00])const encoded = Rlp.encodeBytes(zero)// => Uint8Array([0x00])// Note: Single byte >= 0x80 still needs prefixconst high = new Uint8Array([0x80])const encoded = Rlp.encodeBytes(high)// => Uint8Array([0x81, 0x80])
The threshold of 56 bytes is chosen because lengths 0-55 fit in a single prefix byte (0x80-0xb7). Starting at 56, we need an additional byte to encode the length.
Use encodeBytes instead of generic encode when you know the input is bytes (not a list), to skip type dispatch overhead.
Copy
Ask AI
import { Rlp } from 'tevm'// Direct encodingconst encoded = Rlp.encodeBytes(bytes)// vs generic with dispatch overheadconst encoded = Rlp.encode(bytes)
import { Rlp } from 'tevm'const items = [ new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6]), new Uint8Array([7, 8, 9])]// Encode all at once as listconst encoded = Rlp.encode(items)// vs encoding individually (less efficient)const encoded = items.map(item => Rlp.encodeBytes(item))