rlp
Advanced tools
Comparing version 0.0.0 to 0.0.1
24
index.js
@@ -32,6 +32,6 @@ var internals = {}; | ||
} else { | ||
var hL = internals.intToHex(len); | ||
var LL = Math.ceil((hL.length / 2)); | ||
var firstByte = internals.intToHex(offset + 55 + LL); | ||
return new Buffer(firstByte + hL, 'hex'); | ||
var hexLength = internals.intToHex(len); | ||
var lLength = hexLength.length / 2; | ||
var firstByte = internals.intToHex(offset + 55 + lLength); | ||
return new Buffer(firstByte + hexLength, 'hex'); | ||
} | ||
@@ -54,2 +54,3 @@ }; | ||
if (firstByte <= 0x7f) { | ||
//a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding. | ||
return { | ||
@@ -60,2 +61,4 @@ 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; | ||
@@ -66,3 +69,2 @@ return { | ||
}; | ||
} else if (firstByte <= 0xbf) { | ||
@@ -75,3 +77,2 @@ var llength = firstByte - 0xb6; | ||
}; | ||
} else if (firstByte <= 0xf7) { | ||
@@ -122,9 +123,12 @@ //a list between 0-55 bytes long | ||
internals.toBuffer = function(input) { | ||
if (!isNaN(input)) { | ||
if (Buffer.isBuffer(input)) { | ||
return input; | ||
} else if (input === null) { | ||
return new Buffer(0); | ||
} else if (!isNaN(input)) { | ||
var hex = internals.intToHex(input); | ||
input = new Buffer(hex, 'hex'); | ||
return new Buffer(hex, 'hex'); | ||
} else if (!Buffer.isBuffer(input)) { | ||
input = new Buffer(input.toString()); | ||
return new Buffer(input.toString()); | ||
} | ||
return input; | ||
}; |
{ | ||
"name": "rlp", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Recursive Length Prefix Encoding Module", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
rlp | ||
rlp [![Build Status](https://travis-ci.org/wanderer/rlp.png?branch=master)](https://travis-ci.org/wanderer/rlp) | ||
=== | ||
@@ -6,2 +6,5 @@ | ||
Install | ||
====== | ||
`npm install rlp` | ||
@@ -13,3 +16,3 @@ Usage | ||
`decode` - Decodes and RLP encoded buffer, array or string and returns a buffer or and array of buffers | ||
`decode` - Decodes a RLP encoded buffer, array or string and returns a buffer or an array of buffers | ||
@@ -25,3 +28,3 @@ Example | ||
var decoded = RLP.decode(encoded); | ||
assert.deepEqual(nestedList, decoded); | ||
assert.deepEqual(nestedList, decoded); | ||
}); | ||
@@ -28,0 +31,0 @@ ``` |
@@ -143,3 +143,11 @@ var assert = require('assert'); | ||
}); | ||
}); | ||
describe('null values', function() { | ||
var nestedList = [new Buffer(0)]; | ||
var encoded; | ||
it('encode a null array', function() { | ||
encoded = RLP.encode(nestedList); | ||
assert.deepEqual(encoded, new Buffer([ 0xc1, 0x80 ])); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
29867
7
255
34