Replace [new ]Buffer() with Buffer.alloc() or Buffer.from() in code
This commit is contained in:
parent
2179efa1b4
commit
305bce9397
|
@ -48,7 +48,7 @@ exports.readTime64BE = function(buf, offset) {
|
|||
};
|
||||
|
||||
function writeUInt64BE(buf, value, offset, noAssert) {
|
||||
buf = buf || new Buffer(8);
|
||||
buf = buf || Buffer.alloc(8);
|
||||
offset = offset || 0;
|
||||
assert(noAssert || value <= MAX_SAFE_INTEGER, "Unsafe integer");
|
||||
buf.writeUInt32BE(Math.floor(value / 4294967296), offset, noAssert);
|
||||
|
@ -96,12 +96,12 @@ exports.popkey = function(obj, key) {
|
|||
};
|
||||
|
||||
// See https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses
|
||||
var IPv4_MAPPING = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255]);
|
||||
var IPv4_MAPPING = Buffer.from("00000000000000000000ffff", "hex");
|
||||
exports.IPv4_MAPPING = IPv4_MAPPING;
|
||||
|
||||
// Very simple inet_pton(3) equivalent.
|
||||
exports.inet_pton = function(str) {
|
||||
var buf = new Buffer(16);
|
||||
var buf = Buffer.alloc(16);
|
||||
buf.fill(0);
|
||||
// IPv4-mapped IPv6.
|
||||
if (str.slice(0, 7) === "::ffff:") {
|
||||
|
|
|
@ -96,8 +96,8 @@ Address.decode = function(str) {
|
|||
}
|
||||
|
||||
var bytes = bs58.decode(str);
|
||||
var data = new Buffer(bytes.slice(0, -4));
|
||||
var checksum = new Buffer(bytes.slice(-4));
|
||||
var data = Buffer.from(bytes.slice(0, -4));
|
||||
var checksum = Buffer.from(bytes.slice(-4));
|
||||
assert(bufferEqual(checksum, getaddrchecksum(data)), "Bad checkum");
|
||||
|
||||
var decoded = var_int.decode(data);
|
||||
|
@ -269,7 +269,7 @@ Address.fromRandom = function(opts) {
|
|||
var encPrivateKey, encPublicKey, ripe, len;
|
||||
var signPrivateKey = bmcrypto.getPrivate();
|
||||
var signPublicKey = bmcrypto.getPublic(signPrivateKey);
|
||||
var keysbuf = new Buffer(130);
|
||||
var keysbuf = Buffer.alloc(130);
|
||||
signPublicKey.copy(keysbuf);
|
||||
while (true) {
|
||||
encPrivateKey = bmcrypto.getPrivate();
|
||||
|
@ -313,9 +313,9 @@ Address.fromPassphrase = function(opts) {
|
|||
var ripe, len, tmp;
|
||||
var signnonce = 0;
|
||||
var encnonce = 1;
|
||||
var keysbuf = new Buffer(130);
|
||||
var keysbuf = Buffer.alloc(130);
|
||||
// XXX(Kagami): Spec doesn't mention encoding, using UTF-8.
|
||||
var phrasebuf = new Buffer(passphrase, "utf8");
|
||||
var phrasebuf = Buffer.from(passphrase, "utf8");
|
||||
while (true) {
|
||||
// TODO(Kagami): We may slightly optimize it and pre-create tmp
|
||||
// buffers based on the encoded nonce size (1, 3, 5 and 9 bytes).
|
||||
|
@ -409,7 +409,7 @@ Object.defineProperty(Address.prototype, "ripe", {
|
|||
set: function(ripe) {
|
||||
assertripelen(getripelen(ripe), this.version, ripe);
|
||||
if (ripe.length < 20) {
|
||||
var fullripe = new Buffer(20);
|
||||
var fullripe = Buffer.alloc(20);
|
||||
fullripe.fill(0);
|
||||
ripe.copy(fullripe, 20 - ripe.length);
|
||||
ripe = fullripe;
|
||||
|
|
|
@ -104,15 +104,15 @@ var encrypted = exports.encrypted = {
|
|||
assert(buf.readUInt16BE(16, true) === SECP256K1_TYPE, "Bad curve type");
|
||||
assert(buf.readUInt16BE(18, true) === 32, "Bad Rx length");
|
||||
assert(buf.readUInt16BE(52, true) === 32, "Bad Ry length");
|
||||
var iv = new Buffer(16);
|
||||
var iv = Buffer.alloc(16);
|
||||
buf.copy(iv, 0, 0, 16);
|
||||
var ephemPublicKey = new Buffer(70);
|
||||
var ephemPublicKey = Buffer.alloc(70);
|
||||
buf.copy(ephemPublicKey, 0, 16, 86);
|
||||
// NOTE(Kagami): We do copy instead of slice to protect against
|
||||
// possible source buffer modification by user.
|
||||
var ciphertext = new Buffer(buf.length - 118);
|
||||
var ciphertext = Buffer.alloc(buf.length - 118);
|
||||
buf.copy(ciphertext, 0, 86, buf.length - 32);
|
||||
var mac = new Buffer(32);
|
||||
var mac = Buffer.alloc(32);
|
||||
buf.copy(mac, 0, buf.length - 32);
|
||||
return {
|
||||
iv: iv,
|
||||
|
|
|
@ -133,7 +133,7 @@ var version = exports.version = {
|
|||
var short = {short: true};
|
||||
var addrRecv = structs.net_addr.decode(buf.slice(20, 46), short);
|
||||
var addrFrom = structs.net_addr.decode(buf.slice(46, 72), short);
|
||||
var nonce = new Buffer(8);
|
||||
var nonce = Buffer.alloc(8);
|
||||
buf.copy(nonce, 0, 72, 80);
|
||||
var decodedUa = UserAgent.decode(buf.slice(80));
|
||||
assert(decodedUa.length <= 5000, "User agent is too long");
|
||||
|
@ -202,7 +202,7 @@ var version = exports.version = {
|
|||
var streams = opts.streams || [1];
|
||||
assert(streams.length <= 160000, "Too many streams");
|
||||
// Start encoding.
|
||||
var protoVersion = new Buffer(4);
|
||||
var protoVersion = Buffer.alloc(4);
|
||||
protoVersion.writeUInt32BE(util.PROTOCOL_VERSION, 0);
|
||||
var addrRecv = structs.net_addr.encode({
|
||||
services: services,
|
||||
|
@ -229,9 +229,7 @@ var version = exports.version = {
|
|||
},
|
||||
};
|
||||
|
||||
var IPv6_LOOPBACK = new Buffer(
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
|
||||
);
|
||||
var IPv6_LOOPBACK = Buffer.from("00000000000000000000000000000001", "hex");
|
||||
|
||||
// Check whether given encoded IPv6 or IPv4-mapped IPv6 is in private
|
||||
// network range. See
|
||||
|
@ -566,7 +564,7 @@ var error = exports.error = {
|
|||
assert(rest.length >= vectorLength, "Buffer is too small");
|
||||
var vector = null;
|
||||
if (vectorLength) {
|
||||
vector = new Buffer(vectorLength);
|
||||
vector = Buffer.alloc(vectorLength);
|
||||
rest.copy(vector);
|
||||
rest = rest.slice(vectorLength);
|
||||
}
|
||||
|
@ -616,7 +614,7 @@ var error = exports.error = {
|
|||
var type = opts.type || error.WARNING;
|
||||
var banTime = opts.banTime || 0;
|
||||
// TODO(Kagami): Validate vector length.
|
||||
var vector = opts.vector || new Buffer(0);
|
||||
var vector = opts.vector || Buffer.alloc(0);
|
||||
return Buffer.concat([
|
||||
structs.var_int.encode(type),
|
||||
structs.var_int.encode(banTime),
|
||||
|
|
|
@ -61,7 +61,7 @@ function prependNonce(obj, opts) {
|
|||
opts = objectAssign({}, opts);
|
||||
var nonce, target, powp;
|
||||
if (opts.skipPow) {
|
||||
nonce = new Buffer(8);
|
||||
nonce = Buffer.alloc(8);
|
||||
nonce.fill(0);
|
||||
resolve(Buffer.concat([nonce, obj]));
|
||||
} else {
|
||||
|
@ -71,7 +71,7 @@ function prependNonce(obj, opts) {
|
|||
.then(function(nonce) {
|
||||
// TODO(Kagami): We may want to receive nonce as a Buffer from
|
||||
// POW module to skip conversion step.
|
||||
var payload = new Buffer(opts.payloadLength);
|
||||
var payload = Buffer.alloc(opts.payloadLength);
|
||||
util.writeUInt64BE(payload, nonce, 0, true);
|
||||
obj.copy(payload, 8);
|
||||
return payload;
|
||||
|
@ -193,10 +193,10 @@ function extractPubkey(buf) {
|
|||
// We assume here that input buffer was copied before so it's safe to
|
||||
// return reference to it.
|
||||
decoded.behavior = PubkeyBitfield(buf.slice(0, 4));
|
||||
var signPublicKey = decoded.signPublicKey = new Buffer(65);
|
||||
var signPublicKey = decoded.signPublicKey = Buffer.alloc(65);
|
||||
signPublicKey[0] = 4;
|
||||
buf.copy(signPublicKey, 1, 4, 68);
|
||||
var encPublicKey = decoded.encPublicKey = new Buffer(65);
|
||||
var encPublicKey = decoded.encPublicKey = Buffer.alloc(65);
|
||||
encPublicKey[0] = 4;
|
||||
buf.copy(encPublicKey, 1, 68, 132);
|
||||
return decoded;
|
||||
|
@ -509,20 +509,20 @@ function encodeMessage(opts) {
|
|||
var subject = opts.subject;
|
||||
if (encoding === msg.IGNORE && !message) {
|
||||
// User may omit message for IGNORE encoding.
|
||||
message = new Buffer(0);
|
||||
message = Buffer.alloc(0);
|
||||
} else if (!Buffer.isBuffer(message)) {
|
||||
// User may specify message as a string.
|
||||
message = new Buffer(message, "utf8");
|
||||
message = Buffer.from(message, "utf8");
|
||||
}
|
||||
if (encoding === msg.SIMPLE && subject) {
|
||||
// User may specify subject for SIMPLE encoding.
|
||||
if (!Buffer.isBuffer(subject)) {
|
||||
subject = new Buffer(subject, "utf8");
|
||||
subject = Buffer.from(subject, "utf8");
|
||||
}
|
||||
message = Buffer.concat([
|
||||
new Buffer("Subject:"),
|
||||
Buffer.from("Subject:"),
|
||||
subject,
|
||||
new Buffer("\nBody:"),
|
||||
Buffer.from("\nBody:"),
|
||||
message,
|
||||
]);
|
||||
}
|
||||
|
@ -813,7 +813,7 @@ var msg = exports.msg = {
|
|||
msgData.push(var_int.encode(0));
|
||||
|
||||
// Sign and encrypt.
|
||||
opts.objectPayload = new Buffer(0);
|
||||
opts.objectPayload = Buffer.alloc(0);
|
||||
var obj = object.encodePayloadWithoutNonce(opts);
|
||||
var dataToSign = Buffer.concat([obj].concat(msgData));
|
||||
var msgp = bmcrypto
|
||||
|
@ -1119,7 +1119,7 @@ var broadcast = exports.broadcast = {
|
|||
);
|
||||
|
||||
// Sign and encrypt.
|
||||
opts.objectPayload = from.version >= 4 ? from.getTag() : new Buffer(0);
|
||||
opts.objectPayload = from.version >= 4 ? from.getTag() : Buffer.alloc(0);
|
||||
var obj = object.encodePayloadWithoutNonce(opts);
|
||||
var dataToSign = Buffer.concat([obj].concat(broadData));
|
||||
var broadp = bmcrypto
|
||||
|
|
|
@ -64,7 +64,7 @@ exports.check = function(opts) {
|
|||
initialHash = bmcrypto.sha512(opts.payload.slice(8));
|
||||
} else {
|
||||
if (typeof opts.nonce === "number") {
|
||||
nonce = new Buffer(8);
|
||||
nonce = Buffer.alloc(8);
|
||||
// High 32 bits.
|
||||
nonce.writeUInt32BE(Math.floor(opts.nonce / 4294967296), 0, true);
|
||||
// Low 32 bits.
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
*
|
||||
* var encoded = Buffer.concat([
|
||||
* structs.var_int.encode(4),
|
||||
* Buffer("test"),
|
||||
* Buffer.from("test"),
|
||||
* structs.var_str.encode("test2"),
|
||||
* structs.var_int_list.encode([1, 2, 3]),
|
||||
* ]);
|
||||
|
@ -137,12 +137,12 @@ var message = exports.message = {
|
|||
if (mindex !== 0) {
|
||||
if (mindex === -1) {
|
||||
res.error = new Error("Magic not found, skipping buffer data");
|
||||
res.rest = new Buffer(0);
|
||||
res.rest = Buffer.alloc(0);
|
||||
} else {
|
||||
res.error = new Error(
|
||||
"Magic in the middle of buffer, skipping some data at start"
|
||||
);
|
||||
res.rest = new Buffer(buf.length - mindex);
|
||||
res.rest = Buffer.alloc(buf.length - mindex);
|
||||
buf.copy(res.rest, 0, mindex);
|
||||
}
|
||||
return res;
|
||||
|
@ -155,10 +155,10 @@ var message = exports.message = {
|
|||
if (payloadLength > 1600003) {
|
||||
res.error = new Error("Message is too large, skipping it");
|
||||
if (buf.length > msgLength) {
|
||||
res.rest = new Buffer(buf.length - msgLength);
|
||||
res.rest = Buffer.alloc(buf.length - msgLength);
|
||||
buf.copy(res.rest, 0, msgLength);
|
||||
} else {
|
||||
res.rest = new Buffer(0);
|
||||
res.rest = Buffer.alloc(0);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ var message = exports.message = {
|
|||
}
|
||||
|
||||
// Now we can set `rest` value.
|
||||
res.rest = new Buffer(buf.length - msgLength);
|
||||
res.rest = Buffer.alloc(buf.length - msgLength);
|
||||
buf.copy(res.rest, 0, msgLength);
|
||||
|
||||
// Command.
|
||||
|
@ -190,7 +190,7 @@ var message = exports.message = {
|
|||
command = command.slice(0, firstNonNull).toString("ascii");
|
||||
|
||||
// Payload.
|
||||
var payload = new Buffer(payloadLength);
|
||||
var payload = Buffer.alloc(payloadLength);
|
||||
buf.copy(payload, 0, 24, msgLength);
|
||||
var checksum = buf.slice(20, 24);
|
||||
if (!bufferEqual(checksum, getmsgchecksum(payload))) {
|
||||
|
@ -241,7 +241,7 @@ var message = exports.message = {
|
|||
var checksum = buf.slice(20, 24);
|
||||
// NOTE(Kagami): We do copy instead of slice to protect against
|
||||
// possible source buffer modification by user.
|
||||
var payload = new Buffer(payloadLength);
|
||||
var payload = Buffer.alloc(payloadLength);
|
||||
buf.copy(payload, 0, 24, length);
|
||||
assert(bufferEqual(checksum, getmsgchecksum(payload)), "Bad checksum");
|
||||
var rest = buf.slice(length);
|
||||
|
@ -257,9 +257,9 @@ var message = exports.message = {
|
|||
encode: function(command, payload) {
|
||||
assert(command.length <= 12, "Command is too long");
|
||||
assert(isAscii(command), "Non-ASCII characters in command");
|
||||
payload = payload || new Buffer(0);
|
||||
payload = payload || Buffer.alloc(0);
|
||||
assert(payload.length <= 1600003, "Message payload is too big");
|
||||
var buf = new Buffer(24 + payload.length);
|
||||
var buf = Buffer.alloc(24 + payload.length);
|
||||
buf.fill(0);
|
||||
buf.writeUInt32BE(message.MAGIC, 0, true);
|
||||
buf.write(command, 4);
|
||||
|
@ -342,7 +342,7 @@ var object = exports.object = {
|
|||
assert(buf.length <= 262144, "object message payload is too big");
|
||||
var nonce;
|
||||
if (!opts._validate) {
|
||||
nonce = new Buffer(8);
|
||||
nonce = Buffer.alloc(8);
|
||||
buf.copy(nonce, 0, 0, 8);
|
||||
}
|
||||
|
||||
|
@ -371,7 +371,7 @@ var object = exports.object = {
|
|||
|
||||
if (opts._validate) { return {stream: decodedStream.value}; }
|
||||
|
||||
var objectPayload = new Buffer(decodedStream.rest.length);
|
||||
var objectPayload = Buffer.alloc(decodedStream.rest.length);
|
||||
decodedStream.rest.copy(objectPayload);
|
||||
|
||||
return {
|
||||
|
@ -482,7 +482,7 @@ var object = exports.object = {
|
|||
assert(opts.ttl > 0, "Bad TTL");
|
||||
assert(opts.ttl <= 2430000, "TTL may not be larger than 28 days + 3 hours");
|
||||
var expiresTime = util.tnow() + opts.ttl;
|
||||
var type = new Buffer(4);
|
||||
var type = Buffer.alloc(4);
|
||||
type.writeUInt32BE(opts.type, 0);
|
||||
var stream = opts.stream || 1;
|
||||
var obj = Buffer.concat([
|
||||
|
@ -569,25 +569,26 @@ var var_int = exports.var_int = {
|
|||
if (typeof value === "number") {
|
||||
assert(value >= 0, "Value cannot be less than zero");
|
||||
if (value < 253) {
|
||||
buf = new Buffer([value]);
|
||||
buf = Buffer.alloc(1);
|
||||
buf[0] = value;
|
||||
} else if (value < 65536) {
|
||||
buf = new Buffer(3);
|
||||
buf = Buffer.alloc(3);
|
||||
buf[0] = 253;
|
||||
buf.writeUInt16BE(value, 1, true);
|
||||
} else if (value < 4294967296) {
|
||||
buf = new Buffer(5);
|
||||
buf = Buffer.alloc(5);
|
||||
buf[0] = 254;
|
||||
buf.writeUInt32BE(value, 1, true);
|
||||
} else {
|
||||
assert(value <= 9007199254740991, "Unsafe integer");
|
||||
buf = new Buffer(9);
|
||||
buf = Buffer.alloc(9);
|
||||
buf[0] = 255;
|
||||
buf.writeUInt32BE(Math.floor(value / 4294967296), 1, true); // high32
|
||||
buf.writeUInt32BE(value % 4294967296, 5, true); // low32
|
||||
}
|
||||
} else if (Buffer.isBuffer(value)) {
|
||||
assert(value.length <= 8, "Buffer is too big");
|
||||
buf = new Buffer(9);
|
||||
buf = Buffer.alloc(9);
|
||||
buf.fill(0);
|
||||
buf[0] = 255;
|
||||
targetStart = 1 + (8 - value.length);
|
||||
|
@ -638,7 +639,7 @@ exports.var_str = {
|
|||
*/
|
||||
encode: function(str) {
|
||||
// XXX(Kagami): Spec doesn't mention encoding, using UTF-8.
|
||||
var strBuf = new Buffer(str, "utf8");
|
||||
var strBuf = Buffer.from(str, "utf8");
|
||||
return Buffer.concat([var_int.encode(strBuf.length), strBuf]);
|
||||
},
|
||||
};
|
||||
|
@ -785,10 +786,10 @@ exports.net_addr = {
|
|||
// old browsers). So we use offset instead of `buf = buf.slice`.
|
||||
var buf, shift;
|
||||
if (opts.short) {
|
||||
buf = new Buffer(26);
|
||||
buf = Buffer.alloc(26);
|
||||
shift = 0;
|
||||
} else {
|
||||
buf = new Buffer(38);
|
||||
buf = Buffer.alloc(38);
|
||||
var time = opts.time || new Date();
|
||||
time = Math.floor(time.getTime() / 1000);
|
||||
buf.writeUInt32BE(Math.floor(time / 4294967296), 0, true); // high32
|
||||
|
@ -883,13 +884,13 @@ var Bitfield = function(size) {
|
|||
if (buf) {
|
||||
assert(buf.length === bytesize, "Bad buffer size");
|
||||
if (opts.copy) {
|
||||
var dup = new Buffer(bytesize);
|
||||
var dup = Buffer.alloc(bytesize);
|
||||
dup.fill(0);
|
||||
buf.copy(dup);
|
||||
buf = dup;
|
||||
}
|
||||
} else {
|
||||
buf = new Buffer(bytesize);
|
||||
buf = Buffer.alloc(bytesize);
|
||||
buf.fill(0);
|
||||
}
|
||||
this.buffer = buf;
|
||||
|
|
|
@ -25,8 +25,8 @@ function getwifchecksum(data) {
|
|||
exports.decode = function(wif) {
|
||||
var bytes = bs58.decode(wif);
|
||||
assert(bytes[0] === 0x80, "Bad WIF");
|
||||
var data = new Buffer(bytes.slice(0, -4));
|
||||
var checksum = new Buffer(bytes.slice(-4));
|
||||
var data = Buffer.from(bytes.slice(0, -4));
|
||||
var checksum = Buffer.from(bytes.slice(-4));
|
||||
assert(bufferEqual(checksum, getwifchecksum(data)), "Bad checkum");
|
||||
return data.slice(1);
|
||||
};
|
||||
|
@ -38,7 +38,7 @@ exports.decode = function(wif) {
|
|||
* @return {string} WIF-encoded private key.
|
||||
*/
|
||||
exports.encode = function(privateKey) {
|
||||
var data = Buffer.concat([new Buffer([0x80]), privateKey]);
|
||||
var data = Buffer.concat([Buffer.from([0x80]), privateKey]);
|
||||
var checksum = getwifchecksum(data);
|
||||
var bytes = Buffer.concat([data, checksum]);
|
||||
return bs58.encode(bytes);
|
||||
|
|
|
@ -13,7 +13,7 @@ function sha512(buf) {
|
|||
function pow(opts) {
|
||||
var nonce = opts.num;
|
||||
var poolSize = opts.poolSize;
|
||||
var message = new Buffer(72);
|
||||
var message = Buffer.alloc(72);
|
||||
message.fill(0);
|
||||
Buffer(opts.initialHash).copy(message, 8);
|
||||
var targetHi = Math.floor(opts.target / 4294967296);
|
||||
|
|
Loading…
Reference in New Issue
Block a user