Comparing version 1.0.1 to 1.1.0
172
index.js
const assert = require('assert') | ||
/** | ||
@@ -13,3 +12,3 @@ * RLP Encoding based on: https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP | ||
var output = [] | ||
for (var i=0; i < input.length; i++) { | ||
for (var i = 0; i < input.length; i++) { | ||
output.push(exports.encode(input[i])) | ||
@@ -21,12 +20,14 @@ } | ||
input = toBuffer(input) | ||
if (input.length === 1 && input[0] < 128) | ||
if (input.length === 1 && input[0] < 128) { | ||
return input | ||
else | ||
} else { | ||
return Buffer.concat([encodeLength(input.length, 128), input]) | ||
} | ||
} | ||
} | ||
function safeParseInt(v, base){ | ||
if(v.slice(0, 2) === '00') | ||
throw('invalid RLP: extra zeros') | ||
function safeParseInt (v, base) { | ||
if (v.slice(0, 2) === '00') { | ||
throw (new Error('invalid RLP: extra zeros')) | ||
} | ||
@@ -36,3 +37,3 @@ return parseInt(v, base) | ||
function encodeLength(len, offset) { | ||
function encodeLength (len, offset) { | ||
if (len < 56) { | ||
@@ -50,12 +51,17 @@ return new Buffer([len + offset]) | ||
* RLP Decoding based on: {@link https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP|RLP} | ||
* | ||
* @param {Buffer,String,Integer,Array} data - will be converted to buffer | ||
* @returns {Array} - returns decode Array of Buffers containg the original message | ||
**/ | ||
var decode = exports.decode = function (input) { | ||
if(!input || input.length === 0) | ||
exports.decode = function (input, stream) { | ||
if (!input || input.length === 0) { | ||
return new Buffer([]) | ||
} | ||
input = toBuffer(input) | ||
var decoded = _decode(input) | ||
if (stream) { | ||
return decoded | ||
} | ||
assert.equal(decoded.remainder.length, 0, 'invalid remainder') | ||
@@ -65,6 +71,33 @@ return decoded.data | ||
exports.getLength = function (input) { | ||
if (!input || input.length === 0) { | ||
return new Buffer([]) | ||
} | ||
input = toBuffer(input) | ||
var firstByte = input[0] | ||
if (firstByte <= 0x7f) { | ||
return input.length | ||
} else if (firstByte <= 0xb7) { | ||
return firstByte - 0x7f | ||
} else if (firstByte <= 0xbf) { | ||
return firstByte - 0xb6 | ||
} else if (firstByte <= 0xf7) { | ||
// a list between 0-55 bytes long | ||
return firstByte - 0xbf | ||
} else { | ||
// a list over 55 bytes long | ||
var llength = firstByte - 0xf6 | ||
var length = safeParseInt(input.slice(1, llength).toString('hex'), 16) | ||
return llength + length | ||
} | ||
} | ||
function _decode (input) { | ||
var length, llength, data, innerRemainder, d | ||
var decoded = [] | ||
var firstByte = input[0] | ||
if (firstByte <= 0x7f) { | ||
//a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding. | ||
// a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding. | ||
return { | ||
@@ -75,15 +108,16 @@ data: input.slice(0, 1), | ||
} else if (firstByte <= 0xb7) { | ||
//string is 0-55 bytes long. A single byte with value 0x80 plus the length of the string followed by the string | ||
//The range of the first byte is [0x80, 0xb7] | ||
var length = firstByte - 0x7f, | ||
data | ||
// string is 0-55 bytes long. A single byte with value 0x80 plus the length of the string followed by the string | ||
// The range of the first byte is [0x80, 0xb7] | ||
length = firstByte - 0x7f | ||
//set 0x80 null to 0 | ||
if (firstByte === 0x80) | ||
// set 0x80 null to 0 | ||
if (firstByte === 0x80) { | ||
data = new Buffer([]) | ||
else | ||
} else { | ||
data = input.slice(1, length) | ||
} | ||
if(length === 2 && data[0] < 0x80) | ||
throw 'invalid rlp encoding: byte must be less 0x80' | ||
if (length === 2 && data[0] < 0x80) { | ||
throw new Error('invalid rlp encoding: byte must be less 0x80') | ||
} | ||
@@ -95,7 +129,8 @@ return { | ||
} else if (firstByte <= 0xbf) { | ||
var llength = firstByte - 0xb6 | ||
var length = safeParseInt(input.slice(1, llength).toString('hex'), 16) | ||
var data = input.slice(llength, length + llength) | ||
if(data.length < length) | ||
throw(new Error('invalid RLP')) | ||
llength = firstByte - 0xb6 | ||
length = safeParseInt(input.slice(1, llength).toString('hex'), 16) | ||
data = input.slice(llength, length + llength) | ||
if (data.length < length) { | ||
throw (new Error('invalid RLP')) | ||
} | ||
@@ -107,9 +142,7 @@ return { | ||
} else if (firstByte <= 0xf7) { | ||
//a list between 0-55 bytes long | ||
var length = firstByte - 0xbf | ||
var remainder = input.slice(1) | ||
var innerRemainder = input.slice(1, length) | ||
var decoded = [] | ||
// a list between 0-55 bytes long | ||
length = firstByte - 0xbf | ||
innerRemainder = input.slice(1, length) | ||
while (innerRemainder.length) { | ||
var d = _decode(innerRemainder) | ||
d = _decode(innerRemainder) | ||
decoded.push(d.data) | ||
@@ -123,19 +156,18 @@ innerRemainder = d.remainder | ||
} | ||
} else { | ||
//a list over 55 bytes long | ||
var llength = firstByte - 0xf6 | ||
var length = safeParseInt(input.slice(1, llength).toString('hex'), 16) | ||
// a list over 55 bytes long | ||
llength = firstByte - 0xf6 | ||
length = safeParseInt(input.slice(1, llength).toString('hex'), 16) | ||
var totalLength = llength + length | ||
if(totalLength > input.length) | ||
if (totalLength > input.length) { | ||
throw new Error('invalid rlp: total length is larger than the data') | ||
} | ||
var remainder = input.slice(llength) | ||
var innerRemainder = input.slice(llength, totalLength) | ||
var decoded = [] | ||
if(innerRemainder.length === 0) | ||
innerRemainder = input.slice(llength, totalLength) | ||
if (innerRemainder.length === 0) { | ||
throw new Error('invalid rlp, List has a invalid length') | ||
} | ||
while (innerRemainder.length) { | ||
var d = _decode(innerRemainder) | ||
d = _decode(innerRemainder) | ||
decoded.push(d.data) | ||
@@ -151,23 +183,49 @@ innerRemainder = d.remainder | ||
function isHexPrefixed (str) { | ||
return str.slice(0, 2) === '0x' | ||
} | ||
// Removes 0x from a given String | ||
function stripHexPrefix (str) { | ||
if (typeof str !== 'string') { | ||
return str | ||
} | ||
return isHexPrefixed(str) ? str.slice(2) : str | ||
} | ||
function intToHex (i) { | ||
var hex = i.toString(16) | ||
if (hex.length % 2) | ||
if (hex.length % 2) { | ||
hex = '0' + hex | ||
} | ||
return hex | ||
} | ||
function toBuffer (input) { | ||
if (Buffer.isBuffer(input)) { | ||
if (input.length === 0) | ||
return toBuffer(null) | ||
else | ||
return input | ||
} else if (input === null || input === 0 || input === undefined) { | ||
return new Buffer(0) | ||
} else if (!isNaN(input)) { | ||
var hex = intToHex(input) | ||
return new Buffer(hex, 'hex') | ||
} else if (!Buffer.isBuffer(input)) | ||
return new Buffer(input.toString()) | ||
function padToEven (a) { | ||
if (a.length % 2) a = '0' + a | ||
return a | ||
} | ||
function intToBuffer (i) { | ||
var hex = intToHex(i) | ||
return new Buffer(hex, 'hex') | ||
} | ||
function toBuffer (v) { | ||
if (!Buffer.isBuffer(v)) { | ||
if (typeof v === 'string') { | ||
v = new Buffer(padToEven(stripHexPrefix(v)), 'hex') | ||
} else if (typeof v === 'number') { | ||
v = intToBuffer(v) | ||
} else if (v === null || v === undefined) { | ||
v = new Buffer([]) | ||
} else if (v.toArray) { | ||
// converts a BN to a Buffer | ||
v = new Buffer(v.toArray()) | ||
} else { | ||
throw new Error('invalid type') | ||
} | ||
} | ||
return v | ||
} |
{ | ||
"name": "rlp", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Recursive Length Prefix Encoding Module", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha --timeout 5000 --reporter spec ./test/", | ||
"test": "standard ./index.js && ./node_modules/mocha/bin/mocha --reporter spec ./test/", | ||
"webtest": "browserify test/max.js | testling -u" | ||
@@ -22,7 +22,2 @@ }, | ||
], | ||
"contributors": [ | ||
{ | ||
"name": "josephyzhou" | ||
} | ||
], | ||
"author": { | ||
@@ -32,3 +27,3 @@ "name": "martin becze", | ||
}, | ||
"license": "GPL", | ||
"license": "MPL-2.0", | ||
"bugs": { | ||
@@ -38,5 +33,5 @@ "url": "https://github.com/wanderer/rlp/issues" | ||
"devDependencies": { | ||
"ethereumjs-testing": "git+https://github.com/wanderer/ethereumjs-testing.git", | ||
"mocha": "~1.17.1", | ||
"bn.js": "*", | ||
"ethrereum-tests": "git+https://github.com/ethereum/tests.git#develop" | ||
"standard": "^5.2.2" | ||
}, | ||
@@ -48,3 +43,3 @@ "bin": { | ||
"files": "test/*.js", | ||
"harness" : "mocha-bdd", | ||
"harness": "mocha-bdd", | ||
"browsers": [ | ||
@@ -51,0 +46,0 @@ "chrome/22..latest", |
@@ -1,5 +0,8 @@ | ||
RLP [![Build Status](https://travis-ci.org/wanderer/rlp.png?branch=master)](https://travis-ci.org/wanderer/rlp) | ||
=== | ||
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 | ||
[Recursive Length]( https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP) Prefix Encoding for node.js. | ||
[Recursive Length](https://github.com/ethereum/wiki/wiki/RLP) Prefix Encoding for node.js. | ||
INSTALL | ||
@@ -26,5 +29,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)` - Decodes a RLP encoded `Buffer`, `Array` or `String` and returns a `Buffer` or an `Array` of `Buffers` | ||
`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. | ||
@@ -31,0 +34,0 @@ CLI |
const assert = require('assert') | ||
const fs = require('fs') | ||
const Bignum = require('bn.js') | ||
const RLP = require('../index.js') | ||
const officalTests = require('ethereum-tests').rlptest | ||
const officalTests = require('ethereumjs-testing').tests.rlptest | ||
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]) | ||
try{ | ||
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]) | ||
try { | ||
rlp.decode(a) | ||
}catch(e){ | ||
} catch (e) { | ||
assert(true) | ||
@@ -18,10 +16,10 @@ } | ||
describe('RLP encoding (string):', function() { | ||
it('should return itself if single byte and less than 0x7f:', function() { | ||
var encodedSelf = RLP.encode('a') | ||
describe('RLP encoding (string):', function () { | ||
it('should return itself if single byte and less than 0x7f:', function () { | ||
var encodedSelf = RLP.encode(new Buffer('a')) | ||
assert.equal(encodedSelf.toString(), 'a') | ||
}) | ||
it('length of string 0-55 should return (0x80+len(data)) plus data', function() { | ||
var encodedDog = RLP.encode('dog') | ||
it('length of string 0-55 should return (0x80+len(data)) plus data', function () { | ||
var encodedDog = RLP.encode(new Buffer('dog')) | ||
assert.equal(4, encodedDog.length) | ||
@@ -34,4 +32,4 @@ assert.equal(encodedDog[0], 131) | ||
it('length of string >55 should return 0xb7+len(len(data)) plus len(data) plus data', function() { | ||
var encodedLongString = RLP.encode('zoo255zoo255zzzzzzzzzzzzssssssssssssssssssssssssssssssssssssssssssssss') | ||
it('length of string >55 should return 0xb7+len(len(data)) plus len(data) plus data', function () { | ||
var encodedLongString = RLP.encode(new Buffer('zoo255zoo255zzzzzzzzzzzzssssssssssssssssssssssssssssssssssssssssssssss')) | ||
assert.equal(72, encodedLongString.length) | ||
@@ -46,5 +44,5 @@ assert.equal(encodedLongString[0], 184) | ||
describe('RLP encoding (list):', function() { | ||
it('length of list 0-55 should return (0xc0+len(data)) plus data', function() { | ||
var encodedArrayOfStrings = RLP.encode(['dog', 'god', 'cat']) | ||
describe('RLP encoding (list):', function () { | ||
it('length of list 0-55 should return (0xc0+len(data)) plus data', function () { | ||
var encodedArrayOfStrings = RLP.encode([new Buffer('dog'), new Buffer('god'), new Buffer('cat')]) | ||
assert.equal(13, encodedArrayOfStrings.length) | ||
@@ -57,9 +55,9 @@ assert.equal(encodedArrayOfStrings[0], 204) | ||
it('length of list >55 should return 0xf7+len(len(data)) plus len(data) plus data', function() { | ||
//need a test case here! | ||
}) | ||
// it('length of list >55 should return 0xf7+len(len(data)) plus len(data) plus data', function () { | ||
// // need a test case here! | ||
// }) | ||
}) | ||
describe('RLP encoding (integer):', function() { | ||
it('length of int = 1, less than 0x7f, similar to string', function() { | ||
describe('RLP encoding (integer):', function () { | ||
it('length of int = 1, less than 0x7f, similar to string', function () { | ||
var encodedNumber = RLP.encode(15) | ||
@@ -70,3 +68,3 @@ assert.equal(1, encodedNumber.length) | ||
it('length of int > 55, similar to string', function() { | ||
it('length of int > 55, similar to string', function () { | ||
var encodedNumber = RLP.encode(1024) | ||
@@ -80,23 +78,23 @@ assert.equal(3, encodedNumber.length) | ||
describe('RLP decoding (string):', function() { | ||
it('first byte < 0x7f, return byte itself', function() { | ||
describe('RLP decoding (string):', function () { | ||
it('first byte < 0x7f, return byte itself', function () { | ||
var decodedStr = RLP.decode(new Buffer([97])) | ||
assert.equal(1, decodedStr.length) | ||
assert.equal(decodedStr.toString(), "a") | ||
assert.equal(decodedStr.toString(), 'a') | ||
}) | ||
it('first byte < 0xb7, data is everything except first byte', function() { | ||
it('first byte < 0xb7, data is everything except first byte', function () { | ||
var decodedStr = RLP.decode(new Buffer([131, 100, 111, 103])) | ||
assert.equal(3, decodedStr.length) | ||
assert.equal(decodedStr.toString(), "dog") | ||
assert.equal(decodedStr.toString(), 'dog') | ||
}) | ||
it('array', function() { | ||
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")]) | ||
assert.deepEqual(decodedBufferArray, [new Buffer('dog'), new Buffer('god'), new Buffer('cat')]) | ||
}) | ||
}) | ||
describe('RLP decoding (int):', function() { | ||
it('first byte < 0x7f, return itself', function() { | ||
describe('RLP decoding (int):', function () { | ||
it('first byte < 0x7f, return itself', function () { | ||
var decodedSmallNum = RLP.decode(new Buffer([15])) | ||
@@ -107,13 +105,14 @@ assert.equal(1, decodedSmallNum.length) | ||
it('first byte < 0xb7, data is everything except first byte', function() { | ||
it('first byte < 0xb7, data is everything except first byte', function () { | ||
var decodedNum = RLP.decode(new Buffer([130, 4, 0])) | ||
assert.equal(2, decodedNum.length) | ||
assert.equal(decodedNum.toString('hex'), "0400") | ||
assert.equal(decodedNum.toString('hex'), '0400') | ||
}) | ||
}) | ||
describe('strings over 55 bytes long', function() { | ||
var testString = "This function takes in a data, convert it to buffer if not, and a length for recursion" | ||
describe('strings over 55 bytes long', function () { | ||
var testString = 'This function takes in a data, convert it to buffer if not, and a length for recursion' | ||
testString = new Buffer(testString) | ||
var encoded = null | ||
it("should encode it", function() { | ||
it('should encode it', function () { | ||
encoded = RLP.encode(testString) | ||
@@ -124,3 +123,3 @@ assert.equal(encoded[0], 184) | ||
it("should decode", function() { | ||
it('should decode', function () { | ||
var decoded = RLP.decode(encoded) | ||
@@ -132,10 +131,15 @@ assert.equal(decoded.toString(), testString) | ||
describe('list over 55 bytes long', function() { | ||
var testString = ["This", "function", "takes", "in", "a", "data", "convert", "it", "to", "buffer", "if", "not", "and", "a", "length", "for", "recursion", 'a1', 'a2', 'a3', 'ia4', 'a5', 'a6', 'a7', 'a8', 'ba9'] | ||
describe('list over 55 bytes long', function () { | ||
var testString = ['This', 'function', 'takes', 'in', 'a', 'data', 'convert', 'it', 'to', 'buffer', 'if', 'not', 'and', 'a', 'length', 'for', 'recursion', 'a1', 'a2', 'a3', 'ia4', 'a5', 'a6', 'a7', 'a8', 'ba9'] | ||
var encoded = null | ||
it("should encode it", function() { | ||
encoded = RLP.encode(testString) | ||
var bufString = testString.map(function (i) { | ||
return new Buffer(i) | ||
}) | ||
it("should decode", function() { | ||
it('should encode it', function () { | ||
encoded = RLP.encode(bufString) | ||
}) | ||
it('should decode', function () { | ||
var decoded = RLP.decode(encoded) | ||
@@ -150,3 +154,3 @@ for (var i = 0; i < decoded.length; i++) { | ||
describe('nested lists:', function() { | ||
describe('nested lists:', function () { | ||
var nestedList = [ | ||
@@ -165,3 +169,3 @@ [], | ||
var encoded | ||
it('encode a nested list', function() { | ||
it('encode a nested list', function () { | ||
encoded = RLP.encode(nestedList) | ||
@@ -171,3 +175,3 @@ assert.deepEqual(encoded, new Buffer([0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0])) | ||
it('should decode a nested list', function() { | ||
it('should decode a nested list', function () { | ||
var decoded = RLP.decode(encoded) | ||
@@ -178,6 +182,6 @@ assert.deepEqual(nestedList, decoded) | ||
describe('null values', function() { | ||
describe('null values', function () { | ||
var nestedList = [null] | ||
var encoded | ||
it('encode a null array', function() { | ||
it('encode a null array', function () { | ||
encoded = RLP.encode(nestedList) | ||
@@ -187,3 +191,3 @@ assert.deepEqual(encoded, new Buffer([0xc1, 0x80])) | ||
it('should decode a null value', function(){ | ||
it('should decode a null value', function () { | ||
assert.deepEqual(new Buffer([]), RLP.decode(new Buffer('80', 'hex'))) | ||
@@ -193,6 +197,6 @@ }) | ||
describe('zero values', function() { | ||
describe('zero values', function () { | ||
var nestedList = [null] | ||
var encoded | ||
it('encode a zero', function() { | ||
it('encode a zero', function () { | ||
encoded = RLP.encode(new Buffer([0])) | ||
@@ -202,3 +206,3 @@ assert.deepEqual(encoded, new Buffer([0])) | ||
it('decode a zero', function() { | ||
it('decode a zero', function () { | ||
var decode = RLP.decode(new Buffer([0])) | ||
@@ -209,60 +213,55 @@ assert.deepEqual(decode, new Buffer([0])) | ||
describe('bad values', function() { | ||
it('wrong encoded a zero', function() { | ||
var val = new Buffer("f9005f030182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3", 'hex') | ||
describe('bad values', function () { | ||
it('wrong encoded a zero', function () { | ||
var val = new Buffer('f9005f030182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3', 'hex') | ||
var result | ||
try{ | ||
try { | ||
result = RLP.decode(val) | ||
}catch(e){ | ||
} | ||
} catch (e) {} | ||
assert.equal(result, undefined) | ||
}) | ||
it('invalid length', function(){ | ||
it('invalid length', function () { | ||
var a = new Buffer('f86081000182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3', 'hex') | ||
var result | ||
try{ | ||
try { | ||
result = RLP.decode(a) | ||
}catch(e){ | ||
} | ||
} catch (e) {} | ||
assert.equal(result, undefined) | ||
}) | ||
it('extra data at end', function(){ | ||
it('extra data at end', function () { | ||
var c = 'f90260f901f9a02a3c692012a15502ba9c39f3aebb36694eed978c74b52e6c0cf210d301dbf325a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a0b6c9fd1447d0b414a1f05957927746f58ef5a2ebde17db631d460eaf6a93b18da0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845509814280a00451dd53d9c09f3cfb627b51d9d80632ed801f6330ee584bffc26caac9b9249f88c7bffe5ebd94cc2ff861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba098c3a099885a281885f487fd37550de16436e8c47874cd213531b10fe751617fa044b6b81011ce57bffcaf610bf728fb8a7237ad261ea2d937423d78eb9e137076c0ef' | ||
var a = new Buffer(c, 'hex') | ||
var result | ||
try{ | ||
try { | ||
result = RLP.decode(a) | ||
}catch(e){ | ||
} | ||
} catch (e) {} | ||
assert.equal(result, undefined) | ||
}) | ||
it('extra data at end', function(){ | ||
it('extra data at end', function () { | ||
var c = 'f9ffffffc260f901f9a02a3c692012a15502ba9c39f3aebb36694eed978c74b52e6c0cf210d301dbf325a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a0b6c9fd1447d0b414a1f05957927746f58ef5a2ebde17db631d460eaf6a93b18da0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845509814280a00451dd53d9c09f3cfb627b51d9d80632ed801f6330ee584bffc26caac9b9249f88c7bffe5ebd94cc2ff861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba098c3a099885a281885f487fd37550de16436e8c47874cd213531b10fe751617fa044b6b81011ce57bffcaf610bf728fb8a7237ad261ea2d937423d78eb9e137076c0' | ||
var a = new Buffer(c, 'hex') | ||
var result | ||
try{ | ||
try { | ||
result = RLP.decode(a) | ||
}catch(e){ | ||
} | ||
} catch (e) {} | ||
assert.equal(result, undefined) | ||
}) | ||
it('list length longer than data', function(){ | ||
it('list length longer than data', function () { | ||
var c = 'f9ffffffc260f901f9a02a3c692012a15502ba9c39f3aebb36694eed978c74b52e6c0cf210d301dbf325a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a0b6c9fd1447d0b414a1f05957927746f58ef5a2ebde17db631d460eaf6a93b18da0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845509814280a00451dd53d9c09f3cfb627b51d9d80632ed801f6330ee584bffc26caac9b9249f88c7bffe5ebd94cc2ff861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba098c3a099885a281885f487fd37550de16436e8c47874cd213531b10fe751617fa044b6b81011ce57bffcaf610bf728fb8a7237ad261ea2d937423d78eb9e137076c0' | ||
var a = new Buffer(c, 'hex') | ||
var result | ||
try{ | ||
try { | ||
result = RLP.decode(a) | ||
}catch(e){ | ||
} | ||
} catch (e) {} | ||
assert.equal(result, undefined) | ||
@@ -272,8 +271,15 @@ }) | ||
describe('offical tests', function() { | ||
it('pass all tests', function(done) { | ||
describe('hex prefix', function () { | ||
it('should have the same value', function () { | ||
var a = RLP.encode('0x88f') | ||
var b = RLP.encode('88f') | ||
assert.equal(a.toString('hex'), b.toString('hex')) | ||
}) | ||
}) | ||
describe('offical tests', function () { | ||
it('pass all tests', function (done) { | ||
for (var test in officalTests) { | ||
var incoming = officalTests[test].in | ||
//if we are testing a big number | ||
// if we are testing a big number | ||
if (incoming[0] === '#') { | ||
@@ -280,0 +286,0 @@ var bn = new Bignum(incoming.slice(1)) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
40937
0
449
42
3
0
7
3