|
|
@ -1,12 +1,5 @@ |
|
|
|
var opcodes = require('./opcodes.json') |
|
|
|
|
|
|
|
// 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') |
|
|
|
} |
|
|
|
var varuint = require('varuint-bitcoin') |
|
|
|
|
|
|
|
function pushDataSize (i) { |
|
|
|
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) { |
|
|
|
var a = buffer.readUInt32LE(offset) |
|
|
|
var b = buffer.readUInt32LE(offset + 4) |
|
|
@ -62,37 +63,6 @@ function readUInt64LE (buffer, offset) { |
|
|
|
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) { |
|
|
|
var size = pushDataSize(number) |
|
|
|
|
|
|
@ -126,45 +96,20 @@ function writeUInt64LE (buffer, value, offset) { |
|
|
|
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4) |
|
|
|
} |
|
|
|
|
|
|
|
function varIntSize (i) { |
|
|
|
return i < 253 ? 1 |
|
|
|
: i < 0x10000 ? 3 |
|
|
|
: i < 0x100000000 ? 5 |
|
|
|
: 9 |
|
|
|
} |
|
|
|
|
|
|
|
function writeVarInt (buffer, number, offset) { |
|
|
|
var size = varIntSize(number) |
|
|
|
|
|
|
|
// 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) |
|
|
|
// TODO: remove in 4.0.0?
|
|
|
|
function readVarInt (buffer, offset) { |
|
|
|
var result = varuint.decode(buffer, offset) |
|
|
|
|
|
|
|
// 64 bit
|
|
|
|
} else { |
|
|
|
buffer.writeUInt8(255, offset) |
|
|
|
writeUInt64LE(buffer, number, offset + 1) |
|
|
|
return { |
|
|
|
number: result, |
|
|
|
size: varuint.decode.bytes |
|
|
|
} |
|
|
|
|
|
|
|
return size |
|
|
|
} |
|
|
|
|
|
|
|
function varIntBuffer (i) { |
|
|
|
var size = varIntSize(i) |
|
|
|
var buffer = new Buffer(size) |
|
|
|
writeVarInt(buffer, i, 0) |
|
|
|
|
|
|
|
return buffer |
|
|
|
// TODO: remove in 4.0.0?
|
|
|
|
function writeVarInt (buffer, number, offset) { |
|
|
|
varuint.encode(number, buffer, offset) |
|
|
|
return varuint.encode.bytes |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = { |
|
|
@ -172,13 +117,9 @@ module.exports = { |
|
|
|
readPushDataInt: readPushDataInt, |
|
|
|
readUInt64LE: readUInt64LE, |
|
|
|
readVarInt: readVarInt, |
|
|
|
varIntBuffer: varIntBuffer, |
|
|
|
varIntSize: varIntSize, |
|
|
|
varIntBuffer: varuint.encode, |
|
|
|
varIntSize: varuint.encodingLength, |
|
|
|
writePushDataInt: writePushDataInt, |
|
|
|
writeUInt64LE: writeUInt64LE, |
|
|
|
writeVarInt: writeVarInt, |
|
|
|
|
|
|
|
// TODO: remove in 3.0.0
|
|
|
|
equal: function BufferEquals (a, b) { return a.equals(b) }, |
|
|
|
reverse: require('buffer-reverse') |
|
|
|
writeVarInt: writeVarInt |
|
|
|
} |
|
|
|