rlp
Advanced tools
Comparing version 2.0.0 to 2.1.0
23
index.js
const assert = require('assert') | ||
const Buffer = require('safe-buffer').Buffer | ||
/** | ||
@@ -37,3 +38,3 @@ * RLP Encoding based on: https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP | ||
if (len < 56) { | ||
return new Buffer([len + offset]) | ||
return Buffer.from([len + offset]) | ||
} else { | ||
@@ -43,3 +44,3 @@ var hexLength = intToHex(len) | ||
var firstByte = intToHex(offset + 55 + lLength) | ||
return new Buffer(firstByte + hexLength, 'hex') | ||
return Buffer.from(firstByte + hexLength, 'hex') | ||
} | ||
@@ -55,3 +56,3 @@ } | ||
if (!input || input.length === 0) { | ||
return new Buffer([]) | ||
return Buffer.from([]) | ||
} | ||
@@ -72,3 +73,3 @@ | ||
if (!input || input.length === 0) { | ||
return new Buffer([]) | ||
return Buffer.from([]) | ||
} | ||
@@ -113,3 +114,3 @@ | ||
if (firstByte === 0x80) { | ||
data = new Buffer([]) | ||
data = Buffer.from([]) | ||
} else { | ||
@@ -207,3 +208,3 @@ data = input.slice(1, length) | ||
var hex = intToHex(i) | ||
return new Buffer(hex, 'hex') | ||
return Buffer.from(hex, 'hex') | ||
} | ||
@@ -215,9 +216,9 @@ | ||
if (isHexPrefixed(v)) { | ||
v = new Buffer(padToEven(stripHexPrefix(v)), 'hex') | ||
v = Buffer.from(padToEven(stripHexPrefix(v)), 'hex') | ||
} else { | ||
v = new Buffer(v) | ||
v = Buffer.from(v) | ||
} | ||
} else if (typeof v === 'number') { | ||
if (!v) { | ||
v = new Buffer([]) | ||
v = Buffer.from([]) | ||
} else { | ||
@@ -227,6 +228,6 @@ v = intToBuffer(v) | ||
} else if (v === null || v === undefined) { | ||
v = new Buffer([]) | ||
v = Buffer.from([]) | ||
} else if (v.toArray) { | ||
// converts a BN to a Buffer | ||
v = new Buffer(v.toArray()) | ||
v = Buffer.from(v.toArray()) | ||
} else { | ||
@@ -233,0 +234,0 @@ throw new Error('invalid type') |
{ | ||
"name": "rlp", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Recursive Length Prefix Encoding Module", | ||
@@ -10,3 +10,6 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "standard ./index.js ./bin/rlp && ./node_modules/mocha/bin/mocha --reporter spec ./test/", | ||
"coverage": "istanbul cover _mocha", | ||
"coveralls": "npm run coverage && coveralls <coverage/lcov.info", | ||
"lint": "standard", | ||
"test": "istanbul test mocha -- --reporter spec", | ||
"webtest": "browserify test/max.js | testling -u" | ||
@@ -16,3 +19,3 @@ }, | ||
"type": "git", | ||
"url": "https://github.com/wanderer/rlp.git" | ||
"url": "https://github.com/ethereumjs/rlp.git" | ||
}, | ||
@@ -27,10 +30,19 @@ "keywords": [ | ||
}, | ||
"contributors": [ | ||
"Holger Drewes <Holger.Drewes@gmail.com>" | ||
], | ||
"license": "MPL-2.0", | ||
"bugs": { | ||
"url": "https://github.com/wanderer/rlp/issues" | ||
"url": "https://github.com/ethereumjs/rlp/issues" | ||
}, | ||
"dependencies": { | ||
"safe-buffer": "^5.1.1" | ||
}, | ||
"devDependencies": { | ||
"ethereumjs-testing": "git+https://github.com/wanderer/ethereumjs-testing.git", | ||
"mocha": "~1.17.1", | ||
"standard": "^5.3.1" | ||
"bn.js": "^4.11.1", | ||
"coveralls": "^2.11.4", | ||
"ethereumjs-testing": "git+https://github.com/ethereumjs/ethereumjs-testing.git", | ||
"istanbul": "^0.4.2", | ||
"mocha": "^2.3.4", | ||
"standard": "^6.0.8" | ||
}, | ||
@@ -52,3 +64,9 @@ "bin": { | ||
] | ||
}, | ||
"standard": { | ||
"globals": [ | ||
"describe", | ||
"it" | ||
] | ||
} | ||
} |
SYNOPSIS | ||
===== | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) | ||
[![Build Status](https://travis-ci.org/wanderer/rlp.png?branch=master)](https://travis-ci.org/wanderer/rlp) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/ethereum/ethereumjs-lib?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) or #ethereumjs on freenode | ||
[![NPM Package](https://img.shields.io/npm/v/rlp.svg?style=flat-square)](https://www.npmjs.org/package/rlp) | ||
[![Build Status](https://img.shields.io/travis/ethereumjs/rlp.svg?branch=master&style=flat-square)](https://travis-ci.org/ethereumjs/rlp) | ||
[![Coverage Status](https://img.shields.io/coveralls/ethereumjs/rlp.svg?style=flat-square)](https://coveralls.io/r/ethereumjs/rlp) | ||
[![Gitter](https://img.shields.io/gitter/room/ethereum/ethereumjs-lib.svg?style=flat-square)](https://gitter.im/ethereum/ethereumjs-lib) or #ethereumjs on freenode | ||
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | ||
[Recursive Length](https://github.com/ethereum/wiki/wiki/RLP) Prefix Encoding for node.js. | ||
@@ -11,2 +16,3 @@ | ||
`npm install rlp` | ||
install with `-g` if you want to use the cli. | ||
@@ -18,3 +24,4 @@ | ||
```javascript | ||
var RLP = require('rlp'); | ||
var RLP = require('rlp'); | ||
var assert = require('assert'); | ||
@@ -24,5 +31,5 @@ var nestedList = [ [], [[]], [ [], [[]] ] ]; | ||
var decoded = RLP.decode(encoded); | ||
assert.deepEqual(nestedList, decoded); | ||
}); | ||
assert.deepEqual(nestedList, decoded); | ||
``` | ||
@@ -32,5 +39,5 @@ | ||
===== | ||
`rlp.encode(plain)` - RLP encodes an `Array`, `Buffer` or `String` and returns a `Buffer`. | ||
`rlp.encode(plain)` - RLP encodes an `Array`, `Buffer` or `String` and returns a `Buffer`. | ||
`rlp.decode(encoded, [skipRemainderCheck=false])` - Decodes a RLP encoded `Buffer`, `Array` or `String` and returns a `Buffer` or an `Array` of `Buffers`. If `skipRemainderCheck` is enabled `rlp` will just decode the first rlp sequence in the buffer. By default it would through an error if there is more bytes in Buffer than used by rlp sequence. | ||
`rlp.decode(encoded, [skipRemainderCheck=false])` - Decodes an RLP encoded `Buffer`, `Array` or `String` and returns a `Buffer` or an `Array` of `Buffers`. If `skipRemainderCheck` is enabled, `rlp` will just decode the first rlp sequence in the buffer. By default, it would throw an error if there are more bytes in Buffer than used by rlp sequence. | ||
@@ -44,3 +51,13 @@ CLI | ||
===== | ||
test uses mocha. To run | ||
`npm test` | ||
Test uses mocha. To run `npm test` | ||
CODE COVERAGE | ||
============= | ||
Install dev dependencies | ||
`npm install` | ||
Run | ||
`npm run coverage` | ||
The results are at | ||
`coverage/lcov-report/index.html` |
const assert = require('assert') | ||
const RLP = require('../index.js') | ||
const officalTests = require('ethereumjs-testing').tests.rlptest | ||
const BN = require('bn.js') | ||
const testing = require('ethereumjs-testing') | ||
describe('invalid rlps', function () { | ||
it('should not crash on an invalid rlp', function () { | ||
var a = new Buffer([239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 239, 191, 189, 29, 239, 191, 189, 77, 239, 191, 189, 239, 191, 189, 239, 191, 189, 93, 122, 239, 191, 189, 239, 191, 189, 239, 191, 189, 103, 239, 191, 189, 239, 191, 189, 239, 191, 189, 26, 239, 191, 189, 18, 69, 27, 239, 191, 189, 239, 191, 189, 116, 19, 239, 191, 189, 239, 191, 189, 66, 239, 191, 189, 64, 212, 147, 71, 239, 191, 189, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 239, 191, 189, 11, 222, 155, 122, 54, 42, 194, 169, 239, 191, 189, 70, 239, 191, 189, 72, 239, 191, 189, 239, 191, 189, 54, 53, 239, 191, 189, 100, 73, 239, 191, 189, 55, 239, 191, 189, 239, 191, 189, 59, 1, 239, 191, 189, 109, 239, 191, 189, 239, 191, 189, 93, 239, 191, 189, 208, 128, 239, 191, 189, 239, 191, 189, 0, 239, 191, 189, 239, 191, 189, 239, 191, 189, 15, 66, 64, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 4, 239, 191, 189, 79, 103, 239, 191, 189, 85, 239, 191, 189, 239, 191, 189, 239, 191, 189, 74, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 54, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 83, 239, 191, 189, 14, 239, 191, 189, 239, 191, 189, 239, 191, 189, 4, 63, 239, 191, 189, 63, 239, 191, 189, 41, 239, 191, 189, 239, 191, 189, 239, 191, 189, 67, 28, 239, 191, 189, 239, 191, 189, 11, 239, 191, 189, 31, 239, 191, 189, 239, 191, 189, 104, 96, 100, 239, 191, 189, 239, 191, 189, 12, 239, 191, 189, 239, 191, 189, 206, 152, 239, 191, 189, 239, 191, 189, 31, 112, 111, 239, 191, 189, 239, 191, 189, 65, 239, 191, 189, 41, 239, 191, 189, 239, 191, 189, 53, 84, 11, 239, 191, 189, 239, 191, 189, 12, 102, 24, 12, 42, 105, 109, 239, 191, 189, 58, 239, 191, 189, 4, 239, 191, 189, 104, 82, 9, 239, 191, 189, 6, 66, 91, 43, 38, 102, 117, 239, 191, 189, 105, 239, 191, 189, 239, 191, 189, 239, 191, 189, 89, 127, 239, 191, 189, 114]) | ||
var a = Buffer.from([239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 239, 191, 189, 29, 239, 191, 189, 77, 239, 191, 189, 239, 191, 189, 239, 191, 189, 93, 122, 239, 191, 189, 239, 191, 189, 239, 191, 189, 103, 239, 191, 189, 239, 191, 189, 239, 191, 189, 26, 239, 191, 189, 18, 69, 27, 239, 191, 189, 239, 191, 189, 116, 19, 239, 191, 189, 239, 191, 189, 66, 239, 191, 189, 64, 212, 147, 71, 239, 191, 189, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 239, 191, 189, 11, 222, 155, 122, 54, 42, 194, 169, 239, 191, 189, 70, 239, 191, 189, 72, 239, 191, 189, 239, 191, 189, 54, 53, 239, 191, 189, 100, 73, 239, 191, 189, 55, 239, 191, 189, 239, 191, 189, 59, 1, 239, 191, 189, 109, 239, 191, 189, 239, 191, 189, 93, 239, 191, 189, 208, 128, 239, 191, 189, 239, 191, 189, 0, 239, 191, 189, 239, 191, 189, 239, 191, 189, 15, 66, 64, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 4, 239, 191, 189, 79, 103, 239, 191, 189, 85, 239, 191, 189, 239, 191, 189, 239, 191, 189, 74, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 54, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 83, 239, 191, 189, 14, 239, 191, 189, 239, 191, 189, 239, 191, 189, 4, 63, 239, 191, 189, 63, 239, 191, 189, 41, 239, 191, 189, 239, 191, 189, 239, 191, 189, 67, 28, 239, 191, 189, 239, 191, 189, 11, 239, 191, 189, 31, 239, 191, 189, 239, 191, 189, 104, 96, 100, 239, 191, 189, 239, 191, 189, 12, 239, 191, 189, 239, 191, 189, 206, 152, 239, 191, 189, 239, 191, 189, 31, 112, 111, 239, 191, 189, 239, 191, 189, 65, 239, 191, 189, 41, 239, 191, 189, 239, 191, 189, 53, 84, 11, 239, 191, 189, 239, 191, 189, 12, 102, 24, 12, 42, 105, 109, 239, 191, 189, 58, 239, 191, 189, 4, 239, 191, 189, 104, 82, 9, 239, 191, 189, 6, 66, 91, 43, 38, 102, 117, 239, 191, 189, 105, 239, 191, 189, 239, 191, 189, 239, 191, 189, 89, 127, 239, 191, 189, 114]) | ||
try { | ||
rlp.decode(a) | ||
RLP.decode(a) | ||
} catch (e) { | ||
// FIXME: check for exception name | ||
assert(true) | ||
@@ -20,2 +22,3 @@ } | ||
assert.equal(encodedSelf.toString(), 'a') | ||
assert.equal(RLP.getLength(encodedSelf), 1) | ||
}) | ||
@@ -26,2 +29,3 @@ | ||
assert.equal(4, encodedDog.length) | ||
assert.equal(RLP.getLength(encodedDog), 4) | ||
assert.equal(encodedDog[0], 131) | ||
@@ -36,2 +40,3 @@ assert.equal(encodedDog[1], 100) | ||
assert.equal(72, encodedLongString.length) | ||
assert.equal(RLP.getLength(encodedLongString), 2) | ||
assert.equal(encodedLongString[0], 184) | ||
@@ -82,3 +87,3 @@ assert.equal(encodedLongString[1], 70) | ||
it('first byte < 0x7f, return byte itself', function () { | ||
var decodedStr = RLP.decode(new Buffer([97])) | ||
var decodedStr = RLP.decode(Buffer.from([97])) | ||
assert.equal(1, decodedStr.length) | ||
@@ -89,3 +94,3 @@ assert.equal(decodedStr.toString(), 'a') | ||
it('first byte < 0xb7, data is everything except first byte', function () { | ||
var decodedStr = RLP.decode(new Buffer([131, 100, 111, 103])) | ||
var decodedStr = RLP.decode(Buffer.from([131, 100, 111, 103])) | ||
assert.equal(3, decodedStr.length) | ||
@@ -96,4 +101,4 @@ assert.equal(decodedStr.toString(), 'dog') | ||
it('array', function () { | ||
var decodedBufferArray = RLP.decode(new Buffer([204, 131, 100, 111, 103, 131, 103, 111, 100, 131, 99, 97, 116])) | ||
assert.deepEqual(decodedBufferArray, [new Buffer('dog'), new Buffer('god'), new Buffer('cat')]) | ||
var decodedBufferArray = RLP.decode(Buffer.from([204, 131, 100, 111, 103, 131, 103, 111, 100, 131, 99, 97, 116])) | ||
assert.deepEqual(decodedBufferArray, [Buffer.from('dog'), Buffer.from('god'), Buffer.from('cat')]) | ||
}) | ||
@@ -104,3 +109,3 @@ }) | ||
it('first byte < 0x7f, return itself', function () { | ||
var decodedSmallNum = RLP.decode(new Buffer([15])) | ||
var decodedSmallNum = RLP.decode(Buffer.from([15])) | ||
assert.equal(1, decodedSmallNum.length) | ||
@@ -111,3 +116,3 @@ assert.equal(decodedSmallNum[0], 15) | ||
it('first byte < 0xb7, data is everything except first byte', function () { | ||
var decodedNum = RLP.decode(new Buffer([130, 4, 0])) | ||
var decodedNum = RLP.decode(Buffer.from([130, 4, 0])) | ||
assert.equal(2, decodedNum.length) | ||
@@ -120,3 +125,3 @@ assert.equal(decodedNum.toString('hex'), '0400') | ||
var testString = 'This function takes in a data, convert it to buffer if not, and a length for recursion' | ||
testString = new Buffer(testString) | ||
testString = Buffer.from(testString) | ||
var encoded = null | ||
@@ -133,3 +138,2 @@ it('should encode it', function () { | ||
}) | ||
}) | ||
@@ -152,3 +156,2 @@ | ||
}) | ||
}) | ||
@@ -172,3 +175,3 @@ | ||
encoded = RLP.encode(nestedList) | ||
assert.deepEqual(encoded, new Buffer([0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0])) | ||
assert.deepEqual(encoded, Buffer.from([0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0])) | ||
}) | ||
@@ -187,7 +190,7 @@ | ||
encoded = RLP.encode(nestedList) | ||
assert.deepEqual(encoded, new Buffer([0xc1, 0x80])) | ||
assert.deepEqual(encoded, Buffer.from([0xc1, 0x80])) | ||
}) | ||
it('should decode a null value', function () { | ||
assert.deepEqual(new Buffer([]), RLP.decode(new Buffer('80', 'hex'))) | ||
assert.deepEqual(Buffer.from([]), RLP.decode(Buffer.from('80', 'hex'))) | ||
}) | ||
@@ -197,18 +200,25 @@ }) | ||
describe('zero values', function () { | ||
var nestedList = [null] | ||
var encoded | ||
it('encode a zero', function () { | ||
encoded = RLP.encode(new Buffer([0])) | ||
assert.deepEqual(encoded, new Buffer([0])) | ||
encoded = RLP.encode(Buffer.from([0])) | ||
assert.deepEqual(encoded, Buffer.from([0])) | ||
}) | ||
it('decode a zero', function () { | ||
var decode = RLP.decode(new Buffer([0])) | ||
assert.deepEqual(decode, new Buffer([0])) | ||
var decode = RLP.decode(Buffer.from([0])) | ||
assert.deepEqual(decode, Buffer.from([0])) | ||
}) | ||
}) | ||
describe('empty values', function () { | ||
var decoded | ||
it('decode empty buffer', function () { | ||
decoded = RLP.decode(Buffer.from([])) | ||
assert.deepEqual(decoded, Buffer.from([])) | ||
}) | ||
}) | ||
describe('bad values', function () { | ||
it('wrong encoded a zero', function () { | ||
var val = new Buffer('f9005f030182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3', 'hex') | ||
var val = Buffer.from('f9005f030182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3', 'hex') | ||
var result | ||
@@ -222,3 +232,3 @@ try { | ||
it('invalid length', function () { | ||
var a = new Buffer('f86081000182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3', 'hex') | ||
var a = Buffer.from('f86081000182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3', 'hex') | ||
@@ -235,3 +245,3 @@ var result | ||
var a = new Buffer(c, 'hex') | ||
var a = Buffer.from(c, 'hex') | ||
@@ -248,3 +258,3 @@ var result | ||
var a = new Buffer(c, 'hex') | ||
var a = Buffer.from(c, 'hex') | ||
@@ -261,3 +271,3 @@ var result | ||
var a = new Buffer(c, 'hex') | ||
var a = Buffer.from(c, 'hex') | ||
@@ -282,2 +292,4 @@ var result | ||
it('pass all tests', function (done) { | ||
const officalTests = testing.getSingleFile('RLPTests/rlptest.json') | ||
for (var test in officalTests) { | ||
@@ -287,4 +299,4 @@ var incoming = officalTests[test].in | ||
if (incoming[0] === '#') { | ||
var bn = new Bignum(incoming.slice(1)) | ||
incoming = new Buffer(bn.toArray()) | ||
var bn = new BN(incoming.slice(1)) | ||
incoming = Buffer.from(bn.toArray()) | ||
} | ||
@@ -291,0 +303,0 @@ |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found
Found 1 instance in 1 package
8
3
478
59
26922
1
6
1
+ Addedsafe-buffer@^5.1.1
+ Addedsafe-buffer@5.2.1(transitive)