Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
09386ce
[p2p] lint inventory.js
MichaelAJay Apr 20, 2026
4cf846f
rm lodash - inventory.js
MichaelAJay Apr 20, 2026
74a276f
lint message/utils.js
MichaelAJay Apr 20, 2026
eb75b29
forEach -> for of - messages/util.js
MichaelAJay Apr 20, 2026
1556ee2
rm lodash - messages/utils.js
MichaelAJay Apr 20, 2026
3710880
lint messages/commands/addr.js
MichaelAJay Apr 20, 2026
fb9438e
rm lodash & replace checkArgument conditional - messages/commands/add…
MichaelAJay Apr 20, 2026
c246f27
rm lodash - messages/commands/block.js
MichaelAJay Apr 20, 2026
bc72820
rm lodash - messages/commands/filteradd.js
MichaelAJay Apr 20, 2026
b4cca6e
rm lodash - messages/commands/filterload.js
MichaelAJay Apr 20, 2026
b63c1f8
lint messages/commands/getdata.js (and remove unused lodash import)
MichaelAJay Apr 20, 2026
baa3f9e
lint messages/commands/headers.js
MichaelAJay Apr 20, 2026
7b27054
lint & rm lodash - messages/commands/headers.js
MichaelAJay Apr 20, 2026
d291bde
lint & rm lodash - messages/commands/merkleblock.js
MichaelAJay Apr 20, 2026
e9c08ee
lint & rm lodash (unused) - messages/commands/notfound.js
MichaelAJay Apr 20, 2026
a998b19
lint & rm lodash - messages/commands/ping.js
MichaelAJay Apr 20, 2026
98ba998
lint and rm lodash - messages/commands/pong.js
MichaelAJay Apr 20, 2026
0955ecb
lint & rm lodash - messages/commands/tx.js
MichaelAJay Apr 20, 2026
abdb218
lint test/peer.js
MichaelAJay Apr 20, 2026
dba2cac
forEach -> for-of, test/peer.js
MichaelAJay Apr 20, 2026
684f14e
rm lodash - test/peer.js
MichaelAJay Apr 20, 2026
740e079
lint integration/bitcoind.js
MichaelAJay Apr 20, 2026
0722945
forEach -> for-of, integration/bitcoind.js
MichaelAJay Apr 20, 2026
45677cf
rm lodash - integration/bitcoind.js
MichaelAJay Apr 20, 2026
13c7102
[p2p cash] rm lodash - inventory.js
MichaelAJay Apr 20, 2026
ba1bd92
[p2p cash] lint messages/utils.js
MichaelAJay Apr 20, 2026
be60cbd
[p2p cash] forEach -> for-of, messages/utils.js
MichaelAJay Apr 20, 2026
112939c
rm lodash - messages/utils.js
MichaelAJay Apr 20, 2026
7a2b070
lint & rm lodash - messages/commands/addr.js
MichaelAJay Apr 20, 2026
d23f92b
[p2p-cash] rm lodash from messages/commands/block.js
MichaelAJay Apr 23, 2026
ac62f7f
[p2p cash] lint messages/commands/filteradd.js
MichaelAJay Apr 23, 2026
6f56864
[p2p cash] rm lodash from messages/commands/filteradd.js
MichaelAJay Apr 23, 2026
3bf2d0b
[p2p cash] lint messages/commands/filterload.js
MichaelAJay Apr 23, 2026
596d197
[p2p cash] rm lodash from messages/commands/filterload.js
MichaelAJay Apr 23, 2026
3da2577
[p2p cash] lint messages/commands/headers.js
MichaelAJay Apr 23, 2026
b4b06f3
[p2p cash] rm lodash from messages/commands/headers.js
MichaelAJay Apr 23, 2026
022cded
[p2p cash] lint messages/commands/merkleblock.js
MichaelAJay Apr 23, 2026
1b5db79
[p2p cash] rm lodash from messages/commands/merkleblock.js
MichaelAJay Apr 23, 2026
28ef197
[p2p cash] lint messages/commands/ping.js
MichaelAJay Apr 23, 2026
2e4837c
[p2p cash] rm lodash from messages/commands/ping.js
MichaelAJay Apr 23, 2026
34cf4f3
[p2p cash] lint messages/commands/pong.js
MichaelAJay Apr 23, 2026
771203d
[p2p cash] rm lodash from messages/commands/pong.js
MichaelAJay Apr 23, 2026
8bb1c38
[p2p cash] lint remaining
MichaelAJay Apr 23, 2026
760c669
[p2p cash] forEach -> for-of
MichaelAJay Apr 23, 2026
f25a48f
[p2p cash] rm lodash
MichaelAJay Apr 23, 2026
e82be81
lint target files (with lodash) in p2p doge
MichaelAJay Apr 23, 2026
b675c0a
p2p doge: forEach -> for of
MichaelAJay Apr 23, 2026
a407744
rm lodash from p2p doge
MichaelAJay Apr 23, 2026
ca77ef0
Merge branch 'master' of https://github.com/bitpay/bitcore into p2p-r…
MichaelAJay Apr 23, 2026
8e097c4
replace arg check logic with closer to previously existing arg checks
MichaelAJay Apr 23, 2026
0cde8e4
fix bad test
MichaelAJay Apr 24, 2026
b39c95a
add optional chaining for better runtime behavior
MichaelAJay Apr 27, 2026
e44233f
update checkArgument to perform nullish checks instead of strict unde…
MichaelAJay Apr 27, 2026
2a0ec0e
lint mnemonic.js
MichaelAJay Apr 27, 2026
5635893
rm lodash usage from mnemonic.js
MichaelAJay Apr 27, 2026
515cd4f
Merge branch 'mnemonic-rm-lodash' into p2p-rm-lodash
MichaelAJay Apr 27, 2026
58a36a9
refactor cash & doge checkInventory.$checkArgument condition to match…
MichaelAJay Apr 29, 2026
8911bfc
clean up should.be()
MichaelAJay Apr 29, 2026
f91b403
fix typo, rm unused constants, and replace test data in doge integrat…
MichaelAJay Apr 29, 2026
838e35d
fix failing bitcoind integration test
MichaelAJay Apr 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 34 additions & 35 deletions packages/bitcore-mnemonic/lib/mnemonic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

