Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitute lodash calls in bitcore-lib #3350

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions packages/bitcore-lib/docs/networks.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Take a look at this modified snippet from [networks.js](https://github.com/bitpa

```javascript
var livenet = new Network();
_.extend(livenet, {
Object.assign(livenet, {
name: 'livenet',
alias: 'mainnet',
pubkeyhash: 0x00,
Expand All @@ -45,7 +45,7 @@ _.extend(livenet, {
});

var testnet = new Network();
_.extend(testnet, {
Object.assign(testnet, {
name: 'testnet',
alias: 'testnet',
pubkeyhash: 0x6f,
Expand Down
1 change: 0 additions & 1 deletion packages/bitcore-lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ bitcore.deps.bnjs = require('bn.js');
bitcore.deps.bs58 = require('bs58');
bitcore.deps.Buffer = Buffer;
bitcore.deps.elliptic = require('elliptic');
bitcore.deps._ = require('lodash');

// Internal usage, exposed for testing/advanced tweaking
bitcore.Transaction.sighash = require('./lib/transaction/sighash');
11 changes: 5 additions & 6 deletions packages/bitcore-lib/lib/address.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var $ = require('./util/preconditions');
var errors = require('./errors');
var Base58Check = require('./encoding/base58check');
Expand Down Expand Up @@ -57,7 +56,7 @@ function Address(data, network, type, multisigType) {
return new Address(data, network, type);
}

if (_.isArray(data) && _.isNumber(network)) {
if (Array.isArray(data) && typeof network === 'number') {
return Address.createMultisig(data, network, type, false, multisigType);
}

Expand Down Expand Up @@ -116,7 +115,7 @@ Address.prototype._classifyArguments = function(data, network, type) {
return Address._transformScript(data, network);
} else if (typeof(data) === 'string') {
return Address._transformString(data, network, type);
} else if (_.isObject(data)) {
} else if (typeof data === 'object') {
return Address._transformObject(data);
} else {
throw new TypeError('First argument is an unrecognized data format.');
Expand Down Expand Up @@ -334,9 +333,9 @@ Address.createMultisig = function(publicKeys, threshold, network, nestedWitness,
throw new TypeError('Type must be either scripthash or witnessscripthash to create multisig.');
}
if (nestedWitness || type === Address.PayToWitnessScriptHash) {
publicKeys = _.map(publicKeys, PublicKey);
for (var i = 0; i < publicKeys.length; i++) {
if (!publicKeys[i].compressed) {
publicKeys = publicKeys.map(pubKey => PublicKey(pubKey));
for (let { compressed } of publicKeys) {
if (!compressed) {
throw new TypeError('Witness addresses must use compressed public keys.');
}
}
Expand Down
7 changes: 3 additions & 4 deletions packages/bitcore-lib/lib/block/block.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var BlockHeader = require('./blockheader');
var BN = require('../crypto/bn');
var BufferUtil = require('../util/buffer');
Expand All @@ -22,7 +21,7 @@ function Block(arg) {
if (!(this instanceof Block)) {
return new Block(arg);
}
_.extend(this, Block._from(arg));
Object.assign(this, Block._from(arg));
return this;
}

Expand All @@ -39,7 +38,7 @@ Block._from = function _from(arg) {
var info = {};
if (BufferUtil.isBuffer(arg)) {
info = Block._fromBufferReader(BufferReader(arg));
} else if (_.isObject(arg)) {
} else if (typeof arg === 'object') {
info = Block._fromObject(arg);
} else {
throw new TypeError('Unrecognized argument for Block');
Expand Down Expand Up @@ -261,7 +260,7 @@ var idProperty = {
}
return this._id;
},
set: _.noop
set: () => {}
};
Object.defineProperty(Block.prototype, 'id', idProperty);
Object.defineProperty(Block.prototype, 'hash', idProperty);
Expand Down
9 changes: 4 additions & 5 deletions packages/bitcore-lib/lib/block/blockheader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var BN = require('../crypto/bn');
var BufferUtil = require('../util/buffer');
var BufferReader = require('../encoding/bufferreader');
Expand Down Expand Up @@ -52,7 +51,7 @@ BlockHeader._from = function _from(arg) {
var info = {};
if (BufferUtil.isBuffer(arg)) {
info = BlockHeader._fromBufferReader(BufferReader(arg));
} else if (_.isObject(arg)) {
} else if (typeof arg === 'object') {
info = BlockHeader._fromObject(arg);
} else {
throw new TypeError('Unrecognized argument for BlockHeader');
Expand All @@ -69,10 +68,10 @@ BlockHeader._fromObject = function _fromObject(data) {
$.checkArgument(data, 'data is required');
var prevHash = data.prevHash;
var merkleRoot = data.merkleRoot;
if (_.isString(data.prevHash)) {
if (typeof data.prevHash === 'string') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is false if data.prevHash instanceof String, whereas _.isString(new String('')) == true

prevHash = BufferUtil.reverse(Buffer.from(data.prevHash, 'hex'));
}
if (_.isString(data.merkleRoot)) {
if (typeof data.merkleRoot === 'string') {
merkleRoot = BufferUtil.reverse(Buffer.from(data.merkleRoot, 'hex'));
}
var info = {
Expand Down Expand Up @@ -251,7 +250,7 @@ var idProperty = {
}
return this._id;
},
set: _.noop
set: () => {}
};
Object.defineProperty(BlockHeader.prototype, 'id', idProperty);
Object.defineProperty(BlockHeader.prototype, 'hash', idProperty);
Expand Down
16 changes: 7 additions & 9 deletions packages/bitcore-lib/lib/block/merkleblock.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';

var _ = require('lodash');
var BlockHeader = require('./blockheader');
var BufferUtil = require('../util/buffer');
var BufferReader = require('../encoding/bufferreader');
var BufferWriter = require('../encoding/bufferwriter');
var Hash = require('../crypto/hash');
var JSUtil = require('../util/js');
var Transaction = require('../transaction');
var errors = require('../errors');
var $ = require('../util/preconditions');
Expand All @@ -29,7 +27,7 @@ function MerkleBlock(arg) {
var info = {};
if (BufferUtil.isBuffer(arg)) {
info = MerkleBlock._fromBufferReader(BufferReader(arg));
} else if (_.isObject(arg)) {
} else if (typeof arg === 'object') {
var header;
if(arg.header instanceof BlockHeader) {
header = arg.header;
Expand Down Expand Up @@ -61,7 +59,7 @@ function MerkleBlock(arg) {
} else {
throw new TypeError('Unrecognized argument for MerkleBlock');
}
_.extend(this,info);
Object.assign(this, info);
this._flagBitsUsed = 0;
this._hashesUsed = 0;

Expand Down Expand Up @@ -129,8 +127,8 @@ MerkleBlock.prototype.toObject = MerkleBlock.prototype.toJSON = function toObjec
* @returns {Boolean} - True/False whether this MerkleBlock is Valid
*/
MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
$.checkState(_.isArray(this.flags), 'MerkleBlock flags is not an array');
$.checkState(_.isArray(this.hashes), 'MerkleBlock hashes is not an array');
$.checkState(Array.isArray(this.flags), 'MerkleBlock flags is not an array');
$.checkState(Array.isArray(this.hashes), 'MerkleBlock hashes is not an array');

// Can't have more hashes than numTransactions
if(this.hashes.length > this.numTransactions) {
Expand All @@ -156,8 +154,8 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
* @returns {Array} - txs hash that match the filter
*/
MerkleBlock.prototype.filterdTxsHash = function filterdTxsHash() {
$.checkState(_.isArray(this.flags), 'MerkleBlock flags is not an array');
$.checkState(_.isArray(this.hashes), 'MerkleBlock hashes is not an array');
$.checkState(Array.isArray(this.flags), 'MerkleBlock flags is not an array');
$.checkState(Array.isArray(this.hashes), 'MerkleBlock hashes is not an array');

// Can't have more hashes than numTransactions
if(this.hashes.length > this.numTransactions) {
Expand Down Expand Up @@ -263,7 +261,7 @@ MerkleBlock.prototype._calcTreeHeight = function calcTreeHeight() {
* @private
*/
MerkleBlock.prototype.hasTransaction = function hasTransaction(tx) {
$.checkArgument(!_.isUndefined(tx), 'tx cannot be undefined');
$.checkArgument(tx !== undefined, 'tx cannot be undefined');
$.checkArgument(tx instanceof Transaction || typeof tx === 'string',
'Invalid tx given, tx must be a "string" or "Transaction"');

Expand Down
9 changes: 4 additions & 5 deletions packages/bitcore-lib/lib/crypto/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var BN = require('bn.js');
var $ = require('../util/preconditions');
var _ = require('lodash');

var reversebuf = function(buf) {
var buf2 = Buffer.alloc(buf.length);
Expand All @@ -17,17 +16,17 @@ BN.One = new BN(1);
BN.Minus1 = new BN(-1);

BN.fromNumber = function(n) {
$.checkArgument(_.isNumber(n));
$.checkArgument(typeof n === 'number');
return new BN(n);
};

BN.fromString = function(str, base) {
$.checkArgument(_.isString(str));
$.checkArgument(typeof str === 'string');
return new BN(str, base);
};

BN.fromBuffer = function(buf, opts) {
if (typeof opts !== 'undefined' && opts.endian === 'little') {
if (opts !== undefined && opts.endian === 'little') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make opts !== undefined be opts != null to avoid unnecessary confusion for a user passing in a null variable. There should be no difference between

BN.fromBuffer(buf);

and

var opts = null;
BN.fromBuffer(buf, opts);

Same on line 89

Maybe consider changing all checks for undefined to checks for null as well (i.e. === undefined --> == null)

buf = reversebuf(buf);
}
var hex = buf.toString('hex');
Expand Down Expand Up @@ -87,7 +86,7 @@ BN.prototype.toBuffer = function(opts) {
buf = Buffer.from(hex, 'hex');
}

if (typeof opts !== 'undefined' && opts.endian === 'little') {
if (opts !== undefined && opts.endian === 'little') {
buf = reversebuf(buf);
}

Expand Down
6 changes: 1 addition & 5 deletions packages/bitcore-lib/lib/crypto/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var PublicKey = require('../publickey');
var Random = require('./random');
var Hash = require('./hash');
var BufferUtil = require('../util/buffer');
var _ = require('lodash');
var $ = require('../util/preconditions');

var ECDSA = function ECDSA(obj) {
Expand Down Expand Up @@ -73,14 +72,11 @@ ECDSA.prototype.randomK = function() {


// https://tools.ietf.org/html/rfc6979#section-3.2
ECDSA.prototype.deterministicK = function(badrs) {
ECDSA.prototype.deterministicK = function(badrs = 0) {
/* jshint maxstatements: 25 */
// if r or s were invalid when this function was used in signing,
// we do not want to actually compute r, s here for efficiency, so,
// we can increment badrs. explained at end of RFC 6979 section 3.2
if (_.isUndefined(badrs)) {
badrs = 0;
}
var v = Buffer.alloc(32);
v.fill(0x01);
var k = Buffer.alloc(32);
Expand Down
10 changes: 3 additions & 7 deletions packages/bitcore-lib/lib/crypto/signature.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var BN = require('./bn');
var _ = require('lodash');
var $ = require('../util/preconditions');
var BufferUtil = require('../util/buffer');
var JSUtil = require('../util/js');
Expand All @@ -26,8 +25,8 @@ Signature.prototype.set = function(obj) {
this.r = obj.r || this.r || undefined;
this.s = obj.s || this.s || undefined;

this.i = typeof obj.i !== 'undefined' ? obj.i : this.i; //public key recovery parameter in range [0, 3]
this.compressed = typeof obj.compressed !== 'undefined' ?
this.i = obj.i !== undefined ? obj.i : this.i; //public key recovery parameter in range [0, 3]
this.compressed = obj.compressed !== undefined ?
obj.compressed : this.compressed; //whether the recovered pubkey is compressed
this.nhashtype = obj.nhashtype || this.nhashtype || undefined;
return this;
Expand Down Expand Up @@ -88,11 +87,8 @@ Signature.fromString = function(str) {
/**
* In order to mimic the non-strict DER encoding of OpenSSL, set strict = false.
*/
Signature.parseDER = function(buf, strict) {
Signature.parseDER = function(buf, strict = true) {
$.checkArgument(BufferUtil.isBuffer(buf), new Error('DER formatted signature should be a buffer'));
if (_.isUndefined(strict)) {
strict = true;
}

var header = buf[0];
$.checkArgument(header === 0x30, new Error('Header byte should be 0x30'));
Expand Down
3 changes: 1 addition & 2 deletions packages/bitcore-lib/lib/encoding/base58.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var bs58 = require('bs58');
var buffer = require('buffer');

Expand All @@ -26,7 +25,7 @@ Base58.validCharacters = function validCharacters(chars) {
if (buffer.Buffer.isBuffer(chars)) {
chars = chars.toString();
}
return _.every(_.map(chars, function(char) { return _.includes(ALPHABET, char); }));
return [...chars].every(char => ALPHABET.includes(char));
};

Base58.prototype.set = function(obj) {
Expand Down
5 changes: 2 additions & 3 deletions packages/bitcore-lib/lib/encoding/base58check.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var Base58 = require('./base58');
var buffer = require('buffer');
var sha256sha256 = require('../crypto/hash').sha256sha256;
Expand All @@ -25,10 +24,10 @@ Base58Check.prototype.set = function(obj) {
};

Base58Check.validChecksum = function validChecksum(data, checksum) {
if (_.isString(data)) {
if (typeof data === 'string') {
data = Buffer.from(Base58.decode(data));
}
if (_.isString(checksum)) {
if (typeof checksum === 'string') {
checksum = Buffer.from(Base58.decode(checksum));
}
if (!checksum) {
Expand Down
12 changes: 5 additions & 7 deletions packages/bitcore-lib/lib/encoding/bufferreader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var $ = require('../util/preconditions');
var BufferUtil = require('../util/buffer');
var BN = require('../crypto/bn');
Expand All @@ -9,18 +8,18 @@ var BufferReader = function BufferReader(buf) {
if (!(this instanceof BufferReader)) {
return new BufferReader(buf);
}
if (_.isUndefined(buf)) {
if (buf === undefined) {
return;
}
if (Buffer.isBuffer(buf)) {
this.set({
buf: buf
});
} else if (_.isString(buf)) {
} else if (typeof buf === 'string') {
this.set({
buf: Buffer.from(buf, 'hex'),
});
} else if (_.isObject(buf)) {
} else if (typeof buf === 'object') {
var obj = buf;
this.set(obj);
} else {
Expand All @@ -45,7 +44,7 @@ BufferReader.prototype.eof = function() {
BufferReader.prototype.finished = BufferReader.prototype.eof;

BufferReader.prototype.read = function(len) {
$.checkArgument(!_.isUndefined(len), 'Must specify a length');
$.checkArgument(len !== undefined, 'Must specify a length');
var buf = this.buf.slice(this.pos, this.pos + len);
this.pos = this.pos + len;
return buf;
Expand Down Expand Up @@ -136,7 +135,6 @@ BufferReader.prototype.readVarintNum = function() {
} else {
throw new Error('number too large to retain precision - use readVarintBN');
}
break;
default:
return first;
}
Expand Down Expand Up @@ -191,7 +189,7 @@ BufferReader.prototype.reverse = function() {
};

BufferReader.prototype.readReverse = function(len) {
if (_.isUndefined(len)) {
if (len === undefined) {
len = this.buf.length;
}
var buf = this.buf.slice(this.pos, this.pos + len);
Expand Down