From 6fb8a4641ac3c5f35da38bd0576fa09313b02897 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Fri, 14 May 2021 16:56:12 +0530 Subject: [PATCH] Updating the deprecated new Buffer - replacing new Buffer(int) to Buffer.alloc(int) - replacing new Buffer('string' [, 'encoding']) to Buffer.from('string' [, 'encoding']) --- contrib/getUtxos.js | 2 +- contrib/printKeys.js | 14 +++---- lib/constants.js | 2 +- lib/services/address/encoding.js | 32 ++++++++-------- lib/services/address/index.js | 6 +-- lib/services/block/encoding.js | 2 +- lib/services/db/index.js | 14 +++---- lib/services/header/encoding.js | 28 +++++++------- lib/services/header/index.js | 2 +- lib/services/mempool/encoding.js | 16 ++++---- lib/services/mempool/index.js | 2 +- lib/services/timestamp/encoding.js | 12 +++--- lib/services/transaction/encoding.js | 44 +++++++++++----------- lib/utils.js | 6 +-- test/services/address/encoding.unit.js | 30 +++++++-------- test/services/address/index.unit.js | 6 +-- test/services/block/encoding.unit.js | 6 +-- test/services/block/index.unit.js | 2 +- test/services/db/index.unit.js | 10 ++--- test/services/header/encoding.unit.js | 28 +++++++------- test/services/header/index.unit.js | 12 +++--- test/services/mempool/encoding.unit.js | 36 +++++++++--------- test/services/mempool/index.unit.js | 4 +- test/services/timestamp/encoding.unit.js | 16 ++++---- test/services/transaction/encoding.unit.js | 22 +++++------ test/services/transaction/index.unit.js | 4 +- test/utils.unit.js | 12 +++--- 27 files changed, 185 insertions(+), 185 deletions(-) diff --git a/contrib/getUtxos.js b/contrib/getUtxos.js index 69ed3455..85a69985 100644 --- a/contrib/getUtxos.js +++ b/contrib/getUtxos.js @@ -12,7 +12,7 @@ var dbLocation = process.argv[2]; console.log('Using db location: ', dbLocation); -var addressPrefix = new Buffer('0006', 'hex'); +var addressPrefix = Buffer.from('0006', 'hex'); var startAddress = new Array(35).join('0'); var endAddress = new Array(35).join('f'); diff --git a/contrib/printKeys.js b/contrib/printKeys.js index e70254cd..3076071d 100644 --- a/contrib/printKeys.js +++ b/contrib/printKeys.js @@ -7,19 +7,19 @@ var dbPath = '/Users/chrisk/.bwdb/flocore-node.db'; var flocore = require('flocore-lib'); var db = levelup(dbPath, {keyEncoding: 'binary', valueEncoding: 'binary'}); -var prefix = new Buffer('0002', 'hex'); +var prefix = Buffer.from('0002', 'hex'); var encoding = new Encoding(prefix); var address = '1MfDRRVVKXUe5KNVZzu8CBzUZDHTTYZM94'; -var addressLength = new Buffer(1); +var addressLength = Buffer.alloc(1); addressLength.writeUInt8(address.length); //var startBuffer = prefix; -//var endBuffer = Buffer.concat([prefix, new Buffer('ff', 'hex')]); +//var endBuffer = Buffer.concat([prefix, Buffer.from('ff', 'hex')]); -//var startBuffer = Buffer.concat([prefix, addressLength, new Buffer(address, 'utf8'), new Buffer('00', 'hex')]); -//var endBuffer = Buffer.concat([prefix, addressLength, new Buffer(address, 'utf8'), new Buffer('01', 'hex')]); -var start = Buffer.concat([prefix, new Buffer('0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9', 'hex')]); -var end = Buffer.concat([prefix, new Buffer('0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9', 'hex'), new Buffer('01', 'hex')]); +//var startBuffer = Buffer.concat([prefix, addressLength, Buffer.from(address, 'utf8'), Buffer.from('00', 'hex')]); +//var endBuffer = Buffer.concat([prefix, addressLength, Buffer.from(address, 'utf8'), Buffer.from('01', 'hex')]); +var start = Buffer.concat([prefix, Buffer.from('0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9', 'hex')]); +var end = Buffer.concat([prefix, Buffer.from('0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9', 'hex'), Buffer.from('01', 'hex')]); var stream = db.createReadStream({ gte: start, lt: end diff --git a/lib/constants.js b/lib/constants.js index 08c63d59..785c8d18 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -8,6 +8,6 @@ module.exports = { testnet: '9b7bc86236c34b5e3a39367c036b7fe8807a966c22a7a1f0da2a198a27e03731', //this is testnet3 testnet5: '000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943' //this is testnet5 }, - DB_PREFIX: new Buffer('ffff', 'hex') + DB_PREFIX: Buffer.from('ffff', 'hex') }; diff --git a/lib/services/address/encoding.js b/lib/services/address/encoding.js index 3f13ac1d..0c146cb2 100644 --- a/lib/services/address/encoding.js +++ b/lib/services/address/encoding.js @@ -5,33 +5,33 @@ function Encoding(servicePrefix) { } Encoding.prototype.encodeAddressIndexKey = function(address, height, txid, index, input, timestamp) { - var prefix = new Buffer('00', 'hex'); + var prefix = Buffer.from('00', 'hex'); var buffers = [this.servicePrefix, prefix]; - var addressSizeBuffer = new Buffer(1); + var addressSizeBuffer = Buffer.alloc(1); addressSizeBuffer.writeUInt8(address.length); - var addressBuffer = new Buffer(address, 'utf8'); + var addressBuffer = Buffer.from(address, 'utf8'); buffers.push(addressSizeBuffer); buffers.push(addressBuffer); - var heightBuffer = new Buffer(4); + var heightBuffer = Buffer.alloc(4); heightBuffer.writeUInt32BE(height || 0); buffers.push(heightBuffer); - var txidBuffer = new Buffer(txid || Array(65).join('0'), 'hex'); + var txidBuffer = Buffer.from(txid || Array(65).join('0'), 'hex'); buffers.push(txidBuffer); - var indexBuffer = new Buffer(4); + var indexBuffer = Buffer.alloc(4); indexBuffer.writeUInt32BE(index || 0); buffers.push(indexBuffer); // this is whether the address appears in an input (1) or output (0) - var inputBuffer = new Buffer(1); + var inputBuffer = Buffer.alloc(1); inputBuffer.writeUInt8(input || 0); buffers.push(inputBuffer); - var timestampBuffer = new Buffer(4); + var timestampBuffer = Buffer.alloc(4); timestampBuffer.writeUInt32BE(timestamp || 0); buffers.push(timestampBuffer); @@ -58,20 +58,20 @@ Encoding.prototype.decodeAddressIndexKey = function(buffer) { }; Encoding.prototype.encodeUtxoIndexKey = function(address, txid, outputIndex) { - var prefix = new Buffer('01', 'hex'); + var prefix = Buffer.from('01', 'hex'); var buffers = [this.servicePrefix, prefix]; - var addressSizeBuffer = new Buffer(1); + var addressSizeBuffer = Buffer.alloc(1); addressSizeBuffer.writeUInt8(address.length); - var addressBuffer = new Buffer(address, 'utf8'); + var addressBuffer = Buffer.from(address, 'utf8'); buffers.push(addressSizeBuffer); buffers.push(addressBuffer); - var txidBuffer = new Buffer(txid || new Array(65).join('0'), 'hex'); + var txidBuffer = Buffer.from(txid || new Array(65).join('0'), 'hex'); buffers.push(txidBuffer); - var outputIndexBuffer = new Buffer(4); + var outputIndexBuffer = Buffer.alloc(4); outputIndexBuffer.writeUInt32BE(outputIndex || 0); buffers.push(outputIndexBuffer); @@ -92,11 +92,11 @@ Encoding.prototype.decodeUtxoIndexKey = function(buffer) { }; Encoding.prototype.encodeUtxoIndexValue = function(height, satoshis, timestamp, scriptBuffer) { - var heightBuffer = new Buffer(4); + var heightBuffer = Buffer.alloc(4); heightBuffer.writeUInt32BE(height); - var satoshisBuffer = new Buffer(8); + var satoshisBuffer = Buffer.alloc(8); satoshisBuffer.writeDoubleBE(satoshis); - var timestampBuffer = new Buffer(4); + var timestampBuffer = Buffer.alloc(4); timestampBuffer.writeUInt32BE(timestamp || 0); return Buffer.concat([heightBuffer, satoshisBuffer, timestampBuffer, scriptBuffer]); }; diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 84ba9da0..9e4587f7 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -298,7 +298,7 @@ AddressService.prototype.getAddressUnspentOutputs = function(address, options, c var results = []; var start = self._encoding.encodeUtxoIndexKey(address); - var final = new Buffer(new Array(73).join('f'), 'hex'); + var final = Buffer.from(new Array(73).join('f'), 'hex'); var end = Buffer.concat([ start.slice(0, -36), final ]); var criteria = { @@ -444,7 +444,7 @@ AddressService.prototype._getTxidStream = function(address, options) { var end = Buffer.concat([ start.slice(0, address.length + 4), options.endHeightBuf, - new Buffer(new Array(83).join('f'), 'hex') + Buffer.from(new Array(83).join('f'), 'hex') ]); var criteria = { @@ -500,7 +500,7 @@ AddressService.prototype._getAddressTxidHistory = function(address, options, cal var results = []; - options.endHeightBuf = new Buffer(4); + options.endHeightBuf = Buffer.alloc(4); options.endHeightBuf.writeUInt32BE(options.end); if (_.isUndefined(options.queryMempool)) { diff --git a/lib/services/block/encoding.js b/lib/services/block/encoding.js index b38dce72..251e892e 100644 --- a/lib/services/block/encoding.js +++ b/lib/services/block/encoding.js @@ -10,7 +10,7 @@ function Encoding(servicePrefix) { // ---- hash --> rawblock Encoding.prototype.encodeBlockKey = function(hash) { - return Buffer.concat([ this._servicePrefix, new Buffer(hash, 'hex') ]); + return Buffer.concat([ this._servicePrefix, Buffer.from(hash, 'hex') ]); }; Encoding.prototype.decodeBlockKey = function(buffer) { diff --git a/lib/services/db/index.js b/lib/services/db/index.js index 323509ec..746d8689 100644 --- a/lib/services/db/index.js +++ b/lib/services/db/index.js @@ -69,9 +69,9 @@ DB.prototype._setDataPath = function() { }; DB.prototype._setVersion = function(callback) { - var versionBuffer = new Buffer(new Array(4)); + var versionBuffer = Buffer.from(new Array(4)); versionBuffer.writeUInt32BE(this.version); - this.put(Buffer.concat([ this._dbPrefix, new Buffer('version', 'utf8') ]), versionBuffer, callback); + this.put(Buffer.concat([ this._dbPrefix, Buffer.from('version', 'utf8') ]), versionBuffer, callback); }; DB.prototype.start = function(callback) { @@ -207,7 +207,7 @@ DB.prototype.getPublishEvents = function() { DB.prototype.getServiceTip = function(serviceName, callback) { - var keyBuf = Buffer.concat([ this._dbPrefix, new Buffer('tip-' + serviceName, 'utf8') ]); + var keyBuf = Buffer.concat([ this._dbPrefix, Buffer.from('tip-' + serviceName, 'utf8') ]); var self = this; self.get(keyBuf, function(err, tipBuf) { @@ -242,8 +242,8 @@ DB.prototype.getServiceTip = function(serviceName, callback) { DB.prototype.getPrefix = function(service, callback) { var self = this; - var keyBuf = Buffer.concat([ self._dbPrefix, new Buffer('prefix-', 'utf8'), new Buffer(service, 'utf8') ]); - var unusedBuf = Buffer.concat([ self._dbPrefix, new Buffer('nextUnused', 'utf8') ]); + var keyBuf = Buffer.concat([ self._dbPrefix, Buffer.from('prefix-', 'utf8'), Buffer.from(service, 'utf8') ]); + var unusedBuf = Buffer.concat([ self._dbPrefix, Buffer.from('nextUnused', 'utf8') ]); function getPrefix(next) { @@ -270,7 +270,7 @@ DB.prototype.getPrefix = function(service, callback) { } if(!buffer) { - return next(null, new Buffer('0001', 'hex')); + return next(null, Buffer.from('0001', 'hex')); } next(null, buffer); @@ -294,7 +294,7 @@ DB.prototype.getPrefix = function(service, callback) { function putUnused(buffer, next) { var prefix = buffer.readUInt16BE(); - var nextUnused = new Buffer(2); + var nextUnused = Buffer.alloc(2); nextUnused.writeUInt16BE(prefix + 1); self.put(unusedBuf, nextUnused, function(err) { diff --git a/lib/services/header/encoding.js b/lib/services/header/encoding.js index 0dd7f1d3..a23d04ec 100644 --- a/lib/services/header/encoding.js +++ b/lib/services/header/encoding.js @@ -3,13 +3,13 @@ function Encoding(servicePrefix) { this._servicePrefix = servicePrefix; - this._hashPrefix = new Buffer('00', 'hex'); - this._heightPrefix = new Buffer('01', 'hex'); + this._hashPrefix = Buffer.from('00', 'hex'); + this._heightPrefix = Buffer.from('01', 'hex'); } // ---- hash --> header Encoding.prototype.encodeHeaderHashKey = function(hash) { - var hashBuf = new Buffer(hash, 'hex'); + var hashBuf = Buffer.from(hash, 'hex'); return Buffer.concat([ this._servicePrefix, this._hashPrefix, hashBuf ]); }; @@ -19,7 +19,7 @@ Encoding.prototype.decodeHeaderHashKey = function(buffer) { // ---- height --> header Encoding.prototype.encodeHeaderHeightKey = function(height) { - var heightBuf = new Buffer(4); + var heightBuf = Buffer.alloc(4); heightBuf.writeUInt32BE(height); return Buffer.concat([ this._servicePrefix, this._heightPrefix, heightBuf ]); }; @@ -29,21 +29,21 @@ Encoding.prototype.decodeHeaderHeightKey = function(buffer) { }; Encoding.prototype.encodeHeaderValue = function(header) { - var hashBuf = new Buffer(header.hash, 'hex'); - var versionBuf = new Buffer(4); + var hashBuf = Buffer.from(header.hash, 'hex'); + var versionBuf = Buffer.alloc(4); versionBuf.writeInt32BE(header.version); - var prevHash = new Buffer(header.prevHash, 'hex'); - var merkleRoot = new Buffer(header.merkleRoot, 'hex'); - var tsBuf = new Buffer(4); + var prevHash = Buffer.from(header.prevHash, 'hex'); + var merkleRoot = Buffer.from(header.merkleRoot, 'hex'); + var tsBuf = Buffer.alloc(4); tsBuf.writeUInt32BE(header.timestamp || header.time); - var bitsBuf = new Buffer(4); + var bitsBuf = Buffer.alloc(4); bitsBuf.writeUInt32BE(header.bits); - var nonceBuf = new Buffer(4); + var nonceBuf = Buffer.alloc(4); nonceBuf.writeUInt32BE(header.nonce); - var heightBuf = new Buffer(4); + var heightBuf = Buffer.alloc(4); heightBuf.writeUInt32BE(header.height); - var chainworkBuf = new Buffer(header.chainwork, 'hex'); - var nextHash = new Buffer(header.nextHash || new Array(65).join('0'), 'hex'); + var chainworkBuf = Buffer.from(header.chainwork, 'hex'); + var nextHash = Buffer.from(header.nextHash || new Array(65).join('0'), 'hex'); return Buffer.concat([ hashBuf, versionBuf, diff --git a/lib/services/header/index.js b/lib/services/header/index.js index 27f3016e..8fbbfc65 100644 --- a/lib/services/header/index.js +++ b/lib/services/header/index.js @@ -1010,7 +1010,7 @@ HeaderService.prototype._adjustHeadersForCheckPointTip = function(callback) { HeaderService.prototype._getChainwork = function(header, prevHeader) { - var prevChainwork = new BN(new Buffer(prevHeader.chainwork, 'hex')); + var prevChainwork = new BN(Buffer.from(prevHeader.chainwork, 'hex')); return this._computeChainwork(header.bits, prevChainwork); }; diff --git a/lib/services/mempool/encoding.js b/lib/services/mempool/encoding.js index d016a765..aa4bba38 100644 --- a/lib/services/mempool/encoding.js +++ b/lib/services/mempool/encoding.js @@ -4,13 +4,13 @@ var tx = require('fcoin').TX; function Encoding(servicePrefix) { this.servicePrefix = servicePrefix; - this.txPrefix = new Buffer('00', 'hex'); - this.addressPrefix = new Buffer('01', 'hex'); + this.txPrefix = Buffer.from('00', 'hex'); + this.addressPrefix = Buffer.from('01', 'hex'); } Encoding.prototype.encodeMempoolTransactionKey = function(txid) { var buffers = [this.servicePrefix, this.txPrefix]; - var txidBuffer = new Buffer(txid, 'hex'); + var txidBuffer = Buffer.from(txid, 'hex'); buffers.push(txidBuffer); return Buffer.concat(buffers); }; @@ -30,22 +30,22 @@ Encoding.prototype.decodeMempoolTransactionValue = function(buffer) { Encoding.prototype.encodeMempoolAddressKey = function(address, txid, index, input) { var buffers = [this.servicePrefix, this.addressPrefix]; - var addressSizeBuffer = new Buffer(1); + var addressSizeBuffer = Buffer.alloc(1); addressSizeBuffer.writeUInt8(address.length); - var addressBuffer = new Buffer(address, 'utf8'); + var addressBuffer = Buffer.from(address, 'utf8'); buffers.push(addressSizeBuffer); buffers.push(addressBuffer); - var txidBuffer = new Buffer(txid || Array(65).join('0'), 'hex'); + var txidBuffer = Buffer.from(txid || Array(65).join('0'), 'hex'); buffers.push(txidBuffer); - var indexBuffer = new Buffer(4); + var indexBuffer = Buffer.alloc(4); indexBuffer.writeUInt32BE(index || 0); buffers.push(indexBuffer); // this is whether the address appears in an input (1) or output (0) - var inputBuffer = new Buffer(1); + var inputBuffer = Buffer.alloc(1); inputBuffer.writeUInt8(input || 0); buffers.push(inputBuffer); diff --git a/lib/services/mempool/index.js b/lib/services/mempool/index.js index a0bcd284..e05cf318 100644 --- a/lib/services/mempool/index.js +++ b/lib/services/mempool/index.js @@ -283,7 +283,7 @@ MempoolService.prototype.getTxidsByAddress = function(address, type, callback) { var self = this; var results = []; var start = self._encoding.encodeMempoolAddressKey(address); - var end = Buffer.concat([ start.slice(0, -37), new Buffer(new Array(75).join('f'), 'hex') ]); + var end = Buffer.concat([ start.slice(0, -37), Buffer.from(new Array(75).join('f'), 'hex') ]); var criteria = { gte: start, diff --git a/lib/services/timestamp/encoding.js b/lib/services/timestamp/encoding.js index e34ffe3f..6fbfbc84 100644 --- a/lib/services/timestamp/encoding.js +++ b/lib/services/timestamp/encoding.js @@ -2,13 +2,13 @@ function Encoding(servicePrefix) { this._servicePrefix = servicePrefix; - this._blockPrefix = new Buffer('00', 'hex'); - this._timestampPrefix = new Buffer('01', 'hex'); + this._blockPrefix = Buffer.from('00', 'hex'); + this._timestampPrefix = Buffer.from('01', 'hex'); } // ---- block hash -> timestamp Encoding.prototype.encodeBlockTimestampKey = function(hash) { - return Buffer.concat([this._servicePrefix, this._blockPrefix, new Buffer(hash, 'hex')]); + return Buffer.concat([this._servicePrefix, this._blockPrefix, Buffer.from(hash, 'hex')]); }; Encoding.prototype.decodeBlockTimestampKey = function(buffer) { @@ -16,7 +16,7 @@ Encoding.prototype.decodeBlockTimestampKey = function(buffer) { }; Encoding.prototype.encodeBlockTimestampValue = function(timestamp) { - var timestampBuffer = new Buffer(4); + var timestampBuffer = Buffer.alloc(4); timestampBuffer.writeUInt32BE(timestamp); return timestampBuffer; }; @@ -28,7 +28,7 @@ Encoding.prototype.decodeBlockTimestampValue = function(buffer) { // ---- timestamp -> block hash Encoding.prototype.encodeTimestampBlockKey = function(timestamp) { - var timestampBuffer = new Buffer(4); + var timestampBuffer = Buffer.alloc(4); timestampBuffer.writeUInt32BE(timestamp); return Buffer.concat([this._servicePrefix, this._timestampPrefix, timestampBuffer]); }; @@ -38,7 +38,7 @@ Encoding.prototype.decodeTimestampBlockKey = function(buffer) { }; Encoding.prototype.encodeTimestampBlockValue = function(hash) { - return new Buffer(hash, 'hex'); + return Buffer.from(hash, 'hex'); }; Encoding.prototype.decodeTimestampBlockValue = function(buffer) { diff --git a/lib/services/transaction/encoding.js b/lib/services/transaction/encoding.js index fb3e886f..df59bdac 100644 --- a/lib/services/transaction/encoding.js +++ b/lib/services/transaction/encoding.js @@ -4,13 +4,13 @@ var Tx = require('fcoin').TX; function Encoding(servicePrefix) { this.servicePrefix = servicePrefix; - this.txIndex = new Buffer('00', 'hex'); - this.spentIndex = new Buffer('01', 'hex'); - this.doubleSpentIndex = new Buffer('02', 'hex'); + this.txIndex = Buffer.from('00', 'hex'); + this.spentIndex = Buffer.from('01', 'hex'); + this.doubleSpentIndex = Buffer.from('02', 'hex'); } Encoding.prototype.encodeTransactionKey = function(txid) { - return Buffer.concat([this.servicePrefix, this.txIndex, new Buffer(txid, 'hex')]); + return Buffer.concat([this.servicePrefix, this.txIndex, Buffer.from(txid, 'hex')]); }; Encoding.prototype.decodeTransactionKey = function(buffer) { @@ -18,24 +18,24 @@ Encoding.prototype.decodeTransactionKey = function(buffer) { }; Encoding.prototype.encodeTransactionValue = function(transaction) { - var heightBuffer = new Buffer(4); + var heightBuffer = Buffer.alloc(4); heightBuffer.writeUInt32BE(transaction.__height); - var hashBuffer = new Buffer(transaction.__blockhash, 'hex'); + var hashBuffer = Buffer.from(transaction.__blockhash, 'hex'); - var timestampBuffer = new Buffer(4); + var timestampBuffer = Buffer.alloc(4); timestampBuffer.writeUInt32BE(transaction.__timestamp); var inputValues = transaction.__inputValues; - var inputValuesBuffer = new Buffer(8 * inputValues.length); + var inputValuesBuffer = Buffer.alloc(8 * inputValues.length); for(var i = 0; i < inputValues.length; i++) { inputValuesBuffer.writeDoubleBE(inputValues[i], i * 8); } - var inputValuesLengthBuffer = new Buffer(2); + var inputValuesLengthBuffer = Buffer.alloc(2); inputValuesLengthBuffer.writeUInt16BE(inputValues.length); - return new Buffer.concat([heightBuffer, hashBuffer, timestampBuffer, + return Buffer.concat([heightBuffer, hashBuffer, timestampBuffer, inputValuesLengthBuffer, inputValuesBuffer, transaction.toRaw()]); }; @@ -65,9 +65,9 @@ Encoding.prototype.decodeTransactionValue = function(buffer) { // for every input we receive, we make an entry for what output it spends Encoding.prototype.encodeSpentKey = function(txid, outputIndex) { - var outputIndexBuffer = new Buffer(4); + var outputIndexBuffer = Buffer.alloc(4); outputIndexBuffer.writeUInt32BE(outputIndex); - return Buffer.concat([this.servicePrefix, this.spentIndex, new Buffer(txid, 'hex'), outputIndexBuffer]); + return Buffer.concat([this.servicePrefix, this.spentIndex, Buffer.from(txid, 'hex'), outputIndexBuffer]); }; Encoding.prototype.decodeSpentKey = function(buffer) { @@ -80,12 +80,12 @@ Encoding.prototype.decodeSpentKey = function(buffer) { }; Encoding.prototype.encodeSpentValue = function(txid, inputIndex, blockHeight, blockHash) { - var inputIndexBuffer = new Buffer(4); + var inputIndexBuffer = Buffer.alloc(4); inputIndexBuffer.writeUInt32BE(inputIndex); - var blockHeightBuffer = new Buffer(4); + var blockHeightBuffer = Buffer.alloc(4); blockHeightBuffer.writeUInt32BE(blockHeight); - var blockHashBuffer = new Buffer(blockHash, 'hex'); - return Buffer.concat([new Buffer(txid, 'hex'), inputIndexBuffer, blockHeightBuffer, blockHashBuffer]); + var blockHashBuffer = Buffer.from(blockHash, 'hex'); + return Buffer.concat([Buffer.from(txid, 'hex'), inputIndexBuffer, blockHeightBuffer, blockHashBuffer]); }; Encoding.prototype.decodeSpentValue = function(buffer) { @@ -102,9 +102,9 @@ Encoding.prototype.decodeSpentValue = function(buffer) { }; Encoding.prototype.encodeDoubleSpentKey = function(txid, outputIndex) { - var outputIndexBuffer = new Buffer(4); + var outputIndexBuffer = Buffer.alloc(4); outputIndexBuffer.writeUInt32BE(outputIndex); - return Buffer.concat([this.servicePrefix, this.spentIndex, new Buffer(txid, 'hex'), outputIndexBuffer]); + return Buffer.concat([this.servicePrefix, this.spentIndex, Buffer.from(txid, 'hex'), outputIndexBuffer]); }; Encoding.prototype.decodeDoubleSpentKey = function(buffer) { @@ -117,12 +117,12 @@ Encoding.prototype.decodeDoubleSpentKey = function(buffer) { }; Encoding.prototype.encodeDoubleSpentValue = function(txid, inputIndex, blockHeight, blockHash) { - var inputIndexBuffer = new Buffer(4); + var inputIndexBuffer = Buffer.alloc(4); inputIndexBuffer.writeUInt32BE(inputIndex); - var blockHeightBuffer = new Buffer(4); + var blockHeightBuffer = Buffer.alloc(4); blockHeightBuffer.writeUInt32BE(inputIndex); - var blockHashBuffer = new Buffer(blockHash, 'hex'); - return Buffer.concat([new Buffer(txid, 'hex'), inputIndexBuffer, blockHeightBuffer, blockHashBuffer]); + var blockHashBuffer = Buffer.from(blockHash, 'hex'); + return Buffer.concat([Buffer.from(txid, 'hex'), inputIndexBuffer, blockHeightBuffer, blockHashBuffer]); }; Encoding.prototype.decodeDoubleSpentValue = function(buffer) { diff --git a/lib/utils.js b/lib/utils.js index c6ddb011..089ff4ed 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -58,12 +58,12 @@ utils.sendError = function(err, res) { utils.encodeTip = function(tip, name) { var key = Buffer.concat([ constants.DB_PREFIX, - new Buffer('tip-' + name, 'utf8') ]); + Buffer.from('tip-' + name, 'utf8') ]); - var heightBuf = new Buffer(4); + var heightBuf = Buffer.alloc(4); heightBuf.writeUInt32BE(tip.height); - var value = Buffer.concat([ heightBuf, new Buffer(tip.hash, 'hex') ]); + var value = Buffer.concat([ heightBuf, Buffer.from(tip.hash, 'hex') ]); return { key: key, value: value }; }; diff --git a/test/services/address/encoding.unit.js b/test/services/address/encoding.unit.js index 6dbc6852..ccff5130 100644 --- a/test/services/address/encoding.unit.js +++ b/test/services/address/encoding.unit.js @@ -6,27 +6,27 @@ var Encoding = require('../../../lib/services/address/encoding'); describe('Address service encoding', function() { - var servicePrefix = new Buffer('0000', 'hex'); + var servicePrefix = Buffer.from('0000', 'hex'); var encoding = new Encoding(servicePrefix); var txid = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; var address = '1EZBqbJSHFKSkVPNKzc5v26HA6nAHiTXq6'; var height = 1; - var addressSizeBuf = new Buffer(1); - var prefix0 = new Buffer('00', 'hex'); - var prefix1 = new Buffer('01', 'hex'); + var addressSizeBuf = Buffer.alloc(1); + var prefix0 = Buffer.from('00', 'hex'); + var prefix1 = Buffer.from('01', 'hex'); var ts = Math.floor(new Date('2017-02-28').getTime() / 1000); - var tsBuf = new Buffer(4); + var tsBuf = Buffer.alloc(4); tsBuf.writeUInt32BE(ts); addressSizeBuf.writeUInt8(address.length); var addressIndexKeyBuf = Buffer.concat([ servicePrefix, prefix0, addressSizeBuf, - new Buffer(address), - new Buffer('00000001', 'hex'), - new Buffer(txid, 'hex'), - new Buffer('00000000', 'hex'), - new Buffer('00', 'hex'), + Buffer.from(address), + Buffer.from('00000001', 'hex'), + Buffer.from(txid, 'hex'), + Buffer.from('00000000', 'hex'), + Buffer.from('00', 'hex'), tsBuf ]); var outputIndex = 5; @@ -34,15 +34,15 @@ describe('Address service encoding', function() { servicePrefix, prefix1, addressSizeBuf, - new Buffer(address), - new Buffer(txid, 'hex'), - new Buffer('00000005', 'hex')]); + Buffer.from(address), + Buffer.from(txid, 'hex'), + Buffer.from('00000005', 'hex')]); var txHex = '0100000001cc3ffe0638792c8b39328bb490caaefe2cf418f2ce0144956e0c22515f29724d010000006a473044022030ce9fa68d1a32abf0cd4adecf90fb998375b64fe887c6987278452b068ae74c022036a7d00d1c8af19e298e04f14294c807ebda51a20389ad751b4ff3c032cf8990012103acfcb348abb526526a9f63214639d79183871311c05b2eebc727adfdd016514fffffffff02f6ae7d04000000001976a9144455183e407ee4d3423858c8a3275918aedcd18e88aca99b9b08010000001976a9140beceae2c29bfde08d2b6d80b33067451c5887be88ac00000000'; var tx = new flocore.Transaction(txHex); var sats = tx.outputs[0].satoshis; - var satsBuf = new Buffer(8); + var satsBuf = Buffer.alloc(8); satsBuf.writeDoubleBE(sats); - var utxoValueBuf = Buffer.concat([new Buffer('00000001', 'hex'), satsBuf, tsBuf, tx.outputs[0]._scriptBuffer]); + var utxoValueBuf = Buffer.concat([Buffer.from('00000001', 'hex'), satsBuf, tsBuf, tx.outputs[0]._scriptBuffer]); it('should encode address key' , function() { encoding.encodeAddressIndexKey(address, height, txid, 0, 0, ts).should.deep.equal(addressIndexKeyBuf); diff --git a/test/services/address/index.unit.js b/test/services/address/index.unit.js index afa7744b..262c2232 100644 --- a/test/services/address/index.unit.js +++ b/test/services/address/index.unit.js @@ -24,7 +24,7 @@ describe('Address Service', function() { services: [] } }); - addressService._encoding = new Encoding(new Buffer('0000', 'hex')); + addressService._encoding = new Encoding(Buffer.from('0000', 'hex')); }); afterEach(function() { @@ -34,7 +34,7 @@ describe('Address Service', function() { describe('#start', function() { it('should get prefix for database', function(done) { - var getPrefix = sandbox.stub().callsArgWith(1, null, new Buffer('ffee', 'hex')); + var getPrefix = sandbox.stub().callsArgWith(1, null, Buffer.from('ffee', 'hex')); addressService._db = { getPrefix: getPrefix }; addressService.start(function() { expect(getPrefix.calledOnce).to.be.true; @@ -355,7 +355,7 @@ describe('Address Service', function() { describe('#getAddressUnspentOutputs', function() { it('should get address utxos', function(done) { - var encoding = new Encoding(new Buffer('0001', 'hex')); + var encoding = new Encoding(Buffer.from('0001', 'hex')); addressService._encoding = encoding; var address = 'a'; diff --git a/test/services/block/encoding.unit.js b/test/services/block/encoding.unit.js index fb0d5897..9d962772 100644 --- a/test/services/block/encoding.unit.js +++ b/test/services/block/encoding.unit.js @@ -7,7 +7,7 @@ var Encoding = require('../../../lib/services/block/encoding'); describe('Block service encoding', function() { - var servicePrefix = new Buffer('0000', 'hex'); + var servicePrefix = Buffer.from('0000', 'hex'); var encoding = new Encoding(servicePrefix); var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; @@ -19,14 +19,14 @@ describe('Block service encoding', function() { it('should encode block key' , function() { encoding.encodeBlockKey(hash).should.deep.equal(Buffer.concat([ servicePrefix, - new Buffer(hash, 'hex') + Buffer.from(hash, 'hex') ])); }); it('should decode block key' , function() { var buf = Buffer.concat([ servicePrefix, - new Buffer(hash, 'hex') + Buffer.from(hash, 'hex') ]); var actual = encoding.decodeBlockKey(buf); diff --git a/test/services/block/index.unit.js b/test/services/block/index.unit.js index b2da9ae5..07835462 100644 --- a/test/services/block/index.unit.js +++ b/test/services/block/index.unit.js @@ -23,7 +23,7 @@ describe('Block Service', function() { services: [] } }); - blockService._encoding = new Encoding(new Buffer('0000', 'hex')); + blockService._encoding = new Encoding(Buffer.from('0000', 'hex')); }); afterEach(function() { diff --git a/test/services/db/index.unit.js b/test/services/db/index.unit.js index ec7b323e..66a82000 100644 --- a/test/services/db/index.unit.js +++ b/test/services/db/index.unit.js @@ -121,7 +121,7 @@ describe('DB', function() { it('should put a value in the db', function(done) { var put = sandbox.stub().callsArgWith(2, null); dbService._store = { put: put }; - dbService.put(new Buffer('key'), new Buffer('value'), function(err) { + dbService.put(Buffer.from('key'), Buffer.from('value'), function(err) { if (err) { return done(err); } @@ -132,7 +132,7 @@ describe('DB', function() { it('should not allow an operation while the node is shutting down', function(done) { dbService._stopping = true; - dbService.put(new Buffer('key'), new Buffer('value'), function(err) { + dbService.put(Buffer.from('key'), Buffer.from('value'), function(err) { done(); }); }); @@ -256,7 +256,7 @@ describe('DB', function() { describe('#getServiceTip', function() { it('should get service tip for previously saved', function(done) { - var tipBuf = Buffer.concat([ new Buffer('deadbeef', 'hex'), new Buffer(tx.txid(), 'hex') ]); + var tipBuf = Buffer.concat([ Buffer.from('deadbeef', 'hex'), Buffer.from(tx.txid(), 'hex') ]); var get = sandbox.stub(dbService, 'get').callsArgWith(1, null, tipBuf); dbService.getServiceTip('test', function(err, tip) { @@ -293,7 +293,7 @@ describe('DB', function() { describe('#getPrefix', function() { it('should get the db prefix for a service when one already exists', function(done) { - var get = sandbox.stub(dbService, 'get').callsArgWith(1, null, new Buffer('0000', 'hex')); + var get = sandbox.stub(dbService, 'get').callsArgWith(1, null, Buffer.from('0000', 'hex')); dbService.getPrefix('test', function(err, prefix) { if(err) { @@ -310,7 +310,7 @@ describe('DB', function() { var put = sandbox.stub(dbService, 'put').callsArgWith(2, null); var get = sandbox.stub(dbService, 'get'); get.onCall(0).callsArgWith(1, null, null); - get.onCall(1).callsArgWith(1, null, new Buffer('eeee', 'hex')); + get.onCall(1).callsArgWith(1, null, Buffer.from('eeee', 'hex')); dbService.getPrefix('test', function(err, prefix) { if(err) { diff --git a/test/services/header/encoding.unit.js b/test/services/header/encoding.unit.js index 301b6c07..5a13bf14 100644 --- a/test/services/header/encoding.unit.js +++ b/test/services/header/encoding.unit.js @@ -6,13 +6,13 @@ var Encoding = require('../../../lib/services/header/encoding'); describe('Header service encoding', function() { - var servicePrefix = new Buffer('0000', 'hex'); + var servicePrefix = Buffer.from('0000', 'hex'); - var hashPrefix = new Buffer('00', 'hex'); - var heightPrefix = new Buffer('01', 'hex'); + var hashPrefix = Buffer.from('00', 'hex'); + var heightPrefix = Buffer.from('01', 'hex'); var encoding = new Encoding(servicePrefix); var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; - var hashBuf = new Buffer(hash, 'hex'); + var hashBuf = Buffer.from(hash, 'hex'); var header = { hash: hash, prevHash: '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03ade', @@ -25,15 +25,15 @@ describe('Header service encoding', function() { chainwork: '0000000000000000000000000000000000000000000000000000000200020002', nextHash: '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03ade' }; - var versionBuf = new Buffer(4); - var prevHashBuf = new Buffer(header.prevHash, 'hex'); - var nextHashBuf = new Buffer(header.nextHash, 'hex'); - var merkleRootBuf = new Buffer(header.merkleRoot, 'hex'); - var tsBuf = new Buffer(4); - var bitsBuf = new Buffer(4); - var nonceBuf = new Buffer(4); - var heightBuf = new Buffer(4); - var chainBuf = new Buffer('0000000000000000000000000000000000000000000000000000000200020002', 'hex'); + var versionBuf = Buffer.alloc(4); + var prevHashBuf = Buffer.from(header.prevHash, 'hex'); + var nextHashBuf = Buffer.from(header.nextHash, 'hex'); + var merkleRootBuf = Buffer.from(header.merkleRoot, 'hex'); + var tsBuf = Buffer.alloc(4); + var bitsBuf = Buffer.alloc(4); + var nonceBuf = Buffer.alloc(4); + var heightBuf = Buffer.alloc(4); + var chainBuf = Buffer.from('0000000000000000000000000000000000000000000000000000000200020002', 'hex'); heightBuf.writeUInt32BE(header.height); it('should encode header hash key' , function() { @@ -54,7 +54,7 @@ describe('Header service encoding', function() { .should.deep.equal(header.height); }); it('should encode header value', function() { - var prevHashBuf = new Buffer(header.prevHash, 'hex'); + var prevHashBuf = Buffer.from(header.prevHash, 'hex'); versionBuf.writeInt32BE(header.version); // signed tsBuf.writeUInt32BE(header.timestamp); bitsBuf.writeUInt32BE(header.bits); diff --git a/test/services/header/index.unit.js b/test/services/header/index.unit.js index fcf3b350..0b9eac6e 100644 --- a/test/services/header/index.unit.js +++ b/test/services/header/index.unit.js @@ -16,8 +16,8 @@ describe('Header Service', function() { var headerService; var sandbox; - var prevHeader = new Block(new Buffer('01000000b25c0849b469983b4a5b90a49e4c0e4ba3853122ed141b5bd92d14000000000021a8aaa4995e4ce3b885677730b153741feda66a08492287a45c6a131671ba5a72ff504c5a0c011c456e4d060201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c028208ffffffff0100f2052a010000004341041994d910507ec4b2135dd32a4723caf00f8567f356ffbd5e703786d856b49a89d6597c280d8981238fbde81fa3767161bc3e994c17be41b42235a61c24c73459ac0000000001000000013b517d1aebd89b4034e0cf9b25ecbe82ef162ce71284e92a1f1adebf44ea1409000000008b483045022100c7ebc62e89740ddab42a64435c996e1c91a063f9f2cc004b4f023f7a1be5234402207608837faebec16049461d4ef7de807ce217040fd2a823a29da16ec07e463d440141048f108c0da4b5be3308e2e0b521d02d341de85b36a29285b47f00bc33e57a89cf4b6e76aa4a48ddc9a5e882620779e0f1b19dc98d478052fbd544167c745be1d8ffffffff010026e85a050000001976a914f760ef90462b0a4bde26d597c1f29324f5cd0fc488ac00000000', 'hex')).header.toObject(); - var preObjectHeader = new Block(new Buffer('010000006a39821735ec18a366d95b391a7ff10dee181a198f1789b0550e0d00000000002b0c80fa52b669022c344c3e09e6bb9698ab90707bb4bb412af3fbf31cfd2163a601514c5a0c011c572aef0f0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c022003ffffffff0100f2052a01000000434104c5b694d72e601091fd733c6b18b94795c13e2db6b1474747e7be914b407854cad37cee3058f85373b9f9dbb0014e541c45851d5f85e83a1fd7c45e54423718f3ac00000000', 'hex')).header; + var prevHeader = new Block(Buffer.from('01000000b25c0849b469983b4a5b90a49e4c0e4ba3853122ed141b5bd92d14000000000021a8aaa4995e4ce3b885677730b153741feda66a08492287a45c6a131671ba5a72ff504c5a0c011c456e4d060201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c028208ffffffff0100f2052a010000004341041994d910507ec4b2135dd32a4723caf00f8567f356ffbd5e703786d856b49a89d6597c280d8981238fbde81fa3767161bc3e994c17be41b42235a61c24c73459ac0000000001000000013b517d1aebd89b4034e0cf9b25ecbe82ef162ce71284e92a1f1adebf44ea1409000000008b483045022100c7ebc62e89740ddab42a64435c996e1c91a063f9f2cc004b4f023f7a1be5234402207608837faebec16049461d4ef7de807ce217040fd2a823a29da16ec07e463d440141048f108c0da4b5be3308e2e0b521d02d341de85b36a29285b47f00bc33e57a89cf4b6e76aa4a48ddc9a5e882620779e0f1b19dc98d478052fbd544167c745be1d8ffffffff010026e85a050000001976a914f760ef90462b0a4bde26d597c1f29324f5cd0fc488ac00000000', 'hex')).header.toObject(); + var preObjectHeader = new Block(Buffer.from('010000006a39821735ec18a366d95b391a7ff10dee181a198f1789b0550e0d00000000002b0c80fa52b669022c344c3e09e6bb9698ab90707bb4bb412af3fbf31cfd2163a601514c5a0c011c572aef0f0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c022003ffffffff0100f2052a01000000434104c5b694d72e601091fd733c6b18b94795c13e2db6b1474747e7be914b407854cad37cee3058f85373b9f9dbb0014e541c45851d5f85e83a1fd7c45e54423718f3ac00000000', 'hex')).header; var header = preObjectHeader.toObject(); beforeEach(function() { sandbox = sinon.sandbox.create(); @@ -26,7 +26,7 @@ describe('Header Service', function() { services: [] } }); - headerService._encoding = new Encoding(new Buffer('0000', 'hex')); + headerService._encoding = new Encoding(Buffer.from('0000', 'hex')); }); afterEach(function() { @@ -40,7 +40,7 @@ describe('Header Service', function() { var getServiceTip = sandbox.stub().callsArgWith(1, null, { height: 123, hash: 'a' }); var setListeners = sandbox.stub(headerService, '_setListeners'); - var getPrefix = sandbox.stub().callsArgWith(1, null, new Buffer('ffee', 'hex')); + var getPrefix = sandbox.stub().callsArgWith(1, null, Buffer.from('ffee', 'hex')); var adjustHeadersForCheckPointTip = sandbox.stub(headerService, '_adjustHeadersForCheckPointTip').callsArgWith(0, null); var setGenesisBlock = sandbox.stub(headerService, '_setGenesisBlock').callsArgWith(0, null); headerService.GENESIS_HASH = '00'; @@ -159,8 +159,8 @@ describe('Header Service', function() { describe('#_computeChainwork', function() { it('should calculate chain work correctly', function() { - var expected = new BN(new Buffer('000000000000000000000000000000000000000000677c7b8122f9902c79f4e0', 'hex')); - var prev = new BN(new Buffer('000000000000000000000000000000000000000000677bd68118a98f8779ea90', 'hex')); + var expected = new BN(Buffer.from('000000000000000000000000000000000000000000677c7b8122f9902c79f4e0', 'hex')); + var prev = new BN(Buffer.from('000000000000000000000000000000000000000000677bd68118a98f8779ea90', 'hex')); var actual = headerService._computeChainwork(0x18018d30, prev); assert(actual.eq(expected), 'not equal: actual: ' + actual + ' expected: ' + expected); diff --git a/test/services/mempool/encoding.unit.js b/test/services/mempool/encoding.unit.js index 309e557a..842475a6 100644 --- a/test/services/mempool/encoding.unit.js +++ b/test/services/mempool/encoding.unit.js @@ -7,36 +7,36 @@ var Encoding = require('../../../lib/services/mempool/encoding'); describe('Block service encoding', function() { - var servicePrefix = new Buffer('0000', 'hex'); - var txPrefix = new Buffer('00', 'hex'); - var addressPrefix = new Buffer('01', 'hex'); + var servicePrefix = Buffer.from('0000', 'hex'); + var txPrefix = Buffer.from('00', 'hex'); + var addressPrefix = Buffer.from('01', 'hex'); var encoding = new Encoding(servicePrefix); var hash = '25e28f9fb0ada5353b7d98d85af5524b2f8df5b0b0e2d188f05968bceca603eb'; var txString = '0100000004de9b4bb17f627096a9ee0b4528e4eae17df5b5c69edc29704c2e84a7371db29f010000006b483045022100f5b1a0d33b7be291c3953c25f8ae39d98601aa7099a8674daf638a08b86c7173022006ce372da5ad088a1cc6e5c49c2760a1b6f085eb1b51b502211b6bc9508661f9012102ec5e3731e54475dd2902326f43602a03ae3d62753324139163f81f20e787514cffffffff7a1d4e5fc2b8177ec738cd723a16cf2bf493791e55573445fc0df630fe5e2d64010000006b483045022100cf97f6cb8f126703e9768545dfb20ffb10ba78ae3d101aa46775f5a239b075fc02203150c4a89a11eaf5e404f4f96b62efa4455e9525765a025525c7105a7e47b6db012102c01e11b1d331f999bbdb83e8831de503cd52a01e3834a95ccafd615c67703d77ffffffff9e52447116415ca0d0567418a1a4ef8f27be3ff5a96bf87c922f3723d7db5d7c000000006b483045022100f6c117e536701be41a6b0b544d7c3b1091301e4e64a6265b6eb167b15d16959d022076916de4b115e700964194ce36a24cb9105f86482f4abbc63110c3f537cd5770012102ddf84cc7bee2d6a82ac09628a8ad4a26cd449fc528b81e7e6cc615707b8169dfffffffff5815d9750eb3572e30d6fd9df7afb4dbd76e042f3aa4988ac763b3fdf8397f80010000006a473044022028f4402b736066d93d2a32b28ccd3b7a21d84bb58fcd07fe392a611db94cdec5022018902ee0bf2c3c840c1b81ead4e6c87c88c48b2005bf5eea796464e561a620a8012102b6cdd1a6cd129ef796faeedb0b840fcd0ca00c57e16e38e46ee7028d59812ae7ffffffff0220a10700000000001976a914c342bcd1a7784d9842f7386b8b3b8a3d4171a06e88ac59611100000000001976a91449f8c749a9960dc29b5cbe7d2397cea7d26611bb88ac00000000'; var address = '1234567'; var now = Math.floor(Date.now() / 1000); - var nowBuf = new Buffer(4); + var nowBuf = Buffer.alloc(4); nowBuf.writeUInt32BE(now); describe('Mempool', function() { it('should encode mempool transaction key', function() { - encoding.encodeMempoolTransactionKey(hash).should.deep.equal(Buffer.concat([ servicePrefix, txPrefix, new Buffer(hash, 'hex') ])); + encoding.encodeMempoolTransactionKey(hash).should.deep.equal(Buffer.concat([ servicePrefix, txPrefix, Buffer.from(hash, 'hex') ])); }); it('should decode mempool transaction key', function() { - encoding.decodeMempoolTransactionKey(Buffer.concat([ servicePrefix, txPrefix, new Buffer(hash, 'hex') ])).should.deep.equal(hash); + encoding.decodeMempoolTransactionKey(Buffer.concat([ servicePrefix, txPrefix, Buffer.from(hash, 'hex') ])).should.deep.equal(hash); }); it('should encode mempool transaction value', function() { var mytx = tx.fromRaw(txString, 'hex'); mytx.__inputValues = [1012955, 447698, 446664, 391348]; - encoding.encodeMempoolTransactionValue(mytx).should.deep.equal(new Buffer(txString, 'hex')); + encoding.encodeMempoolTransactionValue(mytx).should.deep.equal(Buffer.from(txString, 'hex')); }); it('should decode mempool transaction value', function() { - var mytx = encoding.decodeMempoolTransactionValue(new Buffer(txString, 'hex')); + var mytx = encoding.decodeMempoolTransactionValue(Buffer.from(txString, 'hex')); mytx.should.deep.equal(tx.fromRaw(txString, 'hex')); }); @@ -46,11 +46,11 @@ describe('Block service encoding', function() { .should.deep.equal(Buffer.concat([ servicePrefix, addressPrefix, - new Buffer('07', 'hex'), - new Buffer(address), - new Buffer(hash, 'hex'), - new Buffer('00000000', 'hex'), - new Buffer('01', 'hex') + Buffer.from('07', 'hex'), + Buffer.from(address), + Buffer.from(hash, 'hex'), + Buffer.from('00000000', 'hex'), + Buffer.from('01', 'hex') ])); }); @@ -58,11 +58,11 @@ describe('Block service encoding', function() { encoding.decodeMempoolAddressKey(Buffer.concat([ servicePrefix, addressPrefix, - new Buffer('07', 'hex'), - new Buffer(address), - new Buffer(hash, 'hex'), - new Buffer('00000000', 'hex'), - new Buffer('01', 'hex') ])).should.deep.equal({ + Buffer.from('07', 'hex'), + Buffer.from(address), + Buffer.from(hash, 'hex'), + Buffer.from('00000000', 'hex'), + Buffer.from('01', 'hex') ])).should.deep.equal({ address: address, txid: hash, index: 0, diff --git a/test/services/mempool/index.unit.js b/test/services/mempool/index.unit.js index 0c83bda6..ddc6198c 100644 --- a/test/services/mempool/index.unit.js +++ b/test/services/mempool/index.unit.js @@ -21,7 +21,7 @@ describe('Mempool Service', function() { services: [] } }); - mempoolService._encoding = new Encoding(new Buffer('0000', 'hex')); + mempoolService._encoding = new Encoding(Buffer.from('0000', 'hex')); }); afterEach(function() { @@ -31,7 +31,7 @@ describe('Mempool Service', function() { describe('#start', function() { it('should get the db prefix', function(done) { - var getPrefix = sandbox.stub().callsArgWith(1, null, new Buffer('0001', 'hex')); + var getPrefix = sandbox.stub().callsArgWith(1, null, Buffer.from('0001', 'hex')); mempoolService._db = { getPrefix: getPrefix }; mempoolService.start(function() { diff --git a/test/services/timestamp/encoding.unit.js b/test/services/timestamp/encoding.unit.js index b5c1ab49..b8c950cd 100644 --- a/test/services/timestamp/encoding.unit.js +++ b/test/services/timestamp/encoding.unit.js @@ -5,21 +5,21 @@ var Encoding = require('../../../lib/services/timestamp/encoding'); describe('Timestamp service encoding', function() { - var servicePrefix = new Buffer('0000', 'hex'); - var blockPrefix = new Buffer('00', 'hex'); - var timestampPrefix = new Buffer('01', 'hex'); + var servicePrefix = Buffer.from('0000', 'hex'); + var blockPrefix = Buffer.from('00', 'hex'); + var timestampPrefix = Buffer.from('01', 'hex'); var encoding = new Encoding(servicePrefix); var blockhash = '00000000000000000115b92b1ff4377441049bff75c6c48b626eb99e8b744297'; var timestamp = 5; - var timestampBuf = new Buffer(4); + var timestampBuf = Buffer.alloc(4); timestampBuf.writeUInt32BE(timestamp); it('should encode block timestamp key' , function() { - encoding.encodeBlockTimestampKey(blockhash).should.deep.equal(Buffer.concat([servicePrefix, blockPrefix, new Buffer(blockhash, 'hex')])); + encoding.encodeBlockTimestampKey(blockhash).should.deep.equal(Buffer.concat([servicePrefix, blockPrefix, Buffer.from(blockhash, 'hex')])); }); it('should decode block timestamp key', function() { - var blockTimestampKey = encoding.decodeBlockTimestampKey(Buffer.concat([servicePrefix, blockPrefix, new Buffer(blockhash, 'hex')])); + var blockTimestampKey = encoding.decodeBlockTimestampKey(Buffer.concat([servicePrefix, blockPrefix, Buffer.from(blockhash, 'hex')])); blockTimestampKey.should.equal(blockhash); }); @@ -40,10 +40,10 @@ describe('Timestamp service encoding', function() { }); it('should encode timestamp block value', function() { - encoding.encodeTimestampBlockValue(blockhash).should.deep.equal(new Buffer(blockhash, 'hex')); + encoding.encodeTimestampBlockValue(blockhash).should.deep.equal(Buffer.from(blockhash, 'hex')); }); it('should decode timestamp block value', function() { - encoding.decodeTimestampBlockValue(new Buffer(blockhash, 'hex')).should.equal(blockhash); + encoding.decodeTimestampBlockValue(Buffer.from(blockhash, 'hex')).should.equal(blockhash); }); }); diff --git a/test/services/transaction/encoding.unit.js b/test/services/transaction/encoding.unit.js index 3b8c28db..7ff73275 100644 --- a/test/services/transaction/encoding.unit.js +++ b/test/services/transaction/encoding.unit.js @@ -6,24 +6,24 @@ var Encoding = require('../../../lib/services/transaction/encoding'); describe('Transaction service encoding', function() { - var servicePrefix = new Buffer('0000', 'hex'); + var servicePrefix = Buffer.from('0000', 'hex'); var encoding = new Encoding(servicePrefix); var txid = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; var blockHash = txid; var txHex = '0100000001cc3ffe0638792c8b39328bb490caaefe2cf418f2ce0144956e0c22515f29724d010000006a473044022030ce9fa68d1a32abf0cd4adecf90fb998375b64fe887c6987278452b068ae74c022036a7d00d1c8af19e298e04f14294c807ebda51a20389ad751b4ff3c032cf8990012103acfcb348abb526526a9f63214639d79183871311c05b2eebc727adfdd016514fffffffff02f6ae7d04000000001976a9144455183e407ee4d3423858c8a3275918aedcd18e88aca99b9b08010000001976a9140beceae2c29bfde08d2b6d80b33067451c5887be88ac00000000'; var tx = Tx.fromRaw(txHex, 'hex'); - var txEncoded = Buffer.concat([new Buffer('00000002', 'hex'), new Buffer(blockHash, 'hex'), new Buffer('00000001', 'hex'), new Buffer('0002', 'hex'), new Buffer('40000000000000004008000000000000', 'hex'), tx.toRaw()]); - var indexBuf = new Buffer(4); + var txEncoded = Buffer.concat([Buffer.from('00000002', 'hex'), Buffer.from(blockHash, 'hex'), Buffer.from('00000001', 'hex'), Buffer.from('0002', 'hex'), Buffer.from('40000000000000004008000000000000', 'hex'), tx.toRaw()]); + var indexBuf = Buffer.alloc(4); indexBuf.writeUInt32BE(3); it('should encode transaction key' , function() { - var txBuf = new Buffer(txid, 'hex'); - encoding.encodeTransactionKey(txid).should.deep.equal(Buffer.concat([servicePrefix, new Buffer('00', 'hex'), txBuf])); + var txBuf = Buffer.from(txid, 'hex'); + encoding.encodeTransactionKey(txid).should.deep.equal(Buffer.concat([servicePrefix, Buffer.from('00', 'hex'), txBuf])); }); it('should decode transaction key', function() { - encoding.decodeTransactionKey(Buffer.concat([servicePrefix, new Buffer('00', 'hex'), new Buffer(txid, 'hex')])) + encoding.decodeTransactionKey(Buffer.concat([servicePrefix, Buffer.from('00', 'hex'), Buffer.from(txid, 'hex')])) .should.equal(txid); }); @@ -47,21 +47,21 @@ describe('Transaction service encoding', function() { it('should encode spent key', function() { encoding.encodeSpentKey(txid, 3).should.deep.equal(Buffer.concat([servicePrefix, - new Buffer('01', 'hex'), new Buffer(txid, 'hex'), indexBuf])); + Buffer.from('01', 'hex'), Buffer.from(txid, 'hex'), indexBuf])); }); it('should decode spent key', function() { encoding.decodeSpentKey(Buffer.concat([servicePrefix, - new Buffer('01', 'hex'), new Buffer(txid, 'hex'), indexBuf])).should.deep.equal({ txid: txid, outputIndex: 3 }); + Buffer.from('01', 'hex'), Buffer.from(txid, 'hex'), indexBuf])).should.deep.equal({ txid: txid, outputIndex: 3 }); }); it('should encode spent value', function() { - encoding.encodeSpentValue(txid, 3, 3, txid).should.deep.equal(Buffer.concat([new Buffer(txid, 'hex'), indexBuf, indexBuf, new Buffer(blockHash, 'hex')])); + encoding.encodeSpentValue(txid, 3, 3, txid).should.deep.equal(Buffer.concat([Buffer.from(txid, 'hex'), indexBuf, indexBuf, Buffer.from(blockHash, 'hex')])); }); it('should decode spent value', function() { - encoding.decodeSpentValue(Buffer.concat([new Buffer(txid, 'hex'), indexBuf, - indexBuf, new Buffer(blockHash, 'hex')])) + encoding.decodeSpentValue(Buffer.concat([Buffer.from(txid, 'hex'), indexBuf, + indexBuf, Buffer.from(blockHash, 'hex')])) .should.deep.equal({ txid: txid, inputIndex: 3, blockHeight: 3, blockHash: blockHash }); }); }); diff --git a/test/services/transaction/index.unit.js b/test/services/transaction/index.unit.js index 850a7e96..54792599 100644 --- a/test/services/transaction/index.unit.js +++ b/test/services/transaction/index.unit.js @@ -21,7 +21,7 @@ describe('Transaction Service', function() { services: [] } }); - txService._encoding = new Encoding(new Buffer('0000', 'hex')); + txService._encoding = new Encoding(Buffer.from('0000', 'hex')); }); afterEach(function() { @@ -30,7 +30,7 @@ describe('Transaction Service', function() { describe('#start', function() { it('should get the prefix and the service tip', function(done) { - var getPrefix = sandbox.stub().callsArgWith(1, null, new Buffer('ffee', 'hex')); + var getPrefix = sandbox.stub().callsArgWith(1, null, Buffer.from('ffee', 'hex')); txService._db = { getPrefix: getPrefix }; txService.start(function() { getPrefix.calledOnce.should.be.true; diff --git a/test/utils.unit.js b/test/utils.unit.js index cc86ce0b..11c610f5 100644 --- a/test/utils.unit.js +++ b/test/utils.unit.js @@ -39,13 +39,13 @@ describe('Utils', function() { describe('#getTerminalKey', function() { it('should get the terminal key for a buffer', function() { - utils.getTerminalKey(new Buffer('ffff', 'hex')) - .should.deep.equal(new Buffer('010000', 'hex')); + utils.getTerminalKey(Buffer.from('ffff', 'hex')) + .should.deep.equal(Buffer.from('010000', 'hex')); }); it('should get the terminal key for a large buffer', function() { - utils.getTerminalKey(Buffer.concat([ new Buffer(new Array(64).join('f'), 'hex'), new Buffer('fe', 'hex') ])) - .should.deep.equal(new Buffer('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex')); + utils.getTerminalKey(Buffer.concat([ Buffer.from(new Array(64).join('f'), 'hex'), Buffer.from('fe', 'hex') ])) + .should.deep.equal(Buffer.from('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex')); }); }); @@ -91,8 +91,8 @@ describe('Utils', function() { it('should encode tip', function() { var res = utils.encodeTip({ height: 0xdeadbeef, hash: new Array(65).join('0') }, 'test'); res.should.deep.equal({ - key: new Buffer('ffff7469702d74657374', 'hex'), - value: new Buffer('deadbeef00000000000000000000000000000000000000000000000000000000000000000', 'hex') + key: Buffer.from('ffff7469702d74657374', 'hex'), + value: Buffer.from('deadbeef00000000000000000000000000000000000000000000000000000000000000000', 'hex') }); }); });