Browse Source

Merge branch 'micahriggan-feature/buffer-performance'

patch-2
Justin Langston 7 years ago
parent
commit
026ddb4d39
No known key found for this signature in database GPG Key ID: EBB3714C72F9FE5D
  1. 2
      benchmark/script.js
  2. 9
      lib/address.js
  3. 6
      lib/block/block.js
  4. 8
      lib/block/blockheader.js
  5. 6
      lib/block/merkleblock.js
  6. 16
      lib/crypto/bn.js
  7. 12
      lib/crypto/ecdsa.js
  8. 10
      lib/crypto/hash.js
  9. 4
      lib/crypto/point.js
  10. 4
      lib/crypto/random.js
  11. 12
      lib/crypto/signature.js
  12. 2
      lib/encoding/base58.js
  13. 4
      lib/encoding/base58check.js
  14. 4
      lib/encoding/bufferreader.js
  15. 32
      lib/encoding/bufferwriter.js
  16. 2
      lib/encoding/varint.js
  17. 2
      lib/hdprivatekey.js
  18. 2
      lib/hdpublickey.js
  19. 2
      lib/opcode.js
  20. 8
      lib/privatekey.js
  21. 10
      lib/publickey.js
  22. 4
      lib/script/interpreter.js
  23. 16
      lib/script/script.js
  24. 2
      lib/transaction/sighash.js
  25. 2
      lib/transaction/signature.js
  26. 5
      lib/transaction/transaction.js
  27. 8
      lib/util/buffer.js

2
benchmark/script.js

@ -5,7 +5,7 @@ var bitcore = require('..');
var async = require('async'); var async = require('async');
var blockData = require('./block-357238.json'); var blockData = require('./block-357238.json');
var maxTime = 10; var maxTime = 30;
console.log('Benchmarking Script'); console.log('Benchmarking Script');
console.log('---------------------------------------'); console.log('---------------------------------------');

9
lib/address.js

