From 5613d65426281ced182980b4249f1f81fa845be9 Mon Sep 17 00:00:00 2001 From: Micah Riggan Date: Mon, 12 Feb 2018 13:52:59 -0500 Subject: [PATCH 1/3] Buffer changes for cash branch --- benchmark/script.js | 2 +- lib/address.js | 4 ++-- lib/block/block.js | 6 +++--- lib/block/blockheader.js | 8 ++++---- lib/block/merkleblock.js | 6 +++--- lib/crypto/bn.js | 16 ++++++++-------- lib/crypto/ecdsa.js | 12 ++++++------ lib/crypto/hash.js | 10 +++++----- lib/crypto/point.js | 4 ++-- lib/crypto/random.js | 4 ++-- lib/crypto/signature.js | 12 ++++++------ lib/encoding/base58.js | 2 +- lib/encoding/base58check.js | 4 ++-- lib/encoding/bufferreader.js | 4 ++-- lib/encoding/bufferwriter.js | 32 ++++++++++++++++++-------------- lib/encoding/varint.js | 2 +- lib/hdprivatekey.js | 2 +- lib/hdpublickey.js | 2 +- lib/opcode.js | 2 +- lib/privatekey.js | 8 ++++---- lib/publickey.js | 10 +++++----- lib/script/interpreter.js | 4 ++-- lib/script/script.js | 16 ++++++++-------- lib/transaction/sighash.js | 2 +- lib/transaction/signature.js | 2 +- lib/transaction/transaction.js | 3 ++- lib/util/buffer.js | 8 ++++---- 27 files changed, 96 insertions(+), 91 deletions(-) diff --git a/benchmark/script.js b/benchmark/script.js index 1ec27f377..f14e8eb3f 100644 --- a/benchmark/script.js +++ b/benchmark/script.js @@ -5,7 +5,7 @@ var bitcore = require('..'); var async = require('async'); var blockData = require('./block-357238.json'); -var maxTime = 10; +var maxTime = 30; console.log('Benchmarking Script'); console.log('---------------------------------------'); diff --git a/lib/address.js b/lib/address.js index a4d575c93..5dc92a266 100644 --- a/lib/address.js +++ b/lib/address.js @@ -150,7 +150,7 @@ Address._transformObject = function(data) { $.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property'); $.checkArgument(data.type, 'Must provide a `type` property'); 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, type: data.type }; @@ -508,7 +508,7 @@ Address.fromObject = function fromObject(obj) { JSUtil.isHexa(obj.hash), '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); }; diff --git a/lib/block/block.js b/lib/block/block.js index 6d83619f6..7f0a024f4 100644 --- a/lib/block/block.js +++ b/lib/block/block.js @@ -117,7 +117,7 @@ Block.fromBuffer = function fromBuffer(buf) { * @returns {Block} - A hex encoded string of the block */ Block.fromString = function fromString(str) { - var buf = new Buffer(str, 'hex'); + var buf = Buffer.from(str, 'hex'); return Block.fromBuffer(buf); }; @@ -127,7 +127,7 @@ Block.fromString = function fromString(str) { */ Block.fromRawBlock = function fromRawBlock(data) { if (!BufferUtil.isBuffer(data)) { - data = new Buffer(data, 'binary'); + data = Buffer.from(data, 'binary'); } var br = BufferReader(data); br.pos = Block.Values.START_OF_BLOCK; @@ -275,7 +275,7 @@ Block.prototype.inspect = function inspect() { Block.Values = { 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; diff --git a/lib/block/blockheader.js b/lib/block/blockheader.js index 45b1b1349..44931228a 100644 --- a/lib/block/blockheader.js +++ b/lib/block/blockheader.js @@ -70,10 +70,10 @@ BlockHeader._fromObject = function _fromObject(data) { var prevHash = data.prevHash; var merkleRoot = data.merkleRoot; if (_.isString(data.prevHash)) { - prevHash = BufferUtil.reverse(new Buffer(data.prevHash, 'hex')); + prevHash = BufferUtil.reverse(Buffer.from(data.prevHash, 'hex')); } if (_.isString(data.merkleRoot)) { - merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex')); + merkleRoot = BufferUtil.reverse(Buffer.from(data.merkleRoot, 'hex')); } var info = { hash: data.hash, @@ -103,7 +103,7 @@ BlockHeader.fromObject = function fromObject(obj) { */ BlockHeader.fromRawBlock = function fromRawBlock(data) { if (!BufferUtil.isBuffer(data)) { - data = new Buffer(data, 'binary'); + data = Buffer.from(data, 'binary'); } var br = BufferReader(data); br.pos = BlockHeader.Constants.START_OF_HEADER; @@ -125,7 +125,7 @@ BlockHeader.fromBuffer = function fromBuffer(buf) { * @returns {BlockHeader} - An instance of block header */ BlockHeader.fromString = function fromString(str) { - var buf = new Buffer(str, 'hex'); + var buf = Buffer.from(str, 'hex'); return BlockHeader.fromBuffer(buf); }; diff --git a/lib/block/merkleblock.js b/lib/block/merkleblock.js index 96e55646d..1b949c3f9 100644 --- a/lib/block/merkleblock.js +++ b/lib/block/merkleblock.js @@ -103,7 +103,7 @@ MerkleBlock.prototype.toBufferWriter = function toBufferWriter(bw) { bw.writeUInt32LE(this.numTransactions); bw.writeVarintNum(this.hashes.length); 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); for (i = 0; i < this.flags.length; i++) { @@ -219,7 +219,7 @@ MerkleBlock.prototype._traverseMerkleTree = function traverseMerkleTree(depth, p if(depth === 0 && isParentOfMatch) { opts.txs.push(hash); } - return new Buffer(hash, 'hex'); + return Buffer.from(hash, 'hex'); } else { var left = this._traverseMerkleTree(depth-1, pos*2, opts); var right = left; @@ -270,7 +270,7 @@ MerkleBlock.prototype.hasTransaction = function hasTransaction(tx) { var hash = tx; if(tx instanceof Transaction) { // 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 = []; diff --git a/lib/crypto/bn.js b/lib/crypto/bn.js index e88c6da43..6136de062 100644 --- a/lib/crypto/bn.js +++ b/lib/crypto/bn.js @@ -5,7 +5,7 @@ var $ = require('../util/preconditions'); var _ = require('lodash'); var reversebuf = function(buf) { - var buf2 = new Buffer(buf.length); + var buf2 = Buffer.alloc(buf.length); for (var i = 0; i < buf.length; i++) { buf2[i] = buf[buf.length - 1 - i]; } @@ -42,7 +42,7 @@ BN.fromBuffer = function(buf, opts) { BN.fromSM = function(buf, opts) { var ret; if (buf.length === 0) { - return BN.fromBuffer(new Buffer([0])); + return BN.fromBuffer(Buffer.from([0])); } var endian = 'big'; @@ -73,7 +73,7 @@ BN.prototype.toBuffer = function(opts) { if (opts && opts.size) { hex = this.toString(16, 2); var natlen = hex.length / 2; - buf = new Buffer(hex, 'hex'); + buf = Buffer.from(hex, 'hex'); if (natlen === opts.size) { buf = buf; @@ -84,7 +84,7 @@ BN.prototype.toBuffer = function(opts) { } } else { hex = this.toString(16, 2); - buf = new Buffer(hex, 'hex'); + buf = Buffer.from(hex, 'hex'); } if (typeof opts !== 'undefined' && opts.endian === 'little') { @@ -99,19 +99,19 @@ BN.prototype.toSMBigEndian = function() { if (this.cmp(BN.Zero) === -1) { buf = this.neg().toBuffer(); if (buf[0] & 0x80) { - buf = Buffer.concat([new Buffer([0x80]), buf]); + buf = Buffer.concat([Buffer.from([0x80]), buf]); } else { buf[0] = buf[0] | 0x80; } } else { buf = this.toBuffer(); 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) { - buf = new Buffer([]); + buf = Buffer.from([]); } return buf; }; @@ -189,7 +189,7 @@ BN.trim = function(buf, natlen) { }; BN.pad = function(buf, natlen, size) { - var rbuf = new Buffer(size); + var rbuf = Buffer.alloc(size); for (var i = 0; i < buf.length; i++) { rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i]; } diff --git a/lib/crypto/ecdsa.js b/lib/crypto/ecdsa.js index 6e054634e..6bea796b8 100644 --- a/lib/crypto/ecdsa.js +++ b/lib/crypto/ecdsa.js @@ -81,17 +81,17 @@ ECDSA.prototype.deterministicK = function(badrs) { if (_.isUndefined(badrs)) { badrs = 0; } - var v = new Buffer(32); + var v = Buffer.alloc(32); v.fill(0x01); - var k = new Buffer(32); + var k = Buffer.alloc(32); k.fill(0x00); var x = this.privkey.bn.toBuffer({ size: 32 }); 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); - 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); 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) 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); T = BN.fromBuffer(v); @@ -192,7 +192,7 @@ ECDSA.prototype.sigError = function() { ECDSA.toLowS = function(s) { //enforce low s //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); } return s; diff --git a/lib/crypto/hash.js b/lib/crypto/hash.js index 01e4d1547..189e8f3fa 100644 --- a/lib/crypto/hash.js +++ b/lib/crypto/hash.js @@ -54,20 +54,20 @@ Hash.hmac = function(hashf, data, key) { if (key.length > blocksize) { key = hashf(key); } else if (key < blocksize) { - var fill = new Buffer(blocksize); + var fill = Buffer.alloc(blocksize); fill.fill(0); key.copy(fill); key = fill; } - var o_key = new Buffer(blocksize); + var o_key = Buffer.alloc(blocksize); o_key.fill(0x5c); - var i_key = new Buffer(blocksize); + var i_key = Buffer.alloc(blocksize); i_key.fill(0x36); - var o_key_pad = new Buffer(blocksize); - var i_key_pad = new Buffer(blocksize); + var o_key_pad = Buffer.alloc(blocksize); + var i_key_pad = Buffer.alloc(blocksize); for (var i = 0; i < blocksize; i++) { o_key_pad[i] = o_key[i] ^ key[i]; i_key_pad[i] = i_key[i] ^ key[i]; diff --git a/lib/crypto/point.js b/lib/crypto/point.js index cd1b34df4..7d5c18017 100644 --- a/lib/crypto/point.js +++ b/lib/crypto/point.js @@ -142,9 +142,9 @@ Point.pointToCompressed = function pointToCompressed(point) { var prefix; var odd = ybuf[ybuf.length - 1] % 2; if (odd) { - prefix = new Buffer([0x03]); + prefix = Buffer.from([0x03]); } else { - prefix = new Buffer([0x02]); + prefix = Buffer.from([0x02]); } return BufferUtil.concat([prefix, xbuf]); }; diff --git a/lib/crypto/random.js b/lib/crypto/random.js index f36968559..9938b3787 100644 --- a/lib/crypto/random.js +++ b/lib/crypto/random.js @@ -29,7 +29,7 @@ Random.getRandomBufferBrowser = function(size) { var bbuf = new Uint8Array(size); crypto.getRandomValues(bbuf); - var buf = new Buffer(bbuf); + var buf = Buffer.from(bbuf); return buf; }; @@ -37,7 +37,7 @@ Random.getRandomBufferBrowser = function(size) { /* insecure random bytes, but it never fails */ Random.getPseudoRandomBuffer = function(size) { var b32 = 0x100000000; - var b = new Buffer(size); + var b = Buffer.alloc(size); var r; for (var i = 0; i <= size; i++) { diff --git a/lib/crypto/signature.js b/lib/crypto/signature.js index 5a74db0b4..2373e08c9 100644 --- a/lib/crypto/signature.js +++ b/lib/crypto/signature.js @@ -80,7 +80,7 @@ Signature.fromTxFormat = function(buf) { }; Signature.fromString = function(str) { - var buf = new Buffer(str, 'hex'); + var buf = Buffer.from(str, 'hex'); return Signature.fromDER(buf); }; @@ -155,7 +155,7 @@ Signature.prototype.toCompact = function(i, compressed) { if (compressed === false) { val = val - 4; } - var b1 = new Buffer([val]); + var b1 = Buffer.from([val]); var b2 = this.r.toBuffer({ size: 32 }); @@ -172,8 +172,8 @@ Signature.prototype.toBuffer = Signature.prototype.toDER = function() { var rneg = rnbuf[0] & 0x80 ? true : false; var sneg = snbuf[0] & 0x80 ? true : false; - var rbuf = rneg ? Buffer.concat([new Buffer([0x00]), rnbuf]) : rnbuf; - var sbuf = sneg ? Buffer.concat([new Buffer([0x00]), snbuf]) : snbuf; + var rbuf = rneg ? Buffer.concat([Buffer.from([0x00]), rnbuf]) : rnbuf; + var sbuf = sneg ? Buffer.concat([Buffer.from([0x00]), snbuf]) : snbuf; var rlength = rbuf.length; var slength = sbuf.length; @@ -182,7 +182,7 @@ Signature.prototype.toBuffer = Signature.prototype.toDER = function() { var sheader = 0x02; 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; }; @@ -300,7 +300,7 @@ Signature.prototype.hasDefinedHashtype = function() { Signature.prototype.toTxFormat = function() { var derbuf = this.toDER(); - var buf = new Buffer(1); + var buf = Buffer.alloc(1); buf.writeUInt8(this.nhashtype, 0); return Buffer.concat([derbuf, buf]); }; diff --git a/lib/encoding/base58.js b/lib/encoding/base58.js index 7360ec37d..a2b606fa1 100644 --- a/lib/encoding/base58.js +++ b/lib/encoding/base58.js @@ -45,7 +45,7 @@ Base58.decode = function(str) { if (typeof str !== '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) { diff --git a/lib/encoding/base58check.js b/lib/encoding/base58check.js index 333f3503f..6372eb974 100644 --- a/lib/encoding/base58check.js +++ b/lib/encoding/base58check.js @@ -42,7 +42,7 @@ Base58Check.decode = function(s) { if (typeof s !== '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) throw new Error("Input string too short"); @@ -66,7 +66,7 @@ Base58Check.checksum = function(buffer) { Base58Check.encode = function(buf) { if (!Buffer.isBuffer(buf)) 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); buf.copy(checkedBuf); hash.copy(checkedBuf, buf.length); diff --git a/lib/encoding/bufferreader.js b/lib/encoding/bufferreader.js index 6c2b43722..1defb6b69 100644 --- a/lib/encoding/bufferreader.js +++ b/lib/encoding/bufferreader.js @@ -22,7 +22,7 @@ var BufferReader = function BufferReader(buf) { throw new TypeError('Invalid hex string'); this.set({ - buf: b, + buf: Buffer.from(buf, 'hex'), }); } else if (_.isObject(buf)) { var obj = buf; @@ -182,7 +182,7 @@ BufferReader.prototype.readVarintBN = 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++) { buf[i] = this.buf[this.buf.length - 1 - i]; } diff --git a/lib/encoding/bufferwriter.js b/lib/encoding/bufferwriter.js index 7967c53d0..ff38d1da4 100644 --- a/lib/encoding/bufferwriter.js +++ b/lib/encoding/bufferwriter.js @@ -6,6 +6,7 @@ var assert = require('assert'); var BufferWriter = function BufferWriter(obj) { if (!(this instanceof BufferWriter)) return new BufferWriter(obj); + this.bufLen = 0; if (obj) this.set(obj); else @@ -14,6 +15,7 @@ var BufferWriter = function BufferWriter(obj) { BufferWriter.prototype.set = function(obj) { this.bufs = obj.bufs || this.bufs || []; + this.bufLen = this.bufs.reduce(function(prev, buf){ return prev + buf.length; }, 0); return this; }; @@ -22,58 +24,60 @@ BufferWriter.prototype.toBuffer = function() { }; BufferWriter.prototype.concat = function() { - return Buffer.concat(this.bufs); + return Buffer.concat(this.bufs, this.bufLen); }; BufferWriter.prototype.write = function(buf) { assert(bufferUtil.isBuffer(buf)); this.bufs.push(buf); + this.bufLen += buf.length; return this; }; BufferWriter.prototype.writeReverse = function(buf) { assert(bufferUtil.isBuffer(buf)); this.bufs.push(bufferUtil.reverse(buf)); + this.bufLen += buf.length; return this; }; BufferWriter.prototype.writeUInt8 = function(n) { - var buf = new Buffer(1); + var buf = Buffer.alloc(1); buf.writeUInt8(n, 0); this.write(buf); return this; }; BufferWriter.prototype.writeUInt16BE = function(n) { - var buf = new Buffer(2); + var buf = Buffer.alloc(2); buf.writeUInt16BE(n, 0); this.write(buf); return this; }; BufferWriter.prototype.writeUInt16LE = function(n) { - var buf = new Buffer(2); + var buf = Buffer.alloc(2); buf.writeUInt16LE(n, 0); this.write(buf); return this; }; BufferWriter.prototype.writeUInt32BE = function(n) { - var buf = new Buffer(4); + var buf = Buffer.alloc(4); buf.writeUInt32BE(n, 0); this.write(buf); return this; }; BufferWriter.prototype.writeInt32LE = function(n) { - var buf = new Buffer(4); + var buf = Buffer.alloc(4); buf.writeInt32LE(n, 0); this.write(buf); return this; }; BufferWriter.prototype.writeUInt32LE = function(n) { - var buf = new Buffer(4); + var buf = Buffer.alloc(4); buf.writeUInt32LE(n, 0); this.write(buf); return this; @@ -106,18 +110,18 @@ BufferWriter.prototype.writeVarintBN = function(bn) { BufferWriter.varintBufNum = function(n) { var buf = undefined; if (n < 253) { - buf = new Buffer(1); + buf = Buffer.alloc(1); buf.writeUInt8(n, 0); } else if (n < 0x10000) { - buf = new Buffer(1 + 2); + buf = Buffer.alloc(1 + 2); buf.writeUInt8(253, 0); buf.writeUInt16LE(n, 1); } else if (n < 0x100000000) { - buf = new Buffer(1 + 4); + buf = Buffer.alloc(1 + 4); buf.writeUInt8(254, 0); buf.writeUInt32LE(n, 1); } else { - buf = new Buffer(1 + 8); + buf = Buffer.alloc(1 + 8); buf.writeUInt8(255, 0); buf.writeInt32LE(n & -1, 1); buf.writeUInt32LE(Math.floor(n / 0x100000000), 5); @@ -129,14 +133,14 @@ BufferWriter.varintBufBN = function(bn) { var buf = undefined; var n = bn.toNumber(); if (n < 253) { - buf = new Buffer(1); + buf = Buffer.alloc(1); buf.writeUInt8(n, 0); } else if (n < 0x10000) { - buf = new Buffer(1 + 2); + buf = Buffer.alloc(1 + 2); buf.writeUInt8(253, 0); buf.writeUInt16LE(n, 1); } else if (n < 0x100000000) { - buf = new Buffer(1 + 4); + buf = Buffer.alloc(1 + 4); buf.writeUInt8(254, 0); buf.writeUInt32LE(n, 1); } else { diff --git a/lib/encoding/varint.js b/lib/encoding/varint.js index c434f4dff..6bb3240e5 100644 --- a/lib/encoding/varint.js +++ b/lib/encoding/varint.js @@ -28,7 +28,7 @@ Varint.prototype.set = function(obj) { Varint.prototype.fromString = function(str) { this.set({ - buf: new Buffer(str, 'hex') + buf: Buffer.from(str, 'hex') }); return this; }; diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 5439e5790..18b16d5d1 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -480,7 +480,7 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) { var network = Network.get(BufferUtil.integerFromBuffer(arg.version)); var xprivkey; 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 publicKey = privateKey.toPublicKey(); diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index e134d4427..39b632ed4 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -348,7 +348,7 @@ HDPublicKey.prototype._buildFromBuffers = function(arg) { var xpubkey; 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 size = HDPublicKey.ParentFingerPrintSize; diff --git a/lib/opcode.js b/lib/opcode.js index edf02cb8f..d3f08d6d3 100644 --- a/lib/opcode.js +++ b/lib/opcode.js @@ -51,7 +51,7 @@ Opcode.prototype.toHex = function() { }; Opcode.prototype.toBuffer = function() { - return new Buffer(this.toHex(), 'hex'); + return Buffer.from(this.toHex(), 'hex'); }; Opcode.prototype.toNumber = function() { diff --git a/lib/privatekey.js b/lib/privatekey.js index f820cae09..95a3bc505 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -103,7 +103,7 @@ PrivateKey.prototype._classifyArguments = function(data, network) { info.network = Networks.get(data); } else if (typeof(data) === 'string'){ if (JSUtil.isHexa(data)) { - info.bn = new BN(new Buffer(data, 'hex')); + info.bn = new BN(Buffer.from(data, 'hex')); } else { info = PrivateKey._transformWIF(data, network); } @@ -310,11 +310,11 @@ PrivateKey.prototype.toWIF = function() { var buf; if (compressed) { - buf = Buffer.concat([new Buffer([network.privatekey]), + buf = Buffer.concat([Buffer.from([network.privatekey]), this.bn.toBuffer({size: 32}), - new Buffer([0x01])]); + Buffer.from([0x01])]); } else { - buf = Buffer.concat([new Buffer([network.privatekey]), + buf = Buffer.concat([Buffer.from([network.privatekey]), this.bn.toBuffer({size: 32})]); } diff --git a/lib/publickey.js b/lib/publickey.js index 76c50ab3c..35e4022ef 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -78,7 +78,7 @@ PublicKey.prototype._classifyArgs = function(data, extra) { } else if (data.x && data.y) { info = PublicKey._transformObject(data); } else if (typeof(data) === 'string') { - info = PublicKey._transformDER(new Buffer(data, 'hex')); + info = PublicKey._transformDER(Buffer.from(data, 'hex')); } else if (PublicKey._isBuffer(data)) { info = PublicKey._transformDER(data); } else if (PublicKey._isPrivateKey(data)) { @@ -260,7 +260,7 @@ PublicKey.fromPoint = function(point, compressed) { * @returns {PublicKey} A new valid instance of PublicKey */ PublicKey.fromString = function(str, encoding) { - var buf = new Buffer(str, encoding || 'hex'); + var buf = Buffer.from(str, encoding || 'hex'); var info = PublicKey._transformDER(buf); return new PublicKey(info.point, { compressed: info.compressed @@ -337,14 +337,14 @@ PublicKey.prototype.toBuffer = PublicKey.prototype.toDER = function() { var prefix; if (!this.compressed) { - prefix = new Buffer([0x04]); + prefix = Buffer.from([0x04]); return Buffer.concat([prefix, xbuf, ybuf]); } else { var odd = ybuf[ybuf.length - 1] % 2; if (odd) { - prefix = new Buffer([0x03]); + prefix = Buffer.from([0x03]); } else { - prefix = new Buffer([0x02]); + prefix = Buffer.from([0x02]); } return Buffer.concat([prefix, xbuf]); } diff --git a/lib/script/interpreter.js b/lib/script/interpreter.js index 31e132f71..78ea24fa3 100644 --- a/lib/script/interpreter.js +++ b/lib/script/interpreter.js @@ -179,8 +179,8 @@ Interpreter.prototype.set = function(obj) { this.flags = typeof obj.flags !== 'undefined' ? obj.flags : this.flags; }; -Interpreter.true = new Buffer([1]); -Interpreter.false = new Buffer([]); +Interpreter.true = Buffer.from([1]); +Interpreter.false = Buffer.from([]); Interpreter.MAX_SCRIPT_ELEMENT_SIZE = 520; diff --git a/lib/script/script.js b/lib/script/script.js index e48d7163f..0206f8be6 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -145,7 +145,7 @@ Script.fromASM = function(str) { var opcodenum = opcode.toNumber(); if (_.isUndefined(opcodenum)) { - var buf = new Buffer(tokens[i], 'hex'); + var buf = Buffer.from(tokens[i], 'hex'); script.chunks.push({ buf: buf, len: buf.length, @@ -156,7 +156,7 @@ Script.fromASM = function(str) { opcodenum === Opcode.OP_PUSHDATA2 || opcodenum === Opcode.OP_PUSHDATA4) { script.chunks.push({ - buf: new Buffer(tokens[i + 2], 'hex'), + buf: Buffer.from(tokens[i + 2], 'hex'), len: parseInt(tokens[i + 1]), opcodenum: opcodenum }); @@ -193,7 +193,7 @@ Script.fromString = function(str) { opcodenum = parseInt(token); if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) { script.chunks.push({ - buf: new Buffer(tokens[i + 1].slice(2), 'hex'), + buf: Buffer.from(tokens[i + 1].slice(2), 'hex'), len: opcodenum, opcodenum: opcodenum }); @@ -208,7 +208,7 @@ Script.fromString = function(str) { throw new Error('Pushdata data must start with 0x'); } 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]), opcodenum: opcodenum }); @@ -480,13 +480,13 @@ Script.prototype.isDataOut = function() { Script.prototype.getData = function() { if (this.isDataOut() || this.isScriptHashOut()) { if (_.isUndefined(this.chunks[1])) { - return new Buffer(0); + return Buffer.alloc(0); } else { - return new Buffer(this.chunks[1].buf); + return Buffer.from(this.chunks[1].buf); } } 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'); }; @@ -831,7 +831,7 @@ Script.buildPublicKeyOut = function(pubkey) { Script.buildDataOut = function(data, encoding) { $.checkArgument(_.isUndefined(data) || _.isString(data) || BufferUtil.isBuffer(data)); if (_.isString(data)) { - data = new Buffer(data, encoding); + data = Buffer.from(data, encoding); } var s = new Script(); s.add(Opcode.OP_RETURN); diff --git a/lib/transaction/sighash.js b/lib/transaction/sighash.js index 6252946e2..cfecae4c2 100644 --- a/lib/transaction/sighash.js +++ b/lib/transaction/sighash.js @@ -202,7 +202,7 @@ var sighash = function sighash(transaction, sighashType, inputNumber, subscript, // The SIGHASH_SINGLE bug. // https://bitcointalk.org/index.php?topic=260595.0 if (inputNumber >= txcopy.outputs.length) { - return new Buffer(SIGHASH_SINGLE_BUG, 'hex'); + return Buffer.from(SIGHASH_SINGLE_BUG, 'hex'); } txcopy.outputs.length = inputNumber + 1; diff --git a/lib/transaction/signature.js b/lib/transaction/signature.js index e98a0b1a1..5e40f9902 100644 --- a/lib/transaction/signature.js +++ b/lib/transaction/signature.js @@ -34,7 +34,7 @@ inherits(TransactionSignature, Signature); TransactionSignature.prototype._fromObject = function(arg) { this._checkObjectArgs(arg); 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.inputIndex = arg.inputIndex; this.signature = (arg.signature instanceof Signature) ? arg.signature : diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index cbefa7c76..6eb721958 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -102,7 +102,8 @@ var hashProperty = { configurable: false, enumerable: true, get: function() { - return new BufferReader(this._getHash()).readReverse().toString('hex'); + this._hash = new BufferReader(this._getHash()).readReverse().toString('hex'); + return this._hash; } }; Object.defineProperty(Transaction.prototype, 'hash', hashProperty); diff --git a/lib/util/buffer.js b/lib/util/buffer.js index c78b9396e..3c126199c 100644 --- a/lib/util/buffer.js +++ b/lib/util/buffer.js @@ -44,7 +44,7 @@ module.exports = { * @return {Buffer} */ copy: function(original) { - var buffer = new Buffer(original.length); + var buffer = Buffer.alloc(original.length); original.copy(buffer); return buffer; }, @@ -109,7 +109,7 @@ module.exports = { bytes.push((integer >> 16) & 0xff); bytes.push((integer >> 8) & 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.EMPTY_BUFFER = new Buffer(0); +module.exports.NULL_HASH = module.exports.fill(Buffer.alloc(32), 0); +module.exports.EMPTY_BUFFER = Buffer.alloc(0); From 9147449933bfbaa0aed0695026d8e72cf2223beb Mon Sep 17 00:00:00 2001 From: Micah Riggan Date: Mon, 12 Feb 2018 14:08:34 -0500 Subject: [PATCH 2/3] One line in buffer reader was different --- lib/encoding/bufferreader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/encoding/bufferreader.js b/lib/encoding/bufferreader.js index 1defb6b69..84c43e7e4 100644 --- a/lib/encoding/bufferreader.js +++ b/lib/encoding/bufferreader.js @@ -17,12 +17,12 @@ var BufferReader = function BufferReader(buf) { buf: buf }); } else if (_.isString(buf)) { - var b = new Buffer(buf, 'hex'); + var b = Buffer.from(buf); if (b.length * 2 != buf.length) throw new TypeError('Invalid hex string'); this.set({ - buf: Buffer.from(buf, 'hex'), + buf: b }); } else if (_.isObject(buf)) { var obj = buf; From 2026bbd8d85e0674f89369a1ab89ecdece209f10 Mon Sep 17 00:00:00 2001 From: Micah Riggan Date: Wed, 14 Feb 2018 10:16:06 -0500 Subject: [PATCH 3/3] Resolving a merge issue --- lib/encoding/bufferreader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/encoding/bufferreader.js b/lib/encoding/bufferreader.js index 84c43e7e4..8f2492e4f 100644 --- a/lib/encoding/bufferreader.js +++ b/lib/encoding/bufferreader.js @@ -17,7 +17,7 @@ var BufferReader = function BufferReader(buf) { buf: buf }); } else if (_.isString(buf)) { - var b = Buffer.from(buf); + var b = Buffer.from(buf, 'hex'); if (b.length * 2 != buf.length) throw new TypeError('Invalid hex string');