Skip to content

Commit

Permalink
Merge branch 'regtestSupport' of https://github.com/kajoseph/bitcore
Browse files Browse the repository at this point in the history
  • Loading branch information
kajoseph committed Mar 6, 2024
2 parents 1ea3dbd + 46ee91c commit 2aeb347
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 230 deletions.
4 changes: 2 additions & 2 deletions packages/bitcore-lib-cash/lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand All @@ -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;
};
Expand Down
13 changes: 10 additions & 3 deletions packages/bitcore-lib-cash/lib/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ 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];
if(networkMaps[arg] && networkMaps[arg].length >= 1) {
return networkMaps[arg][0];
} else {
return networkMaps[arg];
}
}

/***
Expand Down Expand Up @@ -128,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);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/bitcore-lib-cash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion packages/bitcore-lib-cash/test/hdprivatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions packages/bitcore-lib-doge/lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand All @@ -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;
};
Expand Down
98 changes: 11 additions & 87 deletions packages/bitcore-lib-doge/lib/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ addNetwork({
privatekey: 0xf1,
scripthash: 0xc4,
xpubkey: 0x043587cf,
xprivkey: 0x04358394
xprivkey: 0x04358394,
networkMagic: 0xfcc1b7dc,
port: 44556,
dnsSeeds: [
'testseed.jrn.me.uk'
]
});

/**
Expand All @@ -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
*/
Expand All @@ -291,6 +214,7 @@ function enableRegtest() {

/**
* @function
* @deprecated
* @member Networks#disableRegtest
* Will disable regtest features for testnet
*/
Expand Down
14 changes: 4 additions & 10 deletions packages/bitcore-lib-doge/test/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@ describe('Networks', function() {
should.exist(networks.defaultNetwork);
});

it('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(new Buffer('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.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() {
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-lib-ltc/lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
Loading

0 comments on commit 2aeb347

Please sign in to comment.