commit
776b9c674e
5 changed files with 26 additions and 102 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
coverage
|
coverage
|
||||||
node_modules
|
node_modules
|
||||||
.nyc_output
|
.nyc_output
|
||||||
|
npm-debug.log
|
||||||
|
|
|
@ -60,6 +60,7 @@
|
||||||
"ecurve": "^1.0.0",
|
"ecurve": "^1.0.0",
|
||||||
"randombytes": "^2.0.1",
|
"randombytes": "^2.0.1",
|
||||||
"typeforce": "^1.8.7",
|
"typeforce": "^1.8.7",
|
||||||
|
"varuint-bitcoin": "^1.0.4",
|
||||||
"wif": "^2.0.1"
|
"wif": "^2.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -1,12 +1,5 @@
|
||||||
var opcodes = require('./opcodes.json')
|
var opcodes = require('./opcodes.json')
|
||||||
|
var varuint = require('varuint-bitcoin')
|
||||||
// https://github.com/feross/buffer/blob/master/index.js#L1127
|
|
||||||
function verifuint (value, max) {
|
|
||||||
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
|
|
||||||
if (value < 0) throw new Error('specified a negative value for writing an unsigned value')
|
|
||||||
if (value > max) throw new Error('value is larger than maximum value for type')
|
|
||||||
if (Math.floor(value) !== value) throw new Error('value has a fractional component')
|
|
||||||
}
|
|
||||||
|
|
||||||
function pushDataSize (i) {
|
function pushDataSize (i) {
|
||||||
return i < opcodes.OP_PUSHDATA1 ? 1
|
return i < opcodes.OP_PUSHDATA1 ? 1
|
||||||
|
@ -52,6 +45,14 @@ function readPushDataInt (buffer, offset) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/feross/buffer/blob/master/index.js#L1127
|
||||||
|
function verifuint (value, max) {
|
||||||
|
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
|
||||||
|
if (value < 0) throw new Error('specified a negative value for writing an unsigned value')
|
||||||
|
if (value > max) throw new Error('RangeError: value out of range')
|
||||||
|
if (Math.floor(value) !== value) throw new Error('value has a fractional component')
|
||||||
|
}
|
||||||
|
|
||||||
function readUInt64LE (buffer, offset) {
|
function readUInt64LE (buffer, offset) {
|
||||||
var a = buffer.readUInt32LE(offset)
|
var a = buffer.readUInt32LE(offset)
|
||||||
var b = buffer.readUInt32LE(offset + 4)
|
var b = buffer.readUInt32LE(offset + 4)
|
||||||
|
@ -62,37 +63,6 @@ function readUInt64LE (buffer, offset) {
|
||||||
return b + a
|
return b + a
|
||||||
}
|
}
|
||||||
|
|
||||||
function readVarInt (buffer, offset) {
|
|
||||||
var t = buffer.readUInt8(offset)
|
|
||||||
var number, size
|
|
||||||
|
|
||||||
// 8 bit
|
|
||||||
if (t < 253) {
|
|
||||||
number = t
|
|
||||||
size = 1
|
|
||||||
|
|
||||||
// 16 bit
|
|
||||||
} else if (t < 254) {
|
|
||||||
number = buffer.readUInt16LE(offset + 1)
|
|
||||||
size = 3
|
|
||||||
|
|
||||||
// 32 bit
|
|
||||||
} else if (t < 255) {
|
|
||||||
number = buffer.readUInt32LE(offset + 1)
|
|
||||||
size = 5
|
|
||||||
|
|
||||||
// 64 bit
|
|
||||||
} else {
|
|
||||||
number = readUInt64LE(buffer, offset + 1)
|
|
||||||
size = 9
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
number: number,
|
|
||||||
size: size
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function writePushDataInt (buffer, number, offset) {
|
function writePushDataInt (buffer, number, offset) {
|
||||||
var size = pushDataSize(number)
|
var size = pushDataSize(number)
|
||||||
|
|
||||||
|
@ -126,45 +96,20 @@ function writeUInt64LE (buffer, value, offset) {
|
||||||
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4)
|
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
function varIntSize (i) {
|
// TODO: remove in 4.0.0?
|
||||||
return i < 253 ? 1
|
function readVarInt (buffer, offset) {
|
||||||
: i < 0x10000 ? 3
|
var result = varuint.decode(buffer, offset)
|
||||||
: i < 0x100000000 ? 5
|
|
||||||
: 9
|
|
||||||
}
|
|
||||||
|
|
||||||
function writeVarInt (buffer, number, offset) {
|
return {
|
||||||
var size = varIntSize(number)
|
number: result,
|
||||||
|
size: varuint.decode.bytes
|
||||||
// 8 bit
|
|
||||||
if (size === 1) {
|
|
||||||
buffer.writeUInt8(number, offset)
|
|
||||||
|
|
||||||
// 16 bit
|
|
||||||
} else if (size === 3) {
|
|
||||||
buffer.writeUInt8(253, offset)
|
|
||||||
buffer.writeUInt16LE(number, offset + 1)
|
|
||||||
|
|
||||||
// 32 bit
|
|
||||||
} else if (size === 5) {
|
|
||||||
buffer.writeUInt8(254, offset)
|
|
||||||
buffer.writeUInt32LE(number, offset + 1)
|
|
||||||
|
|
||||||
// 64 bit
|
|
||||||
} else {
|
|
||||||
buffer.writeUInt8(255, offset)
|
|
||||||
writeUInt64LE(buffer, number, offset + 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return size
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function varIntBuffer (i) {
|
// TODO: remove in 4.0.0?
|
||||||
var size = varIntSize(i)
|
function writeVarInt (buffer, number, offset) {
|
||||||
var buffer = new Buffer(size)
|
varuint.encode(number, buffer, offset)
|
||||||
writeVarInt(buffer, i, 0)
|
return varuint.encode.bytes
|
||||||
|
|
||||||
return buffer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -172,13 +117,9 @@ module.exports = {
|
||||||
readPushDataInt: readPushDataInt,
|
readPushDataInt: readPushDataInt,
|
||||||
readUInt64LE: readUInt64LE,
|
readUInt64LE: readUInt64LE,
|
||||||
readVarInt: readVarInt,
|
readVarInt: readVarInt,
|
||||||
varIntBuffer: varIntBuffer,
|
varIntBuffer: varuint.encode,
|
||||||
varIntSize: varIntSize,
|
varIntSize: varuint.encodingLength,
|
||||||
writePushDataInt: writePushDataInt,
|
writePushDataInt: writePushDataInt,
|
||||||
writeUInt64LE: writeUInt64LE,
|
writeUInt64LE: writeUInt64LE,
|
||||||
writeVarInt: writeVarInt,
|
writeVarInt: writeVarInt
|
||||||
|
|
||||||
// TODO: remove in 3.0.0
|
|
||||||
equal: function BufferEquals (a, b) { return a.equals(b) },
|
|
||||||
reverse: require('buffer-reverse')
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,25 +6,6 @@ var bufferutils = require('../src/bufferutils')
|
||||||
var fixtures = require('./fixtures/bufferutils.json')
|
var fixtures = require('./fixtures/bufferutils.json')
|
||||||
|
|
||||||
describe('bufferutils', function () {
|
describe('bufferutils', function () {
|
||||||
// TODO: remove
|
|
||||||
describe('equals', function () {
|
|
||||||
it('works', function () {
|
|
||||||
var a = new Buffer('abcd', 'hex')
|
|
||||||
var b = new Buffer('bbbb', 'hex')
|
|
||||||
|
|
||||||
assert.strictEqual(bufferutils.equal(a, a), true)
|
|
||||||
assert.strictEqual(bufferutils.equal(a, b), false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('reverse', function () {
|
|
||||||
it('works', function () {
|
|
||||||
var a = new Buffer('abcd', 'hex')
|
|
||||||
|
|
||||||
assert.strictEqual(bufferutils.reverse(a).toString('hex'), 'cdab')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('pushDataSize', function () {
|
describe('pushDataSize', function () {
|
||||||
fixtures.valid.forEach(function (f) {
|
fixtures.valid.forEach(function (f) {
|
||||||
it('determines the pushDataSize of ' + f.dec + ' correctly', function () {
|
it('determines the pushDataSize of ' + f.dec + ' correctly', function () {
|
||||||
|
|
4
test/fixtures/bufferutils.json
vendored
4
test/fixtures/bufferutils.json
vendored
|
@ -86,14 +86,14 @@
|
||||||
"readUInt64LE": [
|
"readUInt64LE": [
|
||||||
{
|
{
|
||||||
"description": "n === 2^53",
|
"description": "n === 2^53",
|
||||||
"exception": "value is larger than maximum value for type",
|
"exception": "RangeError: value out of range",
|
||||||
"hex64": "0000000000002000",
|
"hex64": "0000000000002000",
|
||||||
"hexVI": "ff0000000000000020",
|
"hexVI": "ff0000000000000020",
|
||||||
"dec": 9007199254740992
|
"dec": 9007199254740992
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "n > 2^53",
|
"description": "n > 2^53",
|
||||||
"exception": "value is larger than maximum value for type",
|
"exception": "RangeError: value out of range",
|
||||||
"hex64": "0100000000002000",
|
"hex64": "0100000000002000",
|
||||||
"hexVI": "ff0100000000000020",
|
"hexVI": "ff0100000000000020",
|
||||||
"dec": 9007199254740993
|
"dec": 9007199254740993
|
||||||
|
|
Loading…
Reference in a new issue