BufferExt: rename to bufferutils
This commit is contained in:
parent
7a740c2e7b
commit
05e0d08098
5 changed files with 14 additions and 101 deletions
|
@ -6,7 +6,7 @@ module.exports = {
|
||||||
Address: require('./address'),
|
Address: require('./address'),
|
||||||
base58: require('./base58'),
|
base58: require('./base58'),
|
||||||
base58check: require('./base58check'),
|
base58check: require('./base58check'),
|
||||||
BufferExt: require('./buffer'),
|
bufferutils: require('./bufferutils'),
|
||||||
convert: require('./convert'),
|
convert: require('./convert'),
|
||||||
crypto: require('./crypto'),
|
crypto: require('./crypto'),
|
||||||
ec: ec,
|
ec: ec,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
/// Implements Bitcoin's feature for signing arbitrary messages.
|
/// Implements Bitcoin's feature for signing arbitrary messages.
|
||||||
|
|
||||||
var Address = require('./address')
|
var Address = require('./address')
|
||||||
var BufferExt = require('./buffer')
|
var bufferutils = require('./bufferutils')
|
||||||
var crypto = require('./crypto')
|
var crypto = require('./crypto')
|
||||||
var ecdsa = require('./ecdsa')
|
var ecdsa = require('./ecdsa')
|
||||||
var ECPubKey = require('./eckey').ECPubKey
|
var ECPubKey = require('./eckey').ECPubKey
|
||||||
|
@ -11,8 +10,8 @@ var MAGIC_PREFIX = new Buffer('\x18Bitcoin Signed Message:\n')
|
||||||
|
|
||||||
function magicHash(message) {
|
function magicHash(message) {
|
||||||
var messageBuffer = new Buffer(message)
|
var messageBuffer = new Buffer(message)
|
||||||
var lengthBuffer = new Buffer(BufferExt.varIntSize(messageBuffer.length))
|
var lengthBuffer = new Buffer(bufferutils.varIntSize(messageBuffer.length))
|
||||||
BufferExt.writeVarInt(lengthBuffer, messageBuffer.length, 0)
|
bufferutils.writeVarInt(lengthBuffer, messageBuffer.length, 0)
|
||||||
|
|
||||||
var buffer = Buffer.concat([
|
var buffer = Buffer.concat([
|
||||||
MAGIC_PREFIX, lengthBuffer, messageBuffer
|
MAGIC_PREFIX, lengthBuffer, messageBuffer
|
||||||
|
|
|
@ -3,14 +3,14 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var Address = require('./address')
|
var Address = require('./address')
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var BufferExt = require('./buffer')
|
var bufferutils = require('./bufferutils')
|
||||||
var Script = require('./script')
|
var Script = require('./script')
|
||||||
var convert = require('./convert')
|
var convert = require('./convert')
|
||||||
var crypto = require('./crypto')
|
var crypto = require('./crypto')
|
||||||
var ECKey = require('./eckey').ECKey
|
var ECKey = require('./eckey').ECKey
|
||||||
var ecdsa = require('./ecdsa')
|
var ecdsa = require('./ecdsa')
|
||||||
|
|
||||||
var Transaction = function (doc) {
|
function Transaction(doc) {
|
||||||
if (!(this instanceof Transaction)) { return new Transaction(doc) }
|
if (!(this instanceof Transaction)) { return new Transaction(doc) }
|
||||||
this.version = 1
|
this.version = 1
|
||||||
this.locktime = 0
|
this.locktime = 0
|
||||||
|
@ -122,17 +122,17 @@ Transaction.prototype.addOutput = function (address, value) {
|
||||||
*/
|
*/
|
||||||
Transaction.prototype.serialize = function () {
|
Transaction.prototype.serialize = function () {
|
||||||
var txInSize = this.ins.reduce(function(a, x) {
|
var txInSize = this.ins.reduce(function(a, x) {
|
||||||
return a + (40 + BufferExt.varIntSize(x.script.buffer.length) + x.script.buffer.length)
|
return a + (40 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length)
|
||||||
}, 0)
|
}, 0)
|
||||||
|
|
||||||
var txOutSize = this.outs.reduce(function(a, x) {
|
var txOutSize = this.outs.reduce(function(a, x) {
|
||||||
return a + (8 + BufferExt.varIntSize(x.script.buffer.length) + x.script.buffer.length)
|
return a + (8 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length)
|
||||||
}, 0)
|
}, 0)
|
||||||
|
|
||||||
var buffer = new Buffer(
|
var buffer = new Buffer(
|
||||||
8 +
|
8 +
|
||||||
BufferExt.varIntSize(this.ins.length) +
|
bufferutils.varIntSize(this.ins.length) +
|
||||||
BufferExt.varIntSize(this.outs.length) +
|
bufferutils.varIntSize(this.outs.length) +
|
||||||
txInSize +
|
txInSize +
|
||||||
txOutSize
|
txOutSize
|
||||||
)
|
)
|
||||||
|
@ -148,11 +148,11 @@ Transaction.prototype.serialize = function () {
|
||||||
offset += 4
|
offset += 4
|
||||||
}
|
}
|
||||||
function writeUInt64(i) {
|
function writeUInt64(i) {
|
||||||
BufferExt.writeUInt64LE(buffer, i, offset)
|
bufferutils.writeUInt64LE(buffer, i, offset)
|
||||||
offset += 8
|
offset += 8
|
||||||
}
|
}
|
||||||
function writeVarInt(i) {
|
function writeVarInt(i) {
|
||||||
var n = BufferExt.writeVarInt(buffer, i, offset)
|
var n = bufferutils.writeVarInt(buffer, i, offset)
|
||||||
offset += n
|
offset += n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,12 +294,12 @@ Transaction.deserialize = function(buffer) {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
function readUInt64() {
|
function readUInt64() {
|
||||||
var i = BufferExt.readUInt64LE(buffer, offset)
|
var i = bufferutils.readUInt64LE(buffer, offset)
|
||||||
offset += 8
|
offset += 8
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
function readVarInt() {
|
function readVarInt() {
|
||||||
var vi = BufferExt.readVarInt(buffer, offset)
|
var vi = bufferutils.readVarInt(buffer, offset)
|
||||||
offset += vi.size
|
offset += vi.size
|
||||||
return vi.number
|
return vi.number
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,86 +0,0 @@
|
||||||
var assert = require('assert')
|
|
||||||
var BufferExt = require('../').BufferExt
|
|
||||||
|
|
||||||
var fixtures = require('./fixtures/buffer.js')
|
|
||||||
|
|
||||||
describe('Buffer Extensions', function() {
|
|
||||||
describe('readUInt64LE', function() {
|
|
||||||
it('matches test vectors', function() {
|
|
||||||
fixtures.valid.forEach(function(f) {
|
|
||||||
var buffer = new Buffer(f.hex64, 'hex')
|
|
||||||
var number = BufferExt.readUInt64LE(buffer, 0)
|
|
||||||
|
|
||||||
assert.equal(number, f.dec)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('readVarInt', function() {
|
|
||||||
it('matches test vectors', function() {
|
|
||||||
fixtures.valid.forEach(function(f) {
|
|
||||||
var buffer = new Buffer(f.hexVI, 'hex')
|
|
||||||
var d = BufferExt.readVarInt(buffer, 0)
|
|
||||||
|
|
||||||
assert.equal(d.number, f.dec)
|
|
||||||
assert.equal(d.size, buffer.length)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('varIntSize', function() {
|
|
||||||
it('matches test vectors', function() {
|
|
||||||
fixtures.valid.forEach(function(f) {
|
|
||||||
var number = parseInt(f.dec)
|
|
||||||
var size = BufferExt.varIntSize(number)
|
|
||||||
|
|
||||||
assert.equal(size, f.hexVI.length / 2)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('writeUInt64LE', function() {
|
|
||||||
it('matches test vectors', function() {
|
|
||||||
fixtures.valid.forEach(function(f) {
|
|
||||||
var buffer = new Buffer(8)
|
|
||||||
buffer.fill(0)
|
|
||||||
|
|
||||||
BufferExt.writeUInt64LE(buffer, f.dec, 0)
|
|
||||||
assert.equal(buffer.toString('hex'), f.hex64)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
fixtures.invalid.forEach(function(f) {
|
|
||||||
it('throws on ' + f.description, function() {
|
|
||||||
assert.throws(function() {
|
|
||||||
var buffer = new Buffer(8)
|
|
||||||
buffer.fill(0)
|
|
||||||
|
|
||||||
BufferExt.writeUInt64LE(buffer, f.dec, 0)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('writeVarInt', function() {
|
|
||||||
it('matches test vectors', function() {
|
|
||||||
fixtures.valid.forEach(function(f) {
|
|
||||||
var buffer = new Buffer(9)
|
|
||||||
buffer.fill(0)
|
|
||||||
|
|
||||||
var n = BufferExt.writeVarInt(buffer, f.dec, 0)
|
|
||||||
assert.equal(buffer.slice(0, n).toString('hex'), f.hexVI)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
fixtures.invalid.forEach(function(f) {
|
|
||||||
it('throws on ' + f.description, function() {
|
|
||||||
assert.throws(function() {
|
|
||||||
var buffer = new Buffer(9)
|
|
||||||
buffer.fill(0)
|
|
||||||
|
|
||||||
BufferExt.writeVarInt(buffer, f.dec, 0)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
Loading…
Reference in a new issue