@ -146,7 +146,7 @@ Address._transformObject = function(data) {
$.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property'); $.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property');
$.checkArgument(data.type, 'Must provide a `type` property'); $.checkArgument(data.type, 'Must provide a `type` property');
return { return {
hashBuffer: data.hash ? new Buffer(data.hash, 'hex') : data.hashBuffer, hashBuffer: data.hash ? Buffer.from(data.hash, 'hex') : data.hashBuffer,
network: Networks.get(data.network) || Networks.defaultNetwork, network: Networks.get(data.network) || Networks.defaultNetwork,
type: data.type type: data.type
}; };
@ -399,7 +399,7 @@ Address.fromObject = function fromObject(obj) {
JSUtil.isHexa(obj.hash), JSUtil.isHexa(obj.hash),
'Unexpected hash property, "' + obj.hash + '", expected to be hex.' 'Unexpected hash property, "' + obj.hash + '", expected to be hex.'
); );
var hashBuffer = new Buffer(obj.hash, 'hex'); var hashBuffer = Buffer.from(obj.hash, 'hex');
return new Address(hashBuffer, obj.network, obj.type); return new Address(hashBuffer, obj.network, obj.type);
}; };
@ -467,9 +467,8 @@ Address.prototype.isPayToScriptHash = function() {
* @returns {Buffer} Bitcoin address buffer * @returns {Buffer} Bitcoin address buffer
*/ */
Address.prototype.toBuffer = function() { Address.prototype.toBuffer = function() {
var version = new Buffer([this.network[this.type]]); var version = Buffer.from([this.network[this.type]]);
var buf = Buffer.concat([version, this.hashBuffer]); return Buffer.concat([version, this.hashBuffer]);
return buf;
}; };
/** /**

6
lib/block/block.js

@ -117,7 +117,7 @@ Block.fromBuffer = function fromBuffer(buf) {
* @returns {Block} - A hex encoded string of the block * @returns {Block} - A hex encoded string of the block
*/ */
Block.fromString = function fromString(str) { Block.fromString = function fromString(str) {
var buf = new Buffer(str, 'hex'); var buf = Buffer.from(str, 'hex');
return Block.fromBuffer(buf); return Block.fromBuffer(buf);
}; };
@ -127,7 +127,7 @@ Block.fromString = function fromString(str) {
*/ */
Block.fromRawBlock = function fromRawBlock(data) { Block.fromRawBlock = function fromRawBlock(data) {
if (!BufferUtil.isBuffer(data)) { if (!BufferUtil.isBuffer(data)) {
data = new Buffer(data, 'binary'); data = Buffer.from(data, 'binary');
} }
var br = BufferReader(data); var br = BufferReader(data);
br.pos = Block.Values.START_OF_BLOCK; br.pos = Block.Values.START_OF_BLOCK;
@ -275,7 +275,7 @@ Block.prototype.inspect = function inspect() {
Block.Values = { Block.Values = {
START_OF_BLOCK: 8, // Start of block in raw block data START_OF_BLOCK: 8, // Start of block in raw block data
NULL_HASH: new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex') NULL_HASH: Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
}; };
module.exports = Block; module.exports = Block;

8
lib/block/blockheader.js

@ -70,10 +70,10 @@ BlockHeader._fromObject = function _fromObject(data) {
var prevHash = data.prevHash; var prevHash = data.prevHash;
var merkleRoot = data.merkleRoot; var merkleRoot = data.merkleRoot;
if (_.isString(data.prevHash)) { if (_.isString(data.prevHash)) {
prevHash = BufferUtil.reverse(new Buffer(data.prevHash, 'hex')); prevHash = BufferUtil.reverse(Buffer.from(data.prevHash, 'hex'));
} }
if (_.isString(data.merkleRoot)) { if (_.isString(data.merkleRoot)) {
merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex')); merkleRoot = BufferUtil.reverse(Buffer.from(data.merkleRoot, 'hex'));
} }
var info = { var info = {
hash: data.hash, hash: data.hash,
@ -103,7 +103,7 @@ BlockHeader.fromObject = function fromObject(obj) {
*/ */
BlockHeader.fromRawBlock = function fromRawBlock(data) { BlockHeader.fromRawBlock = function fromRawBlock(data) {
if (!BufferUtil.isBuffer(data)) { if (!BufferUtil.isBuffer(data)) {
data = new Buffer(data, 'binary'); data = Buffer.from(data, 'binary');
} }
var br = BufferReader(data); var br = BufferReader(data);
br.pos = BlockHeader.Constants.START_OF_HEADER; br.pos = BlockHeader.Constants.START_OF_HEADER;
@ -125,7 +125,7 @@ BlockHeader.fromBuffer = function fromBuffer(buf) {
* @returns {BlockHeader} - An instance of block header * @returns {BlockHeader} - An instance of block header
*/ */
BlockHeader.fromString = function fromString(str) { BlockHeader.fromString = function fromString(str) {
var buf = new Buffer(str, 'hex'); var buf = Buffer.from(str, 'hex');
return BlockHeader.fromBuffer(buf); return BlockHeader.fromBuffer(buf);
}; };

6
lib/block/merkleblock.js

@ -103,7 +103,7 @@ MerkleBlock.prototype.toBufferWriter = function toBufferWriter(bw) {
bw.writeUInt32LE(this.numTransactions); bw.writeUInt32LE(this.numTransactions);
bw.writeVarintNum(this.hashes.length); bw.writeVarintNum(this.hashes.length);
for (var i = 0; i < this.hashes.length; i++) { for (var i = 0; i < this.hashes.length; i++) {
bw.write(new Buffer(this.hashes[i], 'hex')); bw.write(Buffer.from(this.hashes[i], 'hex'));
} }
bw.writeVarintNum(this.flags.length); bw.writeVarintNum(this.flags.length);
for (i = 0; i < this.flags.length; i++) { for (i = 0; i < this.flags.length; i++) {
@ -219,7 +219,7 @@ MerkleBlock.prototype._traverseMerkleTree = function traverseMerkleTree(depth, p
if(depth === 0 && isParentOfMatch) { if(depth === 0 && isParentOfMatch) {
opts.txs.push(hash); opts.txs.push(hash);
} }
return new Buffer(hash, 'hex'); return Buffer.from(hash, 'hex');
} else { } else {
var left = this._traverseMerkleTree(depth-1, pos*2, opts); var left = this._traverseMerkleTree(depth-1, pos*2, opts);
var right = left; var right = left;
@ -270,7 +270,7 @@ MerkleBlock.prototype.hasTransaction = function hasTransaction(tx) {
var hash = tx; var hash = tx;
if(tx instanceof Transaction) { if(tx instanceof Transaction) {
// We need to reverse the id hash for the lookup // We need to reverse the id hash for the lookup
hash = BufferUtil.reverse(new Buffer(tx.id, 'hex')).toString('hex'); hash = BufferUtil.reverse(Buffer.from(tx.id, 'hex')).toString('hex');
} }
var txs = []; var txs = [];

16
lib/crypto/bn.js

@ -5,7 +5,7 @@ var $ = require('../util/preconditions');
var _ = require('lodash'); var _ = require('lodash');
var reversebuf = function(buf) { var reversebuf = function(buf) {
var buf2 = new Buffer(buf.length); var buf2 = Buffer.alloc(buf.length);
for (var i = 0; i < buf.length; i++) { for (var i = 0; i < buf.length; i++) {
buf2[i] = buf[buf.length - 1 - i]; buf2[i] = buf[buf.length - 1 - i];
} }
@ -42,7 +42,7 @@ BN.fromBuffer = function(buf, opts) {
BN.fromSM = function(buf, opts) { BN.fromSM = function(buf, opts) {
var ret; var ret;
if (buf.length === 0) { if (buf.length === 0) {
return BN.fromBuffer(new Buffer([0])); return BN.fromBuffer(Buffer.from([0]));
} }
var endian = 'big'; var endian = 'big';
@ -73,7 +73,7 @@ BN.prototype.toBuffer = function(opts) {
if (opts && opts.size) { if (opts && opts.size) {
hex = this.toString(16, 2); hex = this.toString(16, 2);
var natlen = hex.length / 2; var natlen = hex.length / 2;
buf = new Buffer(hex, 'hex'); buf = Buffer.from(hex, 'hex');
if (natlen === opts.size) { if (natlen === opts.size) {
buf = buf; buf = buf;
@ -84,7 +84,7 @@ BN.prototype.toBuffer = function(opts) {
} }
} else { } else {
hex = this.toString(16, 2); hex = this.toString(16, 2);
buf = new Buffer(hex, 'hex'); buf = Buffer.from(hex, 'hex');
} }
if (typeof opts !== 'undefined' && opts.endian === 'little') { if (typeof opts !== 'undefined' && opts.endian === 'little') {
@ -99,19 +99,19 @@ BN.prototype.toSMBigEndian = function() {
if (this.cmp(BN.Zero) === -1) { if (this.cmp(BN.Zero) === -1) {
buf = this.neg().toBuffer(); buf = this.neg().toBuffer();
if (buf[0] & 0x80) { if (buf[0] & 0x80) {
buf = Buffer.concat([new Buffer([0x80]), buf]); buf = Buffer.concat([Buffer.from([0x80]), buf]);
} else { } else {
buf[0] = buf[0] | 0x80; buf[0] = buf[0] | 0x80;
} }
} else { } else {
buf = this.toBuffer(); buf = this.toBuffer();
if (buf[0] & 0x80) { if (buf[0] & 0x80) {
buf = Buffer.concat([new Buffer([0x00]), buf]); buf = Buffer.concat([Buffer.from([0x00]), buf]);
} }
} }
if (buf.length === 1 & buf[0] === 0) { if (buf.length === 1 & buf[0] === 0) {
buf = new Buffer([]); buf = Buffer.from([]);
} }
return buf; return buf;
}; };
@ -189,7 +189,7 @@ BN.trim = function(buf, natlen) {
}; };
BN.pad = function(buf, natlen, size) { BN.pad = function(buf, natlen, size) {
var rbuf = new Buffer(size); var rbuf = Buffer.alloc(size);
for (var i = 0; i < buf.length; i++) { for (var i = 0; i < buf.length; i++) {
rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i]; rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i];
} }

12
lib/crypto/ecdsa.js

@ -81,17 +81,17 @@ ECDSA.prototype.deterministicK = function(badrs) {
if (_.isUndefined(badrs)) { if (_.isUndefined(badrs)) {
badrs = 0; badrs = 0;
} }
var v = new Buffer(32); var v = Buffer.alloc(32);
v.fill(0x01); v.fill(0x01);
var k = new Buffer(32); var k = Buffer.alloc(32);
k.fill(0x00); k.fill(0x00);
var x = this.privkey.bn.toBuffer({ var x = this.privkey.bn.toBuffer({
size: 32 size: 32
}); });
var hashbuf = this.endian === 'little' ? BufferUtil.reverse(this.hashbuf) : this.hashbuf var hashbuf = this.endian === 'little' ? BufferUtil.reverse(this.hashbuf) : this.hashbuf
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x00]), x, hashbuf]), k); k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x00]), x, hashbuf]), k);
v = Hash.sha256hmac(v, k); v = Hash.sha256hmac(v, k);
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x01]), x, hashbuf]), k); k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x01]), x, hashbuf]), k);
v = Hash.sha256hmac(v, k); v = Hash.sha256hmac(v, k);
v = Hash.sha256hmac(v, k); v = Hash.sha256hmac(v, k);
var T = BN.fromBuffer(v); var T = BN.fromBuffer(v);
@ -99,7 +99,7 @@ ECDSA.prototype.deterministicK = function(badrs) {
// also explained in 3.2, we must ensure T is in the proper range (0, N) // also explained in 3.2, we must ensure T is in the proper range (0, N)
for (var i = 0; i < badrs || !(T.lt(N) && T.gt(BN.Zero)); i++) { for (var i = 0; i < badrs || !(T.lt(N) && T.gt(BN.Zero)); i++) {
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x00])]), k); k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x00])]), k);
v = Hash.sha256hmac(v, k); v = Hash.sha256hmac(v, k);
v = Hash.sha256hmac(v, k); v = Hash.sha256hmac(v, k);
T = BN.fromBuffer(v); T = BN.fromBuffer(v);
@ -192,7 +192,7 @@ ECDSA.prototype.sigError = function() {
ECDSA.toLowS = function(s) { ECDSA.toLowS = function(s) {
//enforce low s //enforce low s
//see BIP 62, "low S values in signatures" //see BIP 62, "low S values in signatures"
if (s.gt(BN.fromBuffer(new Buffer('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex')))) { if (s.gt(BN.fromBuffer(Buffer.from('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex')))) {
s = Point.getN().sub(s); s = Point.getN().sub(s);
} }
return s; return s;

10
lib/crypto/hash.js

@ -54,20 +54,20 @@ Hash.hmac = function(hashf, data, key) {
if (key.length > blocksize) { if (key.length > blocksize) {
key = hashf(key); key = hashf(key);
} else if (key < blocksize) { } else if (key < blocksize) {
var fill = new Buffer(blocksize); var fill = Buffer.alloc(blocksize);
fill.fill(0); fill.fill(0);
key.copy(fill); key.copy(fill);
key = fill; key = fill;
} }
var o_key = new Buffer(blocksize); var o_key = Buffer.alloc(blocksize);
o_key.fill(0x5c); o_key.fill(0x5c);
var i_key = new Buffer(blocksize); var i_key = Buffer.alloc(blocksize);
i_key.fill(0x36); i_key.fill(0x36);
var o_key_pad = new Buffer(blocksize); var o_key_pad = Buffer.alloc(blocksize);
var i_key_pad = new Buffer(blocksize); var i_key_pad = Buffer.alloc(blocksize);
for (var i = 0; i < blocksize; i++) { for (var i = 0; i < blocksize; i++) {
o_key_pad[i] = o_key[i] ^ key[i]; o_key_pad[i] = o_key[i] ^ key[i];
i_key_pad[i] = i_key[i] ^ key[i]; i_key_pad[i] = i_key[i] ^ key[i];

4
lib/crypto/point.js

@ -140,9 +140,9 @@ Point.pointToCompressed = function pointToCompressed(point) {
var prefix; var prefix;
var odd = ybuf[ybuf.length - 1] % 2; var odd = ybuf[ybuf.length - 1] % 2;
if (odd) { if (odd) {
prefix = new Buffer([0x03]); prefix = Buffer.from([0x03]);
} else { } else {
prefix = new Buffer([0x02]); prefix = Buffer.from([0x02]);
} }
return BufferUtil.concat([prefix, xbuf]); return BufferUtil.concat([prefix, xbuf]);
}; };

4
lib/crypto/random.js

@ -29,7 +29,7 @@ Random.getRandomBufferBrowser = function(size) {
var bbuf = new Uint8Array(size); var bbuf = new Uint8Array(size);
crypto.getRandomValues(bbuf); crypto.getRandomValues(bbuf);
var buf = new Buffer(bbuf); var buf = Buffer.from(bbuf);
return buf; return buf;
}; };
@ -37,7 +37,7 @@ Random.getRandomBufferBrowser = function(size) {
/* insecure random bytes, but it never fails */ /* insecure random bytes, but it never fails */
Random.getPseudoRandomBuffer = function(size) { Random.getPseudoRandomBuffer = function(size) {
var b32 = 0x100000000; var b32 = 0x100000000;
var b = new Buffer(size); var b = Buffer.alloc(size);
var r; var r;
for (var i = 0; i <= size; i++) { for (var i = 0; i <= size; i++) {

12
lib/crypto/signature.js

@ -80,7 +80,7 @@ Signature.fromTxFormat = function(buf) {
}; };
Signature.fromString = function(str) { Signature.fromString = function(str) {
var buf = new Buffer(str, 'hex'); var buf = Buffer.from(str, 'hex');
return Signature.fromDER(buf); return Signature.fromDER(buf);
}; };
@ -155,7 +155,7 @@ Signature.prototype.toCompact = function(i, compressed) {
if (compressed === false) { if (compressed === false) {
val = val - 4; val = val - 4;
} }
var b1 = new Buffer([val]); var b1 = Buffer.from([val]);
var b2 = this.r.toBuffer({ var b2 = this.r.toBuffer({
size: 32 size: 32
}); });
@ -172,8 +172,8 @@ Signature.prototype.toBuffer = Signature.prototype.toDER = function() {
var rneg = rnbuf[0] & 0x80 ? true : false; var rneg = rnbuf[0] & 0x80 ? true : false;
var sneg = snbuf[0] & 0x80 ? true : false; var sneg = snbuf[0] & 0x80 ? true : false;
var rbuf = rneg ? Buffer.concat([new Buffer([0x00]), rnbuf]) : rnbuf; var rbuf = rneg ? Buffer.concat([Buffer.from([0x00]), rnbuf]) : rnbuf;
var sbuf = sneg ? Buffer.concat([new Buffer([0x00]), snbuf]) : snbuf; var sbuf = sneg ? Buffer.concat([Buffer.from([0x00]), snbuf]) : snbuf;
var rlength = rbuf.length; var rlength = rbuf.length;
var slength = sbuf.length; var slength = sbuf.length;
@ -182,7 +182,7 @@ Signature.prototype.toBuffer = Signature.prototype.toDER = function() {
var sheader = 0x02; var sheader = 0x02;
var header = 0x30; var header = 0x30;
var der = Buffer.concat([new Buffer([header, length, rheader, rlength]), rbuf, new Buffer([sheader, slength]), sbuf]); var der = Buffer.concat([Buffer.from([header, length, rheader, rlength]), rbuf, Buffer.from([sheader, slength]), sbuf]);
return der; return der;
}; };
@ -300,7 +300,7 @@ Signature.prototype.hasDefinedHashtype = function() {
Signature.prototype.toTxFormat = function() { Signature.prototype.toTxFormat = function() {
var derbuf = this.toDER(); var derbuf = this.toDER();
var buf = new Buffer(1); var buf = Buffer.alloc(1);
buf.writeUInt8(this.nhashtype, 0); buf.writeUInt8(this.nhashtype, 0);
return Buffer.concat([derbuf, buf]); return Buffer.concat([derbuf, buf]);
}; };

2
lib/encoding/base58.js

@ -45,7 +45,7 @@ Base58.decode = function(str) {
if (typeof str !== 'string') { if (typeof str !== 'string') {
throw new Error('Input should be a string'); throw new Error('Input should be a string');
} }
return new Buffer(bs58.decode(str)); return Buffer.from(bs58.decode(str));
}; };
Base58.prototype.fromBuffer = function(buf) { Base58.prototype.fromBuffer = function(buf) {

4
lib/encoding/base58check.js

@ -42,7 +42,7 @@ Base58Check.decode = function(s) {
if (typeof s !== 'string') if (typeof s !== 'string')
throw new Error('Input must be a string'); throw new Error('Input must be a string');
var buf = new Buffer(Base58.decode(s)); var buf = Buffer.from(Base58.decode(s));
if (buf.length < 4) if (buf.length < 4)
throw new Error("Input string too short"); throw new Error("Input string too short");
@ -66,7 +66,7 @@ Base58Check.checksum = function(buffer) {
Base58Check.encode = function(buf) { Base58Check.encode = function(buf) {
if (!Buffer.isBuffer(buf)) if (!Buffer.isBuffer(buf))
throw new Error('Input must be a buffer'); throw new Error('Input must be a buffer');
var checkedBuf = new Buffer(buf.length + 4); var checkedBuf = Buffer.alloc(buf.length + 4);
var hash = Base58Check.checksum(buf); var hash = Base58Check.checksum(buf);
buf.copy(checkedBuf); buf.copy(checkedBuf);
hash.copy(checkedBuf, buf.length); hash.copy(checkedBuf, buf.length);

4
lib/encoding/bufferreader.js

@ -18,7 +18,7 @@ var BufferReader = function BufferReader(buf) {
}); });
} else if (_.isString(buf)) { } else if (_.isString(buf)) {
this.set({ this.set({
buf: new Buffer(buf, 'hex'), buf: Buffer.from(buf, 'hex'),
}); });
} else if (_.isObject(buf)) { } else if (_.isObject(buf)) {
var obj = buf; var obj = buf;
@ -178,7 +178,7 @@ BufferReader.prototype.readVarintBN = function() {
}; };
BufferReader.prototype.reverse = function() { BufferReader.prototype.reverse = function() {
var buf = new Buffer(this.buf.length); var buf = Buffer.alloc(this.buf.length);
for (var i = 0; i < buf.length; i++) { for (var i = 0; i < buf.length; i++) {
buf[i] = this.buf[this.buf.length - 1 - i]; buf[i] = this.buf[this.buf.length - 1 - i];
} }

32
lib/encoding/bufferwriter.js

@ -6,6 +6,7 @@ var assert = require('assert');
var BufferWriter = function BufferWriter(obj) { var BufferWriter = function BufferWriter(obj) {
if (!(this instanceof BufferWriter)) if (!(this instanceof BufferWriter))
return new BufferWriter(obj); return new BufferWriter(obj);
this.bufLen = 0;
if (obj) if (obj)
this.set(obj); this.set(obj);
else else
@ -14,6 +15,7 @@ var BufferWriter = function BufferWriter(obj) {
BufferWriter.prototype.set = function(obj) { BufferWriter.prototype.set = function(obj) {
this.bufs = obj.bufs || this.bufs || []; this.bufs = obj.bufs || this.bufs || [];
this.bufLen = this.bufs.reduce(function(prev, buf){ return prev + buf.length; }, 0);
return this; return this;
}; };
@ -22,58 +24,60 @@ BufferWriter.prototype.toBuffer = function() {
}; };
BufferWriter.prototype.concat = function() { BufferWriter.prototype.concat = function() {
return Buffer.concat(this.bufs); return Buffer.concat(this.bufs, this.bufLen);
}; };
BufferWriter.prototype.write = function(buf) { BufferWriter.prototype.write = function(buf) {
assert(bufferUtil.isBuffer(buf)); assert(bufferUtil.isBuffer(buf));
this.bufs.push(buf); this.bufs.push(buf);
this.bufLen += buf.length;
return this; return this;
}; };
BufferWriter.prototype.writeReverse = function(buf) { BufferWriter.prototype.writeReverse = function(buf) {
assert(bufferUtil.isBuffer(buf)); assert(bufferUtil.isBuffer(buf));
this.bufs.push(bufferUtil.reverse(buf)); this.bufs.push(bufferUtil.reverse(buf));
this.bufLen += buf.length;
return this; return this;
}; };
BufferWriter.prototype.writeUInt8 = function(n) { BufferWriter.prototype.writeUInt8 = function(n) {
var buf = new Buffer(1); var buf = Buffer.alloc(1);
buf.writeUInt8(n, 0); buf.writeUInt8(n, 0);
this.write(buf); this.write(buf);
return this; return this;
}; };
BufferWriter.prototype.writeUInt16BE = function(n) { BufferWriter.prototype.writeUInt16BE = function(n) {
var buf = new Buffer(2); var buf = Buffer.alloc(2);
buf.writeUInt16BE(n, 0); buf.writeUInt16BE(n, 0);
this.write(buf); this.write(buf);
return this; return this;
}; };
BufferWriter.prototype.writeUInt16LE = function(n) { BufferWriter.prototype.writeUInt16LE = function(n) {
var buf = new Buffer(2); var buf = Buffer.alloc(2);
buf.writeUInt16LE(n, 0); buf.writeUInt16LE(n, 0);
this.write(buf); this.write(buf);
return this; return this;
}; };
BufferWriter.prototype.writeUInt32BE = function(n) { BufferWriter.prototype.writeUInt32BE = function(n) {
var buf = new Buffer(4); var buf = Buffer.alloc(4);
buf.writeUInt32BE(n, 0); buf.writeUInt32BE(n, 0);
this.write(buf); this.write(buf);
return this; return this;
}; };
BufferWriter.prototype.writeInt32LE = function(n) { BufferWriter.prototype.writeInt32LE = function(n) {
var buf = new Buffer(4); var buf = Buffer.alloc(4);
buf.writeInt32LE(n, 0); buf.writeInt32LE(n, 0);
this.write(buf); this.write(buf);
return this; return this;
}; };
BufferWriter.prototype.writeUInt32LE = function(n) { BufferWriter.prototype.writeUInt32LE = function(n) {
var buf = new Buffer(4); var buf = Buffer.alloc(4);
buf.writeUInt32LE(n, 0); buf.writeUInt32LE(n, 0);
this.write(buf); this.write(buf);
return this; return this;
@ -106,18 +110,18 @@ BufferWriter.prototype.writeVarintBN = function(bn) {
BufferWriter.varintBufNum = function(n) { BufferWriter.varintBufNum = function(n) {
var buf = undefined; var buf = undefined;
if (n < 253) { if (n < 253) {
buf = new Buffer(1); buf = Buffer.alloc(1);
buf.writeUInt8(n, 0); buf.writeUInt8(n, 0);
} else if (n < 0x10000) { } else if (n < 0x10000) {
buf = new Buffer(1 + 2); buf = Buffer.alloc(1 + 2);
buf.writeUInt8(253, 0); buf.writeUInt8(253, 0);
buf.writeUInt16LE(n, 1); buf.writeUInt16LE(n, 1);
} else if (n < 0x100000000) { } else if (n < 0x100000000) {
buf = new Buffer(1 + 4); buf = Buffer.alloc(1 + 4);
buf.writeUInt8(254, 0); buf.writeUInt8(254, 0);
buf.writeUInt32LE(n, 1); buf.writeUInt32LE(n, 1);
} else { } else {
buf = new Buffer(1 + 8); buf = Buffer.alloc(1 + 8);
buf.writeUInt8(255, 0); buf.writeUInt8(255, 0);
buf.writeInt32LE(n & -1, 1); buf.writeInt32LE(n & -1, 1);
buf.writeUInt32LE(Math.floor(n / 0x100000000), 5); buf.writeUInt32LE(Math.floor(n / 0x100000000), 5);
@ -129,14 +133,14 @@ BufferWriter.varintBufBN = function(bn) {
var buf = undefined; var buf = undefined;
var n = bn.toNumber(); var n = bn.toNumber();
if (n < 253) { if (n < 253) {
buf = new Buffer(1); buf = Buffer.alloc(1);
buf.writeUInt8(n, 0); buf.writeUInt8(n, 0);
} else if (n < 0x10000) { } else if (n < 0x10000) {
buf = new Buffer(1 + 2); buf = Buffer.alloc(1 + 2);
buf.writeUInt8(253, 0); buf.writeUInt8(253, 0);
buf.writeUInt16LE(n, 1); buf.writeUInt16LE(n, 1);
} else if (n < 0x100000000) { } else if (n < 0x100000000) {
buf = new Buffer(1 + 4); buf = Buffer.alloc(1 + 4);
buf.writeUInt8(254, 0); buf.writeUInt8(254, 0);
buf.writeUInt32LE(n, 1); buf.writeUInt32LE(n, 1);
} else { } else {

2
lib/encoding/varint.js

@ -28,7 +28,7 @@ Varint.prototype.set = function(obj) {
Varint.prototype.fromString = function(str) { Varint.prototype.fromString = function(str) {
this.set({ this.set({
buf: new Buffer(str, 'hex') buf: Buffer.from(str, 'hex')
}); });
return this; return this;
}; };

2
lib/hdprivatekey.js

@ -480,7 +480,7 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) {
var network = Network.get(BufferUtil.integerFromBuffer(arg.version)); var network = Network.get(BufferUtil.integerFromBuffer(arg.version));
var xprivkey; var xprivkey;
xprivkey = Base58Check.encode(buffer.Buffer.concat(sequence)); xprivkey = Base58Check.encode(buffer.Buffer.concat(sequence));
arg.xprivkey = new Buffer(xprivkey); arg.xprivkey = Buffer.from(xprivkey);
var privateKey = new PrivateKey(BN.fromBuffer(arg.privateKey), network); var privateKey = new PrivateKey(BN.fromBuffer(arg.privateKey), network);
var publicKey = privateKey.toPublicKey(); var publicKey = privateKey.toPublicKey();

2
lib/hdpublickey.js

@ -348,7 +348,7 @@ HDPublicKey.prototype._buildFromBuffers = function(arg) {
var xpubkey; var xpubkey;
xpubkey = Base58Check.encode(BufferUtil.concat(sequence)); xpubkey = Base58Check.encode(BufferUtil.concat(sequence));
arg.xpubkey = new Buffer(xpubkey); arg.xpubkey = Buffer.from(xpubkey);
var publicKey = new PublicKey(arg.publicKey, {network: network}); var publicKey = new PublicKey(arg.publicKey, {network: network});
var size = HDPublicKey.ParentFingerPrintSize; var size = HDPublicKey.ParentFingerPrintSize;

2
lib/opcode.js

@ -51,7 +51,7 @@ Opcode.prototype.toHex = function() {
}; };
Opcode.prototype.toBuffer = function() { Opcode.prototype.toBuffer = function() {
return new Buffer(this.toHex(), 'hex'); return Buffer.from(this.toHex(), 'hex');
}; };
Opcode.prototype.toNumber = function() { Opcode.prototype.toNumber = function() {

8
lib/privatekey.js

@ -103,7 +103,7 @@ PrivateKey.prototype._classifyArguments = function(data, network) {
info.network = Networks.get(data); info.network = Networks.get(data);
} else if (typeof(data) === 'string'){ } else if (typeof(data) === 'string'){
if (JSUtil.isHexa(data)) { if (JSUtil.isHexa(data)) {
info.bn = new BN(new Buffer(data, 'hex')); info.bn = new BN(Buffer.from(data, 'hex'));
} else { } else {
info = PrivateKey._transformWIF(data, network); info = PrivateKey._transformWIF(data, network);
} }
@ -310,11 +310,11 @@ PrivateKey.prototype.toWIF = function() {
var buf; var buf;
if (compressed) { if (compressed) {
buf = Buffer.concat([new Buffer([network.privatekey]), buf = Buffer.concat([Buffer.from([network.privatekey]),
this.bn.toBuffer({size: 32}), this.bn.toBuffer({size: 32}),
new Buffer([0x01])]); Buffer.from([0x01])]);
} else { } else {
buf = Buffer.concat([new Buffer([network.privatekey]), buf = Buffer.concat([Buffer.from([network.privatekey]),
this.bn.toBuffer({size: 32})]); this.bn.toBuffer({size: 32})]);
} }

10
lib/publickey.js

@ -78,7 +78,7 @@ PublicKey.prototype._classifyArgs = function(data, extra) {
} else if (data.x && data.y) { } else if (data.x && data.y) {
info = PublicKey._transformObject(data); info = PublicKey._transformObject(data);
} else if (typeof(data) === 'string') { } else if (typeof(data) === 'string') {
info = PublicKey._transformDER(new Buffer(data, 'hex')); info = PublicKey._transformDER(Buffer.from(data, 'hex'));
} else if (PublicKey._isBuffer(data)) { } else if (PublicKey._isBuffer(data)) {
info = PublicKey._transformDER(data); info = PublicKey._transformDER(data);
} else if (PublicKey._isPrivateKey(data)) { } else if (PublicKey._isPrivateKey(data)) {
@ -260,7 +260,7 @@ PublicKey.fromPoint = function(point, compressed) {
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromString = function(str, encoding) { PublicKey.fromString = function(str, encoding) {
var buf = new Buffer(str, encoding || 'hex'); var buf = Buffer.from(str, encoding || 'hex');
var info = PublicKey._transformDER(buf); var info = PublicKey._transformDER(buf);
return new PublicKey(info.point, { return new PublicKey(info.point, {
compressed: info.compressed compressed: info.compressed
@ -337,14 +337,14 @@ PublicKey.prototype.toBuffer = PublicKey.prototype.toDER = function() {
var prefix; var prefix;
if (!this.compressed) { if (!this.compressed) {
prefix = new Buffer([0x04]); prefix = Buffer.from([0x04]);
return Buffer.concat([prefix, xbuf, ybuf]); return Buffer.concat([prefix, xbuf, ybuf]);
} else { } else {
var odd = ybuf[ybuf.length - 1] % 2; var odd = ybuf[ybuf.length - 1] % 2;
if (odd) { if (odd) {
prefix = new Buffer([0x03]); prefix = Buffer.from([0x03]);
} else { } else {
prefix = new Buffer([0x02]); prefix = Buffer.from([0x02]);
} }
return Buffer.concat([prefix, xbuf]); return Buffer.concat([prefix, xbuf]);
} }

4
lib/script/interpreter.js

@ -308,8 +308,8 @@ Interpreter.prototype.set = function(obj) {
this.flags = typeof obj.flags !== 'undefined' ? obj.flags : this.flags; this.flags = typeof obj.flags !== 'undefined' ? obj.flags : this.flags;
}; };
Interpreter.true = new Buffer([1]); Interpreter.true = Buffer.from([1]);
Interpreter.false = new Buffer([]); Interpreter.false = Buffer.from([]);
Interpreter.MAX_SCRIPT_ELEMENT_SIZE = 520; Interpreter.MAX_SCRIPT_ELEMENT_SIZE = 520;

16
lib/script/script.js

@ -145,7 +145,7 @@ Script.fromASM = function(str) {
var opcodenum = opcode.toNumber(); var opcodenum = opcode.toNumber();
if (_.isUndefined(opcodenum)) { if (_.isUndefined(opcodenum)) {
var buf = new Buffer(tokens[i], 'hex'); var buf = Buffer.from(tokens[i], 'hex');
script.chunks.push({ script.chunks.push({
buf: buf, buf: buf,
len: buf.length, len: buf.length,
@ -156,7 +156,7 @@ Script.fromASM = function(str) {
opcodenum === Opcode.OP_PUSHDATA2 || opcodenum === Opcode.OP_PUSHDATA2 ||
opcodenum === Opcode.OP_PUSHDATA4) { opcodenum === Opcode.OP_PUSHDATA4) {
script.chunks.push({ script.chunks.push({
buf: new Buffer(tokens[i + 2], 'hex'), buf: Buffer.from(tokens[i + 2], 'hex'),
len: parseInt(tokens[i + 1]), len: parseInt(tokens[i + 1]),
opcodenum: opcodenum opcodenum: opcodenum
}); });
@ -193,7 +193,7 @@ Script.fromString = function(str) {
opcodenum = parseInt(token); opcodenum = parseInt(token);
if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) { if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) {
script.chunks.push({ script.chunks.push({
buf: new Buffer(tokens[i + 1].slice(2), 'hex'), buf: Buffer.from(tokens[i + 1].slice(2), 'hex'),
len: opcodenum, len: opcodenum,
opcodenum: opcodenum opcodenum: opcodenum
}); });
@ -208,7 +208,7 @@ Script.fromString = function(str) {
throw new Error('Pushdata data must start with 0x'); throw new Error('Pushdata data must start with 0x');
} }
script.chunks.push({ script.chunks.push({
buf: new Buffer(tokens[i + 2].slice(2), 'hex'), buf: Buffer.from(tokens[i + 2].slice(2), 'hex'),
len: parseInt(tokens[i + 1]), len: parseInt(tokens[i + 1]),
opcodenum: opcodenum opcodenum: opcodenum
}); });
@ -523,13 +523,13 @@ Script.prototype.isDataOut = function() {
Script.prototype.getData = function() { Script.prototype.getData = function() {
if (this.isDataOut() || this.isScriptHashOut()) { if (this.isDataOut() || this.isScriptHashOut()) {
if (_.isUndefined(this.chunks[1])) { if (_.isUndefined(this.chunks[1])) {
return new Buffer(0); return Buffer.alloc(0);
} else { } else {
return new Buffer(this.chunks[1].buf); return Buffer.from(this.chunks[1].buf);
} }
} }
if (this.isPublicKeyHashOut()) { if (this.isPublicKeyHashOut()) {
return new Buffer(this.chunks[2].buf); return Buffer.from(this.chunks[2].buf);
} }
throw new Error('Unrecognized script type to get data from'); throw new Error('Unrecognized script type to get data from');
}; };
@ -894,7 +894,7 @@ Script.buildPublicKeyOut = function(pubkey) {
Script.buildDataOut = function(data, encoding) { Script.buildDataOut = function(data, encoding) {
$.checkArgument(_.isUndefined(data) || _.isString(data) || BufferUtil.isBuffer(data)); $.checkArgument(_.isUndefined(data) || _.isString(data) || BufferUtil.isBuffer(data));
if (_.isString(data)) { if (_.isString(data)) {
data = new Buffer(data, encoding); data = Buffer.from(data, encoding);
} }
var s = new Script(); var s = new Script();
s.add(Opcode.OP_RETURN); s.add(Opcode.OP_RETURN);

2
lib/transaction/sighash.js

@ -63,7 +63,7 @@ var sighash = function sighash(transaction, sighashType, inputNumber, subscript)
// The SIGHASH_SINGLE bug. // The SIGHASH_SINGLE bug.
// https://bitcointalk.org/index.php?topic=260595.0 // https://bitcointalk.org/index.php?topic=260595.0
if (inputNumber >= txcopy.outputs.length) { if (inputNumber >= txcopy.outputs.length) {
return new Buffer(SIGHASH_SINGLE_BUG, 'hex'); return Buffer.from(SIGHASH_SINGLE_BUG, 'hex');
} }
txcopy.outputs.length = inputNumber + 1; txcopy.outputs.length = inputNumber + 1;

2
lib/transaction/signature.js

@ -34,7 +34,7 @@ inherits(TransactionSignature, Signature);
TransactionSignature.prototype._fromObject = function(arg) { TransactionSignature.prototype._fromObject = function(arg) {
this._checkObjectArgs(arg); this._checkObjectArgs(arg);
this.publicKey = new PublicKey(arg.publicKey); this.publicKey = new PublicKey(arg.publicKey);
this.prevTxId = BufferUtil.isBuffer(arg.prevTxId) ? arg.prevTxId : new Buffer(arg.prevTxId, 'hex'); this.prevTxId = BufferUtil.isBuffer(arg.prevTxId) ? arg.prevTxId : Buffer.from(arg.prevTxId, 'hex');
this.outputIndex = arg.outputIndex; this.outputIndex = arg.outputIndex;
this.inputIndex = arg.inputIndex; this.inputIndex = arg.inputIndex;
this.signature = (arg.signature instanceof Signature) ? arg.signature : this.signature = (arg.signature instanceof Signature) ? arg.signature :

5
lib/transaction/transaction.js

@ -102,7 +102,8 @@ var hashProperty = {
configurable: false, configurable: false,
enumerable: true, enumerable: true,
get: function() { get: function() {
return new BufferReader(this._getHash()).readReverse().toString('hex'); this._hash = new BufferReader(this._getHash()).readReverse().toString('hex');
return this._hash;
} }
}; };
@ -546,7 +547,7 @@ Transaction.prototype.getLockTime = function() {
}; };
Transaction.prototype.fromString = function(string) { Transaction.prototype.fromString = function(string) {
this.fromBuffer(new buffer.Buffer(string, 'hex')); this.fromBuffer(buffer.Buffer.from(string, 'hex'));
}; };
Transaction.prototype._newTransaction = function() { Transaction.prototype._newTransaction = function() {

8
lib/util/buffer.js

@ -44,7 +44,7 @@ module.exports = {
* @return {Buffer} * @return {Buffer}
*/ */
copy: function(original) { copy: function(original) {
var buffer = new Buffer(original.length); var buffer = Buffer.alloc(original.length);
original.copy(buffer); original.copy(buffer);
return buffer; return buffer;
}, },
@ -109,7 +109,7 @@ module.exports = {
bytes.push((integer >> 16) & 0xff); bytes.push((integer >> 16) & 0xff);
bytes.push((integer >> 8) & 0xff); bytes.push((integer >> 8) & 0xff);
bytes.push(integer & 0xff); bytes.push(integer & 0xff);
return new Buffer(bytes); return Buffer.from(bytes);
}, },
/** /**
@ -173,5 +173,5 @@ module.exports = {
} }
}; };
module.exports.NULL_HASH = module.exports.fill(new Buffer(32), 0); module.exports.NULL_HASH = module.exports.fill(Buffer.alloc(32), 0);
module.exports.EMPTY_BUFFER = new Buffer(0); module.exports.EMPTY_BUFFER = Buffer.alloc(0);

Loading…
Cancel
Save