1 | # rlp
|
2 |
|
3 | [![NPM Package][rlp-npm-badge]][rlp-npm-link]
|
4 | [![GitHub Issues][rlp-issues-badge]][rlp-issues-link]
|
5 | [![Actions Status][rlp-actions-badge]][rlp-actions-link]
|
6 | [![Code Coverage][rlp-coverage-badge]][rlp-coverage-link]
|
7 | [![Discord][discord-badge]][discord-link]
|
8 |
|
9 | [Recursive Length Prefix](https://eth.wiki/en/fundamentals/rlp) encoding for Node.js and the browser.
|
10 |
|
11 | ## INSTALL
|
12 |
|
13 | `npm install rlp`
|
14 |
|
15 | install with `-g` if you want to use the CLI.
|
16 |
|
17 | ## USAGE
|
18 |
|
19 | ```typescript
|
20 | import assert from 'assert'
|
21 | import RLP from 'rlp'
|
22 |
|
23 | const nestedList = [[], [[]], [[], [[]]]]
|
24 | const encoded = RLP.encode(nestedList)
|
25 | const decoded = RLP.decode(encoded)
|
26 | assert.deepEqual(nestedList, decoded)
|
27 | ```
|
28 |
|
29 | ## API
|
30 |
|
31 | `RLP.encode(plain)` - RLP encodes an `Array`, `Uint8Array` or `String` and returns a `Uint8Array`.
|
32 |
|
33 | `RLP.decode(encoded, [stream=false])` - Decodes an RLP encoded `Uint8Array`, `Array` or `String` and returns a `Uint8Array` or `NestedUint8Array`. If `stream` is enabled, it will just decode the first rlp sequence in the Uint8Array. By default, it would throw an error if there are more bytes in Uint8Array than used by the rlp sequence.
|
34 |
|
35 | ### Buffer compatibility
|
36 |
|
37 | If you would like to continue using Buffers like in rlp v2, you can use:
|
38 |
|
39 | ```typescript
|
40 | import assert from 'assert'
|
41 | import { arrToBufArr, bufArrToArr } from 'ethereumjs-util'
|
42 | import RLP from 'rlp'
|
43 |
|
44 | const bufferList = [Buffer.from('123', 'hex'), Buffer.from('456', 'hex')]
|
45 | const encoded = RLP.encode(bufArrToArr(bufferList))
|
46 | const encodedAsBuffer = Buffer.from(encoded)
|
47 | const decoded = RLP.decode(Uint8Array.from(encodedAsBuffer)) // or RLP.decode(encoded)
|
48 | const decodedAsBuffers = arrToBufArr(decoded)
|
49 | assert.deepEqual(bufferList, decodedAsBuffers)
|
50 | ```
|
51 |
|
52 | ## CLI
|
53 |
|
54 | `rlp encode <JSON string>`\
|
55 | `rlp decode <0x-prefixed hex string>`
|
56 |
|
57 | ### Examples
|
58 |
|
59 | - `rlp encode '5'` -> `0x05`
|
60 | - `rlp encode '[5]'` -> `0xc105`
|
61 | - `rlp encode '["cat", "dog"]'` -> `0xc88363617483646f67`
|
62 | - `rlp decode 0xc88363617483646f67` -> `["cat","dog"]`
|
63 |
|
64 | ## TESTS
|
65 |
|
66 | Tests use mocha.
|
67 |
|
68 | To run tests and linting: `npm test`
|
69 |
|
70 | To auto-fix linting problems run: `npm run lint:fix`
|
71 |
|
72 | ## CODE COVERAGE
|
73 |
|
74 | Install dev dependencies: `npm install`
|
75 |
|
76 | Run coverage: `npm run coverage`
|
77 |
|
78 | The results will be at: `coverage/lcov-report/index.html`
|
79 |
|
80 | # EthereumJS
|
81 |
|
82 | See our organizational [documentation](https://ethereumjs.readthedocs.io) for an introduction to `EthereumJS` as well as information on current standards and best practices.
|
83 |
|
84 | If you want to join for work or do improvements on the libraries have a look at our [contribution guidelines](https://ethereumjs.readthedocs.io/en/latest/contributing.html).
|
85 |
|
86 | [discord-badge]: https://img.shields.io/static/v1?logo=discord&label=discord&message=Join&color=blue
|
87 | [discord-link]: https://discord.gg/TNwARpR
|
88 | [rlp-npm-badge]: https://img.shields.io/npm/v/rlp.svg
|
89 | [rlp-npm-link]: https://www.npmjs.com/package/rlp
|
90 | [rlp-issues-badge]: https://img.shields.io/github/issues/ethereumjs/ethereumjs-monorepo/package:%20rlp?label=issues
|
91 | [rlp-issues-link]: https://github.com/ethereumjs/ethereumjs-monorepo/issues?q=is%3Aopen+is%3Aissue+label%3A"package%3A+rlp"
|
92 | [rlp-actions-badge]: https://github.com/ethereumjs/ethereumjs-monorepo/workflows/rlp/badge.svg
|
93 | [rlp-actions-link]: https://github.com/ethereumjs/ethereumjs-monorepo/actions?query=workflow%3A%22rlp%22
|
94 | [rlp-coverage-badge]: https://codecov.io/gh/ethereumjs/ethereumjs-monorepo/branch/master/graph/badge.svg?flag=rlp
|
95 | [rlp-coverage-link]: https://codecov.io/gh/ethereumjs/ethereumjs-monorepo/tree/master/packages/rlp
|