diff --git a/bench/buffer.js b/bench/buffer.js index ad509f8b..c466fe69 100644 --- a/bench/buffer.js +++ b/bench/buffer.js @@ -11,7 +11,7 @@ var tx3 = parseTX('../test/data/tx3.hex'); var wtx = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8'); var i, tx, end, raw; -wtx = new Buffer(wtx.trim(), 'hex'); +wtx = Buffer.from(wtx.trim(), 'hex'); tx = TX.fromRaw(wtx); function parseTX(file) { diff --git a/bench/chacha.js b/bench/chacha.js index 34ca9bcc..9d1f2673 100644 --- a/bench/chacha.js +++ b/bench/chacha.js @@ -8,11 +8,11 @@ var i, chacha, iv, poly, key, data, end; console.log('note: rate measured in kb/s'); chacha = new chachapoly.ChaCha20(); -key = new Buffer(32); +key = Buffer.allocUnsafe(32); key.fill(2); -iv = new Buffer('0102030405060708', 'hex'); +iv = Buffer.from('0102030405060708', 'hex'); chacha.init(key, iv, 0); -data = new Buffer(32); +data = Buffer.allocUnsafe(32); for (i = 0; i < 32; i++) data[i] = i; end = bench('encrypt'); @@ -21,11 +21,11 @@ for (i = 0; i < 1000000; i++) end(i * 32 / 1024); poly = new chachapoly.Poly1305(); -key = new Buffer(32); +key = Buffer.allocUnsafe(32); key.fill(2); poly.init(key); -data = new Buffer(32); +data = Buffer.allocUnsafe(32); for (i = 0; i < 32; i++) data[i] = i & 0xff; diff --git a/bench/tx.js b/bench/tx.js index 0c81c3e2..9b852098 100644 --- a/bench/tx.js +++ b/bench/tx.js @@ -20,7 +20,7 @@ var tx3 = parseTX('../test/data/tx3.hex'); var wtx = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8'); var i, tx, raw, end, flags, input; -wtx = new Buffer(wtx.trim(), 'hex'); +wtx = Buffer.from(wtx.trim(), 'hex'); tx = json.txs[397]; for (i = 0; i < tx.inputs.length; i++) { @@ -116,7 +116,7 @@ for (i = 0; i < 100; i++) { index: 0 }, script: [ - new Buffer(9), + Buffer.allocUnsafe(9), crypto.randomBytes(33) ] }); diff --git a/browser/transform.js b/browser/transform.js index bdd0ecb9..fef5f49e 100644 --- a/browser/transform.js +++ b/browser/transform.js @@ -31,13 +31,13 @@ function transformer(file, process) { stream._transform = function(chunk, encoding, callback) { assert(Buffer.isBuffer(chunk)); str += decoder.write(chunk); - callback(null, new Buffer(0)); + callback(null, Buffer.allocUnsafe(0)); }; stream._flush = function(callback) { str = process(str); - stream.push(new Buffer(str, 'utf8')); + stream.push(Buffer.from(str, 'utf8')); callback(); }; diff --git a/browser/wsproxy.js b/browser/wsproxy.js index e0c0fbe6..700c1380 100644 --- a/browser/wsproxy.js +++ b/browser/wsproxy.js @@ -10,7 +10,7 @@ var BufferWriter = require('../lib/utils/writer'); var NAME_REGEX = /^[a-z0-9\-\.]+?\.(?:be|me|org|com|net|ch|de)$/i; -var TARGET = new Buffer( +var TARGET = Buffer.from( '0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex'); @@ -185,7 +185,7 @@ WSProxy.prototype._handleConnect = function _handleConnect(ws, port, host, nonce ws.on('tcp data', function(data) { if (typeof data !== 'string') return; - socket.write(new Buffer(data, 'hex')); + socket.write(Buffer.from(data, 'hex')); }); ws.on('tcp keep alive', function(enable, delay) { diff --git a/lib/bip70/payment.js b/lib/bip70/payment.js index ded75ac3..82a84c29 100644 --- a/lib/bip70/payment.js +++ b/lib/bip70/payment.js @@ -145,7 +145,7 @@ Payment.prototype.fromRaw = function fromRaw(data) { Payment.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Payment().fromRaw(data); }; diff --git a/lib/bip70/paymentack.js b/lib/bip70/paymentack.js index 462705d1..12ce792e 100644 --- a/lib/bip70/paymentack.js +++ b/lib/bip70/paymentack.js @@ -85,7 +85,7 @@ PaymentACK.prototype.fromRaw = function fromRaw(data) { PaymentACK.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new PaymentACK().fromRaw(data); }; diff --git a/lib/bip70/paymentdetails.js b/lib/bip70/paymentdetails.js index 5b46e556..e947f1fc 100644 --- a/lib/bip70/paymentdetails.js +++ b/lib/bip70/paymentdetails.js @@ -127,11 +127,11 @@ PaymentDetails.prototype.setData = function setData(data, enc) { if (typeof data !== 'string') { assert(!enc || enc === 'json'); - this.merchantData = new Buffer(JSON.stringify(data), 'utf8'); + this.merchantData = Buffer.from(JSON.stringify(data), 'utf8'); return; } - this.merchantData = new Buffer(data, enc); + this.merchantData = Buffer.from(data, enc); }; /** @@ -200,7 +200,7 @@ PaymentDetails.prototype.fromRaw = function fromRaw(data) { PaymentDetails.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new PaymentDetails().fromRaw(data); }; diff --git a/lib/bip70/paymentrequest.js b/lib/bip70/paymentrequest.js index 25c9d4d0..ddc3b498 100644 --- a/lib/bip70/paymentrequest.js +++ b/lib/bip70/paymentrequest.js @@ -116,7 +116,7 @@ PaymentRequest.prototype.fromRaw = function fromRaw(data) { PaymentRequest.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new PaymentRequest().fromRaw(data); }; @@ -179,7 +179,7 @@ PaymentRequest.prototype.signatureData = function signatureData() { var signature = this.signature; var data; - this.signature = new Buffer(0); + this.signature = Buffer.alloc(0); data = this.toRaw(); diff --git a/lib/bip70/x509.js b/lib/bip70/x509.js index 0ac22019..ed7631e1 100644 --- a/lib/bip70/x509.js +++ b/lib/bip70/x509.js @@ -176,7 +176,7 @@ x509.setFingerprints = function setFingerprints(hashes) { hash = hashes[i]; if (typeof hash === 'string') - hash = new Buffer(hash, 'hex'); + hash = Buffer.from(hash, 'hex'); assert(Buffer.isBuffer(hash), 'Fingerprint must be a buffer.'); assert(hash.length === 32, 'Fingerprint must be a sha256 hash.'); diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index a7c1010b..3c66596e 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -28,7 +28,7 @@ var ChainEntry = require('./chainentry'); var TXMeta = require('../primitives/txmeta'); var U8 = encoding.U8; var U32 = encoding.U32; -var DUMMY = new Buffer([0]); +var DUMMY = Buffer.from([0]); /** * The database backend for the {@link Chain} object. @@ -2137,7 +2137,7 @@ ChainState.prototype.spend = function spend(coin) { ChainState.prototype.commit = function commit(hash) { if (typeof hash === 'string') - hash = new Buffer(hash, 'hex'); + hash = Buffer.from(hash, 'hex'); this.tip = hash; this.committed = true; return this.toRaw(); diff --git a/lib/blockchain/layout.js b/lib/blockchain/layout.js index ede79632..d3b0d99e 100644 --- a/lib/blockchain/layout.js +++ b/lib/blockchain/layout.js @@ -29,9 +29,9 @@ var layout = { binary: true, - R: new Buffer([0x52]), - O: new Buffer([0x4f]), - V: new Buffer([0x76]), + R: Buffer.from([0x52]), + O: Buffer.from([0x4f]), + V: Buffer.from([0x76]), e: function e(hash) { return pair(0x65, hash); }, @@ -60,7 +60,7 @@ var layout = { return pair(0x75, hash); }, v: function v(bit, hash) { - var key = new Buffer(1 + 1 + 32); + var key = Buffer.allocUnsafe(1 + 1 + 32); key[0] = 0x76; key[1] = bit; write(key, hash, 2); @@ -77,12 +77,12 @@ var layout = { len /= 2; if (len === 32) { - key = new Buffer(65); + key = Buffer.allocUnsafe(65); key[0] = 0xab; // W + T write(key, address, 1); write(key, hash, 33); } else { - key = new Buffer(53); + key = Buffer.allocUnsafe(53); key[0] = 0x54; // T write(key, address, 1); write(key, hash, 21); @@ -98,13 +98,13 @@ var layout = { len /= 2; if (len === 32) { - key = new Buffer(69); + key = Buffer.allocUnsafe(69); key[0] = 0x9a; // W + C write(key, address, 1); write(key, hash, 33); key.writeUInt32BE(index, 65, true); } else { - key = new Buffer(57); + key = Buffer.allocUnsafe(57); key[0] = 0x43; // C write(key, address, 1); write(key, hash, 21); @@ -147,14 +147,14 @@ function write(data, str, off) { } function pair(prefix, hash) { - var key = new Buffer(33); + var key = Buffer.allocUnsafe(33); key[0] = prefix; write(key, hash, 1); return key; } function ipair(prefix, num) { - var key = new Buffer(5); + var key = Buffer.allocUnsafe(5); key[0] = prefix; key.writeUInt32BE(num, 1, true); return key; diff --git a/lib/coins/compress.js b/lib/coins/compress.js index 978e52f6..1c8fdbcf 100644 --- a/lib/coins/compress.js +++ b/lib/coins/compress.js @@ -21,7 +21,7 @@ var consensus = require('../protocol/consensus'); */ var COMPRESS_TYPES = 10; // Space for 4 extra. -var EMPTY_BUFFER = new Buffer(0); +var EMPTY_BUFFER = Buffer.alloc(0); /** * Compress a script, write directly to the buffer. diff --git a/lib/crypto/aes.js b/lib/crypto/aes.js index dd94abb7..56b9b7b2 100644 --- a/lib/crypto/aes.js +++ b/lib/crypto/aes.js @@ -357,7 +357,7 @@ AESKey.prototype.encryptBlock = function encryptBlock(input) { ^ (TE1[(t2 >>> 0) & 0xff] & 0x000000ff) ^ key[kp + 3]; - output = new Buffer(16); + output = Buffer.allocUnsafe(16); writeU32(output, s0, 0); writeU32(output, s1, 4); writeU32(output, s2, 8); @@ -462,7 +462,7 @@ AESKey.prototype.decryptBlock = function decryptBlock(input) { ^ (TD4[(t0 >>> 0) & 0xff] << 0) ^ key[kp + 3]; - output = new Buffer(16); + output = Buffer.allocUnsafe(16); writeU32(output, s0, 0); writeU32(output, s1, 4); writeU32(output, s2, 8); @@ -536,12 +536,12 @@ AESCipher.prototype.final = function final() { // Handle padding on the last block. if (!this.waiting) { - block = new Buffer(16); + block = Buffer.allocUnsafe(16); block.fill(16); } else { block = this.waiting; left = 16 - block.length; - pad = new Buffer(left); + pad = Buffer.allocUnsafe(left); pad.fill(left); block = concat(block, pad); } @@ -652,7 +652,7 @@ AESDecipher.prototype.final = function final() { // Slice off the padding unless // the entire block was padding. if (n === 16) - return new Buffer(0); + return Buffer.alloc(0); block = block.slice(0, -n); @@ -771,7 +771,7 @@ AES.decipher = AES.cbc.decrypt; */ function xor(v1, v2) { - var out = new Buffer(v1.length); + var out = Buffer.allocUnsafe(v1.length); for (var i = 0; i < v1.length; i++) out[i] = v1[i] ^ v2[i]; return out; @@ -792,7 +792,7 @@ function writeU32(data, value, i) { } function concat(a, b) { - var data = new Buffer(a.length + b.length); + var data = Buffer.allocUnsafe(a.length + b.length); a.copy(data, 0); b.copy(data, a.length); return data; diff --git a/lib/crypto/backend-browser.js b/lib/crypto/backend-browser.js index ac1f00e8..fd3b90e4 100644 --- a/lib/crypto/backend-browser.js +++ b/lib/crypto/backend-browser.js @@ -30,7 +30,7 @@ backend.hash = function hash(alg, data) { assert(hash != null, 'Unknown algorithm.'); - return new Buffer(hash().update(data).digest()); + return Buffer.from(hash().update(data).digest()); }; backend.ripemd160 = function ripemd160(data) { @@ -61,7 +61,7 @@ backend.hmac = function _hmac(alg, data, key) { hmac = hashjs.hmac(hash, key); - return new Buffer(hmac.update(data).digest()); + return Buffer.from(hmac.update(data).digest()); }; /* @@ -69,11 +69,11 @@ backend.hmac = function _hmac(alg, data, key) { */ backend.pbkdf2 = function pbkdf2(key, salt, iter, len, alg) { - var size = backend.hash(alg, new Buffer(0)).length; + var size = backend.hash(alg, Buffer.alloc(0)).length; var blocks = Math.ceil(len / size); - var out = new Buffer(len); - var buf = new Buffer(salt.length + 4); - var block = new Buffer(size); + var out = Buffer.allocUnsafe(len); + var buf = Buffer.allocUnsafe(salt.length + 4); + var block = Buffer.allocUnsafe(size); var pos = 0; var i, j, k, mac; @@ -114,7 +114,7 @@ backend.pbkdf2Async = function pbkdf2Async(key, salt, iter, len, alg) { return promise.then(function(key) { return subtle.deriveBits(options, key, length); }).then(function(result) { - return new Buffer(result); + return Buffer.from(result); }); }; @@ -144,13 +144,13 @@ backend.decipher = function decipher(data, key, iv) { backend.randomBytes = function randomBytes(n) { var data = new Uint8Array(n); crypto.getRandomValues(data); - return new Buffer(data.buffer); + return Buffer.from(data.buffer); }; if (!crypto.getRandomValues) { // Out of luck here. Use bad randomness for now. backend.randomBytes = function randomBytes(n) { - var data = new Buffer(n); + var data = Buffer.allocUnsafe(n); var i; for (i = 0; i < data.length; i++) diff --git a/lib/crypto/chachapoly.js b/lib/crypto/chachapoly.js index 23fcaf94..faafed09 100644 --- a/lib/crypto/chachapoly.js +++ b/lib/crypto/chachapoly.js @@ -31,7 +31,7 @@ function ChaCha20() { this.bytes = new Uint8Array(this.stream.buffer); if (BIG_ENDIAN) - this.bytes = new Buffer(64); + this.bytes = Buffer.allocUnsafe(64); this.pos = 0; this.ivSize = 0; @@ -223,7 +223,7 @@ function Poly1305() { this.pad = new Uint16Array(8); this.fin = 0; this.leftover = 0; - this.buffer = new Buffer(16); + this.buffer = Buffer.allocUnsafe(16); } /** @@ -382,7 +382,7 @@ Poly1305.prototype.update = function update(data) { */ Poly1305.prototype.finish = function finish() { - var mac = new Buffer(16); + var mac = Buffer.allocUnsafe(16); var g = new Uint16Array(10); var c, mask, f, i; @@ -527,7 +527,7 @@ function AEAD() { */ AEAD.prototype.init = function init(key, iv) { - var polyKey = new Buffer(32); + var polyKey = Buffer.allocUnsafe(32); polyKey.fill(0); this.chacha20.init(key, iv); @@ -536,7 +536,7 @@ AEAD.prototype.init = function init(key, iv) { // We need to encrypt a full block // to get the cipher in the correct state. - this.chacha20.encrypt(new Buffer(32)); + this.chacha20.encrypt(Buffer.allocUnsafe(32)); // Counter should be one. assert(this.chacha20.getCounter() === 1); @@ -613,7 +613,7 @@ AEAD.prototype.auth = function auth(data) { */ AEAD.prototype.finish = function finish() { - var len = new Buffer(16); + var len = Buffer.allocUnsafe(16); var lo, hi; // The RFC says these are supposed to be @@ -652,7 +652,7 @@ AEAD.prototype.pad16 = function pad16(size) { if (size === 0) return; - pad = new Buffer(16 - size); + pad = Buffer.allocUnsafe(16 - size); pad.fill(0); this.poly1305.update(pad); diff --git a/lib/crypto/crypto.js b/lib/crypto/crypto.js index b8b9ffbb..637636c6 100644 --- a/lib/crypto/crypto.js +++ b/lib/crypto/crypto.js @@ -160,19 +160,19 @@ crypto.hkdfExtract = function hkdfExtract(ikm, key, alg) { */ crypto.hkdfExpand = function hkdfExpand(prk, info, len, alg) { - var size = crypto.hash(alg, new Buffer(0)).length; + var size = crypto.hash(alg, Buffer.alloc(0)).length; var blocks = Math.ceil(len / size); var i, okm, buf, out; if (blocks > 255) throw new Error('Too many blocks.'); - okm = new Buffer(len); + okm = Buffer.allocUnsafe(len); if (blocks === 0) return okm; - buf = new Buffer(size + info.length + 1); + buf = Buffer.allocUnsafe(size + info.length + 1); // First round: info.copy(buf, size); @@ -204,13 +204,13 @@ crypto.createMerkleTree = function createMerkleTree(leaves) { var i, j, k, hash, left, right, lr; if (size === 0) { - hash = new Buffer(32); + hash = Buffer.allocUnsafe(32); hash.fill(0); nodes.push(hash); return new MerkleTree(nodes, malleated); } - lr = new Buffer(64); + lr = Buffer.allocUnsafe(64); for (j = 0; size > 1; size = ((size + 1) / 2) | 0) { for (i = 0; i < size; i += 2) { @@ -290,7 +290,7 @@ crypto.verifyMerkleBranch = function verifyMerkleBranch(hash, branch, index) { if (branch.length === 0) return hash; - lr = new Buffer(64); + lr = Buffer.allocUnsafe(64); for (i = 0; i < branch.length; i++) { otherside = branch[i]; diff --git a/lib/crypto/ec-elliptic.js b/lib/crypto/ec-elliptic.js index 8b94cb4b..758b41ce 100644 --- a/lib/crypto/ec-elliptic.js +++ b/lib/crypto/ec-elliptic.js @@ -54,7 +54,7 @@ ec.publicKeyCreate = function publicKeyCreate(priv, compressed) { key = secp256k1.keyPair({ priv: priv }); key = key.getPublic(compressed !== false, 'array'); - return new Buffer(key); + return Buffer.from(key); }; /** @@ -65,7 +65,7 @@ ec.publicKeyCreate = function publicKeyCreate(priv, compressed) { ec.publicKeyConvert = function publicKeyConvert(key, compressed) { var point = curve.decodePoint(key); - return new Buffer(point.encode('array', compressed !== false)); + return Buffer.from(point.encode('array', compressed !== false)); }; /** @@ -98,7 +98,7 @@ ec.privateKeyTweakAdd = function privateKeyTweakAdd(privateKey, tweak) { ec.publicKeyTweakAdd = function publicKeyTweakAdd(publicKey, tweak, compressed) { var key = curve.decodePoint(publicKey); var point = curve.g.mul(new BN(tweak)).add(key); - var pub = new Buffer(point.encode('array', compressed !== false)); + var pub = Buffer.from(point.encode('array', compressed !== false)); if (!ec.publicKeyVerify(pub)) throw new Error('Public key is invalid.'); @@ -142,7 +142,7 @@ ec.recover = function recover(msg, sig, j, compressed) { key = point.encode('array', compressed !== false); - return new Buffer(key); + return Buffer.from(key); }; /** @@ -233,7 +233,7 @@ ec.sign = function sign(msg, key) { sig = secp256k1.sign(msg, key, { canonical: true }); // Convert to DER array - return new Buffer(sig.toDER()); + return Buffer.from(sig.toDER()); }; /** @@ -248,7 +248,7 @@ ec.fromDER = function fromDER(sig) { assert(Buffer.isBuffer(sig)); sig = new Signature(sig); - out = new Buffer(64); + out = Buffer.allocUnsafe(64); sig.r.toArrayLike(Buffer, 'be', 32).copy(out, 0); sig.s.toArrayLike(Buffer, 'be', 32).copy(out, 32); @@ -272,7 +272,7 @@ ec.toDER = function toDER(sig) { s: new BN(sig.slice(32, 64), 'be') }); - return new Buffer(out.toDER()); + return Buffer.from(out.toDER()); }; /** diff --git a/lib/crypto/ec-secp256k1.js b/lib/crypto/ec-secp256k1.js index 4b853a7b..e26c0c5b 100644 --- a/lib/crypto/ec-secp256k1.js +++ b/lib/crypto/ec-secp256k1.js @@ -22,12 +22,12 @@ var ec = exports; * Constants */ -var ZERO_S = new Buffer( +var ZERO_S = Buffer.from( '0000000000000000000000000000000000000000000000000000000000000000', 'hex' ); -var HALF_ORDER = new Buffer( +var HALF_ORDER = Buffer.from( '7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 'hex'); diff --git a/lib/crypto/pk-browser.js b/lib/crypto/pk-browser.js index 965828a7..a7dccf44 100644 --- a/lib/crypto/pk-browser.js +++ b/lib/crypto/pk-browser.js @@ -33,13 +33,13 @@ rsa = {}; */ rsa.prefixes = { - md5: new Buffer('3020300c06082a864886f70d020505000410', 'hex'), - sha1: new Buffer('3021300906052b0e03021a05000414', 'hex'), - sha224: new Buffer('302d300d06096086480165030402040500041c', 'hex'), - sha256: new Buffer('3031300d060960864801650304020105000420', 'hex'), - sha384: new Buffer('3041300d060960864801650304020205000430', 'hex'), - sha512: new Buffer('3051300d060960864801650304020305000440', 'hex'), - ripemd160: new Buffer('30203008060628cf060300310414', 'hex') + md5: Buffer.from('3020300c06082a864886f70d020505000410', 'hex'), + sha1: Buffer.from('3021300906052b0e03021a05000414', 'hex'), + sha224: Buffer.from('302d300d06096086480165030402040500041c', 'hex'), + sha256: Buffer.from('3031300d060960864801650304020105000420', 'hex'), + sha384: Buffer.from('3041300d060960864801650304020205000430', 'hex'), + sha512: Buffer.from('3051300d060960864801650304020305000440', 'hex'), + ripemd160: Buffer.from('30203008060628cf060300310414', 'hex') }; /** @@ -114,7 +114,7 @@ rsa.sign = function sign(alg, msg, key) { if (k < len + 11) throw new Error('Message too long.'); - em = new Buffer(k); + em = Buffer.allocUnsafe(k); em.fill(0); em[1] = 0x01; @@ -214,7 +214,7 @@ ecdsa.sign = function sign(curve, alg, msg, key) { ec = elliptic.ec(curve); hash = backend.hash(alg, msg); - return new Buffer(ec.sign(hash, key)); + return Buffer.from(ec.sign(hash, key)); }; /* @@ -228,7 +228,7 @@ function leftpad(input, size) { if (n > size) n = size; - out = new Buffer(size); + out = Buffer.allocUnsafe(size); out.fill(0); input.copy(out, out.length - n); diff --git a/lib/crypto/pk.js b/lib/crypto/pk.js index 5b5f5b4d..2bb20eb8 100644 --- a/lib/crypto/pk.js +++ b/lib/crypto/pk.js @@ -100,7 +100,7 @@ ecdsa.sign = function sign(curve, alg, msg, key) { ec = elliptic.ec(curve); hash = backend.hash(alg, msg); - return new Buffer(ec.sign(hash, key)); + return Buffer.from(ec.sign(hash, key)); }; /* diff --git a/lib/crypto/schnorr.js b/lib/crypto/schnorr.js index a745b48d..7f68bad7 100644 --- a/lib/crypto/schnorr.js +++ b/lib/crypto/schnorr.js @@ -32,7 +32,7 @@ var schnorr = exports; schnorr.hash = function _hash(msg, r, hash) { var R = r.toArrayLike(Buffer, 'be', 32); - var B = new Buffer(64); + var B = Buffer.allocUnsafe(64); var H; if (!hash) @@ -211,7 +211,7 @@ schnorr.recover = function recover(signature, msg, hash) { if (rl.getX().cmp(sig.r) !== 0) throw new Error('Could not recover pubkey.'); - return new Buffer(k.encode('array', true)); + return Buffer.from(k.encode('array', true)); }; /** @@ -273,7 +273,7 @@ schnorr.combineKeys = function combineKeys(keys) { point = point.add(key); } - return new Buffer(point.encode('array', true)); + return Buffer.from(point.encode('array', true)); }; /** @@ -309,7 +309,7 @@ schnorr.partialSign = function partialSign(msg, priv, privnonce, pubs, hash) { * @const {Buffer} */ -schnorr.alg = new Buffer('Schnorr+SHA256 ', 'ascii'); +schnorr.alg = Buffer.from('Schnorr+SHA256 ', 'ascii'); /** * Instantiate an HMAC-DRBG. @@ -320,7 +320,7 @@ schnorr.alg = new Buffer('Schnorr+SHA256 ', 'ascii'); */ schnorr.drbg = function drbg(msg, priv, data) { - var kdata = new Buffer(112); + var kdata = Buffer.allocUnsafe(112); var prv, pers; kdata.fill(0); @@ -356,7 +356,7 @@ schnorr.drbg = function drbg(msg, priv, data) { schnorr.rfc6979 = function rfc6979(msg, priv, data) { var drbg = schnorr.drbg(msg, priv, data); var bytes = drbg.generate(curve.n.byteLength()); - return new Buffer(bytes); + return Buffer.from(bytes); }; /** @@ -397,7 +397,7 @@ schnorr.generateNoncePair = function generateNoncePair(msg, priv, data, ncb) { if (k.cmp(curve.n) >= 0) throw new Error('Bad nonce.'); - return new Buffer(curve.g.mul(k).encode('array', true)); + return Buffer.from(curve.g.mul(k).encode('array', true)); }; /* diff --git a/lib/crypto/scrypt.js b/lib/crypto/scrypt.js index ca84d0bb..a53d4bd2 100644 --- a/lib/crypto/scrypt.js +++ b/lib/crypto/scrypt.js @@ -70,8 +70,8 @@ function scrypt(passwd, salt, N, r, p, len) { if (N > 0xffffffff) throw new Error('EINVAL'); - XY = new Buffer(256 * r); - V = new Buffer(128 * r * N); + XY = Buffer.allocUnsafe(256 * r); + V = Buffer.allocUnsafe(128 * r * N); B = backend.pbkdf2(passwd, salt, 1, p * 128 * r, 'sha256'); @@ -149,7 +149,7 @@ function R(a, b) { } function blockmix_salsa8(B, Y, Yo, r) { - var X = new Buffer(64); + var X = Buffer.allocUnsafe(64); var i; blkcpy(X, B, 0, (2 * r - 1) * 64, 64); @@ -227,8 +227,8 @@ scryptAsync = co(function* scryptAsync(passwd, salt, N, r, p, len) { if (N > 0xffffffff) throw new Error('EINVAL'); - XY = new Buffer(256 * r); - V = new Buffer(128 * r * N); + XY = Buffer.allocUnsafe(256 * r); + V = Buffer.allocUnsafe(128 * r * N); B = yield backend.pbkdf2Async(passwd, salt, 1, p * 128 * r, 'sha256'); diff --git a/lib/crypto/sha256.js b/lib/crypto/sha256.js index eaa3e6fa..328898d5 100644 --- a/lib/crypto/sha256.js +++ b/lib/crypto/sha256.js @@ -16,12 +16,12 @@ * Constants */ -var DESC = new Buffer(8); -var BUFFER64 = new Buffer(64); +var DESC = Buffer.allocUnsafe(8); +var BUFFER64 = Buffer.allocUnsafe(64); var K, PADDING; var ctx, mctx; -PADDING = new Buffer(64); +PADDING = Buffer.allocUnsafe(64); PADDING.fill(0); PADDING[0] = 0x80; @@ -60,7 +60,7 @@ function SHA256() { this.s = new Array(8); this.w = new Array(64); - this.block = new Buffer(64); + this.block = Buffer.allocUnsafe(64); this.bytes = 0; } @@ -95,7 +95,7 @@ SHA256.prototype.update = function update(data) { */ SHA256.prototype.finish = function finish() { - return this._finish(new Buffer(32)); + return this._finish(Buffer.allocUnsafe(32)); }; /** @@ -367,7 +367,7 @@ function sha256(data) { */ function hash256(data) { - var out = new Buffer(32); + var out = Buffer.allocUnsafe(32); ctx.init(); ctx.update(data); ctx._finish(out); diff --git a/lib/crypto/siphash.js b/lib/crypto/siphash.js index 67924dce..4261173a 100644 --- a/lib/crypto/siphash.js +++ b/lib/crypto/siphash.js @@ -226,7 +226,7 @@ U64.prototype.rotl = function rotl(b) { }; U64.prototype.toRaw = function toRaw() { - var data = new Buffer(8); + var data = Buffer.allocUnsafe(8); data.writeUInt32LE(this.hi, 4, true); data.writeUInt32LE(this.lo, 0, true); return data; diff --git a/lib/db/level.js b/lib/db/level.js index b80de0ae..9a6a0acb 100644 --- a/lib/db/level.js +++ b/lib/db/level.js @@ -130,10 +130,10 @@ Iterator.prototype.next = function(callback) { } if (key && self.db.bufferKeys) - key = new Buffer(key, 'hex'); + key = Buffer.from(key, 'hex'); if (value && !Buffer.isBuffer(value) && value.buffer) - value = new Buffer(value.buffer); + value = Buffer.from(value.buffer); callback(err, key, value); }); diff --git a/lib/db/lowlevelup.js b/lib/db/lowlevelup.js index d750e480..1647424d 100644 --- a/lib/db/lowlevelup.js +++ b/lib/db/lowlevelup.js @@ -410,10 +410,10 @@ LowlevelUp.prototype.compactRange = function compactRange(start, end) { var self = this; if (!start) - start = new Buffer([0x00]); + start = Buffer.from([0x00]); if (!end) - end = new Buffer([0xff]); + end = Buffer.from([0xff]); return new Promise(function(resolve, reject) { if (!self.loaded) { @@ -580,8 +580,8 @@ LowlevelUp.prototype.dump = co(function* dump() { var i, items, item, key, value; items = yield this.range({ - gte: new Buffer([0x00]), - lte: new Buffer([0xff]) + gte: Buffer.from([0x00]), + lte: Buffer.from([0xff]) }); for (i = 0; i < items.length; i++) { @@ -605,7 +605,7 @@ LowlevelUp.prototype.checkVersion = co(function* checkVersion(key, version) { var data = yield this.get(key); if (!data) { - data = new Buffer(4); + data = Buffer.allocUnsafe(4); data.writeUInt32LE(version, 0, true); yield this.put(key, data); return; diff --git a/lib/db/memdb.js b/lib/db/memdb.js index 9b0495c6..542c6ee1 100644 --- a/lib/db/memdb.js +++ b/lib/db/memdb.js @@ -9,7 +9,7 @@ var assert = require('assert'); var util = require('../utils/util'); var RBT = require('../utils/rbt'); -var DUMMY = new Buffer(0); +var DUMMY = Buffer.alloc(0); /** * In memory database for bcoin @@ -41,7 +41,7 @@ MemDB.prototype.search = function search(key) { var node; if (typeof key === 'string') - key = new Buffer(key, 'utf8'); + key = Buffer.from(key, 'utf8'); assert(Buffer.isBuffer(key), 'Key must be a Buffer.'); @@ -62,10 +62,10 @@ MemDB.prototype.search = function search(key) { MemDB.prototype.insert = function insert(key, value) { if (typeof key === 'string') - key = new Buffer(key, 'utf8'); + key = Buffer.from(key, 'utf8'); if (typeof value === 'string') - value = new Buffer(value, 'utf8'); + value = Buffer.from(value, 'utf8'); if (value == null) value = DUMMY; @@ -85,7 +85,7 @@ MemDB.prototype.insert = function insert(key, value) { MemDB.prototype.remove = function remove(key) { if (typeof key === 'string') - key = new Buffer(key, 'utf8'); + key = Buffer.from(key, 'utf8'); assert(Buffer.isBuffer(key), 'Key must be a Buffer.'); @@ -102,10 +102,10 @@ MemDB.prototype.remove = function remove(key) { MemDB.prototype.range = function range(min, max) { if (typeof min === 'string') - min = new Buffer(min, 'utf8'); + min = Buffer.from(min, 'utf8'); if (typeof max === 'string') - max = new Buffer(max, 'utf8'); + max = Buffer.from(max, 'utf8'); assert(!min || Buffer.isBuffer(min), 'Key must be a Buffer.'); assert(!max || Buffer.isBuffer(max), 'Key must be a Buffer.'); @@ -552,7 +552,7 @@ Iterator.prototype.seek = function seek(key) { assert(this.iter, 'Already ended.'); if (typeof key === 'string') - key = new Buffer(key, 'utf8'); + key = Buffer.from(key, 'utf8'); assert(Buffer.isBuffer(key), 'Key must be a Buffer.'); @@ -646,13 +646,13 @@ IteratorOptions.prototype.fromOptions = function fromOptions(options) { if (this.start != null) { if (typeof this.start === 'string') - this.start = new Buffer(this.start, 'utf8'); + this.start = Buffer.from(this.start, 'utf8'); assert(Buffer.isBuffer(this.start), '`start` must be a Buffer.'); } if (this.end != null) { if (typeof this.end === 'string') - this.end = new Buffer(this.end, 'utf8'); + this.end = Buffer.from(this.end, 'utf8'); assert(Buffer.isBuffer(this.end), '`end` must be a Buffer.'); } diff --git a/lib/hd/common.js b/lib/hd/common.js index f129aa18..2110c805 100644 --- a/lib/hd/common.js +++ b/lib/hd/common.js @@ -47,7 +47,7 @@ common.MAX_ENTROPY = 512; * @default */ -common.SEED_SALT = new Buffer('Bitcoin seed', 'ascii'); +common.SEED_SALT = Buffer.from('Bitcoin seed', 'ascii'); /** * LRU cache to avoid deriving keys twice. diff --git a/lib/hd/mnemonic.js b/lib/hd/mnemonic.js index 1ee2a5e7..02cb14d4 100644 --- a/lib/hd/mnemonic.js +++ b/lib/hd/mnemonic.js @@ -148,8 +148,8 @@ Mnemonic.prototype.toSeed = function toSeed(passphrase) { passwd = nfkd('mnemonic' + passphrase); return crypto.pbkdf2( - new Buffer(phrase, 'utf8'), - new Buffer(passwd, 'utf8'), + Buffer.from(phrase, 'utf8'), + Buffer.from(passwd, 'utf8'), 2048, 64, 'sha512'); }; @@ -192,7 +192,7 @@ Mnemonic.prototype.getPhrase = function getPhrase() { // Append the hash to the entropy to // make things easy when grabbing // the checksum bits. - entropy = new Buffer(Math.ceil(bits / 8)); + entropy = Buffer.allocUnsafe(Math.ceil(bits / 8)); ent.copy(entropy, 0); crypto.sha256(ent).copy(entropy, ent.length); @@ -244,7 +244,7 @@ Mnemonic.prototype.fromPhrase = function fromPhrase(phrase) { assert(bits % 32 === 0); assert(cbits !== 0, 'Invalid checksum.'); - ent = new Buffer(Math.ceil((bits + cbits) / 8)); + ent = Buffer.allocUnsafe(Math.ceil((bits + cbits) / 8)); ent.fill(0); lang = Mnemonic.getLanguage(words[0]); @@ -398,7 +398,7 @@ Mnemonic.prototype.fromJSON = function fromJSON(json) { this.bits = json.bits; this.language = json.language; - this.entropy = new Buffer(json.entropy, 'hex'); + this.entropy = Buffer.from(json.entropy, 'hex'); this.phrase = json.phrase; this.passphrase = json.passphrase; diff --git a/lib/hd/private.js b/lib/hd/private.js index 92f9a4e4..f7b18ee2 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -476,7 +476,7 @@ HDPrivateKey.prototype.fromSeed = function fromSeed(seed, network) { this.network = Network.get(network); this.depth = 0; - this.parentFingerPrint = new Buffer([0, 0, 0, 0]); + this.parentFingerPrint = Buffer.from([0, 0, 0, 0]); this.childIndex = 0; this.chainCode = right; this.privateKey = left; @@ -556,7 +556,7 @@ HDPrivateKey.prototype.fromKey = function fromKey(key, entropy, network) { assert(Buffer.isBuffer(entropy) && entropy.length === 32); this.network = Network.get(network); this.depth = 0; - this.parentFingerPrint = new Buffer([0, 0, 0, 0]); + this.parentFingerPrint = Buffer.from([0, 0, 0, 0]); this.childIndex = 0; this.chainCode = entropy; this.privateKey = key; diff --git a/lib/http/base.js b/lib/http/base.js index f55f49a0..11cb4d5e 100644 --- a/lib/http/base.js +++ b/lib/http/base.js @@ -196,13 +196,13 @@ HTTPBase.prototype.basicAuth = function basicAuth(options) { if (user) { if (typeof user === 'string') - user = new Buffer(user, 'utf8'); + user = Buffer.from(user, 'utf8'); assert(Buffer.isBuffer(user)); user = crypto.hash256(user); } if (typeof pass === 'string') - pass = new Buffer(pass, 'utf8'); + pass = Buffer.from(pass, 'utf8'); assert(Buffer.isBuffer(pass)); pass = crypto.hash256(pass); @@ -233,21 +233,21 @@ HTTPBase.prototype.basicAuth = function basicAuth(options) { if (parts[0] !== 'Basic') return fail(res); - auth = new Buffer(parts[1], 'base64').toString('utf8'); + auth = Buffer.from(parts[1], 'base64').toString('utf8'); parts = auth.split(':'); username = parts.shift(); password = parts.join(':'); if (user) { - digest = new Buffer(username, 'utf8'); + digest = Buffer.from(username, 'utf8'); digest = crypto.hash256(digest); if (!crypto.ccmp(digest, user)) return fail(res); } - digest = new Buffer(password, 'utf8'); + digest = Buffer.from(password, 'utf8'); digest = crypto.hash256(digest); if (!crypto.ccmp(digest, pass)) diff --git a/lib/http/request.js b/lib/http/request.js index 727a6dd4..1a5f67d4 100644 --- a/lib/http/request.js +++ b/lib/http/request.js @@ -142,13 +142,13 @@ RequestOptions.prototype.fromOptions = function fromOptions(options) { if (options.json != null) { assert(typeof options.json === 'object'); - this.body = new Buffer(JSON.stringify(options.json), 'utf8'); + this.body = Buffer.from(JSON.stringify(options.json), 'utf8'); this.type = 'json'; } if (options.form != null) { assert(typeof options.form === 'object'); - this.body = new Buffer(qs.stringify(options.form), 'utf8'); + this.body = Buffer.from(qs.stringify(options.form), 'utf8'); this.type = 'form'; } @@ -166,7 +166,7 @@ RequestOptions.prototype.fromOptions = function fromOptions(options) { if (options.body != null) { if (typeof options.body === 'string') { - this.body = new Buffer(options.body, 'utf8'); + this.body = Buffer.from(options.body, 'utf8'); } else { assert(Buffer.isBuffer(options.body)); this.body = options.body; @@ -245,7 +245,7 @@ RequestOptions.prototype.getHeaders = function getHeaders() { if (this.auth) { auth = this.auth.username + ':' + this.auth.password; headers['Authorization'] = - 'Basic ' + new Buffer(auth, 'utf8').toString('base64'); + 'Basic ' + Buffer.from(auth, 'utf8').toString('base64'); } return headers; diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 2974e0af..cc0f4400 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -1127,7 +1127,7 @@ RPC.prototype._createWork = co(function* _createWork() { var ts = attempt.ts; var data, root, head; - data = new Buffer(128); + data = Buffer.allocUnsafe(128); data.fill(0); root = attempt.getRoot(n1, n2); @@ -2064,7 +2064,7 @@ RPC.prototype.verifyMessage = co(function* verifyMessage(args, help) { addr = parseAddress(b58, this.network); - msg = new Buffer(MAGIC_STRING + msg, 'utf8'); + msg = Buffer.from(MAGIC_STRING + msg, 'utf8'); msg = crypto.hash256(msg); key = ec.recover(msg, sig, 0, true); @@ -2089,7 +2089,7 @@ RPC.prototype.signMessageWithPrivkey = co(function* signMessageWithPrivkey(args, } key = parseSecret(key, this.network); - msg = new Buffer(MAGIC_STRING + msg, 'utf8'); + msg = Buffer.from(MAGIC_STRING + msg, 'utf8'); msg = crypto.hash256(msg); sig = key.sign(msg); diff --git a/lib/http/server.js b/lib/http/server.js index dd25cb2a..b6ed3a50 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -844,12 +844,12 @@ HTTPOptions.fromOptions = function fromOptions(options) { function hash256(data) { if (typeof data !== 'string') - return new Buffer(0); + return Buffer.alloc(0); if (data.length > 200) - return new Buffer(0); + return Buffer.alloc(0); - return crypto.hash256(new Buffer(data, 'utf8')); + return crypto.hash256(Buffer.from(data, 'utf8')); } function enforce(value, msg) { diff --git a/lib/mempool/layout.js b/lib/mempool/layout.js index 70228297..45dc4b1e 100644 --- a/lib/mempool/layout.js +++ b/lib/mempool/layout.js @@ -15,11 +15,11 @@ var layout = { binary: true, - R: new Buffer([0x52]), - V: new Buffer([0x76]), - F: new Buffer([0x46]), + R: Buffer.from([0x52]), + V: Buffer.from([0x76]), + F: Buffer.from([0x46]), e: function e(hash) { - var key = new Buffer(33); + var key = Buffer.allocUnsafe(33); key[0] = 0x65; write(key, hash, 1); return key; diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 456957e6..524f64b3 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -2514,7 +2514,7 @@ MempoolCache.prototype.sync = function sync(hash) { if (!this.db) return; - this.batch.put(layout.R, new Buffer(hash, 'hex')); + this.batch.put(layout.R, Buffer.from(hash, 'hex')); }; MempoolCache.prototype.writeFees = function writeFees(fees) { @@ -2541,7 +2541,7 @@ MempoolCache.prototype.flush = co(function* flush() { MempoolCache.prototype.init = co(function* init(hash) { var batch = this.db.batch(); batch.put(layout.V, encoding.U32(MempoolCache.VERSION)); - batch.put(layout.R, new Buffer(hash, 'hex')); + batch.put(layout.R, Buffer.from(hash, 'hex')); yield batch.write(); }); @@ -2596,7 +2596,7 @@ MempoolCache.prototype.wipe = co(function* wipe() { } batch.put(layout.V, encoding.U32(MempoolCache.VERSION)); - batch.put(layout.R, new Buffer(this.chain.tip.hash, 'hex')); + batch.put(layout.R, Buffer.from(this.chain.tip.hash, 'hex')); batch.del(layout.F); yield batch.write(); diff --git a/lib/mining/common.js b/lib/mining/common.js index e2bff308..1e818467 100644 --- a/lib/mining/common.js +++ b/lib/mining/common.js @@ -50,7 +50,7 @@ common.swap32 = function swap32(data) { */ common.swap32hex = function swap32hex(str) { - var data = new Buffer(str, 'hex'); + var data = Buffer.from(str, 'hex'); return common.swap32(data).toString('hex'); }; diff --git a/lib/mining/miner.js b/lib/mining/miner.js index 350425f9..9701ea42 100644 --- a/lib/mining/miner.js +++ b/lib/mining/miner.js @@ -393,7 +393,7 @@ function MinerOptions(options) { this.version = -1; this.addresses = []; - this.coinbaseFlags = new Buffer('mined by bcoin', 'ascii'); + this.coinbaseFlags = Buffer.from('mined by bcoin', 'ascii'); this.preverify = false; this.minWeight = policy.MIN_BLOCK_WEIGHT; @@ -458,7 +458,7 @@ MinerOptions.prototype.fromOptions = function fromOptions(options) { if (options.coinbaseFlags) { flags = options.coinbaseFlags; if (typeof flags === 'string') - flags = new Buffer(flags, 'utf8'); + flags = Buffer.from(flags, 'utf8'); assert(Buffer.isBuffer(flags)); assert(flags.length <= 20, 'Coinbase flags > 20 bytes.'); this.coinbaseFlags = flags; diff --git a/lib/mining/template.js b/lib/mining/template.js index 91c97155..6bd9033d 100644 --- a/lib/mining/template.js +++ b/lib/mining/template.js @@ -23,7 +23,7 @@ var encoding = require('../utils/encoding'); var CoinView = require('../coins/coinview'); var Script = require('../script/script'); var common = require('./common'); -var DUMMY = new Buffer(0); +var DUMMY = Buffer.alloc(0); /** * Block Template diff --git a/lib/net/bip150.js b/lib/net/bip150.js index 46a73770..6fc3bd1d 100644 --- a/lib/net/bip150.js +++ b/lib/net/bip150.js @@ -251,7 +251,7 @@ BIP150.prototype.toChallenge = function toChallenge() { */ BIP150.prototype.rekey = function rekey(sid, key, req, res) { - var seed = new Buffer(130); + var seed = Buffer.allocUnsafe(130); sid.copy(seed, 0); key.copy(seed, 32); req.copy(seed, 64); @@ -296,7 +296,7 @@ BIP150.prototype.rekeyOutput = function rekeyOutput() { */ BIP150.prototype.hash = function hash(sid, ch, key) { - var data = new Buffer(66); + var data = Buffer.allocUnsafe(66); sid.copy(data, 0); data[32] = ch.charCodeAt(0); key.copy(data, 33); @@ -739,7 +739,7 @@ AuthDB.prototype.parseKnown = function parseKnown(text) { } key = parts[1].trim(); - key = new Buffer(key, 'hex'); + key = Buffer.from(key, 'hex'); if (key.length !== 33) throw new Error('Invalid key: ' + parts[1]); @@ -801,7 +801,7 @@ AuthDB.prototype.parseAuth = function parseAuth(text) { if (/^\s*#/.test(line)) continue; - key = new Buffer(line, 'hex'); + key = Buffer.from(line, 'hex'); if (key.length !== 33) throw new Error('Invalid key: ' + line); diff --git a/lib/net/bip151.js b/lib/net/bip151.js index ebc9b50c..b3d5ce32 100644 --- a/lib/net/bip151.js +++ b/lib/net/bip151.js @@ -30,10 +30,10 @@ var EncackPacket = packets.EncackPacket; * Constants */ -var HKDF_SALT = new Buffer('bitcoinecdh', 'ascii'); -var INFO_KEY1 = new Buffer('BitcoinK1', 'ascii'); -var INFO_KEY2 = new Buffer('BitcoinK2', 'ascii'); -var INFO_SID = new Buffer('BitcoinSessionID', 'ascii'); +var HKDF_SALT = Buffer.from('bitcoinecdh', 'ascii'); +var INFO_KEY1 = Buffer.from('BitcoinK1', 'ascii'); +var INFO_KEY2 = Buffer.from('BitcoinK2', 'ascii'); +var INFO_SID = Buffer.from('BitcoinSessionID', 'ascii'); var HIGH_WATERMARK = 1024 * (1 << 20); /** @@ -78,7 +78,7 @@ function BIP151Stream(cipher) { this.aead = new chachapoly.AEAD(); this.tag = null; this.seq = 0; - this.iv = new Buffer(8); + this.iv = Buffer.allocUnsafe(8); this.iv.fill(0); this.processed = 0; @@ -148,7 +148,7 @@ BIP151Stream.prototype.rekey = function rekey(k1, k2) { assert(this.prk, 'Cannot rekey before initialization.'); if (!k1) { - seed = new Buffer(64); + seed = Buffer.allocUnsafe(64); this.sid.copy(seed, 0); @@ -654,7 +654,7 @@ BIP151.prototype.read = function read(size) { assert(this.total >= size, 'Reading too much.'); if (size === 0) - return new Buffer(0); + return Buffer.alloc(0); pending = this.pending[0]; @@ -671,7 +671,7 @@ BIP151.prototype.read = function read(size) { return chunk; } - chunk = new Buffer(size); + chunk = Buffer.allocUnsafe(size); off = 0; len = 0; diff --git a/lib/net/bip152.js b/lib/net/bip152.js index 4e02441f..e95cba05 100644 --- a/lib/net/bip152.js +++ b/lib/net/bip152.js @@ -171,7 +171,7 @@ CompactBlock.prototype.fromRaw = function fromRaw(data) { CompactBlock.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new CompactBlock().fromRaw(data); }; @@ -385,7 +385,7 @@ CompactBlock.prototype.sid = function sid(hash) { var lo, hi; if (typeof hash === 'string') - hash = new Buffer(hash, 'hex'); + hash = Buffer.from(hash, 'hex'); hash = siphash(hash, this.sipKey); diff --git a/lib/net/framer.js b/lib/net/framer.js index 29426d49..e042bfc1 100644 --- a/lib/net/framer.js +++ b/lib/net/framer.js @@ -40,7 +40,7 @@ Framer.prototype.packet = function packet(cmd, payload, checksum) { assert(cmd.length < 12); assert(payload.length <= 0xffffffff); - packet = new Buffer(24 + payload.length); + packet = Buffer.allocUnsafe(24 + payload.length); // Magic value packet.writeUInt32LE(this.network.magic, 0, true); diff --git a/lib/net/packets.js b/lib/net/packets.js index a3ad3f54..d88678f3 100644 --- a/lib/net/packets.js +++ b/lib/net/packets.js @@ -25,7 +25,7 @@ var TX = require('../primitives/tx'); var BufferReader = require('../utils/reader'); var StaticWriter = require('../utils/staticwriter'); var encoding = require('../utils/encoding'); -var DUMMY = new Buffer(0); +var DUMMY = Buffer.alloc(0); /** * Packet types. @@ -350,7 +350,7 @@ VersionPacket.prototype.fromRaw = function fromRaw(data) { VersionPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new VersionPacket().fromRaw(data, enc); }; @@ -390,7 +390,7 @@ VerackPacket.fromReader = function fromReader(br) { VerackPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new VerackPacket().fromRaw(data); }; @@ -486,7 +486,7 @@ PingPacket.fromReader = function fromReader(br) { PingPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new PingPacket().fromRaw(data); }; @@ -579,7 +579,7 @@ PongPacket.fromReader = function fromReader(br) { PongPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new PongPacket().fromRaw(data); }; @@ -619,7 +619,7 @@ GetAddrPacket.fromReader = function fromReader(br) { GetAddrPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new GetAddrPacket().fromRaw(data); }; @@ -721,7 +721,7 @@ AddrPacket.fromReader = function fromReader(br) { AddrPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new AddrPacket().fromRaw(data); }; @@ -836,7 +836,7 @@ InvPacket.fromReader = function fromReader(br) { InvPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new InvPacket().fromRaw(data); }; @@ -878,7 +878,7 @@ GetDataPacket.fromReader = function fromReader(br) { GetDataPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new GetDataPacket().fromRaw(data); }; @@ -920,7 +920,7 @@ NotFoundPacket.fromReader = function fromReader(br) { NotFoundPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new NotFoundPacket().fromRaw(data); }; @@ -1039,7 +1039,7 @@ GetBlocksPacket.prototype.fromRaw = function fromRaw(data) { GetBlocksPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new GetBlocksPacket().fromRaw(data); }; @@ -1082,7 +1082,7 @@ GetHeadersPacket.fromReader = function fromReader(br) { GetHeadersPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new GetHeadersPacket().fromRaw(data); }; @@ -1193,7 +1193,7 @@ HeadersPacket.prototype.fromRaw = function fromRaw(data) { HeadersPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new HeadersPacket().fromRaw(data); }; @@ -1233,7 +1233,7 @@ SendHeadersPacket.fromReader = function fromReader(br) { SendHeadersPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new SendHeadersPacket().fromRaw(data); }; @@ -1335,7 +1335,7 @@ BlockPacket.fromReader = function fromReader(br) { BlockPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new BlockPacket().fromRaw(data); }; @@ -1437,7 +1437,7 @@ TXPacket.fromReader = function fromReader(br) { TXPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new TXPacket().fromRaw(data); }; @@ -1665,7 +1665,7 @@ RejectPacket.fromReader = function fromReader(br) { RejectPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new RejectPacket().fromRaw(data, enc); }; @@ -1775,7 +1775,7 @@ MempoolPacket.fromReader = function fromReader(br) { MempoolPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new MempoolPacket().fromRaw(data); }; @@ -1867,7 +1867,7 @@ FilterLoadPacket.fromReader = function fromReader(br) { FilterLoadPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new FilterLoadPacket().fromRaw(data); }; @@ -1960,7 +1960,7 @@ FilterAddPacket.prototype.fromRaw = function fromRaw(data) { FilterAddPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new FilterAddPacket().fromRaw(data); }; @@ -1990,7 +1990,7 @@ FilterClearPacket.prototype.type = exports.types.FILTERCLEAR; FilterClearPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new FilterClearPacket().fromRaw(data); }; @@ -2073,7 +2073,7 @@ MerkleBlockPacket.prototype.fromRaw = function fromRaw(data) { MerkleBlockPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new MerkleBlockPacket().fromRaw(data); }; @@ -2166,7 +2166,7 @@ FeeFilterPacket.fromReader = function fromReader(br) { FeeFilterPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new FeeFilterPacket().fromRaw(data); }; @@ -2274,7 +2274,7 @@ SendCmpctPacket.fromReader = function fromReader(br) { SendCmpctPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new SendCmpctPacket().fromRaw(data); }; @@ -2376,7 +2376,7 @@ CmpctBlockPacket.fromReader = function fromReader(br) { CmpctBlockPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new CmpctBlockPacket().fromRaw(data); }; @@ -2469,7 +2469,7 @@ GetBlockTxnPacket.fromReader = function fromReader(br) { GetBlockTxnPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new GetBlockTxnPacket().fromRaw(data); }; @@ -2571,7 +2571,7 @@ BlockTxnPacket.fromReader = function fromReader(br) { BlockTxnPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new BlockTxnPacket().fromRaw(data); }; @@ -2669,7 +2669,7 @@ EncinitPacket.fromReader = function fromReader(br) { EncinitPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new EncinitPacket().fromRaw(data); }; @@ -2762,7 +2762,7 @@ EncackPacket.fromReader = function fromReader(br) { EncackPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new EncackPacket().fromRaw(data); }; @@ -2855,7 +2855,7 @@ AuthChallengePacket.fromReader = function fromReader(br) { AuthChallengePacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new AuthChallengePacket().fromRaw(data); }; @@ -2948,7 +2948,7 @@ AuthReplyPacket.fromReader = function fromReader(br) { AuthReplyPacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new AuthReplyPacket().fromRaw(data); }; @@ -3041,7 +3041,7 @@ AuthProposePacket.fromReader = function fromReader(br) { AuthProposePacket.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new AuthProposePacket().fromRaw(data); }; @@ -3118,7 +3118,7 @@ UnknownPacket.prototype.fromRaw = function fromRaw(cmd, data) { UnknownPacket.fromRaw = function fromRaw(cmd, data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new UnknownPacket().fromRaw(cmd, data); }; diff --git a/lib/net/parser.js b/lib/net/parser.js index 5977f66a..d8f6034c 100644 --- a/lib/net/parser.js +++ b/lib/net/parser.js @@ -63,7 +63,7 @@ Parser.prototype.feed = function feed(data) { this.pending.push(data); while (this.total >= this.waiting) { - chunk = new Buffer(this.waiting); + chunk = Buffer.allocUnsafe(this.waiting); off = 0; len = 0; diff --git a/lib/net/proxysocket.js b/lib/net/proxysocket.js index 2b1dc691..7ae8681e 100644 --- a/lib/net/proxysocket.js +++ b/lib/net/proxysocket.js @@ -42,8 +42,8 @@ ProxySocket.prototype._init = function _init() { self.info = info; if (info.pow) { - self.snonce = new Buffer(info.snonce, 'hex'); - self.target = new Buffer(info.target, 'hex'); + self.snonce = Buffer.from(info.snonce, 'hex'); + self.target = Buffer.from(info.target, 'hex'); } self.emit('info', info); @@ -62,7 +62,7 @@ ProxySocket.prototype._init = function _init() { }); this.socket.on('tcp data', function(data) { - data = new Buffer(data, 'hex'); + data = Buffer.from(data, 'hex'); if (self.paused) { self.recvBuffer.push(data); return; diff --git a/lib/net/socks.js b/lib/net/socks.js index 155042e8..de33423e 100644 --- a/lib/net/socks.js +++ b/lib/net/socks.js @@ -257,13 +257,13 @@ SOCKS.prototype.sendHandshake = function sendHandshake() { var packet; if (this.username) { - packet = new Buffer(4); + packet = Buffer.allocUnsafe(4); packet[0] = 0x05; packet[1] = 0x02; packet[2] = 0x00; packet[3] = 0x02; } else { - packet = new Buffer(3); + packet = Buffer.allocUnsafe(3); packet[0] = 0x05; packet[1] = 0x01; packet[2] = 0x00; @@ -389,7 +389,7 @@ SOCKS.prototype.sendProxy = function sendProxy() { break; default: type = 0x03; - name = new Buffer(host, 'ascii'); + name = Buffer.from(host, 'ascii'); len = 1 + name.length; break; } diff --git a/lib/node/config.js b/lib/node/config.js index 2be15322..796f050e 100644 --- a/lib/node/config.js +++ b/lib/node/config.js @@ -424,7 +424,7 @@ Config.prototype.buf = function buf(key, fallback) { return value; } - data = new Buffer(value, 'hex'); + data = Buffer.from(value, 'hex'); if (data.length !== value.length / 2) throw new Error(key + ' must be a hex string.'); diff --git a/lib/node/logger.js b/lib/node/logger.js index 748e183c..7a011aa7 100644 --- a/lib/node/logger.js +++ b/lib/node/logger.js @@ -279,7 +279,7 @@ Logger.prototype.truncate = co(function* truncate() { fd = yield fs.open(this.filename, 'r+'); - data = new Buffer(maxSize); + data = Buffer.allocUnsafe(maxSize); yield fs.read(fd, data, 0, maxSize, stat.size - maxSize); yield fs.ftruncate(fd, maxSize); yield fs.write(fd, data, 0, maxSize, 0); diff --git a/lib/primitives/address.js b/lib/primitives/address.js index ca922c67..0f5fe116 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -556,7 +556,7 @@ Address.fromScript = function fromScript(script) { Address.prototype.fromHash = function fromHash(hash, type, version, network) { if (typeof hash === 'string') - hash = new Buffer(hash, 'hex'); + hash = Buffer.from(hash, 'hex'); if (typeof type === 'string') { type = Address.types[type.toUpperCase()]; @@ -728,7 +728,7 @@ Address.prototype.fromProgram = function fromProgram(version, hash, network) { assert(version >= 0, 'Bad version for witness program.'); if (typeof hash === 'string') - hash = new Buffer(hash, 'hex'); + hash = Buffer.from(hash, 'hex'); return this.fromHash(hash, type, version, network); }; @@ -830,7 +830,7 @@ Address.getHash = function getHash(data, enc, network) { if (typeof data === 'string') { if (data.length === 40 || data.length === 64) - return enc === 'hex' ? data : new Buffer(data, 'hex'); + return enc === 'hex' ? data : Buffer.from(data, 'hex'); hash = Address.fromString(data, network).hash; } else if (Buffer.isBuffer(data)) { diff --git a/lib/primitives/block.js b/lib/primitives/block.js index e16a95e3..6d9d8e3a 100644 --- a/lib/primitives/block.js +++ b/lib/primitives/block.js @@ -356,7 +356,7 @@ Block.prototype.createCommitmentHash = function createCommitmentHash(enc) { Block.prototype.getMerkleRoot = function getMerkleRoot(enc) { if (enc === 'hex') return this.merkleRoot; - return new Buffer(this.merkleRoot, 'hex'); + return Buffer.from(this.merkleRoot, 'hex'); }; /** @@ -720,7 +720,7 @@ Block.fromReader = function fromReader(data) { Block.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Block().fromRaw(data); }; diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index 02003980..e1efe1ea 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -383,7 +383,7 @@ Coin.fromReader = function fromReader(br) { Coin.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Coin().fromRaw(data); }; diff --git a/lib/primitives/headers.js b/lib/primitives/headers.js index 930d33ea..ca46c1ea 100644 --- a/lib/primitives/headers.js +++ b/lib/primitives/headers.js @@ -114,7 +114,7 @@ Headers.fromReader = function fromReader(br) { Headers.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Headers().fromRaw(data); }; @@ -157,7 +157,7 @@ Headers.fromAbbrReader = function fromAbbrReader(br) { Headers.fromAbbr = function fromAbbr(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Headers().fromAbbr(data); }; @@ -169,7 +169,7 @@ Headers.fromAbbr = function fromAbbr(data, enc) { Headers.fromEntry = function fromEntry(entry) { var headers = new Headers(entry); - headers._hash = new Buffer(entry.hash, 'hex'); + headers._hash = Buffer.from(entry.hash, 'hex'); return headers; }; diff --git a/lib/primitives/input.js b/lib/primitives/input.js index 8724f8c3..c441beb9 100644 --- a/lib/primitives/input.js +++ b/lib/primitives/input.js @@ -404,7 +404,7 @@ Input.fromReader = function fromReader(br) { Input.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Input().fromRaw(data); }; diff --git a/lib/primitives/invitem.js b/lib/primitives/invitem.js index 0d8bbe06..9c46945e 100644 --- a/lib/primitives/invitem.js +++ b/lib/primitives/invitem.js @@ -130,7 +130,7 @@ InvItem.fromReader = function fromReader(br) { InvItem.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new InvItem().fromRaw(data); }; diff --git a/lib/primitives/keyring.js b/lib/primitives/keyring.js index a66b72a9..d069f499 100644 --- a/lib/primitives/keyring.js +++ b/lib/primitives/keyring.js @@ -796,10 +796,10 @@ KeyRing.prototype.fromJSON = function fromJSON(json) { this.nework = Network.get(json.network); this.witness = json.witness; this.nested = json.nested; - this.publicKey = new Buffer(json.publicKey, 'hex'); + this.publicKey = Buffer.from(json.publicKey, 'hex'); if (json.script) - this.script = new Buffer(json.script, 'hex'); + this.script = Buffer.from(json.script, 'hex'); return this; }; diff --git a/lib/primitives/memblock.js b/lib/primitives/memblock.js index 9b589197..c0179753 100644 --- a/lib/primitives/memblock.js +++ b/lib/primitives/memblock.js @@ -13,7 +13,7 @@ var Block = require('./block'); var Script = require('../script/script'); var Headers = require('./headers'); var BufferReader = require('../utils/reader'); -var DUMMY = new Buffer(0); +var DUMMY = Buffer.alloc(0); /** * A block object which is essentially a "placeholder" diff --git a/lib/primitives/merkleblock.js b/lib/primitives/merkleblock.js index 2228bd92..4bf455bb 100644 --- a/lib/primitives/merkleblock.js +++ b/lib/primitives/merkleblock.js @@ -17,7 +17,7 @@ var StaticWriter = require('../utils/staticwriter'); var encoding = require('../utils/encoding'); var consensus = require('../protocol/consensus'); var Headers = require('./headers'); -var DUMMY = new Buffer([0]); +var DUMMY = Buffer.from([0]); /** * Represents a merkle (filtered) block. @@ -66,7 +66,7 @@ MerkleBlock.prototype.fromOptions = function fromOptions(options) { for (i = 0; i < options.hashes.length; i++) { hash = options.hashes[i]; if (typeof hash === 'string') - hash = new Buffer(hash, 'hex'); + hash = Buffer.from(hash, 'hex'); this.hashes.push(hash); } } @@ -262,7 +262,7 @@ MerkleBlock.prototype.extractTree = function extractTree() { height++; if (height > 0) - buf = new Buffer(64); + buf = Buffer.allocUnsafe(64); root = traverse(height, 0); @@ -427,7 +427,7 @@ MerkleBlock.fromReader = function fromReader(br) { MerkleBlock.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new MerkleBlock().fromRaw(data); }; @@ -488,10 +488,10 @@ MerkleBlock.prototype.fromJSON = function fromJSON(json) { for (i = 0; i < json.hashes.length; i++) { hash = util.revHex(json.hashes[i]); - this.hashes.push(new Buffer(hash, 'hex')); + this.hashes.push(Buffer.from(hash, 'hex')); } - this.flags = new Buffer(json.flags, 'hex'); + this.flags = Buffer.from(json.flags, 'hex'); this.totalTX = json.totalTX; @@ -629,11 +629,11 @@ MerkleBlock.fromMatches = function fromMatches(block, matches) { height++; if (height > 0) - buf = new Buffer(64); + buf = Buffer.allocUnsafe(64); traverse(height, 0, leaves, matches); - flags = new Buffer((bits.length + 7) / 8 | 0); + flags = Buffer.allocUnsafe((bits.length + 7) / 8 | 0); flags.fill(0); for (p = 0; p < bits.length; p++) diff --git a/lib/primitives/mtx.js b/lib/primitives/mtx.js index 6bf26866..0576643c 100644 --- a/lib/primitives/mtx.js +++ b/lib/primitives/mtx.js @@ -1392,7 +1392,7 @@ MTX.fromReader = function fromReader(br) { MTX.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new MTX().fromRaw(data); }; diff --git a/lib/primitives/output.js b/lib/primitives/output.js index 89608d4e..c3bdb367 100644 --- a/lib/primitives/output.js +++ b/lib/primitives/output.js @@ -327,7 +327,7 @@ Output.fromReader = function fromReader(br) { Output.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Output().fromRaw(data); }; diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index 0989b6c4..9a3ae0b3 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -2270,7 +2270,7 @@ TX.fromJSON = function fromJSON(json) { TX.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new TX().fromRaw(data); }; diff --git a/lib/primitives/txmeta.js b/lib/primitives/txmeta.js index 30c91ead..42d7c659 100644 --- a/lib/primitives/txmeta.js +++ b/lib/primitives/txmeta.js @@ -281,7 +281,7 @@ TXMeta.prototype.fromRaw = function fromRaw(data) { TXMeta.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new TXMeta().fromRaw(data); }; diff --git a/lib/script/common.js b/lib/script/common.js index 38d522d6..cbfd7e88 100644 --- a/lib/script/common.js +++ b/lib/script/common.js @@ -298,21 +298,21 @@ exports.typesByVal = util.revMap(exports.types); * @const {Buffer} */ -exports.STACK_FALSE = new Buffer([]); +exports.STACK_FALSE = Buffer.alloc(0); /** * True stack return value. * @const {Buffer} */ -exports.STACK_TRUE = new Buffer([0x01]); +exports.STACK_TRUE = Buffer.from([0x01]); /** * -1 stack return value. * @const {Buffer} */ -exports.STACK_NEGATE = new Buffer([0x81]); +exports.STACK_NEGATE = Buffer.from([0x81]); /** * Test whether the data element is a ripemd160 hash. @@ -720,10 +720,10 @@ exports.num = function num(value, flags, size) { * numbers to a little-endian buffer while taking into * account negative zero, minimaldata, etc. * @example - * assert.deepEqual(Script.array(0), new Buffer(0)); - * assert.deepEqual(Script.array(0xffee), new Buffer('eeff00', 'hex')); - * assert.deepEqual(Script.array(new BN(0xffee)), new Buffer('eeff00', 'hex')); - * assert.deepEqual(Script.array(new BN(0x1e).ineg()), new Buffer('9e', 'hex')); + * assert.deepEqual(Script.array(0), Buffer.alloc(0)); + * assert.deepEqual(Script.array(0xffee), Buffer.from('eeff00', 'hex')); + * assert.deepEqual(Script.array(new BN(0xffee)), Buffer.from('eeff00', 'hex')); + * assert.deepEqual(Script.array(new BN(0x1e).ineg()), Buffer.from('9e', 'hex')); * @param {Number|BN} value * @returns {Buffer} */ @@ -763,7 +763,7 @@ exports.array = function(value) { else if (neg) result[result.length - 1] |= 0x80; - return new Buffer(result); + return Buffer.from(result); }; /** diff --git a/lib/script/opcode.js b/lib/script/opcode.js index 94168bbc..14e7f0d6 100644 --- a/lib/script/opcode.js +++ b/lib/script/opcode.js @@ -302,7 +302,7 @@ Opcode.fromSmall = function fromSmall(num) { Opcode.fromString = function fromString(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return Opcode.fromData(data); }; diff --git a/lib/script/script.js b/lib/script/script.js index 344ee5dd..42cea82a 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -403,7 +403,7 @@ Script.prototype.toJSON = function toJSON() { Script.prototype.fromJSON = function fromJSON(json) { assert(typeof json === 'string', 'Code must be a string.'); - return this.fromRaw(new Buffer(json, 'hex')); + return this.fromRaw(Buffer.from(json, 'hex')); }; /** @@ -667,7 +667,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers case opcodes.OP_14: case opcodes.OP_15: case opcodes.OP_16: { - stack.push(new Buffer([op - 0x50])); + stack.push(Buffer.from([op - 0x50])); break; } case opcodes.OP_NOP: { @@ -1391,14 +1391,14 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers v2 = stack.top(-1); if (v1.length < v2.length) { - v3 = new Buffer(v2.length); + v3 = Buffer.allocUnsafe(v2.length); v3.fill(0); v1.copy(v3, 0); v1 = v3; } if (v2.length < v1.length) { - v3 = new Buffer(v1.length); + v3 = Buffer.allocUnsafe(v1.length); v3.fill(0); v2.copy(v3, 0); v2 = v3; @@ -1478,10 +1478,10 @@ Script.num = function num(value, flags, size) { * numbers to a little-endian buffer while taking into * account negative zero, minimaldata, etc. * @example - * assert.deepEqual(Script.array(0), new Buffer(0)); - * assert.deepEqual(Script.array(0xffee), new Buffer('eeff00', 'hex')); - * assert.deepEqual(Script.array(new BN(0xffee)), new Buffer('eeff00', 'hex')); - * assert.deepEqual(Script.array(new BN(0x1e).ineg()), new Buffer('9e', 'hex')); + * assert.deepEqual(Script.array(0), Buffer.alloc(0)); + * assert.deepEqual(Script.array(0xffee), Buffer.from('eeff00', 'hex')); + * assert.deepEqual(Script.array(new BN(0xffee)), Buffer.from('eeff00', 'hex')); + * assert.deepEqual(Script.array(new BN(0x1e).ineg()), Buffer.from('9e', 'hex')); * @param {Number|BN} value * @returns {Buffer} */ @@ -1645,7 +1645,7 @@ Script.isCode = function isCode(raw) { Script.prototype.fromPubkey = function fromPubkey(key) { assert(Buffer.isBuffer(key) && key.length >= 33 && key.length <= 65); - this.raw = new Buffer(1 + key.length + 1); + this.raw = Buffer.allocUnsafe(1 + key.length + 1); this.raw[0] = key.length; key.copy(this.raw, 1); this.raw[1 + key.length] = opcodes.OP_CHECKSIG; @@ -1677,7 +1677,7 @@ Script.fromPubkey = function fromPubkey(key) { Script.prototype.fromPubkeyhash = function fromPubkeyhash(hash) { assert(Buffer.isBuffer(hash) && hash.length === 20); - this.raw = new Buffer(25); + this.raw = Buffer.allocUnsafe(25); this.raw[0] = opcodes.OP_DUP; this.raw[1] = opcodes.OP_HASH160; this.raw[2] = 0x14; @@ -1759,7 +1759,7 @@ Script.fromMultisig = function fromMultisig(m, n, keys) { Script.prototype.fromScripthash = function fromScripthash(hash) { assert(Buffer.isBuffer(hash) && hash.length === 20); - this.raw = new Buffer(23); + this.raw = Buffer.allocUnsafe(23); this.raw[0] = opcodes.OP_HASH160; this.raw[1] = 0x14; hash.copy(this.raw, 2); @@ -1824,7 +1824,7 @@ Script.prototype.fromProgram = function fromProgram(version, data) { op = Opcode.fromSmall(version); - this.raw = new Buffer(2 + data.length); + this.raw = Buffer.allocUnsafe(2 + data.length); this.raw[0] = op.value; this.raw[1] = data.length; data.copy(this.raw, 2); @@ -3141,7 +3141,7 @@ Script.prototype.fromString = function fromString(code) { assert(op.indexOf('0x') === 0, 'Unknown opcode.'); op = op.substring(2); assert(util.isHex(op), 'Unknown opcode.'); - op = new Buffer(op, 'hex'); + op = Buffer.from(op, 'hex'); bw.writeBytes(op); continue; } @@ -3607,7 +3607,7 @@ Script.fromReader = function fromReader(br) { Script.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Script().fromRaw(data); }; diff --git a/lib/script/scriptnum.js b/lib/script/scriptnum.js index 98eae28c..a313a130 100644 --- a/lib/script/scriptnum.js +++ b/lib/script/scriptnum.js @@ -8,7 +8,7 @@ var assert = require('assert'); var ScriptError = require('./common').ScriptError; -var EMPTY_ARRAY = new Buffer(0); +var EMPTY_ARRAY = Buffer.alloc(0); /** * ScriptNum @@ -360,7 +360,7 @@ ScriptNum.prototype.toRaw = function toRaw() { } // Write number. - data = new Buffer(size + offset); + data = Buffer.allocUnsafe(size + offset); switch (size) { case 6: diff --git a/lib/script/witness.js b/lib/script/witness.js index f2a6f98f..5afae4b6 100644 --- a/lib/script/witness.js +++ b/lib/script/witness.js @@ -402,7 +402,7 @@ Witness.prototype.toJSON = function toJSON() { Witness.prototype.fromJSON = function fromJSON(json) { assert(typeof json === 'string', 'Witness must be a string.'); - return this.fromRaw(new Buffer(json, 'hex')); + return this.fromRaw(Buffer.from(json, 'hex')); }; /** @@ -564,7 +564,7 @@ Witness.encodeItem = function encodeItem(data) { return STACK_FALSE; if (data >= opcodes.OP_1 && data <= opcodes.OP_16) - return new Buffer([data - 0x50]); + return Buffer.from([data - 0x50]); throw new Error('Non-push opcode in witness.'); } @@ -573,7 +573,7 @@ Witness.encodeItem = function encodeItem(data) { return common.array(data); if (typeof data === 'string') - return new Buffer(data, 'utf8'); + return Buffer.from(data, 'utf8'); return data; }; @@ -622,7 +622,7 @@ Witness.fromReader = function fromReader(br) { Witness.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Witness().fromRaw(data); }; @@ -647,7 +647,7 @@ Witness.prototype.fromString = function fromString(items) { } for (i = 0; i < items.length; i++) - this.items.push(new Buffer(items[i], 'hex')); + this.items.push(Buffer.from(items[i], 'hex')); return this; }; diff --git a/lib/utils/asn1.js b/lib/utils/asn1.js index a3faa8aa..59902d7f 100644 --- a/lib/utils/asn1.js +++ b/lib/utils/asn1.js @@ -252,7 +252,7 @@ ASN1.alignBitstr = function(data) { if (shift === 8 || buf.length === 0) return buf; - out = new Buffer(buf.length); + out = Buffer.allocUnsafe(buf.length); out[0] = buf[0] >>> shift; for (i = 1; i < buf.length; i++) { diff --git a/lib/utils/base32.js b/lib/utils/base32.js index aec74529..11714614 100644 --- a/lib/utils/base32.js +++ b/lib/utils/base32.js @@ -78,7 +78,7 @@ exports.encode = function(data) { */ exports.decode = function decode(str) { - var data = new Buffer(str.length * 5 / 8 | 0); + var data = Buffer.allocUnsafe(str.length * 5 / 8 | 0); var mode = 0; var left = 0; var j = 0; diff --git a/lib/utils/base58.js b/lib/utils/base58.js index edec079b..7e2c8789 100644 --- a/lib/utils/base58.js +++ b/lib/utils/base58.js @@ -47,7 +47,7 @@ exports.encode = function encode(data) { zeroes++; } - b58 = new Buffer(((data.length * 138 / 100) | 0) + 1); + b58 = Buffer.allocUnsafe(((data.length * 138 / 100) | 0) + 1); b58.fill(0); for (; i < data.length; i++) { @@ -102,7 +102,7 @@ exports.decode = function decode(str) { zeroes++; } - b256 = new Buffer(((str.length * 733) / 1000 | 0) + 1); + b256 = Buffer.allocUnsafe(((str.length * 733) / 1000 | 0) + 1); b256.fill(0); for (; i < str.length; i++) { @@ -129,7 +129,7 @@ exports.decode = function decode(str) { while (i < b256.length && b256[i] === 0) i++; - out = new Buffer(zeroes + (b256.length - i)); + out = Buffer.allocUnsafe(zeroes + (b256.length - i)); for (j = 0; j < zeroes; j++) out[j] = 0; diff --git a/lib/utils/bech32.js b/lib/utils/bech32.js index 3ba29c92..fefc2ffe 100644 --- a/lib/utils/bech32.js +++ b/lib/utils/bech32.js @@ -35,7 +35,7 @@ var native = require('./native').binding; * @module utils/bech32 */ -var POOL65 = new Buffer(65); +var POOL65 = Buffer.allocUnsafe(65); var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; var TABLE = [ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, @@ -147,7 +147,7 @@ function deserialize(str) { throw new Error('Invalid bech32 data length.'); dlen -= 6; - data = new Buffer(dlen); + data = Buffer.allocUnsafe(dlen); for (i = 0; i < hlen; i++) { ch = str.charCodeAt(i); diff --git a/lib/utils/bloom.js b/lib/utils/bloom.js index 2cc355a9..37268d1c 100644 --- a/lib/utils/bloom.js +++ b/lib/utils/bloom.js @@ -14,7 +14,7 @@ var StaticWriter = require('./staticwriter'); var encoding = require('./encoding'); var sum32 = murmur3.sum32; var mul32 = murmur3.mul32; -var DUMMY = new Buffer(0); +var DUMMY = Buffer.alloc(0); /* * Constants @@ -125,7 +125,7 @@ Bloom.prototype.fromOptions = function fromOptions(size, n, tweak, update) { size -= size % 8; - filter = new Buffer(size / 8); + filter = Buffer.allocUnsafe(size / 8); filter.fill(0); if (tweak == null || tweak === -1) @@ -197,7 +197,7 @@ Bloom.prototype.add = function add(val, enc) { var i, index; if (typeof val === 'string') - val = new Buffer(val, enc); + val = Buffer.from(val, enc); for (i = 0; i < this.n; i++) { index = this.hash(val, i); @@ -216,7 +216,7 @@ Bloom.prototype.test = function test(val, enc) { var i, index; if (typeof val === 'string') - val = new Buffer(val, enc); + val = Buffer.from(val, enc); for (i = 0; i < this.n; i++) { index = this.hash(val, i); @@ -240,7 +240,7 @@ Bloom.prototype.added = function added(val, enc) { var i, index; if (typeof val === 'string') - val = new Buffer(val, enc); + val = Buffer.from(val, enc); for (i = 0; i < this.n; i++) { index = this.hash(val, i); @@ -380,7 +380,7 @@ Bloom.fromReader = function fromReader(br) { Bloom.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc); + data = Buffer.from(data, enc); return new Bloom().fromRaw(data); }; @@ -442,7 +442,7 @@ RollingFilter.prototype.fromRate = function fromRate(items, rate) { tweak = (Math.random() * 0x100000000) >>> 0; - filter = new Buffer(items * 8); + filter = Buffer.allocUnsafe(items * 8); filter.fill(0); this.n = n; @@ -501,7 +501,7 @@ RollingFilter.prototype.add = function add(val, enc) { var m1, m2, v1, v2, mhi, mlo; if (typeof val === 'string') - val = new Buffer(val, enc); + val = Buffer.from(val, enc); if (this.entries === this.limit) { this.entries = 0; @@ -565,7 +565,7 @@ RollingFilter.prototype.test = function test(val, enc) { return false; if (typeof val === 'string') - val = new Buffer(val, enc); + val = Buffer.from(val, enc); for (i = 0; i < this.n; i++) { hash = this.hash(val, i); @@ -599,7 +599,7 @@ RollingFilter.prototype.test = function test(val, enc) { RollingFilter.prototype.added = function added(val, enc) { if (typeof val === 'string') - val = new Buffer(val, enc); + val = Buffer.from(val, enc); if (!this.test(val)) { this.add(val); diff --git a/lib/utils/encoding.js b/lib/utils/encoding.js index 77e8e8a1..f0d6c64c 100644 --- a/lib/utils/encoding.js +++ b/lib/utils/encoding.js @@ -51,7 +51,7 @@ encoding.MAX_SAFE_ADDITION = 0xfffffffffffff; * @default */ -encoding.DUMMY = new Buffer([0]); +encoding.DUMMY = Buffer.from([0]); /** * A hash of all zeroes with a `1` at the @@ -60,7 +60,7 @@ encoding.DUMMY = new Buffer([0]); * @default */ -encoding.ONE_HASH = new Buffer( +encoding.ONE_HASH = Buffer.from( '0100000000000000000000000000000000000000000000000000000000000000', 'hex' ); @@ -71,7 +71,7 @@ encoding.ONE_HASH = new Buffer( * @default */ -encoding.ZERO_HASH = new Buffer( +encoding.ZERO_HASH = Buffer.from( '0000000000000000000000000000000000000000000000000000000000000000', 'hex' ); @@ -82,7 +82,7 @@ encoding.ZERO_HASH = new Buffer( * @default */ -encoding.MAX_HASH = new Buffer( +encoding.MAX_HASH = Buffer.from( 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex' ); @@ -111,7 +111,7 @@ encoding.HIGH_HASH = * @default */ -encoding.ZERO_HASH160 = new Buffer( +encoding.ZERO_HASH160 = Buffer.from( '0000000000000000000000000000000000000000', 'hex' ); @@ -122,7 +122,7 @@ encoding.ZERO_HASH160 = new Buffer( * @default */ -encoding.MAX_HASH160 = new Buffer( +encoding.MAX_HASH160 = Buffer.from( 'ffffffffffffffffffffffffffffffffffffffff', 'hex' ); @@ -149,7 +149,7 @@ encoding.HIGH_HASH160 = 'ffffffffffffffffffffffffffffffffffffffff'; * @default */ -encoding.ZERO_KEY = new Buffer( +encoding.ZERO_KEY = Buffer.from( '000000000000000000000000000000000000000000000000000000000000000000', 'hex' ); @@ -160,7 +160,7 @@ encoding.ZERO_KEY = new Buffer( * @default */ -encoding.ZERO_SIG = new Buffer('' +encoding.ZERO_SIG = Buffer.from('' + '0000000000000000000000000000000000000000000000000000000000000000' + '0000000000000000000000000000000000000000000000000000000000000000' + '000000000000000000', @@ -173,7 +173,7 @@ encoding.ZERO_SIG = new Buffer('' * @default */ -encoding.ZERO_SIG64 = new Buffer('' +encoding.ZERO_SIG64 = Buffer.from('' + '0000000000000000000000000000000000000000000000000000000000000000' + '0000000000000000000000000000000000000000000000000000000000000000', 'hex' @@ -185,7 +185,7 @@ encoding.ZERO_SIG64 = new Buffer('' * @default */ -encoding.ZERO_U32 = new Buffer('00000000', 'hex'); +encoding.ZERO_U32 = Buffer.from('00000000', 'hex'); /** * 8 zero bytes. @@ -193,7 +193,7 @@ encoding.ZERO_U32 = new Buffer('00000000', 'hex'); * @default */ -encoding.ZERO_U64 = new Buffer('0000000000000000', 'hex'); +encoding.ZERO_U64 = Buffer.from('0000000000000000', 'hex'); /** * Read uint64 as a js number. @@ -987,7 +987,7 @@ encoding.sizeVarint2BN = function sizeVarint2BN(num) { */ encoding.U8 = function U8(num) { - var data = new Buffer(1); + var data = Buffer.allocUnsafe(1); data[0] = num >>> 0; return data; }; @@ -999,7 +999,7 @@ encoding.U8 = function U8(num) { */ encoding.U32 = function U32(num) { - var data = new Buffer(4); + var data = Buffer.allocUnsafe(4); data.writeUInt32LE(num, 0, true); return data; }; @@ -1011,7 +1011,7 @@ encoding.U32 = function U32(num) { */ encoding.U32BE = function U32BE(num) { - var data = new Buffer(4); + var data = Buffer.allocUnsafe(4); data.writeUInt32BE(num, 0, true); return data; }; diff --git a/lib/utils/ip.js b/lib/utils/ip.js index 84f75a26..da2bcbd4 100644 --- a/lib/utils/ip.js +++ b/lib/utils/ip.js @@ -25,13 +25,13 @@ var IP = exports; * Constants */ -var ZERO_IP = new Buffer('00000000000000000000000000000000', 'hex'); -var LOCAL_IP = new Buffer('00000000000000000000000000000001', 'hex'); -var RFC6052 = new Buffer('0064ff9b0000000000000000', 'hex'); -var RFC4862 = new Buffer('fe80000000000000', 'hex'); -var RFC6145 = new Buffer('0000000000000000ffff0000', 'hex'); -var TOR_ONION = new Buffer('fd87d87eeb43', 'hex'); -var SHIFTED = new Buffer('00000000000000ffff', 'hex'); +var ZERO_IP = Buffer.from('00000000000000000000000000000000', 'hex'); +var LOCAL_IP = Buffer.from('00000000000000000000000000000001', 'hex'); +var RFC6052 = Buffer.from('0064ff9b0000000000000000', 'hex'); +var RFC4862 = Buffer.from('fe80000000000000', 'hex'); +var RFC6145 = Buffer.from('0000000000000000ffff0000', 'hex'); +var TOR_ONION = Buffer.from('fd87d87eeb43', 'hex'); +var SHIFTED = Buffer.from('00000000000000ffff', 'hex'); var IPV4_REGEX = /^(\d{1,3}\.){3}\d{1,3}$/; var IPV6_REGEX = @@ -273,7 +273,7 @@ IP.isMapped = function isMapped(raw) { */ IP.toBuffer = function toBuffer(str) { - var raw = new Buffer(16); + var raw = Buffer.allocUnsafe(16); var data; assert(typeof str === 'string'); diff --git a/lib/utils/pem.js b/lib/utils/pem.js index 15113bec..8ac998d0 100644 --- a/lib/utils/pem.js +++ b/lib/utils/pem.js @@ -35,7 +35,7 @@ PEM.parse = function parse(pem) { if (s = /^-----END ([^\-]+)-----/.exec(pem)) { pem = pem.substring(s[0].length); assert(tag === s[1], 'Tag mismatch.'); - buf = new Buffer(buf, 'base64'); + buf = Buffer.from(buf, 'base64'); type = tag.split(' ')[0].toLowerCase(); chunks.push({ tag: tag, type: type, data: buf }); buf = ''; diff --git a/lib/utils/protobuf.js b/lib/utils/protobuf.js index d6541375..4565eb57 100644 --- a/lib/utils/protobuf.js +++ b/lib/utils/protobuf.js @@ -198,7 +198,7 @@ ProtoWriter.prototype.writeVarint = function writeVarint(num) { this.writeU8(value); break; default: - value = new Buffer(size); + value = Buffer.allocUnsafe(size); exports.writeVarint(value, num, 0); this.writeBytes(value); break; @@ -230,7 +230,7 @@ ProtoWriter.prototype.writeFieldBytes = function writeFieldBytes(tag, data) { ProtoWriter.prototype.writeFieldString = function writeFieldString(tag, data, enc) { if (typeof data === 'string') - data = new Buffer(data, enc || 'utf8'); + data = Buffer.from(data, enc || 'utf8'); this.writeFieldBytes(tag, data); }; diff --git a/lib/utils/reader.js b/lib/utils/reader.js index c68dc315..0d71877f 100644 --- a/lib/utils/reader.js +++ b/lib/utils/reader.js @@ -138,7 +138,7 @@ BufferReader.prototype.endData = function endData(zeroCopy) { if (this.zeroCopy || zeroCopy) return data.slice(start, end); - ret = new Buffer(size); + ret = Buffer.allocUnsafe(size); data.copy(ret, 0, start, end); return ret; @@ -584,7 +584,7 @@ BufferReader.prototype.readBytes = function readBytes(size, zeroCopy) { if (this.zeroCopy || zeroCopy) { ret = this.data.slice(this.offset, this.offset + size); } else { - ret = new Buffer(size); + ret = Buffer.allocUnsafe(size); this.data.copy(ret, 0, this.offset, this.offset + size); } diff --git a/lib/utils/staticwriter.js b/lib/utils/staticwriter.js index 1555652d..833832ae 100644 --- a/lib/utils/staticwriter.js +++ b/lib/utils/staticwriter.js @@ -21,7 +21,7 @@ function StaticWriter(size) { if (!(this instanceof StaticWriter)) return new StaticWriter(size); - this.data = new Buffer(size); + this.data = Buffer.allocUnsafe(size); this.written = 0; } diff --git a/lib/utils/util.js b/lib/utils/util.js index 61f8e2a6..8196ab89 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -88,7 +88,7 @@ util.nop = function() {}; */ util.copy = function copy(data) { - var clone = new Buffer(data.length); + var clone = Buffer.allocUnsafe(data.length); data.copy(clone, 0, 0, data.length); return clone; }; @@ -101,7 +101,7 @@ util.copy = function copy(data) { */ util.concat = function concat(a, b) { - var data = new Buffer(a.length + b.length); + var data = Buffer.allocUnsafe(a.length + b.length); a.copy(data, 0); b.copy(data, a.length); return data; @@ -575,14 +575,14 @@ util.nonce = function _nonce(size) { switch (size) { case 8: - nonce = new Buffer(8); + nonce = Buffer.allocUnsafe(8); n = util.random(0, 0x100000000); nonce.writeUInt32LE(n, 0, true); n = util.random(0, 0x100000000); nonce.writeUInt32LE(n, 4, true); break; case 4: - nonce = new Buffer(4); + nonce = Buffer.allocUnsafe(4); n = util.random(0, 0x100000000); nonce.writeUInt32LE(n, 0, true); break; diff --git a/lib/utils/validator.js b/lib/utils/validator.js index 1ba0d118..2fb1bb34 100644 --- a/lib/utils/validator.js +++ b/lib/utils/validator.js @@ -490,7 +490,7 @@ Validator.prototype.buf = function buf(key, fallback, enc) { return value; } - data = new Buffer(value, enc); + data = Buffer.from(value, enc); if (data.length !== Buffer.byteLength(value, enc)) throw new ValidationError(fmt(key) + ' must be a ' + enc + ' string.'); diff --git a/lib/utils/writer.js b/lib/utils/writer.js index c1b53c45..d2c20155 100644 --- a/lib/utils/writer.js +++ b/lib/utils/writer.js @@ -70,7 +70,7 @@ function BufferWriter() { */ BufferWriter.prototype.render = function render(keep) { - var data = new Buffer(this.written); + var data = Buffer.allocUnsafe(this.written); var off = 0; var i, op; diff --git a/lib/wallet/client.js b/lib/wallet/client.js index d0582f86..6f25e7c8 100644 --- a/lib/wallet/client.js +++ b/lib/wallet/client.js @@ -338,7 +338,7 @@ function parseEntry(data, enc) { var br, block, hash, height; if (typeof data === 'string') - data = new Buffer(data, 'hex'); + data = Buffer.from(data, 'hex'); block = Headers.fromAbbr(data); diff --git a/lib/wallet/http.js b/lib/wallet/http.js index da1ee8e9..60fe07a3 100644 --- a/lib/wallet/http.js +++ b/lib/wallet/http.js @@ -1088,12 +1088,12 @@ HTTPOptions.fromOptions = function fromOptions(options) { function hash256(data) { if (typeof data !== 'string') - return new Buffer(0); + return Buffer.alloc(0); if (data.length > 200) - return new Buffer(0); + return Buffer.alloc(0); - return crypto.hash256(new Buffer(data, 'utf8')); + return crypto.hash256(Buffer.from(data, 'utf8')); } function enforce(value, msg) { diff --git a/lib/wallet/layout.js b/lib/wallet/layout.js index 1c98838b..f4737157 100644 --- a/lib/wallet/layout.js +++ b/lib/wallet/layout.js @@ -28,7 +28,7 @@ var layouts = exports; layouts.walletdb = { binary: true, p: function p(hash) { - var key = new Buffer(1 + (hash.length / 2)); + var key = Buffer.allocUnsafe(1 + (hash.length / 2)); key[0] = 0x70; key.write(hash, 1, 'hex'); return key; @@ -37,7 +37,7 @@ layouts.walletdb = { return key.toString('hex', 1); }, P: function P(wid, hash) { - var key = new Buffer(1 + 4 + (hash.length / 2)); + var key = Buffer.allocUnsafe(1 + 4 + (hash.length / 2)); key[0] = 0x50; key.writeUInt32BE(wid, 1, true); key.write(hash, 5, 'hex'); @@ -47,7 +47,7 @@ layouts.walletdb = { return key.toString('hex', 5); }, r: function r(wid, index, hash) { - var key = new Buffer(1 + 4 + 4 + (hash.length / 2)); + var key = Buffer.allocUnsafe(1 + 4 + 4 + (hash.length / 2)); key[0] = 0x72; key.writeUInt32BE(wid, 1, true); key.writeUInt32BE(index, 5, true); @@ -58,7 +58,7 @@ layouts.walletdb = { return key.toString('hex', 9); }, w: function w(wid) { - var key = new Buffer(5); + var key = Buffer.allocUnsafe(5); key[0] = 0x77; key.writeUInt32BE(wid, 1, true); return key; @@ -68,7 +68,7 @@ layouts.walletdb = { }, l: function l(id) { var len = Buffer.byteLength(id, 'ascii'); - var key = new Buffer(1 + len); + var key = Buffer.allocUnsafe(1 + len); key[0] = 0x6c; if (len > 0) key.write(id, 1, 'ascii'); @@ -78,7 +78,7 @@ layouts.walletdb = { return key.toString('ascii', 1); }, a: function a(wid, index) { - var key = new Buffer(9); + var key = Buffer.allocUnsafe(9); key[0] = 0x61; key.writeUInt32BE(wid, 1, true); key.writeUInt32BE(index, 5, true); @@ -86,7 +86,7 @@ layouts.walletdb = { }, i: function i(wid, name) { var len = Buffer.byteLength(name, 'ascii'); - var key = new Buffer(5 + len); + var key = Buffer.allocUnsafe(5 + len); key[0] = 0x69; key.writeUInt32BE(wid, 1, true); if (len > 0) @@ -97,21 +97,21 @@ layouts.walletdb = { return [key.readUInt32BE(1, true), key.toString('ascii', 5)]; }, n: function n(wid, index) { - var key = new Buffer(9); + var key = Buffer.allocUnsafe(9); key[0] = 0x6e; key.writeUInt32BE(wid, 1, true); key.writeUInt32BE(index, 5, true); return key; }, - R: new Buffer([0x52]), + R: Buffer.from([0x52]), h: function h(height) { - var key = new Buffer(5); + var key = Buffer.allocUnsafe(5); key[0] = 0x68; key.writeUInt32BE(height, 1, true); return key; }, b: function b(height) { - var key = new Buffer(5); + var key = Buffer.allocUnsafe(5); key[0] = 0x62; key.writeUInt32BE(height, 1, true); return key; @@ -120,7 +120,7 @@ layouts.walletdb = { return key.readUInt32BE(1, true); }, o: function o(hash, index) { - var key = new Buffer(37); + var key = Buffer.allocUnsafe(37); key[0] = 0x6f; key.write(hash, 1, 'hex'); key.writeUInt32BE(index, 33, true); @@ -152,7 +152,7 @@ layouts.walletdb = { layouts.txdb = { binary: true, prefix: function prefix(wid, key) { - var out = new Buffer(5 + key.length); + var out = Buffer.allocUnsafe(5 + key.length); out[0] = 0x74; out.writeUInt32BE(wid, 1); key.copy(out, 5); @@ -161,9 +161,9 @@ layouts.txdb = { pre: function prefix(key) { return key.readUInt32BE(1, true); }, - R: new Buffer([0x52]), + R: Buffer.from([0x52]), hi: function hi(ch, hash, index) { - var key = new Buffer(37); + var key = Buffer.allocUnsafe(37); key[0] = ch; key.write(hash, 1, 'hex'); key.writeUInt32BE(index, 33, true); @@ -174,7 +174,7 @@ layouts.txdb = { return [key.toString('hex', 0, 32), key.readUInt32BE(32, true)]; }, ih: function ih(ch, index, hash) { - var key = new Buffer(37); + var key = Buffer.allocUnsafe(37); key[0] = ch; key.writeUInt32BE(index, 1, true); key.write(hash, 5, 'hex'); @@ -185,7 +185,7 @@ layouts.txdb = { return [key.readUInt32BE(0, true), key.toString('hex', 4, 36)]; }, iih: function iih(ch, index, num, hash) { - var key = new Buffer(41); + var key = Buffer.allocUnsafe(41); key[0] = ch; key.writeUInt32BE(index, 1, true); key.writeUInt32BE(num, 5, true); @@ -201,7 +201,7 @@ layouts.txdb = { ]; }, ihi: function ihi(ch, index, hash, num) { - var key = new Buffer(41); + var key = Buffer.allocUnsafe(41); key[0] = ch; key.writeUInt32BE(index, 1, true); key.write(hash, 5, 'hex'); @@ -217,7 +217,7 @@ layouts.txdb = { ]; }, ha: function ha(ch, hash) { - var key = new Buffer(33); + var key = Buffer.allocUnsafe(33); key[0] = ch; key.write(hash, 1, 'hex'); return key; @@ -302,7 +302,7 @@ layouts.txdb = { return this.ha(0x72, hash); }, b: function b(height) { - var key = new Buffer(5); + var key = Buffer.allocUnsafe(5); key[0] = 0x62; key.writeUInt32BE(height, 1, true); return key; diff --git a/lib/wallet/masterkey.js b/lib/wallet/masterkey.js index 6e601660..5e3ff8ae 100644 --- a/lib/wallet/masterkey.js +++ b/lib/wallet/masterkey.js @@ -56,7 +56,7 @@ function MasterKey(options) { * @default */ -MasterKey.SALT = new Buffer('bcoin', 'ascii'); +MasterKey.SALT = Buffer.from('bcoin', 'ascii'); /** * Key derivation algorithms. @@ -256,7 +256,7 @@ MasterKey.prototype.derive = co(function* derive(passwd) { var p = this.p; if (typeof passwd === 'string') - passwd = new Buffer(passwd, 'utf8'); + passwd = Buffer.from(passwd, 'utf8'); switch (this.alg) { case MasterKey.alg.PBKDF2: @@ -280,7 +280,7 @@ MasterKey.prototype.encipher = function encipher(data, iv) { return; if (typeof iv === 'string') - iv = new Buffer(iv, 'hex'); + iv = Buffer.from(iv, 'hex'); return crypto.encipher(data, this.aesKey, iv.slice(0, 16)); }; @@ -297,7 +297,7 @@ MasterKey.prototype.decipher = function decipher(data, iv) { return; if (typeof iv === 'string') - iv = new Buffer(iv, 'hex'); + iv = Buffer.from(iv, 'hex'); return crypto.decipher(data, this.aesKey, iv.slice(0, 16)); }; diff --git a/lib/wallet/records.js b/lib/wallet/records.js index a0a57134..881e0a39 100644 --- a/lib/wallet/records.js +++ b/lib/wallet/records.js @@ -125,7 +125,7 @@ BlockMeta.prototype.clone = function clone() { */ BlockMeta.prototype.toHash = function toHash() { - return new Buffer(this.hash, 'hex'); + return Buffer.from(this.hash, 'hex'); }; /** diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index e86ed623..aa00e8a9 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -1489,7 +1489,7 @@ RPC.prototype.signMessage = co(function* signMessage(args, help) { if (!wallet.master.key) throw new RPCError(errs.WALLET_UNLOCK_NEEDED, 'Wallet is locked.'); - msg = new Buffer(MAGIC_STRING + msg, 'utf8'); + msg = Buffer.from(MAGIC_STRING + msg, 'utf8'); msg = crypto.hash256(msg); sig = ring.sign(msg); diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index e762791a..510641e1 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -25,7 +25,7 @@ var Script = require('../script/script'); var BlockMapRecord = records.BlockMapRecord; var OutpointMapRecord = records.OutpointMapRecord; var TXRecord = records.TXRecord; -var DUMMY = new Buffer([0]); +var DUMMY = Buffer.from([0]); /** * TXDB @@ -629,7 +629,7 @@ TXDB.prototype.addBlock = co(function* addBlock(hash, meta) { data = block.toRaw(); } - block = new Buffer(data.length + 32); + block = Buffer.allocUnsafe(data.length + 32); data.copy(block, 0); size = block.readUInt32LE(40, true); diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 80e4cb33..2f00573f 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -2042,7 +2042,7 @@ Wallet.prototype.getRedeem = co(function* getRedeem(hash) { var ring; if (typeof hash === 'string') - hash = new Buffer(hash, 'hex'); + hash = Buffer.from(hash, 'hex'); ring = yield this.getKey(hash.toString('hex')); diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 01bd6bf8..ef717c62 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -36,7 +36,7 @@ var PathMapRecord = records.PathMapRecord; var OutpointMapRecord = records.OutpointMapRecord; var TXRecord = records.TXRecord; var U32 = encoding.U32; -var DUMMY = new Buffer([0]); +var DUMMY = Buffer.from([0]); /** * WalletDB @@ -611,8 +611,8 @@ WalletDB.prototype.wipe = co(function* wipe() { this.logger.warning('I hope you know what you\'re doing.'); iter = this.db.iterator({ - gte: new Buffer([0x00]), - lte: new Buffer([0xff]) + gte: Buffer.from([0x00]), + lte: Buffer.from([0xff]) }); for (;;) { @@ -1008,7 +1008,7 @@ WalletDB.prototype.auth = co(function* auth(wid, token) { if (typeof token === 'string') { if (!util.isHex256(token)) throw new Error('WDB: Authentication error.'); - token = new Buffer(token, 'hex'); + token = Buffer.from(token, 'hex'); } // Compare in constant time: @@ -1172,7 +1172,7 @@ WalletDB.prototype.saveAccount = function saveAccount(account) { batch.put(layout.i(wid, name), U32(index)); // Index->Name lookups - batch.put(layout.n(wid, index), new Buffer(name, 'ascii')); + batch.put(layout.n(wid, index), Buffer.from(name, 'ascii')); wallet.accountCache.push(index, account); }; @@ -1419,7 +1419,7 @@ WalletDB.prototype.encryptKeys = co(function* encryptKeys(wallet, key) { assert(!path.encrypted); - iv = new Buffer(path.hash, 'hex'); + iv = Buffer.from(path.hash, 'hex'); iv = iv.slice(0, 16); path = path.clone(); @@ -1453,7 +1453,7 @@ WalletDB.prototype.decryptKeys = co(function* decryptKeys(wallet, key) { assert(path.encrypted); - iv = new Buffer(path.hash, 'hex'); + iv = Buffer.from(path.hash, 'hex'); iv = iv.slice(0, 16); path = path.clone(); diff --git a/lib/workers/master.js b/lib/workers/master.js index a51fef0c..3c9e7277 100644 --- a/lib/workers/master.js +++ b/lib/workers/master.js @@ -88,7 +88,7 @@ Master.prototype._initWebWorkers = function _initWebWorkers() { data = event.data.buf; data.__proto__ = Buffer.prototype; } else { - data = new Buffer(event.data, 'hex'); + data = Buffer.from(event.data, 'hex'); } self.emit('data', data); }; diff --git a/lib/workers/parser.js b/lib/workers/parser.js index ad3b6771..f8fd5dc7 100644 --- a/lib/workers/parser.js +++ b/lib/workers/parser.js @@ -50,7 +50,7 @@ Parser.prototype.read = function read(size) { assert(this.total >= size, 'Reading too much.'); if (size === 0) - return new Buffer(0); + return Buffer.alloc(0); pending = this.pending[0]; @@ -67,7 +67,7 @@ Parser.prototype.read = function read(size) { return chunk; } - chunk = new Buffer(size); + chunk = Buffer.allocUnsafe(size); off = 0; len = 0; diff --git a/lib/workers/workerpool.js b/lib/workers/workerpool.js index 266b94e6..89fafc62 100644 --- a/lib/workers/workerpool.js +++ b/lib/workers/workerpool.js @@ -516,7 +516,7 @@ Worker.prototype._initWebWorkers = function _initWebWorkers() { data = event.data.buf; data.__proto__ = Buffer.prototype; } else { - data = new Buffer(event.data, 'hex'); + data = Buffer.from(event.data, 'hex'); } self.emit('data', data); }; diff --git a/migrate/chaindb0to1.js b/migrate/chaindb0to1.js index 250aa190..dcd3a240 100644 --- a/migrate/chaindb0to1.js +++ b/migrate/chaindb0to1.js @@ -19,7 +19,7 @@ var db = bcoin.ldb({ function makeKey(data) { var height = data.readUInt32LE(1, true); - var key = new Buffer(5); + var key = Buffer.allocUnsafe(5); key[0] = 0x48; key.writeUInt32BE(height, 1, true); return key; @@ -64,7 +64,7 @@ var updateState = co(function* updateState() { batch.put('R', p); - ver = new Buffer(4); + ver = Buffer.allocUnsafe(4); ver.writeUInt32LE(1, 0, true); batch.put('V', ver); @@ -82,8 +82,8 @@ var updateEndian = co(function* updateEndian() { console.log('Iterating...'); iter = db.iterator({ - gte: new Buffer('4800000000', 'hex'), - lte: new Buffer('48ffffffff', 'hex'), + gte: Buffer.from('4800000000', 'hex'), + lte: Buffer.from('48ffffffff', 'hex'), values: true }); diff --git a/migrate/chaindb1to2.js b/migrate/chaindb1to2.js index 96f2f015..a7e254ae 100644 --- a/migrate/chaindb1to2.js +++ b/migrate/chaindb1to2.js @@ -57,7 +57,7 @@ var updateVersion = co(function* updateVersion() { if (ver !== 1) throw Error('DB is version ' + ver + '.'); - ver = new Buffer(4); + ver = Buffer.allocUnsafe(4); ver.writeUInt32LE(2, 0, true); batch.put('V', ver); }); @@ -205,7 +205,7 @@ function write(data, str, off) { } function pair(prefix, hash) { - var key = new Buffer(33); + var key = Buffer.allocUnsafe(33); if (typeof prefix === 'string') prefix = prefix.charCodeAt(0); key[0] = prefix; @@ -214,7 +214,7 @@ function pair(prefix, hash) { } function ipair(prefix, num) { - var key = new Buffer(5); + var key = Buffer.allocUnsafe(5); if (typeof prefix === 'string') prefix = prefix.charCodeAt(0); key[0] = prefix; diff --git a/migrate/ensure-tip-index.js b/migrate/ensure-tip-index.js index ef03c434..7d3da36d 100644 --- a/migrate/ensure-tip-index.js +++ b/migrate/ensure-tip-index.js @@ -7,7 +7,7 @@ var crypto = require('../lib/crypto/crypto'); var util = require('../lib/utils/util'); var LDB = require('../lib/db/ldb'); var BN = require('bn.js'); -var DUMMY = new Buffer([0]); +var DUMMY = Buffer.from([0]); var file = process.argv[2]; var db, batch; @@ -128,7 +128,7 @@ function write(data, str, off) { } function pair(prefix, hash) { - var key = new Buffer(33); + var key = Buffer.allocUnsafe(33); if (typeof prefix === 'string') prefix = prefix.charCodeAt(0); key[0] = prefix; @@ -137,7 +137,7 @@ function pair(prefix, hash) { } function ipair(prefix, num) { - var key = new Buffer(5); + var key = Buffer.allocUnsafe(5); if (typeof prefix === 'string') prefix = prefix.charCodeAt(0); key[0] = prefix; diff --git a/migrate/walletdb2to3.js b/migrate/walletdb2to3.js index 2bd136b6..2f56189f 100644 --- a/migrate/walletdb2to3.js +++ b/migrate/walletdb2to3.js @@ -44,7 +44,7 @@ var updateVersion = co(function* updateVersion() { yield db.backup(bak); - ver = new Buffer(4); + ver = Buffer.allocUnsafe(4); ver.writeUInt32LE(3, 0, true); batch.put('V', ver); }); @@ -123,7 +123,7 @@ var updateAccounts = co(function* updateAccounts() { if (account._old) { batch.del(layout.i(account.wid, account._old)); - buf = new Buffer(4); + buf = Buffer.allocUnsafe(4); buf.writeUInt32LE(account.accountIndex, 0, true); batch.put(layout.i(account.wid, account.name), buf); } @@ -157,7 +157,7 @@ var updateWallets = co(function* updateWallets() { if (wallet._old) { batch.del(layout.l(wallet._old)); - buf = new Buffer(4); + buf = Buffer.allocUnsafe(4); buf.writeUInt32LE(wallet.wid, 0, true); batch.put(layout.l(wallet.id), buf); } diff --git a/migrate/walletdb3to4.js b/migrate/walletdb3to4.js index 0c82d13a..b0f72512 100644 --- a/migrate/walletdb3to4.js +++ b/migrate/walletdb3to4.js @@ -45,7 +45,7 @@ var updateVersion = co(function* updateVersion() { yield db.backup(bak); - ver = new Buffer(4); + ver = Buffer.allocUnsafe(4); ver.writeUInt32LE(4, 0, true); batch.put('V', ver); }); @@ -55,8 +55,8 @@ var updateTXDB = co(function* updateTXDB() { var i, keys, key, hash, tx, walletdb; keys = yield db.keys({ - gte: new Buffer([0x00]), - lte: new Buffer([0xff]) + gte: Buffer.from([0x00]), + lte: Buffer.from([0xff]) }); for (i = 0; i < keys.length; i++) { diff --git a/migrate/walletdb4to5.js b/migrate/walletdb4to5.js index bcfc3258..e2e02ac6 100644 --- a/migrate/walletdb4to5.js +++ b/migrate/walletdb4to5.js @@ -38,7 +38,7 @@ var updateVersion = co(function* updateVersion() { yield db.backup(bak); - ver = new Buffer(4); + ver = Buffer.allocUnsafe(4); ver.writeUInt32LE(5, 0, true); batch.put('V', ver); }); @@ -47,8 +47,8 @@ var updateTXDB = co(function* updateTXDB() { var i, keys, key; keys = yield db.keys({ - gte: new Buffer([0x00]), - lte: new Buffer([0xff]) + gte: Buffer.from([0x00]), + lte: Buffer.from([0xff]) }); for (i = 0; i < keys.length; i++) { diff --git a/migrate/walletdb5to6.js b/migrate/walletdb5to6.js index d7b53bdd..e83c4bac 100644 --- a/migrate/walletdb5to6.js +++ b/migrate/walletdb5to6.js @@ -39,7 +39,7 @@ var updateVersion = co(function* updateVersion() { yield db.backup(bak); - ver = new Buffer(4); + ver = Buffer.allocUnsafe(4); ver.writeUInt32LE(6, 0, true); batch.put('V', ver); }); @@ -49,8 +49,8 @@ var wipeTXDB = co(function* wipeTXDB() { var i, keys, key; keys = yield db.keys({ - gte: new Buffer([0x00]), - lte: new Buffer([0xff]) + gte: Buffer.from([0x00]), + lte: Buffer.from([0xff]) }); for (i = 0; i < keys.length; i++) { @@ -68,7 +68,7 @@ var wipeTXDB = co(function* wipeTXDB() { } } - batch.del(new Buffer([0x52])); // R + batch.del(Buffer.from([0x52])); // R console.log('Wiped %d txdb records.', total); }); @@ -77,8 +77,8 @@ var patchAccounts = co(function* patchAccounts() { var i, items, item, wid, index, account; items = yield db.range({ - gte: new Buffer('610000000000000000', 'hex'), // a - lte: new Buffer('61ffffffffffffffff', 'hex') // a + gte: Buffer.from('610000000000000000', 'hex'), // a + lte: Buffer.from('61ffffffffffffffff', 'hex') // a }); for (i = 0; i < items.length; i++) { @@ -89,7 +89,7 @@ var patchAccounts = co(function* patchAccounts() { console.log('a[%d][%d] -> lookahead=%d', wid, index, account.lookahead); batch.put(item.key, accountToRaw(account)); console.log('n[%d][%d] -> %s', wid, index, account.name); - batch.put(n(wid, index), new Buffer(account.name, 'ascii')); + batch.put(n(wid, index), Buffer.from(account.name, 'ascii')); } }); @@ -97,8 +97,8 @@ var indexPaths = co(function* indexPaths() { var i, items, item, wid, index, hash; items = yield db.range({ - gte: new Buffer('5000000000' + encoding.NULL_HASH, 'hex'), // P - lte: new Buffer('50ffffffff' + encoding.HIGH_HASH, 'hex') // P + gte: Buffer.from('5000000000' + encoding.NULL_HASH, 'hex'), // P + lte: Buffer.from('50ffffffff' + encoding.HIGH_HASH, 'hex') // P }); for (i = 0; i < items.length; i++) { @@ -107,7 +107,7 @@ var indexPaths = co(function* indexPaths() { hash = item.key.toString('hex', 5); index = item.value.readUInt32LE(0, true); console.log('r[%d][%d][%s] -> NUL', wid, index, hash); - batch.put(r(wid, index, hash), new Buffer([0])); + batch.put(r(wid, index, hash), Buffer.from([0])); } }); @@ -115,8 +115,8 @@ var patchPathMaps = co(function* patchPathMaps() { var i, items, item, hash, wids; items = yield db.range({ - gte: new Buffer('70' + encoding.NULL_HASH, 'hex'), // p - lte: new Buffer('70' + encoding.HIGH_HASH, 'hex') // p + gte: Buffer.from('70' + encoding.NULL_HASH, 'hex'), // p + lte: Buffer.from('70' + encoding.HIGH_HASH, 'hex') // p }); for (i = 0; i < items.length; i++) { @@ -208,7 +208,7 @@ function accountFromRaw(data) { } function n(wid, index) { - var key = new Buffer(9); + var key = Buffer.allocUnsafe(9); key[0] = 0x6e; key.writeUInt32BE(wid, 1, true); key.writeUInt32BE(index, 5, true); @@ -216,7 +216,7 @@ function n(wid, index) { } function r(wid, index, hash) { - var key = new Buffer(1 + 4 + 4 + (hash.length / 2)); + var key = Buffer.allocUnsafe(1 + 4 + 4 + (hash.length / 2)); key[0] = 0x72; key.writeUInt32BE(wid, 1, true); key.writeUInt32BE(index, 5, true); diff --git a/scripts/gen.js b/scripts/gen.js index eadd0012..aa7fbea3 100644 --- a/scripts/gen.js +++ b/scripts/gen.js @@ -18,14 +18,14 @@ function createGenesisBlock(options) { var tx, block; if (!flags) { - flags = new Buffer( + flags = Buffer.from( 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks', 'ascii'); } if (!script) { script = Script.fromArray([ - new Buffer('04678afdb0fe5548271967f1a67130b7105cd6a828e039' + Buffer.from('04678afdb0fe5548271967f1a67130b7105cd6a828e039' + '09a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c3' + '84df7ba0b8d578a4c702b6bf11d5f', 'hex'), opcodes.OP_CHECKSIG @@ -45,7 +45,7 @@ function createGenesisBlock(options) { }, script: [ Opcode.fromNumber(new BN(486604799)), - Opcode.fromPush(new Buffer([4])), + Opcode.fromPush(Buffer.from([4])), Opcode.fromData(flags) ], sequence: 0xffffffff diff --git a/test/aes-test.js b/test/aes-test.js index af6f9426..f3e06430 100644 --- a/test/aes-test.js +++ b/test/aes-test.js @@ -21,10 +21,10 @@ describe('AES', function() { assert(passphrase, 'No passphrase.'); if (typeof data === 'string') - data = new Buffer(data, 'utf8'); + data = Buffer.from(data, 'utf8'); if (typeof passphrase === 'string') - passphrase = new Buffer(passphrase, 'utf8'); + passphrase = Buffer.from(passphrase, 'utf8'); key = pbkdf2key(passphrase, 2048, 32, 16); cipher = nativeCrypto.createCipheriv('aes-256-cbc', key.key, key.iv); @@ -42,10 +42,10 @@ describe('AES', function() { assert(passphrase, 'No passphrase.'); if (typeof data === 'string') - data = new Buffer(data, 'hex'); + data = Buffer.from(data, 'hex'); if (typeof passphrase === 'string') - passphrase = new Buffer(passphrase, 'utf8'); + passphrase = Buffer.from(passphrase, 'utf8'); key = pbkdf2key(passphrase, 2048, 32, 16); decipher = nativeCrypto.createDecipheriv('aes-256-cbc', key.key, key.iv); @@ -63,10 +63,10 @@ describe('AES', function() { assert(passphrase, 'No passphrase.'); if (typeof data === 'string') - data = new Buffer(data, 'utf8'); + data = Buffer.from(data, 'utf8'); if (typeof passphrase === 'string') - passphrase = new Buffer(passphrase, 'utf8'); + passphrase = Buffer.from(passphrase, 'utf8'); key = pbkdf2key(passphrase, 2048, 32, 16); return crypto.encipher(data, key.key, key.iv); @@ -79,10 +79,10 @@ describe('AES', function() { assert(passphrase, 'No passphrase.'); if (typeof data === 'string') - data = new Buffer(data, 'hex'); + data = Buffer.from(data, 'hex'); if (typeof passphrase === 'string') - passphrase = new Buffer(passphrase, 'utf8'); + passphrase = Buffer.from(passphrase, 'utf8'); key = pbkdf2key(passphrase, 2048, 32, 16); return crypto.decipher(data, key.key, key.iv); @@ -95,10 +95,10 @@ describe('AES', function() { assert(passphrase, 'No passphrase.'); if (typeof data === 'string') - data = new Buffer(data, 'utf8'); + data = Buffer.from(data, 'utf8'); if (typeof passphrase === 'string') - passphrase = new Buffer(passphrase, 'utf8'); + passphrase = Buffer.from(passphrase, 'utf8'); key = pbkdf2key(passphrase, 2048, 32, 16); @@ -112,10 +112,10 @@ describe('AES', function() { assert(passphrase, 'No passphrase.'); if (typeof data === 'string') - data = new Buffer(data, 'hex'); + data = Buffer.from(data, 'hex'); if (typeof passphrase === 'string') - passphrase = new Buffer(passphrase, 'utf8'); + passphrase = Buffer.from(passphrase, 'utf8'); key = pbkdf2key(passphrase, 2048, 32, 16); @@ -123,15 +123,15 @@ describe('AES', function() { } it('should encrypt and decrypt a hash with 2 blocks', function() { - var hash = crypto.sha256(new Buffer([])); + var hash = crypto.sha256(Buffer.alloc(0)); var enchash = encrypt(hash, 'foo'); var dechash = decrypt(enchash, 'foo'); - var hash2 = crypto.sha256(new Buffer([])); + var hash2 = crypto.sha256(Buffer.alloc(0)); var enchash2 = nencrypt(hash2, 'foo'); var dechash2 = ndecrypt(enchash2, 'foo'); - var hash3 = crypto.sha256(new Buffer([])); + var hash3 = crypto.sha256(Buffer.alloc(0)); var enchash3 = bencrypt(hash3, 'foo'); var dechash3 = bdecrypt(enchash3, 'foo'); @@ -142,11 +142,11 @@ describe('AES', function() { }); it('should encrypt and decrypt a hash with uneven blocks', function() { - var hash = Buffer.concat([crypto.sha256(new Buffer([])), new Buffer([1,2,3])]); + var hash = Buffer.concat([crypto.sha256(Buffer.alloc(0)), Buffer.from([1,2,3])]); var enchash = encrypt(hash, 'foo'); var dechash = decrypt(enchash, 'foo'); - var hash2 = Buffer.concat([crypto.sha256(new Buffer([])), new Buffer([1,2,3])]); + var hash2 = Buffer.concat([crypto.sha256(Buffer.alloc(0)), Buffer.from([1,2,3])]); var enchash2 = nencrypt(hash2, 'foo'); var dechash2 = ndecrypt(enchash2, 'foo'); diff --git a/test/bech32-test.js b/test/bech32-test.js index 3e8fb49b..36011aee 100644 --- a/test/bech32-test.js +++ b/test/bech32-test.js @@ -39,14 +39,14 @@ describe('Bech32', function() { var VALID_ADDRESS = [ [ 'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4', - new Buffer([ + Buffer.from([ 0x00, 0x14, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6 ]) ], [ 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7', - new Buffer([ + Buffer.from([ 0x00, 0x20, 0x18, 0x63, 0x14, 0x3c, 0x14, 0xc5, 0x16, 0x68, 0x04, 0xbd, 0x19, 0x20, 0x33, 0x56, 0xda, 0x13, 0x6c, 0x98, 0x56, 0x78, 0xcd, 0x4d, 0x27, 0xa1, 0xb8, 0xc6, 0x32, 0x96, 0x04, 0x90, 0x32, @@ -55,7 +55,7 @@ describe('Bech32', function() { ], [ 'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx', - new Buffer([ + Buffer.from([ 0x81, 0x28, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, @@ -64,20 +64,20 @@ describe('Bech32', function() { ], [ 'BC1SW50QA3JX3S', - new Buffer([ + Buffer.from([ 0x90, 0x02, 0x75, 0x1e ]) ], [ 'bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj', - new Buffer([ + Buffer.from([ 0x82, 0x10, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23 ]) ], [ 'tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy', - new Buffer([ + Buffer.from([ 0x00, 0x20, 0x00, 0x00, 0x00, 0xc4, 0xa5, 0xca, 0xd4, 0x62, 0x21, 0xb2, 0xa1, 0x87, 0x90, 0x5e, 0x52, 0x66, 0x36, 0x2b, 0x99, 0xd5, 0xe9, 0x1c, 0x6c, 0xe2, 0x4d, 0x16, 0x5d, 0xab, 0x93, 0xe8, 0x64, @@ -105,7 +105,7 @@ describe('Bech32', function() { if (dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16) throw new Error('Invalid bech32 prefix or data length.'); - data = bech32.convert(dec.data, new Buffer(84), 5, 8, -1, 1); + data = bech32.convert(dec.data, Buffer.allocUnsafe(84), 5, 8, -1, 1); if (data.length < 2 || data.length > 40) throw new Error('Invalid witness program size.'); @@ -120,7 +120,7 @@ describe('Bech32', function() { } function toAddress(hrp, version, program) { - var data = bech32.convert(program, new Buffer(65), 8, 5, version, 0); + var data = bech32.convert(program, Buffer.allocUnsafe(65), 8, 5, version, 0); var ret = bech32.serialize(hrp, data); fromAddress(hrp, ret); @@ -129,7 +129,7 @@ describe('Bech32', function() { } function createProgram(version, program) { - var ver = new Buffer([version ? version + 0x80 : 0, program.length]); + var ver = Buffer.from([version ? version + 0x80 : 0, program.length]); return Buffer.concat([ver, program]); } diff --git a/test/bip150-test.js b/test/bip150-test.js index 7f48fea8..b6708bcd 100644 --- a/test/bip150-test.js +++ b/test/bip150-test.js @@ -20,7 +20,7 @@ describe('BIP150', function() { server.bip150 = new BIP150(server, '127.0.0.1', false, db, sk); function payload() { - return new Buffer('deadbeef', 'hex'); + return Buffer.from('deadbeef', 'hex'); } it('should do encinit', function() { diff --git a/test/bip151-test.js b/test/bip151-test.js index bfb44724..866bff54 100644 --- a/test/bip151-test.js +++ b/test/bip151-test.js @@ -8,7 +8,7 @@ describe('BIP151', function() { var server = new BIP151(); function payload() { - return new Buffer('deadbeef', 'hex'); + return Buffer.from('deadbeef', 'hex'); } it('should do encinit', function() { diff --git a/test/bip70-test.js b/test/bip70-test.js index b400afc0..d5150016 100644 --- a/test/bip70-test.js +++ b/test/bip70-test.js @@ -8,14 +8,14 @@ var x509 = bip70.x509; var tests = require('./data/bip70.json'); -tests.valid = new Buffer(tests.valid, 'hex'); -tests.invalid = new Buffer(tests.invalid, 'hex'); -tests.untrusted = new Buffer(tests.untrusted, 'hex'); -tests.ack = new Buffer(tests.ack, 'hex'); +tests.valid = Buffer.from(tests.valid, 'hex'); +tests.invalid = Buffer.from(tests.invalid, 'hex'); +tests.untrusted = Buffer.from(tests.untrusted, 'hex'); +tests.ack = Buffer.from(tests.ack, 'hex'); tests.ca = { - crt: new Buffer(tests.ca.crt, 'hex'), - priv: new Buffer(tests.ca.priv, 'hex'), - pub: new Buffer(tests.ca.pub, 'hex') + crt: Buffer.from(tests.ca.crt, 'hex'), + priv: Buffer.from(tests.ca.priv, 'hex'), + pub: Buffer.from(tests.ca.pub, 'hex') }; x509.allowUntrusted = true; diff --git a/test/block-test.js b/test/block-test.js index 8f7d4b95..11c43183 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -77,7 +77,7 @@ describe('Block', function() { '1f5e46b9da3a8b1241f4a1501741d3453bafddf6135b600b926e3f4056c6d564', '33825657ba32afe269819f01993bd77baba86379043168c94845d32370e53562' ], - flags: new Buffer([245, 122, 0]) + flags: Buffer.from([245, 122, 0]) }); raw = mblock.toRaw().toString('hex'); diff --git a/test/bloom-test.js b/test/bloom-test.js index f9dc4ae1..c6980899 100644 --- a/test/bloom-test.js +++ b/test/bloom-test.js @@ -12,7 +12,7 @@ describe('Bloom', function() { + '0000000001000000800000080000000'; function mm(str, seed, expect, enc) { - assert.equal(murmur3(new Buffer(str, enc || 'ascii'), seed), expect); + assert.equal(murmur3(Buffer.from(str, enc || 'ascii'), seed), expect); } this.timeout(20000); diff --git a/test/chachapoly-test.js b/test/chachapoly-test.js index e24b4eac..fd9e88c0 100644 --- a/test/chachapoly-test.js +++ b/test/chachapoly-test.js @@ -15,14 +15,14 @@ describe('ChaCha20 / Poly1305 / AEAD', function() { var counter = options.counter; var chacha, plainenc; - key = new Buffer(key, 'hex'); - nonce = new Buffer(nonce, 'hex'); - plain = new Buffer(plain, 'hex'); - ciphertext = new Buffer(ciphertext, 'hex'); + key = Buffer.from(key, 'hex'); + nonce = Buffer.from(nonce, 'hex'); + plain = Buffer.from(plain, 'hex'); + ciphertext = Buffer.from(ciphertext, 'hex'); chacha = new ChaCha20(); chacha.init(key, nonce, counter); - plainenc = new Buffer(plain); + plainenc = Buffer.from(plain); chacha.encrypt(plainenc); assert.deepEqual(plainenc, ciphertext); @@ -42,20 +42,20 @@ describe('ChaCha20 / Poly1305 / AEAD', function() { var tag = options.tag; var aead, plainenc; - plain = new Buffer(plain, 'hex'); - aad = new Buffer(aad, 'hex'); - key = new Buffer(key, 'hex'); - nonce = new Buffer(nonce, 'hex'); - pk = new Buffer(pk, 'hex'); - ciphertext = new Buffer(ciphertext, 'hex'); - tag = new Buffer(tag, 'hex'); + plain = Buffer.from(plain, 'hex'); + aad = Buffer.from(aad, 'hex'); + key = Buffer.from(key, 'hex'); + nonce = Buffer.from(nonce, 'hex'); + pk = Buffer.from(pk, 'hex'); + ciphertext = Buffer.from(ciphertext, 'hex'); + tag = Buffer.from(tag, 'hex'); aead = new AEAD(); aead.init(key, nonce); assert.equal(aead.chacha20.getCounter(), 1); assert.deepEqual(aead.polyKey, pk); aead.aad(aad); - plainenc = new Buffer(plain); + plainenc = Buffer.from(plain); aead.encrypt(plainenc); assert.deepEqual(plainenc, ciphertext); assert.deepEqual(aead.finish(), tag); @@ -160,9 +160,9 @@ describe('ChaCha20 / Poly1305 / AEAD', function() { }); it('should perform poly1305', function() { - var expected = new Buffer('ddb9da7ddd5e52792730ed5cda5f90a4', 'hex'); - var key = new Buffer(32); - var msg = new Buffer(73); + var expected = Buffer.from('ddb9da7ddd5e52792730ed5cda5f90a4', 'hex'); + var key = Buffer.allocUnsafe(32); + var msg = Buffer.allocUnsafe(73); var mac; var i; @@ -183,9 +183,9 @@ describe('ChaCha20 / Poly1305 / AEAD', function() { var tag = 'a8061dc1305136c6c22b8baf0c0127a9'; var mac; - key = new Buffer(key, 'hex'); - msg = new Buffer(msg, 'ascii'); - tag = new Buffer(tag, 'hex'); + key = Buffer.from(key, 'hex'); + msg = Buffer.from(msg, 'ascii'); + tag = Buffer.from(tag, 'hex'); mac = Poly1305.auth(msg, key); assert(Poly1305.verify(mac, tag)); diff --git a/test/chain-test.js b/test/chain-test.js index 55acc3f1..d42dcdec 100644 --- a/test/chain-test.js +++ b/test/chain-test.js @@ -523,7 +523,7 @@ describe('Chain', function() { var block = yield cpu.mineBlock(); var tx = block.txs[0]; var input = tx.inputs[0]; - input.witness.set(0, new Buffer(33)); + input.witness.set(0, Buffer.allocUnsafe(33)); input.witness.compile(); block.refresh(true); assert.equal(yield addBlock(block), 'bad-witness-nonce-size'); diff --git a/test/hd-test.js b/test/hd-test.js index 4c2f2ed9..d27f7377 100644 --- a/test/hd-test.js +++ b/test/hd-test.js @@ -26,7 +26,7 @@ describe('HD', function() { }); it('should create master private key', function() { - master = HD.PrivateKey.fromSeed(new Buffer(vectors.seed, 'hex')); + master = HD.PrivateKey.fromSeed(Buffer.from(vectors.seed, 'hex')); assert.equal(master.toBase58(), vectors.master_priv); assert.equal(master.toPublic().toBase58(), vectors.master_pub); }); @@ -95,7 +95,7 @@ describe('HD', function() { var master; it('should create from a seed', function() { - master = HD.PrivateKey.fromSeed(new Buffer(vector.seed, 'hex')); + master = HD.PrivateKey.fromSeed(Buffer.from(vector.seed, 'hex')); equal(master.toBase58(), vector.m.prv); equal(master.toPublic().toBase58(), vector.m.pub); }); diff --git a/test/mempool-test.js b/test/mempool-test.js index 3c07cf01..09e12f3e 100644 --- a/test/mempool-test.js +++ b/test/mempool-test.js @@ -262,7 +262,7 @@ describe('Mempool', function() { sig = tx.signature(0, prev, 70000, kp.privateKey, Script.hashType.ALL, 0); tx.inputs[0].script = new Script([sig]); - tx.inputs[0].witness.push(new Buffer(0)); + tx.inputs[0].witness.push(Buffer.alloc(0)); tx = tx.toTX(); try { diff --git a/test/mnemonic-test.js b/test/mnemonic-test.js index 734c1f77..cad6e567 100644 --- a/test/mnemonic-test.js +++ b/test/mnemonic-test.js @@ -8,9 +8,9 @@ var mnemonic2 = require('./data/mnemonic2'); describe('Mnemonic', function() { mnemonic1.forEach(function(data, i) { - var entropy = new Buffer(data[0], 'hex'); + var entropy = Buffer.from(data[0], 'hex'); var phrase = data[1]; - var seed = new Buffer(data[2], 'hex'); + var seed = Buffer.from(data[2], 'hex'); var xpriv = data[3]; it('should create an english mnemonic (' + i + ')', function() { var mnemonic, key; @@ -30,9 +30,9 @@ describe('Mnemonic', function() { }); mnemonic2.forEach(function(data, i) { - var entropy = new Buffer(data.entropy, 'hex'); + var entropy = Buffer.from(data.entropy, 'hex'); var phrase = data.mnemonic; - var seed = new Buffer(data.seed, 'hex'); + var seed = Buffer.from(data.seed, 'hex'); var passphrase = data.passphrase; var xpriv = data.bip32_xprv; it('should create a japanese mnemonic (' + i + ')', function() { diff --git a/test/protocol-test.js b/test/protocol-test.js index eda9e553..539c470b 100644 --- a/test/protocol-test.js +++ b/test/protocol-test.js @@ -24,7 +24,7 @@ describe('Protocol', function() { function packetTest(command, payload, test) { it('should encode/decode ' + command, function(cb) { - var ver = new Buffer(framer.packet(command, payload.toRaw())); + var ver = Buffer.from(framer.packet(command, payload.toRaw())); parser.once('packet', function(packet) { assert.equal(packet.cmd, command); test(packet); @@ -109,7 +109,7 @@ describe('Protocol', function() { 'parsed transaction', function() { var tx, rawTwoTxs, rawFirstTx; - rawTwoTxs = new Buffer( + rawTwoTxs = Buffer.from( '0100000004b124cca7e9686375380c845d0fd002ed704aef4472f4cc193' + 'fca4aa1b3404da400000000b400493046022100d3c9ba786488323c975f' + 'e61593df6a8041c5442736f361887abfe5c97175c72b022100ca61688f4' + @@ -161,7 +161,7 @@ describe('Protocol', function() { 'dc5c0500000017a9149eb21980dc9d413d8eac27314938b9da920ee53e8' + '700000000', 'hex'); - rawFirstTx = new Buffer( + rawFirstTx = Buffer.from( '0100000004b124cca7e9686375380c845d0fd002ed704aef4472f4cc193' + 'fca4aa1b3404da400000000b400493046022100d3c9ba786488323c975f' + 'e61593df6a8041c5442736f361887abfe5c97175c72b022100ca61688f4' + diff --git a/test/script-test.js b/test/script-test.js index bc87a020..1fb5654c 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -165,14 +165,14 @@ describe('Script', function() { var input, output, stack; input = new Script([ - new Buffer('ffffff7f', 'hex'), + Buffer.from('ffffff7f', 'hex'), opcodes.OP_NEGATE, opcodes.OP_DUP, opcodes.OP_ADD ]); output = new Script([ - new Buffer('feffffff80', 'hex'), + Buffer.from('feffffff80', 'hex'), opcodes.OP_EQUAL ]); @@ -207,15 +207,15 @@ describe('Script', function() { var input, output, stack; input = new Script([ - new Buffer([0x16]), - new Buffer([0x15]), - new Buffer([0x14]) + Buffer.from([0x16]), + Buffer.from([0x15]), + Buffer.from([0x14]) ]); output = new Script([ opcodes.OP_0, opcodes.OP_ROLL, - new Buffer([0x14]), + Buffer.from([0x14]), opcodes.OP_EQUALVERIFY, opcodes.OP_DEPTH, opcodes.OP_2, diff --git a/test/scrypt-test.js b/test/scrypt-test.js index 158f45cd..da03af9c 100644 --- a/test/scrypt-test.js +++ b/test/scrypt-test.js @@ -5,8 +5,8 @@ var scrypt = require('../lib/crypto/crypto').scrypt; describe('Scrypt', function() { it('should perform scrypt with N=16', function() { - var pass = new Buffer(''); - var salt = new Buffer(''); + var pass = Buffer.from(''); + var salt = Buffer.from(''); var result = scrypt(pass, salt, 16, 1, 1, 64); assert.equal(result.toString('hex'), '' + '77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3f' @@ -15,8 +15,8 @@ describe('Scrypt', function() { }); it('should perform scrypt with N=1024', function() { - var pass = new Buffer('password'); - var salt = new Buffer('NaCl'); + var pass = Buffer.from('password'); + var salt = Buffer.from('NaCl'); var result = scrypt(pass, salt, 1024, 8, 16, 64); assert.equal(result.toString('hex'), '' + 'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e773' @@ -25,8 +25,8 @@ describe('Scrypt', function() { }); it('should perform scrypt with N=16384', function() { - var pass = new Buffer('pleaseletmein'); - var salt = new Buffer('SodiumChloride'); + var pass = Buffer.from('pleaseletmein'); + var salt = Buffer.from('SodiumChloride'); var result = scrypt(pass, salt, 16384, 8, 1, 64); assert.equal(result.toString('hex'), '' + '7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b54' @@ -36,8 +36,8 @@ describe('Scrypt', function() { // Only enable if you want to wait a while. // it('should perform scrypt with N=1048576', function() { - // var pass = new Buffer('pleaseletmein'); - // var salt = new Buffer('SodiumChloride'); + // var pass = Buffer.from('pleaseletmein'); + // var salt = Buffer.from('SodiumChloride'); // var result = scrypt(pass, salt, 1048576, 8, 1, 64); // assert.equal(result.toString('hex'), '' // + '2101cb9b6a511aaeaddbbe09cf70f881ec568d574a2ffd4dabe5' diff --git a/test/siphash-test.js b/test/siphash-test.js index a5282c31..c0640128 100644 --- a/test/siphash-test.js +++ b/test/siphash-test.js @@ -9,7 +9,7 @@ describe('SipHash', function() { var k0 = U64(0x07060504, 0x03020100).toRaw(); var k1 = U64(0x0f0e0d0c, 0x0b0a0908).toRaw(); var key = Buffer.concat([k0, k1]); - assert.equal(siphash(new Buffer(0), key).toString('hex'), '310e0edd47db6f72'); + assert.equal(siphash(Buffer.alloc(0), key).toString('hex'), '310e0edd47db6f72'); }); it('should perform siphash with data', function() { @@ -23,7 +23,7 @@ describe('SipHash', function() { it('should perform siphash with uint256', function() { var k0 = U64(0x07060504, 0x03020100).toRaw(); var k1 = U64(0x0f0e0d0c, 0x0b0a0908).toRaw(); - var hash = new Buffer('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', 'hex'); + var hash = Buffer.from('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', 'hex'); var key = Buffer.concat([k0, k1]); assert.equal(siphash(hash, key).toString('hex'), 'ce7cf2722f512771'); }); diff --git a/test/tx-test.js b/test/tx-test.js index 0967d3bd..099d457b 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -720,8 +720,8 @@ describe('TX', function() { input = new Script(); witness = new Witness([ - new Buffer([0]), - new Buffer([0]) + Buffer.from([0]), + Buffer.from([0]) ]); ctx = sigopContext(input, witness, output); @@ -759,8 +759,8 @@ describe('TX', function() { ]); witness = new Witness([ - new Buffer([0]), - new Buffer([0]) + Buffer.from([0]), + Buffer.from([0]) ]); ctx = sigopContext(input, witness, output); @@ -780,8 +780,8 @@ describe('TX', function() { input = new Script(); witness = new Witness([ - new Buffer([0]), - new Buffer([0]), + Buffer.from([0]), + Buffer.from([0]), redeem.toRaw() ]); @@ -808,8 +808,8 @@ describe('TX', function() { ]); witness = new Witness([ - new Buffer([0]), - new Buffer([0]), + Buffer.from([0]), + Buffer.from([0]), wscript.toRaw() ]); diff --git a/test/utils-test.js b/test/utils-test.js index 65fb23a9..133f82ab 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -30,7 +30,7 @@ describe('Utils', function() { ]; it('should encode/decode base58', function() { - var buf = new Buffer('000000deadbeef', 'hex'); + var buf = Buffer.from('000000deadbeef', 'hex'); var b = base58.encode(buf); var i, r; @@ -38,7 +38,7 @@ describe('Utils', function() { assert.deepEqual(base58.decode(b), buf); for (i = 0; i < vectors.length; i++) { - r = new Buffer(vectors[i][0], 'hex'); + r = Buffer.from(vectors[i][0], 'hex'); b = vectors[i][1]; assert.equal(base58.encode(r), b); assert.deepEqual(base58.decode(b), r); @@ -49,7 +49,7 @@ describe('Utils', function() { var bits = 0x1900896c; var hash; - hash = new Buffer( + hash = Buffer.from( '672b3f1bb11a994267ea4171069ba0aa4448a840f38e8f340000000000000000', 'hex' ); @@ -110,63 +110,63 @@ describe('Utils', function() { */ n = 0; - b = new Buffer(1); + b = Buffer.allocUnsafe(1); b.fill(0x00); encoding.writeVarint2(b, 0, 0); assert.equal(encoding.readVarint2(b, 0).value, 0); assert.deepEqual(b, [0]); - b = new Buffer(1); + b = Buffer.allocUnsafe(1); b.fill(0x00); encoding.writeVarint2(b, 1, 0); assert.equal(encoding.readVarint2(b, 0).value, 1); assert.deepEqual(b, [1]); - b = new Buffer(1); + b = Buffer.allocUnsafe(1); b.fill(0x00); encoding.writeVarint2(b, 127, 0); assert.equal(encoding.readVarint2(b, 0).value, 127); assert.deepEqual(b, [0x7f]); - b = new Buffer(2); + b = Buffer.allocUnsafe(2); b.fill(0x00); encoding.writeVarint2(b, 128, 0); assert.equal(encoding.readVarint2(b, 0).value, 128); assert.deepEqual(b, [0x80, 0x00]); - b = new Buffer(2); + b = Buffer.allocUnsafe(2); b.fill(0x00); encoding.writeVarint2(b, 255, 0); assert.equal(encoding.readVarint2(b, 0).value, 255); assert.deepEqual(b, [0x80, 0x7f]); - b = new Buffer(2); + b = Buffer.allocUnsafe(2); b.fill(0x00); encoding.writeVarint2(b, 16383, 0); assert.equal(encoding.readVarint2(b, 0).value, 16383); assert.deepEqual(b, [0xfe, 0x7f]); - b = new Buffer(2); + b = Buffer.allocUnsafe(2); b.fill(0x00); encoding.writeVarint2(b, 16384, 0); assert.equal(encoding.readVarint2(b, 0).value, 16384); assert.deepEqual(b, [0xff, 0x00]); - b = new Buffer(3); + b = Buffer.allocUnsafe(3); b.fill(0x00); encoding.writeVarint2(b, 16511, 0); assert.equal(encoding.readVarint2(b, 0).value, 16511); // assert.deepEqual(b, [0x80, 0xff, 0x7f]); assert.deepEqual(b, [0xff, 0x7f, 0x00]); - b = new Buffer(3); + b = Buffer.allocUnsafe(3); b.fill(0x00); encoding.writeVarint2(b, 65535, 0); assert.equal(encoding.readVarint2(b, 0).value, 65535); // assert.deepEqual(b, [0x82, 0xfd, 0x7f]); assert.deepEqual(b, [0x82, 0xfe, 0x7f]); - b = new Buffer(5); + b = Buffer.allocUnsafe(5); b.fill(0x00); encoding.writeVarint2(b, Math.pow(2, 32), 0); assert.equal(encoding.readVarint2(b, 0).value, Math.pow(2, 32)); @@ -198,8 +198,8 @@ describe('Utils', function() { ]; unsigned.forEach(function(num) { - var buf1 = new Buffer(8); - var buf2 = new Buffer(8); + var buf1 = Buffer.allocUnsafe(8); + var buf2 = Buffer.allocUnsafe(8); var msg = 'should write+read a ' + num.bitLength() + ' bit unsigned int'; it(msg, function() { @@ -216,8 +216,8 @@ describe('Utils', function() { }); signed.forEach(function(num) { - var buf1 = new Buffer(8); - var buf2 = new Buffer(8); + var buf1 = Buffer.allocUnsafe(8); + var buf2 = Buffer.allocUnsafe(8); var msg = 'should write+read a ' + num.bitLength() + ' bit ' + (num.isNeg() ? 'negative' : 'positive') + ' int'; @@ -268,9 +268,9 @@ describe('Utils', function() { okmE = '3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1' + 'a5a4c5db02d56ecc4c5bf34007208d5b887185865'; - ikm = new Buffer(ikm, 'hex'); - salt = new Buffer(salt, 'hex'); - info = new Buffer(info, 'hex'); + ikm = Buffer.from(ikm, 'hex'); + salt = Buffer.from(salt, 'hex'); + info = Buffer.from(info, 'hex'); prk = crypto.hkdfExtract(ikm, salt, alg); okm = crypto.hkdfExpand(prk, info, len, alg); @@ -310,9 +310,9 @@ describe('Utils', function() { + 'cc30c58179ec3e87c14c01d5c1f3434f' + '1d87'; - ikm = new Buffer(ikm, 'hex'); - salt = new Buffer(salt, 'hex'); - info = new Buffer(info, 'hex'); + ikm = Buffer.from(ikm, 'hex'); + salt = Buffer.from(salt, 'hex'); + info = Buffer.from(info, 'hex'); prk = crypto.hkdfExtract(ikm, salt, alg); okm = crypto.hkdfExpand(prk, info, len, alg); @@ -324,7 +324,7 @@ describe('Utils', function() { it('should do proper schnorr', function() { var key = ec.generatePrivateKey(); var pub = ec.publicKeyCreate(key, true); - var msg = crypto.hash256(new Buffer('foo', 'ascii')); + var msg = crypto.hash256(Buffer.from('foo', 'ascii')); var sig = schnorr.sign(msg, key); assert(schnorr.verify(msg, sig, pub)); assert.deepEqual(schnorr.recover(sig, msg), pub);