Skip to content

Commit

Permalink
rm new buffer warning in bitcore-lib cash
Browse files Browse the repository at this point in the history
  • Loading branch information
matiu committed Nov 11, 2019
1 parent 68718a0 commit c8d98a6
Show file tree
Hide file tree
Showing 32 changed files with 191 additions and 191 deletions.
4 changes: 2 additions & 2 deletions packages/bitcore-lib-cash/lib/encoding/base58check.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ Base58Check.prototype.set = function(obj) {

Base58Check.validChecksum = function validChecksum(data, checksum) {
if (_.isString(data)) {
data = new buffer.Buffer(Base58.decode(data));
data = Buffer.from(Base58.decode(data));
}
if (_.isString(checksum)) {
checksum = new buffer.Buffer(Base58.decode(checksum));
checksum = Buffer.from(Base58.decode(checksum));
}
if (!checksum) {
checksum = data.slice(-4);
Expand Down
6 changes: 3 additions & 3 deletions packages/bitcore-lib-cash/lib/hdprivatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonComplian
// The private key serialization in this case will not be exactly 32 bytes and can be
// any value less, and the value is not zero-padded.
var nonZeroPadded = this.privateKey.bn.toBuffer();
data = BufferUtil.concat([new buffer.Buffer([0]), nonZeroPadded, indexBuffer]);
data = BufferUtil.concat([Buffer.from([0]), nonZeroPadded, indexBuffer]);
} else if (hardened) {
// This will use a 32 byte zero padded serialization of the private key
var privateKeyBuffer = this.privateKey.bn.toBuffer({size: 32});
assert(privateKeyBuffer.length === 32, 'length of private key buffer is expected to be 32 bytes');
data = BufferUtil.concat([new buffer.Buffer([0]), privateKeyBuffer, indexBuffer]);
data = BufferUtil.concat([Buffer.from([0]), privateKeyBuffer, indexBuffer]);
} else {
data = BufferUtil.concat([this.publicKey.toBuffer(), indexBuffer]);
}
Expand Down Expand Up @@ -417,7 +417,7 @@ HDPrivateKey.fromSeed = function(hexa, network) {
if (hexa.length > MAXIMUM_ENTROPY_BITS * BITS_TO_BYTES) {
throw new hdErrors.InvalidEntropyArgument.TooMuchEntropy(hexa);
}
var hash = Hash.sha512hmac(hexa, new buffer.Buffer('Bitcoin seed'));
var hash = Hash.sha512hmac(hexa, Buffer.from('Bitcoin seed'));

return new HDPrivateKey({
network: Network.get(network) || Network.defaultNetwork,
Expand Down
6 changes: 3 additions & 3 deletions packages/bitcore-lib-cash/lib/transaction/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Input.fromObject = function(obj) {
Input.prototype._fromObject = function(params) {
var prevTxId;
if (_.isString(params.prevTxId) && JSUtil.isHexa(params.prevTxId)) {
prevTxId = new buffer.Buffer(params.prevTxId, 'hex');
prevTxId = Buffer.from(params.prevTxId, 'hex');
} else {
prevTxId = params.prevTxId;
}
Expand Down Expand Up @@ -124,15 +124,15 @@ Input.prototype.setScript = function(script) {
this._scriptBuffer = this._script.toBuffer();
} else if (JSUtil.isHexa(script)) {
// hex string script
this._scriptBuffer = new buffer.Buffer(script, 'hex');
this._scriptBuffer = Buffer.from(script, 'hex');
} else if (_.isString(script)) {
// human readable string script
this._script = new Script(script);
this._script._isInput = true;
this._scriptBuffer = this._script.toBuffer();
} else if (BufferUtil.isBuffer(script)) {
// buffer script
this._scriptBuffer = new buffer.Buffer(script);
this._scriptBuffer = Buffer.from(script);
} else {
throw new TypeError('Invalid argument type: script');
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bitcore-lib-cash/lib/transaction/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function Output(args) {
} else {
var script;
if (_.isString(args.script) && JSUtil.isHexa(args.script)) {
script = new buffer.Buffer(args.script, 'hex');
script = Buffer.from(args.script, 'hex');
} else {
script = args.script;
}
Expand Down Expand Up @@ -167,7 +167,7 @@ Output.fromBufferReader = function(br) {
if (size !== 0) {
obj.script = br.read(size);
} else {
obj.script = new buffer.Buffer([]);
obj.script = Buffer.from([]);
}
return new Output(obj);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-lib-cash/lib/transaction/sighash.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ var sighash = function sighash(transaction, sighashType, inputNumber, subscript,

for (i = 0; i < inputNumber; i++) {
txcopy.outputs[i] = new Output({
satoshis: BN.fromBuffer(new buffer.Buffer(BITS_64_ON, 'hex')),
satoshis: BN.fromBuffer(Buffer.from(BITS_64_ON, 'hex')),
script: Script.empty()
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bitcore-lib-cash/lib/util/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = {
*/
emptyBuffer: function emptyBuffer(bytes) {
$.checkArgumentType(bytes, 'number', 'bytes');
var result = new buffer.Buffer(bytes);
var result = Buffer.alloc(bytes);
for (var i = 0; i < bytes; i++) {
result.write('\0', i);
}
Expand All @@ -93,7 +93,7 @@ module.exports = {
*/
integerAsSingleByteBuffer: function integerAsSingleByteBuffer(integer) {
$.checkArgumentType(integer, 'number', 'integer');
return new buffer.Buffer([integer & 0xff]);
return Buffer.from([integer & 0xff]);
},

/**
Expand Down
12 changes: 6 additions & 6 deletions packages/bitcore-lib-cash/test/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var validCashAddr = require('./data/cashaddr.json')

describe('Address', function() {

var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex');
var buf = Buffer.concat([new Buffer([28]), pubkeyhash]);
var pubkeyhash = Buffer.from('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex');
var buf = Buffer.concat([Buffer.from([28]), pubkeyhash]);
var str = 'bitcoincash:qq7rlg754h903afdtvvy8967zgj5sf5exueg36nyc7';

it('can\'t build without data', function() {
Expand Down Expand Up @@ -396,25 +396,25 @@ describe('Address', function() {

it('should error because of incorrect length buffer for transform buffer', function() {
(function() {
return Address._transformBuffer(new Buffer(20));
return Address._transformBuffer(Buffer.alloc(20));
}).should.throw('Address buffers must be exactly 21 bytes.');
});

it('should error because of incorrect type for pubkey transform', function() {
(function() {
return Address._transformPublicKey(new Buffer(20));
return Address._transformPublicKey(Buffer.alloc(20));
}).should.throw('Address must be an instance of PublicKey.');
});

it('should error because of incorrect type for script transform', function() {
(function() {
return Address._transformScript(new Buffer(20));
return Address._transformScript( Buffer.alloc(20));
}).should.throw('Invalid Argument: script must be a Script instance');
});

it('should error because of incorrect type for string transform', function() {
(function() {
return Address._transformString(new Buffer(20));
return Address._transformString(Buffer.alloc(20));
}).should.throw('data parameter supplied is not a string.');
});

Expand Down
12 changes: 6 additions & 6 deletions packages/bitcore-lib-cash/test/block/block.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/bitcore-lib-cash/test/block/blockheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ describe('BlockHeader', function() {

before(function () {
version = data.version;
prevblockidbuf = new Buffer(data.prevblockidhex, 'hex');
merklerootbuf = new Buffer(data.merkleroothex, 'hex');
prevblockidbuf = Buffer.from(data.prevblockidhex, 'hex');
merklerootbuf = Buffer.from(data.merkleroothex, 'hex');
time = data.time;
bits = data.bits;
nonce = data.nonce;
Expand All @@ -42,7 +42,7 @@ describe('BlockHeader', function() {
nonce: nonce
});
bhhex = data.blockheaderhex;
bhbuf = new Buffer(bhhex, 'hex');
bhbuf = Buffer.from(bhhex, 'hex');
});

it('should make a new blockheader', function() {
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('BlockHeader', function() {
describe('version', function() {
it('is interpreted as an int32le', function() {
var hex = 'ffffffff00000000000000000000000000000000000000000000000000000000000000004141414141414141414141414141414141414141414141414141414141414141010000000200000003000000';
var header = BlockHeader.fromBuffer(new Buffer(hex, 'hex'));
var header = BlockHeader.fromBuffer(Buffer.from(hex, 'hex'));
header.version.should.equal(-1);
header.timestamp.should.equal(1);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/bitcore-lib-cash/test/block/merkleblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('MerkleBlock', function() {

before(function() {
blockhex = data.HEX[0];
blockbuf = new Buffer(blockhex,'hex');
blockbuf = Buffer.from(blockhex,'hex');
blockJSON = JSON.stringify(data.JSON[0]);
blockObject = JSON.parse(JSON.stringify(data.JSON[0]));
});
Expand Down Expand Up @@ -193,15 +193,15 @@ describe('MerkleBlock', function() {

it('should find transactions via hash string', function() {
var jsonData = data.JSON[0];
var txId = new Buffer(jsonData.hashes[1],'hex').toString('hex');
var txId = Buffer.from(jsonData.hashes[1],'hex').toString('hex');
var b = MerkleBlock(jsonData);
b.hasTransaction(txId).should.equal(true);
b.hasTransaction(txId + 'abcd').should.equal(false);
});

it('should find transactions via Transaction object', function() {
var jsonData = data.JSON[0];
var txBuf = new Buffer(data.TXHEX[0][0],'hex');
var txBuf = Buffer.from(data.TXHEX[0][0],'hex');
var tx = new Transaction().fromBuffer(txBuf);
var b = MerkleBlock(jsonData);
b.hasTransaction(tx).should.equal(true);
Expand All @@ -210,7 +210,7 @@ describe('MerkleBlock', function() {
it('should not find non-existant Transaction object', function() {
// Reuse another transaction already in data/ dir
var serialized = transactionVector[0][7];
var tx = new Transaction().fromBuffer(new Buffer(serialized, 'hex'));
var tx = new Transaction().fromBuffer( Buffer.from(serialized, 'hex'));
var b = MerkleBlock(data.JSON[0]);
b.hasTransaction(tx).should.equal(false);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/bitcore-lib-cash/test/crypto/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ describe('BN', function() {
describe('@fromBuffer', function() {

it('should work with big endian', function() {
var bn = BN.fromBuffer(new Buffer('0001', 'hex'), {
var bn = BN.fromBuffer( Buffer.from('0001', 'hex'), {
endian: 'big'
});
bn.toString().should.equal('1');
});

it('should work with big endian 256', function() {
var bn = BN.fromBuffer(new Buffer('0100', 'hex'), {
var bn = BN.fromBuffer( Buffer.from('0100', 'hex'), {
endian: 'big'
});
bn.toString().should.equal('256');
});

it('should work with little endian if we specify the size', function() {
var bn = BN.fromBuffer(new Buffer('0100', 'hex'), {
var bn = BN.fromBuffer( Buffer.from('0100', 'hex'), {
size: 2,
endian: 'little'
});
Expand Down
32 changes: 16 additions & 16 deletions packages/bitcore-lib-cash/test/crypto/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ describe('ECDSA', function() {
});

var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('test data'));
ecdsa.hashbuf = Hash.sha256(Buffer.from('test data'));
ecdsa.privkey = new Privkey(BN.fromBuffer(
new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex')
Buffer.from('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex')
));
ecdsa.privkey2pubkey();

Expand All @@ -41,11 +41,11 @@ describe('ECDSA', function() {
});

it('calulates this known i', function() {
var hashbuf = Hash.sha256(new Buffer('some data'));
var hashbuf = Hash.sha256(Buffer.from('some data'));
var r = new BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10);
var s = new BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10);
var ecdsa = new ECDSA({
privkey: new Privkey(BN.fromBuffer(Hash.sha256(new Buffer('test')))),
privkey: new Privkey(BN.fromBuffer(Hash.sha256(Buffer.from('test')))),
hashbuf: hashbuf,
sig: new Signature({
r: r,
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('ECDSA', function() {
// test fixture from bitcoinjs
// https://github.com/bitcoinjs/bitcoinjs-lib/blob/10630873ebaa42381c5871e20336fbfb46564ac8/test/fixtures/ecdsa.json#L6
var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('Everything should be made as simple as possible, but not simpler.'));
ecdsa.hashbuf = Hash.sha256(Buffer.from('Everything should be made as simple as possible, but not simpler.'));
ecdsa.privkey = new Privkey(new BN(1));
ecdsa.privkey2pubkey();
ecdsa.deterministicK();
Expand Down Expand Up @@ -162,8 +162,8 @@ describe('ECDSA', function() {

it('should return an error if r, s are invalid', function() {
var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('test'));
var pk = Pubkey.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49' +
ecdsa.hashbuf = Hash.sha256(Buffer.from('test'));
var pk = Pubkey.fromDER(Buffer.from('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49' +
'710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
ecdsa.pubkey = pk;
ecdsa.sig = new Signature();
Expand Down Expand Up @@ -212,9 +212,9 @@ describe('ECDSA', function() {
});

it('should generate right K', function() {
var msg1 = new Buffer('52204d20fd0131ae1afd173fd80a3a746d2dcc0cddced8c9dc3d61cc7ab6e966', 'hex');
var msg2 = [].reverse.call(new Buffer(msg1))
var pk = new Buffer('16f243e962c59e71e54189e67e66cf2440a1334514c09c00ddcc21632bac9808', 'hex');
var msg1 = Buffer.from('52204d20fd0131ae1afd173fd80a3a746d2dcc0cddced8c9dc3d61cc7ab6e966', 'hex');
var msg2 = [].reverse.call(Buffer.from(msg1))
var pk = Buffer.from('16f243e962c59e71e54189e67e66cf2440a1334514c09c00ddcc21632bac9808', 'hex');
var signature1 = ECDSA.sign(msg1, Privkey.fromBuffer(pk)).toBuffer().toString('hex');
var signature2 = ECDSA.sign(msg2, Privkey.fromBuffer(pk), 'little').toBuffer().toString('hex');
signature1.should.equal(signature2);
Expand Down Expand Up @@ -275,9 +275,9 @@ describe('ECDSA', function() {
vectors.valid.forEach(function(obj, i) {
it('should validate valid vector ' + i, function() {
var ecdsa = ECDSA().set({
privkey: new Privkey(BN.fromBuffer(new Buffer(obj.d, 'hex'))),
k: BN.fromBuffer(new Buffer(obj.k, 'hex')),
hashbuf: Hash.sha256(new Buffer(obj.message)),
privkey: new Privkey(BN.fromBuffer(Buffer.from(obj.d, 'hex'))),
k: BN.fromBuffer(Buffer.from(obj.k, 'hex')),
hashbuf: Hash.sha256(Buffer.from(obj.message)),
sig: new Signature().set({
r: new BN(obj.signature.r),
s: new BN(obj.signature.s),
Expand All @@ -300,16 +300,16 @@ describe('ECDSA', function() {
var ecdsa = ECDSA().set({
pubkey: Pubkey.fromPoint(point.fromX(true, 1)),
sig: new Signature(new BN(obj.signature.r), new BN(obj.signature.s)),
hashbuf: Hash.sha256(new Buffer(obj.message))
hashbuf: Hash.sha256(Buffer.from(obj.message))
});
ecdsa.sigError().should.equal(obj.exception);
});
});

vectors.deterministicK.forEach(function(obj, i) {
it('should validate deterministicK vector ' + i, function() {
var hashbuf = Hash.sha256(new Buffer(obj.message));
var privkey = Privkey(BN.fromBuffer(new Buffer(obj.privkey, 'hex')), 'mainnet');
var hashbuf = Hash.sha256(Buffer.from(obj.message));
var privkey = Privkey(BN.fromBuffer(Buffer.from(obj.privkey, 'hex')), 'mainnet');
var ecdsa = ECDSA({
privkey: privkey,
hashbuf: hashbuf
Expand Down
Loading

0 comments on commit c8d98a6

Please sign in to comment.