const bitcore = require('@bitpay-labs/bitcore-lib');
const unorm = require('unorm');
const pbkdf2 = require('./pbkdf2');
const errors = require('./errors');
const pbkdf2 = require('./pbkdf2');

const BN = bitcore.crypto.BN;
const _ = bitcore.deps._;
const Hash = bitcore.crypto.Hash;
const Random = bitcore.crypto.Random;
const Network = bitcore.Networks;
Expand Down Expand Up @@ -41,20 +40,20 @@ const Mnemonic = function(data, wordlist) {
return new Mnemonic(data, wordlist);
}

if (_.isArray(data)) {
if (Array.isArray(data)) {
wordlist = data;
data = null;
}


// handle data overloading
var ent, phrase, seed;
let ent, phrase, seed;
if (Buffer.isBuffer(data)) {
seed = data;
ent = seed.length * 8;
} else if (_.isString(data)) {
} else if (typeof data === 'string') {
phrase = unorm.nfkd(data);
} else if (_.isNumber(data)) {
} else if (typeof data === 'number') {
ent = data;
} else if (data) {
throw new bitcore.errors.InvalidArgument('data', 'Must be a Buffer, a string or an integer');
Expand Down Expand Up @@ -120,22 +119,22 @@ Mnemonic.isValid = function(mnemonic, wordlist) {
return false;
}

var words = mnemonic.split(' ');
var bin = '';
for (var i = 0; i < words.length; i++) {
var ind = wordlist.indexOf(words[i]);
const words = mnemonic.split(' ');
let bin = '';
for (let i = 0; i < words.length; i++) {
const ind = wordlist.indexOf(words[i]);
if (ind < 0) return false;
bin = bin + ('00000000000' + ind.toString(2)).slice(-11);
}

var cs = bin.length / 33;
var hash_bits = bin.slice(-cs);
var nonhash_bits = bin.slice(0, bin.length - cs);
var buf = Buffer.alloc(nonhash_bits.length / 8);
for (i = 0; i < nonhash_bits.length / 8; i++) {
const cs = bin.length / 33;
const hash_bits = bin.slice(-cs);
const nonhash_bits = bin.slice(0, bin.length - cs);
const buf = Buffer.alloc(nonhash_bits.length / 8);
for (let i = 0; i < nonhash_bits.length / 8; i++) {
buf.writeUInt8(parseInt(bin.slice(i * 8, (i + 1) * 8), 2), i);
}
var expected_hash_bits = Mnemonic._entropyChecksum(buf);
const expected_hash_bits = Mnemonic._entropyChecksum(buf);
return expected_hash_bits === hash_bits;
};

Expand All @@ -147,9 +146,9 @@ Mnemonic.isValid = function(mnemonic, wordlist) {
* @returns {boolean}
*/
Mnemonic._belongsToWordlist = function(mnemonic, wordlist) {
var words = unorm.nfkd(mnemonic).split(' ');
for (var i = 0; i < words.length; i++) {
var ind = wordlist.indexOf(words[i]);
const words = unorm.nfkd(mnemonic).split(' ');
for (let i = 0; i < words.length; i++) {
const ind = wordlist.indexOf(words[i]);
if (ind < 0) return false;
}
return true;
Expand All @@ -164,9 +163,9 @@ Mnemonic._belongsToWordlist = function(mnemonic, wordlist) {
Mnemonic._getDictionary = function(mnemonic) {
if (!mnemonic) return null;

var dicts = Object.keys(Mnemonic.Words);
for (var i = 0; i < dicts.length; i++) {
var key = dicts[i];
const dicts = Object.keys(Mnemonic.Words);
for (let i = 0; i < dicts.length; i++) {
const key = dicts[i];
if (Mnemonic._belongsToWordlist(mnemonic, Mnemonic.Words[key])) {
return Mnemonic.Words[key];
}
Expand Down Expand Up @@ -194,7 +193,7 @@ Mnemonic.prototype.toSeed = function(passphrase) {
*/
Mnemonic.fromSeed = function(seed, wordlist) {
$.checkArgument(Buffer.isBuffer(seed), 'seed must be a Buffer.');
$.checkArgument(_.isArray(wordlist) || _.isString(wordlist), 'wordlist must be a string or an array.');
$.checkArgument(Array.isArray(wordlist) || typeof wordlist === 'string', 'wordlist must be a string or an array.');
return new Mnemonic(seed, wordlist);
};

Expand Down Expand Up @@ -274,7 +273,7 @@ Mnemonic.prototype.inspect = function() {
* @returns {String} Mnemonic string
*/
Mnemonic._mnemonic = function(ENT, wordlist) {
var buf = Random.getRandomBuffer(ENT / 8);
const buf = Random.getRandomBuffer(ENT / 8);
return Mnemonic._entropy2mnemonic(buf, wordlist);
};

Expand All @@ -286,21 +285,21 @@ Mnemonic._mnemonic = function(ENT, wordlist) {
* @returns {String} Mnemonic string
*/
Mnemonic._entropy2mnemonic = function(entropy, wordlist) {
var bin = '';
for (var i = 0; i < entropy.length; i++) {
let bin = '';
for (let i = 0; i < entropy.length; i++) {
bin = bin + ('00000000' + entropy[i].toString(2)).slice(-8);
}

bin = bin + Mnemonic._entropyChecksum(entropy);
if (bin.length % 11 !== 0) {
throw new errors.InvalidEntropy(bin);
}
var mnemonic = [];
for (i = 0; i < bin.length / 11; i++) {
var wi = parseInt(bin.slice(i * 11, (i + 1) * 11), 2);
const mnemonic = [];
for (let i = 0; i < bin.length / 11; i++) {
const wi = parseInt(bin.slice(i * 11, (i + 1) * 11), 2);
mnemonic.push(wordlist[wi]);
}
var ret;
let ret;
if (wordlist === Mnemonic.Words.JAPANESE) {
ret = mnemonic.join('\u3000');
} else {
Expand All @@ -317,18 +316,18 @@ Mnemonic._entropy2mnemonic = function(entropy, wordlist) {
* @private
*/
Mnemonic._entropyChecksum = function(entropy) {
var hash = Hash.sha256(entropy);
var bits = entropy.length * 8;
var cs = bits / 32;
const hash = Hash.sha256(entropy);
const bits = entropy.length * 8;
const cs = bits / 32;

var hashbits = new BN(hash.toString('hex'), 16).toString(2);
let hashbits = new BN(hash.toString('hex'), 16).toString(2);

// zero pad the hash bits
while (hashbits.length % 256 !== 0) {
hashbits = '0' + hashbits;
}

var checksum = hashbits.slice(0, cs);
const checksum = hashbits.slice(0, cs);

return checksum;
};
Expand Down
73 changes: 32 additions & 41 deletions packages/bitcore-p2p-cash/integration/bitcoind.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
'use strict';

const chai = require('chai');
const sinon = require('sinon');
const bitcore = require('@bitpay-labs/bitcore-lib-cash');
const chai = require('chai');
const p2p = require('../');

const should = chai.should();
const _ = bitcore.deps._;
const Random = bitcore.crypto.Random;
const BN = bitcore.crypto.BN;
const BufferUtil = bitcore.util.buffer;
const Peer = p2p.Peer;
const Pool = p2p.Pool;
const Networks = bitcore.Networks;
const Messages = p2p.Messages;
const Inventory = p2p.Inventory;
const Block = bitcore.Block;
const Transaction = bitcore.Transaction;

// config
const network = process.env.NETWORK === 'testnet' ? Networks.testnet : Networks.livenet;
Expand All @@ -31,24 +26,20 @@ const stopBlock = {
'livenet': '00000000000000000b539ef570128acb953af3dbcfc19dd8e6066949672311a1',
'testnet': '00000000d0bc4271bcefaa7eb25000e345910ba16b91eb375cd944b68624de9f'
};
const txHash = {
'livenet': '22231e8219a0617a0ded618b5dc713fdf9b0db8ebd5bb3322d3011a703119d3b',
'testnet': '22231e8219a0617a0ded618b5dc713fdf9b0db8ebd5bb3322d3011a703119d3b'
};

// These tests require a running bitcoind instance
describe('Integration with ' + network.name + ' bitcoind', function() {

this.timeout(15000);
var opts = {
const opts = {
host: 'localhost',
network: network.name
};
it('handshakes', function(cb) {
var peer = new Peer(opts);
const peer = new Peer(opts);
peer.once('version', function(m) {
m.version.should.be.above(70000);
m.services.toString().should.equal('1');
m.services.and(new BN(1)).toString().should.equal('1');
Math.abs(new Date() - m.timestamp).should.be.below(10000); // less than 10 seconds of time difference
m.nonce.length.should.equal(8);
m.startHeight.should.be.above(300000);
Expand All @@ -60,8 +51,8 @@ describe('Integration with ' + network.name + ' bitcoind', function() {
});
peer.connect();
});
var connect = function(cb) {
var peer = new Peer(opts);
const connect = function(cb) {
const peer = new Peer(opts);
peer.once('ready', function() {
cb(peer);
});
Expand All @@ -73,8 +64,8 @@ describe('Integration with ' + network.name + ' bitcoind', function() {
it('connects', function(cb) {
connect(function(peer) {
peer.version.should.be.above(70000);
_.isString(peer.subversion).should.equal(true);
_.isNumber(peer.bestHeight).should.equal(true);
(typeof peer.subversion === 'string').should.equal(true);
(typeof peer.bestHeight === 'number').should.equal(true);
cb();
});
});
Expand All @@ -91,14 +82,14 @@ describe('Integration with ' + network.name + ' bitcoind', function() {
it('handles addr', function(cb) {
connect(function(peer) {
peer.once('addr', function(message) {
message.addresses.forEach(function(address) {
for (const address of message.addresses) {
(address.time instanceof Date).should.equal(true);
should.exist(address.ip);
(address.services instanceof BN).should.equal(true);
});
}
cb();
});
var message = messages.GetAddr();
const message = messages.GetAddr();
peer.sendMessage(message);
});
});
Expand All @@ -113,23 +104,23 @@ describe('Integration with ' + network.name + ' bitcoind', function() {
cb();
});
peer.once('inv', function(message) {
var get = messages.GetData(message.inventory);
const get = messages.GetData(message.inventory);
peer.sendMessage(get);
});
});
});
it('sends tx inv and receives getdata for that tx', function(cb) {
connect(function(peer) {
var type = Inventory.TYPE.TX;
var inv = [{
const type = Inventory.TYPE.TX;
const inv = [{
type: type,
hash: Buffer.from(Random.getRandomBuffer(32)) // needs to be random for repeatability
}];
peer.once('getdata', function(message) {
message.inventory[0].should.deep.equal(inv[0]);
cb();
});
var message = messages.Inventory(inv);
const message = messages.Inventory(inv);
message.inventory[0].hash.length.should.equal(32);
peer.sendMessage(message);
});
Expand All @@ -140,35 +131,35 @@ describe('Integration with ' + network.name + ' bitcoind', function() {
(message.block instanceof Block).should.equal(true);
cb();
});
var message = messages.GetData.forBlock(blockHash[network.name]);
const message = messages.GetData.forBlock(blockHash[network.name]);
peer.sendMessage(message);
});
});
var fakeHash = 'e2dfb8afe1575bfacae1a0b4afc49af7ddda69285857267bae0e22be15f74a3a';
const fakeHash = 'e2dfb8afe1575bfacae1a0b4afc49af7ddda69285857267bae0e22be15f74a3a';
it('handles request tx data not found', function(cb) {
connect(function(peer) {
var expected = messages.NotFound.forTransaction(fakeHash);
const expected = messages.NotFound.forTransaction(fakeHash);
peer.once('notfound', function(message) {
message.command.should.equal('notfound');
message.inventory[0].type.should.equal(Inventory.TYPE.TX);
var expectedHash = expected.inventory[0].hash.toString('hex');
const expectedHash = expected.inventory[0].hash.toString('hex');
message.inventory[0].hash.toString('hex').should.equal(expectedHash);
cb();
});
var message = messages.GetData.forTransaction(fakeHash);
const message = messages.GetData.forTransaction(fakeHash);
peer.sendMessage(message);
});
});
var from = [blockHash[network.name]];
var stop = stopBlock[network.name];
const from = [blockHash[network.name]];
const stop = stopBlock[network.name];
it('gets headers', function(cb) {
connect(function(peer) {
peer.once('headers', function(message) {
message.command.should.equal('headers');
message.headers.length.should.equal(3);
cb();
});
var message = messages.GetHeaders({
const message = messages.GetHeaders({
starts: from,
stop: stop
});
Expand All @@ -185,35 +176,35 @@ describe('Integration with ' + network.name + ' bitcoind', function() {
cb();
}
});
var message = messages.GetBlocks({
const message = messages.GetBlocks({
starts: from,
stop: stop
});
peer.sendMessage(message);
});
});
var testInvGetData = function(expected, message, cb) {
const testInvGetData = function(expected, message, cb) {
connect(function(peer) {
peer.once('getdata', function(message) {
message.command.should.equal('getdata');
message.inventory[0].type.should.equal(expected.inventory[0].type);
var expectedHash = expected.inventory[0].hash.toString('hex');
const expectedHash = expected.inventory[0].hash.toString('hex');
message.inventory[0].hash.toString('hex').should.equal(expectedHash);
cb();
});
peer.sendMessage(message);
});
};
it('sends block inv and receives getdata', function(cb) {
var randomHash = Buffer.from(Random.getRandomBuffer(32)); // slow buffer
var expected = messages.GetData.forBlock(randomHash);
var message = messages.Inventory.forBlock(randomHash);
const randomHash = Buffer.from(Random.getRandomBuffer(32)); // slow buffer
const expected = messages.GetData.forBlock(randomHash);
const message = messages.Inventory.forBlock(randomHash);
testInvGetData(expected, message, cb);
});
it('sends tx inv and receives getdata', function(cb) {
var randomHash = Buffer.from(Random.getRandomBuffer(32)); // slow buffer
var expected = messages.GetData.forTransaction(randomHash);
var message = messages.Inventory.forTransaction(randomHash);
const randomHash = Buffer.from(Random.getRandomBuffer(32)); // slow buffer
const expected = messages.GetData.forTransaction(randomHash);
const message = messages.Inventory.forTransaction(randomHash);
testInvGetData(expected, message, cb);
});
});
3 changes: 1 addition & 2 deletions packages/bitcore-p2p-cash/lib/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const $ = bitcore.util.preconditions;
const BufferUtil = bitcore.util.buffer;
const BufferReader = bitcore.encoding.BufferReader;
const BufferWriter = bitcore.encoding.BufferWriter;
const _ = bitcore.deps._;

/**
* A constructor for inventory related Bitcoin messages such as
Expand All @@ -32,7 +31,7 @@ function Inventory(obj) {
*/
Inventory.forItem = function(type, hash) {
$.checkArgument(hash);
if (_.isString(hash)) {
if (typeof hash === 'string') {
hash = Buffer.from(hash, 'hex');
hash = BufferUtil.reverse(hash);
}
Expand Down
Loading