bufferutils: add varIntBuffer
This commit is contained in:
parent
c0c47f076a
commit
2214ccfdd8
4 changed files with 22 additions and 10 deletions
|
@ -120,13 +120,7 @@ Block.prototype.toBuffer = function(headersOnly) {
|
||||||
|
|
||||||
if (headersOnly || !this.transactions) return buffer
|
if (headersOnly || !this.transactions) return buffer
|
||||||
|
|
||||||
function varIntBuffer(i) {
|
var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length)
|
||||||
var ib = new Buffer(bufferutils.varIntSize(i))
|
|
||||||
bufferutils.writeVarInt(ib, i, 0)
|
|
||||||
return ib
|
|
||||||
}
|
|
||||||
|
|
||||||
var txLenBuffer = varIntBuffer(this.transactions.length)
|
|
||||||
var txBuffers = this.transactions.map(function(tx) {
|
var txBuffers = this.transactions.map(function(tx) {
|
||||||
return tx.toBuffer()
|
return tx.toBuffer()
|
||||||
})
|
})
|
||||||
|
|
|
@ -159,6 +159,14 @@ function writeVarInt(buffer, number, offset) {
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function varIntBuffer(i) {
|
||||||
|
var size = varIntSize(i)
|
||||||
|
var buffer = new Buffer(size)
|
||||||
|
writeVarInt(buffer, i, 0)
|
||||||
|
|
||||||
|
return buffer
|
||||||
|
}
|
||||||
|
|
||||||
function reverse(buffer) {
|
function reverse(buffer) {
|
||||||
var buffer2 = new Buffer(buffer)
|
var buffer2 = new Buffer(buffer)
|
||||||
Array.prototype.reverse.call(buffer2)
|
Array.prototype.reverse.call(buffer2)
|
||||||
|
@ -171,6 +179,7 @@ module.exports = {
|
||||||
readUInt64LE: readUInt64LE,
|
readUInt64LE: readUInt64LE,
|
||||||
readVarInt: readVarInt,
|
readVarInt: readVarInt,
|
||||||
reverse: reverse,
|
reverse: reverse,
|
||||||
|
varIntBuffer: varIntBuffer,
|
||||||
varIntSize: varIntSize,
|
varIntSize: varIntSize,
|
||||||
writePushDataInt: writePushDataInt,
|
writePushDataInt: writePushDataInt,
|
||||||
writeUInt64LE: writeUInt64LE,
|
writeUInt64LE: writeUInt64LE,
|
||||||
|
|
|
@ -13,8 +13,7 @@ var ecparams = ecurve.getCurveByName('secp256k1')
|
||||||
function magicHash(message, network) {
|
function magicHash(message, network) {
|
||||||
var magicPrefix = new Buffer(network.magicPrefix)
|
var magicPrefix = new Buffer(network.magicPrefix)
|
||||||
var messageBuffer = new Buffer(message)
|
var messageBuffer = new Buffer(message)
|
||||||
var lengthBuffer = new Buffer(bufferutils.varIntSize(messageBuffer.length))
|
var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length)
|
||||||
bufferutils.writeVarInt(lengthBuffer, messageBuffer.length, 0)
|
|
||||||
|
|
||||||
var buffer = Buffer.concat([magicPrefix, lengthBuffer, messageBuffer])
|
var buffer = Buffer.concat([magicPrefix, lengthBuffer, messageBuffer])
|
||||||
return crypto.hash256(buffer)
|
return crypto.hash256(buffer)
|
||||||
|
|
|
@ -88,6 +88,16 @@ describe('bufferutils', function() {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('varIntBuffer', function() {
|
||||||
|
fixtures.valid.forEach(function(f) {
|
||||||
|
it('encodes ' + f.dec + ' correctly', function() {
|
||||||
|
var buffer = bufferutils.varIntBuffer(f.dec)
|
||||||
|
|
||||||
|
assert.equal(buffer.toString('hex'), f.hexVI)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('varIntSize', function() {
|
describe('varIntSize', function() {
|
||||||
fixtures.valid.forEach(function(f) {
|
fixtures.valid.forEach(function(f) {
|
||||||
it('determines the varIntSize of ' + f.dec + ' correctly', function() {
|
it('determines the varIntSize of ' + f.dec + ' correctly', function() {
|
||||||
|
@ -99,7 +109,7 @@ describe('bufferutils', function() {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('writePushDataInt', function() {
|
describe('writePushDataInt', function() {
|
||||||
fixtures.valid.forEach(function(f, i) {
|
fixtures.valid.forEach(function(f) {
|
||||||
if (!f.hexPD) return
|
if (!f.hexPD) return
|
||||||
|
|
||||||
it('encodes ' + f.dec + ' correctly', function() {
|
it('encodes ' + f.dec + ' correctly', function() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue