# @ganache/rlp

> Recursive Length Prefix Encoding Module

Latest version **0.9.2** (published 2023-12-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ganache/rlp
pnpm add @ganache/rlp
yarn add @ganache/rlp
bun add @ganache/rlp
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.9.2 |
| Published | 2023-12-21 |
| First published | 2021-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 49.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2627 |
| Author | David Murdoch |
| Maintainers | jeff.smale, truffle-cicd, davidmurdoch, micaiahreid |
| Keywords | ganache, ganache-rlp, ethereum, evm, blockchain, smart contracts, dapps, solidity, vyper, fe, web3, tooling, truffle |

## Links

- npm: https://www.npmjs.com/package/@ganache/rlp
- Repository: https://github.com/trufflesuite/ganache
- Homepage: https://github.com/trufflesuite/ganache/tree/develop/packages/rlp#readme
- Issues: https://github.com/trufflesuite/ganache/issues
- npm.io page: https://npm.io/package/@ganache/rlp

## Dependencies (2)

- [rlp](https://npm.io/package/rlp.md) 2.2.7
- [@ganache/utils](https://npm.io/package/@ganache/utils.md) 0.9.2

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 0.9.2 (latest) — 2023-12-21
- 0.5.3-alpha.0 (alpha) — 2022-09-23
- 0.1.1-rc.0 (rc) — 2022-01-12
- 0.1.1-beta.2 (beta) — 2021-12-20
- 0.1.1-canary.1342 (canary) — 2021-08-24
- 0.1.1-internal.0 (internal) — 2021-08-03
- 0.9.1 — 2023-08-22
- 0.9.0 — 2023-07-05
- 0.8.0 — 2023-04-13
- 0.7.1 — 2023-02-01
- 0.7.0 — 2022-12-15
- 0.6.0 — 2022-10-25
- 0.5.3 — 2022-10-05
- 0.5.2 — 2022-09-07
- 0.5.2-alpha.2 — 2022-09-06
- … 49 more at https://npm.io/package/@ganache/rlp/versions

## README

# `@ganache/rlp`

[Recursive Length](https://github.com/ethereum/wiki/wiki/RLP) Prefix Encoding
for Node.js.

Based on https://github.com/ethereumjs/rlp, with some additional optimizations
for encoding:

```typescript
function encodeRange<
  T extends EncodingInput | Readonly<EncodingInput>,
  Start extends RangeOf<T["length"]>
>(
  items: T,
  start: Start,
  length: Exclude<Remainders<T["length"], Start>, 0>
): { length: number; output: Buffer[] } {
  //...;
}
```

Begin RLP encoding of `items`, from `start` until `length`. Call `RLP.digest` to
finish encoding.

Returns an object containing the total `length` of all data in the encoded parts,
and `output`, an array of encoded Buffers.

```typescript
function digest(ranges: Readonly<Buffer[]>[], length: number) {
  // ...
}
```

Finishes encoding started by `encodeRange`.

Returns a Buffer of encoded data.

Additional changes:

- minor optimisations to `encode`
- `encode` only accepts buffers or nested buffer arrays, strings, number, bigint,
  and BN support have been removed.

---

These changes enable encoding data in chunks, avoiding processing the same
inputs multiple times in common scenarios withing Ganache. Example:

```typescript
type EthereumRawTx = [
  nonce: Buffer,
  gasPrice: Buffer,
  gas: Buffer,
  to: Buffer,
  value: Buffer,
  data: Buffer,
  v: Buffer,
  r: Buffer,
  s: Buffer
];
// encode the first 6 entries
const partialRlp = encodeRange(raw, 0, 6);
// encode the last 3 entires
const signature = encodeRange(raw, 6, 3);
// combine all entries
const serialized = digest(
  [partialRlp.output, signature.output],
  partialRlp.length + signature.length
);
return {
  // computing the `from` address requires entries 0-6
  from: computeFromAddress(partialRlp, v.toNumber(), raw, chainId),
  // hash requires entries 0-9
  hash: Data.from(keccak(serialized), 32),
  // serialized uses all entries for storage/transmission
  serialized
};
```

---
_Source: https://npm.io/package/@ganache/rlp · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
