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
|
||||
|
||||
function varIntBuffer(i) {
|
||||
var ib = new Buffer(bufferutils.varIntSize(i))
|
||||
bufferutils.writeVarInt(ib, i, 0)
|
||||
return ib
|
||||
}
|
||||
|
||||
var txLenBuffer = varIntBuffer(this.transactions.length)
|
||||
var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length)
|
||||
var txBuffers = this.transactions.map(function(tx) {
|
||||
return tx.toBuffer()
|
||||
})
|
||||
|
|
|
@ -159,6 +159,14 @@ function writeVarInt(buffer, number, offset) {
|
|||
return size
|
||||
}
|
||||
|
||||
function varIntBuffer(i) {
|
||||
var size = varIntSize(i)
|
||||
var buffer = new Buffer(size)
|
||||
writeVarInt(buffer, i, 0)
|
||||
|
||||
return buffer
|
||||
}
|
||||
|
||||
function reverse(buffer) {
|
||||
var buffer2 = new Buffer(buffer)
|
||||
Array.prototype.reverse.call(buffer2)
|
||||
|
@ -171,6 +179,7 @@ module.exports = {
|
|||
readUInt64LE: readUInt64LE,
|
||||
readVarInt: readVarInt,
|
||||
reverse: reverse,
|
||||
varIntBuffer: varIntBuffer,
|
||||
varIntSize: varIntSize,
|
||||
writePushDataInt: writePushDataInt,
|
||||
writeUInt64LE: writeUInt64LE,
|
||||
|
|
|
@ -13,8 +13,7 @@ var ecparams = ecurve.getCurveByName('secp256k1')
|
|||
function magicHash(message, network) {
|
||||
var magicPrefix = new Buffer(network.magicPrefix)
|
||||
var messageBuffer = new Buffer(message)
|
||||
var lengthBuffer = new Buffer(bufferutils.varIntSize(messageBuffer.length))
|
||||
bufferutils.writeVarInt(lengthBuffer, messageBuffer.length, 0)
|
||||
var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length)
|
||||
|
||||
var buffer = Buffer.concat([magicPrefix, lengthBuffer, messageBuffer])
|
||||
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() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
it('determines the varIntSize of ' + f.dec + ' correctly', function() {
|
||||
|
@ -99,7 +109,7 @@ describe('bufferutils', function() {
|
|||
})
|
||||
|
||||
describe('writePushDataInt', function() {
|
||||
fixtures.valid.forEach(function(f, i) {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
if (!f.hexPD) return
|
||||
|
||||
it('encodes ' + f.dec + ' correctly', function() {
|
||||
|
|
Loading…
Reference in a new issue