From 4a0fd2de90f49143e72e827fc75da9fe91678541 Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Mon, 12 Feb 2024 11:00:43 -0500 Subject: [PATCH 01/12] regtest support --- packages/bitcore-lib-cash/lib/address.js | 4 +- packages/bitcore-lib-cash/package.json | 1 - packages/bitcore-lib-doge/lib/address.js | 10 +- packages/bitcore-lib-doge/lib/networks.js | 98 +++---------------- packages/bitcore-lib-ltc/lib/address.js | 2 +- packages/bitcore-lib-ltc/lib/networks.js | 109 ++++++---------------- packages/bitcore-lib/lib/address.js | 2 +- 7 files changed, 52 insertions(+), 174 deletions(-) diff --git a/packages/bitcore-lib-cash/lib/address.js b/packages/bitcore-lib-cash/lib/address.js index bbc259909b1..0f31a2688fc 100644 --- a/packages/bitcore-lib-cash/lib/address.js +++ b/packages/bitcore-lib-cash/lib/address.js @@ -206,7 +206,7 @@ Address._transformBuffer = function(buffer, network, type) { throw new TypeError('Unknown network'); } - if (!bufferVersion.network || (networkObj && networkObj !== bufferVersion.network)) { + if (!bufferVersion.network || (networkObj && networkObj.xpubkey !== bufferVersion.network.xpubkey)) { throw new TypeError('Address has mismatched network type.'); } @@ -215,7 +215,7 @@ Address._transformBuffer = function(buffer, network, type) { } info.hashBuffer = buffer.slice(1); - info.network = bufferVersion.network; + info.network = networkObj || bufferVersion.network; info.type = bufferVersion.type; return info; }; diff --git a/packages/bitcore-lib-cash/package.json b/packages/bitcore-lib-cash/package.json index f1fe706dd7a..348af10675e 100644 --- a/packages/bitcore-lib-cash/package.json +++ b/packages/bitcore-lib-cash/package.json @@ -35,7 +35,6 @@ "request": "browser-request" }, "dependencies": { - "bitcore-lib": "^10.0.23", "bn.js": "=4.11.8", "bs58": "^4.0.1", "buffer-compare": "=1.1.1", diff --git a/packages/bitcore-lib-doge/lib/address.js b/packages/bitcore-lib-doge/lib/address.js index 91077fa0891..4d5bcb9450e 100644 --- a/packages/bitcore-lib-doge/lib/address.js +++ b/packages/bitcore-lib-doge/lib/address.js @@ -195,10 +195,14 @@ Address._transformBuffer = function(buffer, network, type) { throw new TypeError('Address buffers must be exactly 21 bytes.'); } - network = Networks.get(network); + var networkObj = Networks.get(network); var bufferVersion = Address._classifyFromVersion(buffer); - if (!bufferVersion.network || (network && network !== bufferVersion.network)) { + if (network && !networkObj) { + throw new TypeError('Unknown network'); + } + + if (!bufferVersion.network || (networkObj && networkObj.xpubkey !== bufferVersion.network.xpubkey)) { throw new TypeError('Address has mismatched network type.'); } @@ -207,7 +211,7 @@ Address._transformBuffer = function(buffer, network, type) { } info.hashBuffer = buffer.slice(1); - info.network = bufferVersion.network; + info.network = networkObj || bufferVersion.network; info.type = bufferVersion.type; return info; }; diff --git a/packages/bitcore-lib-doge/lib/networks.js b/packages/bitcore-lib-doge/lib/networks.js index 94450d1b9e4..a265a8cdb0f 100644 --- a/packages/bitcore-lib-doge/lib/networks.js +++ b/packages/bitcore-lib-doge/lib/networks.js @@ -171,7 +171,12 @@ addNetwork({ privatekey: 0xf1, scripthash: 0xc4, xpubkey: 0x043587cf, - xprivkey: 0x04358394 + xprivkey: 0x04358394, + networkMagic: 0xfcc1b7dc, + port: 44556, + dnsSeeds: [ + 'testseed.jrn.me.uk' + ] }); /** @@ -189,99 +194,17 @@ addNetwork({ scripthash: 0xc4, xpubkey: 0x043587cf, xprivkey: 0x04358394, + networkMagic: 0xfabfb5da, + port: 18444, + dnsSeeds: [] }); var regtest = get('regtest'); -// Add configurable values for testnet/regtest - -var TESTNET = { - PORT: 44556, - NETWORK_MAGIC: BufferUtil.integerAsBuffer(0xfcc1b7dc), - DNS_SEEDS: [ - 'testseed.jrn.me.uk' - ] -}; - -for (var key in TESTNET) { - if (!_.isObject(TESTNET[key])) { - networkMaps[TESTNET[key]] = testnet; - } -} - -var REGTEST = { - PORT: 18444, - NETWORK_MAGIC: BufferUtil.integerAsBuffer(0xfabfb5da), - DNS_SEEDS: [] -}; - -for (var key in REGTEST) { - if (!_.isObject(REGTEST[key])) { - networkMaps[REGTEST[key]] = testnet; - } -} - -Object.defineProperty(testnet, 'port', { - enumerable: true, - configurable: false, - get: function() { - if (this.regtestEnabled) { - return REGTEST.PORT; - } else { - return TESTNET.PORT; - } - } -}); - -Object.defineProperty(testnet, 'networkMagic', { - enumerable: true, - configurable: false, - get: function() { - if (this.regtestEnabled) { - return REGTEST.NETWORK_MAGIC; - } else { - return TESTNET.NETWORK_MAGIC; - } - } -}); - -Object.defineProperty(testnet, 'dnsSeeds', { - enumerable: true, - configurable: false, - get: function() { - if (this.regtestEnabled) { - return REGTEST.DNS_SEEDS; - } else { - return TESTNET.DNS_SEEDS; - } - } -}); - -Object.defineProperty(regtest, 'networkMagic', { - enumerable: true, - configurable: false, - get: function() { - return REGTEST.NETWORK_MAGIC; - } -}); -Object.defineProperty(regtest, 'dnsSeeds', { - enumerable: true, - configurable: false, - get: function() { - return REGTEST.DNS_SEEDS; - } -}); -Object.defineProperty(regtest, 'port', { - enumerable: true, - configurable: false, - get: function() { - return REGTEST.PORT; - } -}); - /** * @function + * @deprecated * @member Networks#enableRegtest * Will enable regtest features for testnet */ @@ -291,6 +214,7 @@ function enableRegtest() { /** * @function + * @deprecated * @member Networks#disableRegtest * Will disable regtest features for testnet */ diff --git a/packages/bitcore-lib-ltc/lib/address.js b/packages/bitcore-lib-ltc/lib/address.js index f69c936a25e..dae460864ac 100644 --- a/packages/bitcore-lib-ltc/lib/address.js +++ b/packages/bitcore-lib-ltc/lib/address.js @@ -250,7 +250,7 @@ Address._transformBuffer = function(buffer, network, type) { } else { info.hashBuffer = buffer.slice(1); } - info.network = bufferVersion.network; + info.network = networkObj || bufferVersion.network; info.type = bufferVersion.type; return info; }; diff --git a/packages/bitcore-lib-ltc/lib/networks.js b/packages/bitcore-lib-ltc/lib/networks.js index 865b312f409..b43e891f29a 100644 --- a/packages/bitcore-lib-ltc/lib/networks.js +++ b/packages/bitcore-lib-ltc/lib/networks.js @@ -76,6 +76,7 @@ function addNetwork(data) { privatekey: data.privatekey, scripthash: data.scripthash, scripthash2: data.scripthash2, + bech32prefix: data.bech32prefix, xpubkey: data.xpubkey, xprivkey: data.xprivkey }); @@ -181,8 +182,15 @@ addNetwork({ privatekey: 0xef, // 239 scripthash: 0x3a, // 58 scripthash2: 0xc4, // 196 + bech32prefix: 'tltc', xpubkey: 0x043587cf, - xprivkey: 0x04358394 + xprivkey: 0x04358394, + networkMagic: 0xfdd2c8f1, + port: 19335, + dnsSeeds: [ + 'testnet-seed.litecointools.com', + 'seed-b.litecoin.loshan.co.uk' + ] }); /** @@ -191,89 +199,30 @@ addNetwork({ */ var testnet = get('testnet'); -// Add configurable values for testnet/regtest - -var TESTNET = { - PORT: 19335, - NETWORK_MAGIC: BufferUtil.integerAsBuffer(0xfdd2c8f1), - DNS_SEEDS: [ - 'testnet-seed.litecointools.com', - 'seed-b.litecoin.loshan.co.uk' - ], - BECH32_PREFIX: 'tltc' -}; - -for (var key in TESTNET) { - if (!_.isObject(TESTNET[key])) { - networkMaps[TESTNET[key]] = testnet; - } -} -networkMaps[TESTNET.NETWORK_MAGIC.toString('hex')] = testnet; - -var REGTEST = { - PORT: 19444, - NETWORK_MAGIC: BufferUtil.integerAsBuffer(0xfabfb5da), - DNS_SEEDS: [], - BECH32_PREFIX: 'rltc' -}; - -for (var key in REGTEST) { - if (!_.isObject(REGTEST[key])) { - networkMaps[REGTEST[key]] = testnet; - } -} -networkMaps[REGTEST.NETWORK_MAGIC.toString('hex')] = testnet; - -Object.defineProperty(testnet, 'port', { - enumerable: true, - configurable: false, - get: function() { - if (this.regtestEnabled) { - return REGTEST.PORT; - } else { - return TESTNET.PORT; - } - } -}); - -Object.defineProperty(testnet, 'networkMagic', { - enumerable: true, - configurable: false, - get: function() { - if (this.regtestEnabled) { - return REGTEST.NETWORK_MAGIC; - } else { - return TESTNET.NETWORK_MAGIC; - } - } -}); - -Object.defineProperty(testnet, 'dnsSeeds', { - enumerable: true, - configurable: false, - get: function() { - if (this.regtestEnabled) { - return REGTEST.DNS_SEEDS; - } else { - return TESTNET.DNS_SEEDS; - } - } +addNetwork({ + name: 'regtest', + alias: 'dev', + pubkeyhash: 0x6f, // 111 + privatekey: 0xef, // 239 + scripthash: 0x3a, // 58 + scripthash2: 0xc4, // 196 + bech32prefix: 'rltc', + xpubkey: 0x043587cf, + xprivkey: 0x04358394, + networkMagic: 0xfabfb5da, + port: 19444, + dnsSeeds: [] }); -Object.defineProperty(testnet, 'bech32prefix', { - enumerable: true, - configurable: false, - get: function() { - if (this.regtestEnabled) { - return REGTEST.BECH32_PREFIX - } else { - return TESTNET.BECH32_PREFIX - } - } -}) +/** + * @instance + * @member Networks#testnet + */ +var regtest = get('regtest'); /** * @function + * @deprecated * @member Networks#enableRegtest * Will enable regtest features for testnet */ @@ -283,6 +232,7 @@ function enableRegtest() { /** * @function + * @deprecated * @member Networks#disableRegtest * Will disable regtest features for testnet */ @@ -300,6 +250,7 @@ module.exports = { livenet: livenet, mainnet: livenet, testnet: testnet, + regtest: regtest, get: get, enableRegtest: enableRegtest, disableRegtest: disableRegtest diff --git a/packages/bitcore-lib/lib/address.js b/packages/bitcore-lib/lib/address.js index ead78aaab0b..1bf25172f97 100644 --- a/packages/bitcore-lib/lib/address.js +++ b/packages/bitcore-lib/lib/address.js @@ -263,7 +263,7 @@ Address._transformBuffer = function(buffer, network, type) { } else { info.hashBuffer = buffer.slice(1); } - info.network = bufferVersion.network; + info.network = networkObj || bufferVersion.network; info.type = bufferVersion.type; return info; }; From 93a64a4f9f734cca49792531f9c87f9e5ec1a619 Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Mon, 12 Feb 2024 11:41:54 -0500 Subject: [PATCH 02/12] regtest support --- packages/bitcore-wallet-client/src/lib/api.ts | 4 ++-- packages/bitcore-wallet-client/src/lib/key.ts | 12 ++++++------ .../bitcore-wallet-service/bws.example.config.js | 1 + .../src/lib/chain/btc/index.ts | 5 ++--- .../bitcore-wallet-service/src/lib/common/utils.ts | 10 ++++++++++ packages/bitcore-wallet-service/src/lib/server.ts | 7 ++++++- 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/bitcore-wallet-client/src/lib/api.ts b/packages/bitcore-wallet-client/src/lib/api.ts index 52a275dc1fc..e722bd73184 100644 --- a/packages/bitcore-wallet-client/src/lib/api.ts +++ b/packages/bitcore-wallet-client/src/lib/api.ts @@ -913,8 +913,8 @@ export class API extends EventEmitter { return cb(new Error('Invalid coin')); var network = opts.network || 'livenet'; - if (!_.includes(['testnet', 'livenet'], network)) - return cb(new Error('Invalid network')); + if (!_.includes(['testnet', 'livenet', 'regtest'], network)) + return cb(new Error('Invalid network: ' + network)); if (!this.credentials) { return cb(new Error('Import credentials first with setCredentials()')); diff --git a/packages/bitcore-wallet-client/src/lib/key.ts b/packages/bitcore-wallet-client/src/lib/key.ts index 9151e37ed40..8fe61325e0c 100644 --- a/packages/bitcore-wallet-client/src/lib/key.ts +++ b/packages/bitcore-wallet-client/src/lib/key.ts @@ -365,8 +365,8 @@ export class Key { }; _checkNetwork = function (network) { - if (!_.includes(['livenet', 'testnet'], network)) - throw new Error('Invalid network'); + if (!_.includes(['livenet', 'testnet', 'regtest'], network)) + throw new Error('Invalid network ' + network); }; /* @@ -384,7 +384,7 @@ export class Key { // checking in chains for simplicity if ( - opts.network == 'testnet' && + ['testnet', 'regtest]'].includes(opts.network) && Constants.UTXO_CHAINS.includes(opts.coin) ) { coinCode = '1'; @@ -439,11 +439,11 @@ export class Key { Constants.PATHS.REQUEST_KEY ).privateKey.toString(); - if (opts.network == 'testnet') { + if (['testnet', 'regtest'].includes(opts.network)) { // Hacky: BTC/BCH xPriv depends on network: This code is to - // convert a livenet xPriv to a testnet xPriv + // convert a livenet xPriv to a testnet/regtest xPriv let x = xPrivKey.toObject(); - x.network = 'testnet'; + x.network = opts.network; delete x.xprivkey; delete x.checksum; x.privateKey = _.padStart(x.privateKey, 64, '0'); diff --git a/packages/bitcore-wallet-service/bws.example.config.js b/packages/bitcore-wallet-service/bws.example.config.js index d9e91539341..976d9501b08 100644 --- a/packages/bitcore-wallet-service/bws.example.config.js +++ b/packages/bitcore-wallet-service/bws.example.config.js @@ -2,6 +2,7 @@ module.exports = { basePath: '/bws/api', disableLogs: false, port: 3232, + allowRegtest: false, // Uncomment to make BWS a forking server // cluster: true, diff --git a/packages/bitcore-wallet-service/src/lib/chain/btc/index.ts b/packages/bitcore-wallet-service/src/lib/chain/btc/index.ts index 156d39bd0bd..a1b72d262a7 100644 --- a/packages/bitcore-wallet-service/src/lib/chain/btc/index.ts +++ b/packages/bitcore-wallet-service/src/lib/chain/btc/index.ts @@ -950,10 +950,9 @@ export class BtcChain implements IChain { protected _isCorrectNetwork(wallet, addr) { const addrNetwork = addr.network.toString(); const walNetwork = wallet.network; - const walChain = wallet.chain || wallet.coin; // wallet.coin is for backward compatibility - if (addrNetwork === 'regtest' && walNetwork === 'testnet') { - return config.blockchainExplorerOpts?.[walChain]?.testnet?.regtestEnabled; + if (addrNetwork === 'testnet' && walNetwork === 'regtest') { + return !!config.allowRegtest; } return addrNetwork === walNetwork; } diff --git a/packages/bitcore-wallet-service/src/lib/common/utils.ts b/packages/bitcore-wallet-service/src/lib/common/utils.ts index cced9615e77..f6b25bfbe0a 100644 --- a/packages/bitcore-wallet-service/src/lib/common/utils.ts +++ b/packages/bitcore-wallet-service/src/lib/common/utils.ts @@ -1,5 +1,6 @@ import * as CWC from 'crypto-wallet-core'; import _ from 'lodash'; +import Config from '../../config'; import { logger } from '../logger'; const $ = require('preconditions').singleton(); @@ -273,4 +274,13 @@ export class Utils { const result = Bitcore_[coin].Address.fromObject(origObj); return coin == 'bch' ? result.toLegacyAddress() : result.toString(); } + + static compareNetworks(network1, network2) { + network1 = network1?.toLowerCase(); + network2 = network2?.toLowerCase(); + + if (network1 == network2) return true; + if (Config.allowRegtest && ['testnet', 'regtest'].includes(network1) && ['testnet', 'regtest'].includes(network2)) return true; + return false; + } } diff --git a/packages/bitcore-wallet-service/src/lib/server.ts b/packages/bitcore-wallet-service/src/lib/server.ts index ed60bf15154..b1dcd810a02 100644 --- a/packages/bitcore-wallet-service/src/lib/server.ts +++ b/packages/bitcore-wallet-service/src/lib/server.ts @@ -439,6 +439,7 @@ export class WalletService implements IWalletService { if (typeof message === 'string' && args.length > 0 && !message.endsWith('%o')) { for (let i = 0; i < args.length; i++) { message += ' %o'; + args[i] = args[i]?.stack || args[i]?.message || args[i]; } } @@ -570,6 +571,10 @@ export class WalletService implements IWalletService { return cb(new ClientError('Invalid network')); } + if (opts.network === 'regtest' && !config.allowRegtest) { + return cb(new ClientError('Regtest is not allowed for this environment')); + } + const derivationStrategy = Constants.DERIVATION_STRATEGIES.BIP44; let addressType = opts.n === 1 ? Constants.SCRIPT_TYPES.P2PKH : Constants.SCRIPT_TYPES.P2SH; @@ -1159,7 +1164,7 @@ export class WalletService implements IWalletService { return cb(new ClientError('The wallet you are trying to join was created for a different chain')); } - if (wallet.network != xPubKey.network.name) { + if (!Utils.compareNetworks(wallet.network, xPubKey.network.name)) { return cb(new ClientError('The wallet you are trying to join was created for a different network')); } From 1860e304bd25c9ca83afaedf30c4b6d17c014b6e Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Mon, 12 Feb 2024 15:11:03 -0500 Subject: [PATCH 03/12] change polygon regtest chainid --- .../crypto-wallet-core/src/transactions/matic/index.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/crypto-wallet-core/src/transactions/matic/index.ts b/packages/crypto-wallet-core/src/transactions/matic/index.ts index d30eed77032..201085181cc 100644 --- a/packages/crypto-wallet-core/src/transactions/matic/index.ts +++ b/packages/crypto-wallet-core/src/transactions/matic/index.ts @@ -1,8 +1,5 @@ -import { ethers } from 'ethers'; -import Web3 from 'web3'; import { ETHTxProvider } from '../eth'; -const utils = require('web3-utils'); -const { toBN } = Web3.utils; + export class MATICTxProvider extends ETHTxProvider { getChainId(network: string) { let chainId = 137; @@ -12,7 +9,7 @@ export class MATICTxProvider extends ETHTxProvider { chainId = 80001; break; case 'regtest': - chainId = 1337; + chainId = 13375; break; default: chainId = 137; From b0bcff7cfb3324e2089471cda8bc605458c09c51 Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Wed, 14 Feb 2024 13:42:47 -0500 Subject: [PATCH 04/12] fix regtest unit tests --- packages/bitcore-lib-doge/test/networks.js | 13 ++++++++++--- packages/bitcore-lib-ltc/test/networks.js | 9 ++++++++- packages/bitcore-lib/test/networks.js | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/bitcore-lib-doge/test/networks.js b/packages/bitcore-lib-doge/test/networks.js index c231b6643a6..875d71feca4 100644 --- a/packages/bitcore-lib-doge/test/networks.js +++ b/packages/bitcore-lib-doge/test/networks.js @@ -15,19 +15,26 @@ describe('Networks', function() { should.exist(networks.defaultNetwork); }); - it('will enable/disable regtest Network', function() { + it('#DEPRECATED will enable/disable regtest Network', function() { + const beforeEnable = networks.testnet; networks.enableRegtest(); - networks.testnet.networkMagic.should.deep.equal(new Buffer('fabfb5da', 'hex')); + /* + networks.testnet.networkMagic.should.deep.equal(Buffer.from('fabfb5da', 'hex')); networks.testnet.port.should.equal(18444); networks.testnet.dnsSeeds.should.deep.equal([]); networks.testnet.regtestEnabled.should.equal(true); + */ + networks.testnet.should.deep.equal(beforeEnable); networks.disableRegtest(); - networks.testnet.networkMagic.should.deep.equal(new Buffer('fcc1b7dc', 'hex')); + /* + networks.testnet.networkMagic.should.deep.equal(Buffer.from('fcc1b7dc', 'hex')); networks.testnet.port.should.equal(44556); networks.testnet.dnsSeeds.should.deep.equal([ 'testseed.jrn.me.uk', ]); + */ + networks.testnet.should.deep.equal(beforeEnable); }); it('will get network based on string "regtest" value', function() { diff --git a/packages/bitcore-lib-ltc/test/networks.js b/packages/bitcore-lib-ltc/test/networks.js index 6dc4f76ef16..97cfcc8af4c 100644 --- a/packages/bitcore-lib-ltc/test/networks.js +++ b/packages/bitcore-lib-ltc/test/networks.js @@ -15,20 +15,27 @@ describe('Networks', function() { should.exist(networks.defaultNetwork); }); - it('will enable/disable regtest Network', function() { + it('#DEPRECATED will enable/disable regtest Network', function() { + const beforeEnable = networks.testnet; networks.enableRegtest(); + /* networks.testnet.networkMagic.should.deep.equal(Buffer.from('fabfb5da', 'hex')); networks.testnet.port.should.equal(19444); networks.testnet.dnsSeeds.should.deep.equal([]); networks.testnet.regtestEnabled.should.equal(true); + */ + networks.testnet.should.deep.equal(beforeEnable); networks.disableRegtest(); + /* networks.testnet.networkMagic.should.deep.equal(Buffer.from('fdd2c8f1', 'hex')); networks.testnet.port.should.equal(19335); networks.testnet.dnsSeeds.should.deep.equal([ 'testnet-seed.litecointools.com', 'seed-b.litecoin.loshan.co.uk' ]); + */ + networks.testnet.should.deep.equal(beforeEnable); }); it('will get network based on string "regtest" value', function() { diff --git a/packages/bitcore-lib/test/networks.js b/packages/bitcore-lib/test/networks.js index c89db09e494..faac7c8e25e 100644 --- a/packages/bitcore-lib/test/networks.js +++ b/packages/bitcore-lib/test/networks.js @@ -19,7 +19,7 @@ describe('Networks', function() { const beforeEnable = networks.testnet; networks.enableRegtest(); /* - *networks.testnet.networkMagic.should.deep.equal(new Buffer('fabfb5da', 'hex')); + *networks.testnet.networkMagic.should.deep.equal(Buffer.from('fabfb5da', 'hex')); *networks.testnet.port.should.equal(18444); *networks.testnet.dnsSeeds.should.deep.equal([]); *networks.testnet.regtestEnabled.should.equal(true); From ceeddd09d9895614d33f71308dd08a4edc0aa668 Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Wed, 14 Feb 2024 13:44:42 -0500 Subject: [PATCH 05/12] update chainid for polygon regtest --- packages/crypto-wallet-core/test/transactions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/crypto-wallet-core/test/transactions.ts b/packages/crypto-wallet-core/test/transactions.ts index fe9cc99027d..d94e4d99cbb 100644 --- a/packages/crypto-wallet-core/test/transactions.ts +++ b/packages/crypto-wallet-core/test/transactions.ts @@ -1098,6 +1098,6 @@ describe('MATIC Transaction getChainId', () => { expect(testId).to.equal(80001); const regtestId = Transactions.get({ chain: 'MATIC' }).getChainId('regtest'); - expect(regtestId).to.equal(1337); + expect(regtestId).to.equal(13375); }); }); \ No newline at end of file From 0bc3f3c89ec74cbf6778610a3eaed593fe1c698c Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Thu, 15 Feb 2024 11:54:35 -0500 Subject: [PATCH 06/12] fix ltc tests --- packages/bitcore-lib-ltc/lib/networks.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/bitcore-lib-ltc/lib/networks.js b/packages/bitcore-lib-ltc/lib/networks.js index b43e891f29a..b8555486b7c 100644 --- a/packages/bitcore-lib-ltc/lib/networks.js +++ b/packages/bitcore-lib-ltc/lib/networks.js @@ -44,7 +44,11 @@ function get(arg, keys) { } return undefined; } - return networkMaps[arg]; + if(networkMaps[arg] && networkMaps[arg].length >= 1) { + return networkMaps[arg][0]; + } else { + return networkMaps[arg]; + } } /** @@ -108,7 +112,10 @@ function addNetwork(data) { _.each(network, function(value) { if (!_.isUndefined(value) && !_.isObject(value)) { - networkMaps[value] = network; + if(!networkMaps[value]) { + networkMaps[value] = []; + } + networkMaps[value].push(network); } }); @@ -177,7 +184,7 @@ var livenet = get('livenet'); addNetwork({ name: 'testnet', - alias: 'regtest', + alias: 'test', pubkeyhash: 0x6f, // 111 privatekey: 0xef, // 239 scripthash: 0x3a, // 58 From 88d88da933de16496418cd811857c6b4062f7087 Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Thu, 15 Feb 2024 11:55:03 -0500 Subject: [PATCH 07/12] lint --- packages/bitcore-lib-cash/lib/networks.js | 3 +-- packages/bitcore-lib/lib/networks.js | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/bitcore-lib-cash/lib/networks.js b/packages/bitcore-lib-cash/lib/networks.js index 2bf1f2f8293..b67eb1f3036 100644 --- a/packages/bitcore-lib-cash/lib/networks.js +++ b/packages/bitcore-lib-cash/lib/networks.js @@ -39,12 +39,11 @@ function get(arg, keys) { var filteredNet = _.pick(network, keys); var netValues = _.values(filteredNet); if(~netValues.indexOf(arg)) { - return network; + return network; } } return undefined; } - return networkMaps[arg]; } /*** diff --git a/packages/bitcore-lib/lib/networks.js b/packages/bitcore-lib/lib/networks.js index dd369cc2ab4..f2b2fbdca1f 100644 --- a/packages/bitcore-lib/lib/networks.js +++ b/packages/bitcore-lib/lib/networks.js @@ -113,7 +113,6 @@ function addNetwork(data) { networks.push(network); return network; - } /** From 8002977525dbc87a31b7284320879676ab9c34b0 Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Thu, 15 Feb 2024 12:04:54 -0500 Subject: [PATCH 08/12] fix ltc regtest test --- packages/bitcore-lib-ltc/test/networks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bitcore-lib-ltc/test/networks.js b/packages/bitcore-lib-ltc/test/networks.js index 97cfcc8af4c..d72bb8d026d 100644 --- a/packages/bitcore-lib-ltc/test/networks.js +++ b/packages/bitcore-lib-ltc/test/networks.js @@ -40,7 +40,7 @@ describe('Networks', function() { it('will get network based on string "regtest" value', function() { var network = networks.get('regtest'); - network.should.equal(networks.testnet); + network.should.equal(networks.regtest); }); it('should be able to define a custom Network', function() { From 4d0dce3facc0b163c28d480da4b73b4aeef310b6 Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Thu, 15 Feb 2024 12:36:15 -0500 Subject: [PATCH 09/12] fix bch networks for consistency --- packages/bitcore-lib-cash/lib/networks.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/bitcore-lib-cash/lib/networks.js b/packages/bitcore-lib-cash/lib/networks.js index b67eb1f3036..44b9f7bcf31 100644 --- a/packages/bitcore-lib-cash/lib/networks.js +++ b/packages/bitcore-lib-cash/lib/networks.js @@ -44,6 +44,11 @@ function get(arg, keys) { } return undefined; } + if(networkMaps[arg] && networkMaps[arg].length >= 1) { + return networkMaps[arg][0]; + } else { + return networkMaps[arg]; + } } /*** @@ -127,7 +132,10 @@ function indexNetworkBy(network, keys) { var key = keys[i]; var networkValue = network[key]; if(!_.isUndefined(networkValue) && !_.isObject(networkValue)) { - networkMaps[networkValue] = network; + if(!networkMaps[networkValue]) { + networkMaps[networkValue] = []; + } + networkMaps[networkValue].push(network); } } } From 86e98c59c669d0240863c2469e612c2654f7a249 Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Thu, 15 Feb 2024 12:36:35 -0500 Subject: [PATCH 10/12] lint --- packages/bitcore-lib-cash/test/hdprivatekey.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/bitcore-lib-cash/test/hdprivatekey.js b/packages/bitcore-lib-cash/test/hdprivatekey.js index 7947f826ead..5d4eae7c230 100644 --- a/packages/bitcore-lib-cash/test/hdprivatekey.js +++ b/packages/bitcore-lib-cash/test/hdprivatekey.js @@ -7,7 +7,6 @@ var expect = require('chai').expect; var bitcore = require('..'); var errors = bitcore.errors; var hdErrors = errors.HDPrivateKey; -var buffer = require('buffer'); var Networks = bitcore.Networks; var BufferUtil = bitcore.util.buffer; var HDPrivateKey = bitcore.HDPrivateKey; From a1495174b7fbb08df4b54bd0a9c45c48170c415b Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Thu, 15 Feb 2024 13:05:32 -0500 Subject: [PATCH 11/12] fix polygon tests due to chainid change --- packages/bitcore-node/test/integration/matic/p2p.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/bitcore-node/test/integration/matic/p2p.spec.ts b/packages/bitcore-node/test/integration/matic/p2p.spec.ts index ebe17de8846..2ff5fb63c63 100644 --- a/packages/bitcore-node/test/integration/matic/p2p.spec.ts +++ b/packages/bitcore-node/test/integration/matic/p2p.spec.ts @@ -1,6 +1,6 @@ import * as BitcoreClient from 'bitcore-client'; import { expect } from 'chai'; -import { Web3 } from 'crypto-wallet-core'; +import { Web3, Transactions } from 'crypto-wallet-core'; import sinon from 'sinon'; import config from '../../../src/config'; import { CacheStorage } from '../../../src/models/cache'; @@ -65,16 +65,19 @@ async function sendTransaction(from, to, amount, web3, wallet, nonce = 0) { describe('Polygon', function() { const suite = this; this.timeout(50000); + const sandbox = sinon.createSandbox(); before(async () => { await intBeforeHelper(); await resetDatabase(); await Api.start(); + sandbox.stub(Transactions.get({ chain }), 'getChainId').returns(1337); }); after(async () => { await Api.stop(); await intAfterHelper(suite); + sandbox.restore(); }); it('should be able to create a wallet with an address', async () => { From 46ee91c5d3efe425e9dbe9fa029dccadf20161b2 Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Tue, 5 Mar 2024 15:19:39 -0500 Subject: [PATCH 12/12] feedback --- packages/bitcore-lib-doge/test/networks.js | 15 +-------------- packages/bitcore-lib-ltc/test/networks.js | 16 +--------------- packages/bitcore-lib/test/networks.js | 8 +------- 3 files changed, 3 insertions(+), 36 deletions(-) diff --git a/packages/bitcore-lib-doge/test/networks.js b/packages/bitcore-lib-doge/test/networks.js index 875d71feca4..7c1b812a3a7 100644 --- a/packages/bitcore-lib-doge/test/networks.js +++ b/packages/bitcore-lib-doge/test/networks.js @@ -15,25 +15,12 @@ describe('Networks', function() { should.exist(networks.defaultNetwork); }); - it('#DEPRECATED will enable/disable regtest Network', function() { + it('should not replace testnet network with regtest', function() { const beforeEnable = networks.testnet; networks.enableRegtest(); - /* - networks.testnet.networkMagic.should.deep.equal(Buffer.from('fabfb5da', 'hex')); - networks.testnet.port.should.equal(18444); - networks.testnet.dnsSeeds.should.deep.equal([]); - networks.testnet.regtestEnabled.should.equal(true); - */ networks.testnet.should.deep.equal(beforeEnable); networks.disableRegtest(); - /* - networks.testnet.networkMagic.should.deep.equal(Buffer.from('fcc1b7dc', 'hex')); - networks.testnet.port.should.equal(44556); - networks.testnet.dnsSeeds.should.deep.equal([ - 'testseed.jrn.me.uk', - ]); - */ networks.testnet.should.deep.equal(beforeEnable); }); diff --git a/packages/bitcore-lib-ltc/test/networks.js b/packages/bitcore-lib-ltc/test/networks.js index d72bb8d026d..c73db640230 100644 --- a/packages/bitcore-lib-ltc/test/networks.js +++ b/packages/bitcore-lib-ltc/test/networks.js @@ -15,26 +15,12 @@ describe('Networks', function() { should.exist(networks.defaultNetwork); }); - it('#DEPRECATED will enable/disable regtest Network', function() { + it('should not replace testnet network with regtest', function() { const beforeEnable = networks.testnet; networks.enableRegtest(); - /* - networks.testnet.networkMagic.should.deep.equal(Buffer.from('fabfb5da', 'hex')); - networks.testnet.port.should.equal(19444); - networks.testnet.dnsSeeds.should.deep.equal([]); - networks.testnet.regtestEnabled.should.equal(true); - */ networks.testnet.should.deep.equal(beforeEnable); networks.disableRegtest(); - /* - networks.testnet.networkMagic.should.deep.equal(Buffer.from('fdd2c8f1', 'hex')); - networks.testnet.port.should.equal(19335); - networks.testnet.dnsSeeds.should.deep.equal([ - 'testnet-seed.litecointools.com', - 'seed-b.litecoin.loshan.co.uk' - ]); - */ networks.testnet.should.deep.equal(beforeEnable); }); diff --git a/packages/bitcore-lib/test/networks.js b/packages/bitcore-lib/test/networks.js index faac7c8e25e..9f28ea544f2 100644 --- a/packages/bitcore-lib/test/networks.js +++ b/packages/bitcore-lib/test/networks.js @@ -15,15 +15,9 @@ describe('Networks', function() { should.exist(networks.defaultNetwork); }); - it('#DEPRECATED will enable/disable regtest Network', function() { + it('should not replace testnet network with regtest', function() { const beforeEnable = networks.testnet; networks.enableRegtest(); - /* - *networks.testnet.networkMagic.should.deep.equal(Buffer.from('fabfb5da', 'hex')); - *networks.testnet.port.should.equal(18444); - *networks.testnet.dnsSeeds.should.deep.equal([]); - *networks.testnet.regtestEnabled.should.equal(true); - */ networks.testnet.should.deep.equal(beforeEnable); networks.disableRegtest();