modularize network version check/tests issues/1265
This commit is contained in:
parent
49e621f4f2
commit
a38c0c2d93
@ -18,6 +18,15 @@ Network.prototype.toString = function toString() {
|
|||||||
return this.name;
|
return this.name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @member Networks#all
|
||||||
|
* Retrieves all networks registered.
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
function all() {
|
||||||
|
return networks;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function
|
* @function
|
||||||
* @member Networks#get
|
* @member Networks#get
|
||||||
@ -172,5 +181,6 @@ module.exports = {
|
|||||||
livenet: livenet,
|
livenet: livenet,
|
||||||
mainnet: livenet,
|
mainnet: livenet,
|
||||||
testnet: testnet,
|
testnet: testnet,
|
||||||
get: get
|
get: get,
|
||||||
|
all: all
|
||||||
};
|
};
|
||||||
|
|||||||
@ -157,14 +157,18 @@ PrivateKey._transformBuffer = function(buf, network) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
info.network = Networks.get(buf[0], 'privatekey');
|
info.network = Networks.get(buf[0], 'privatekey');
|
||||||
if (buf[0] === Networks.livenet.privatekey) {
|
|
||||||
info.network = Networks.livenet;
|
var allNetworks = Networks.all();
|
||||||
} else if (buf[0] === Networks.testnet.privatekey) {
|
var matches = _.filter( allNetworks, function( network) {
|
||||||
info.network = Networks.testnet;
|
return buf[0] === network.privatekey;
|
||||||
} else {
|
});
|
||||||
|
|
||||||
|
if (matches.length !== 1) {
|
||||||
throw new Error('Invalid network');
|
throw new Error('Invalid network');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info.network = matches[0];
|
||||||
|
|
||||||
if (network && info.network !== Networks.get(network)) {
|
if (network && info.network !== Networks.get(network)) {
|
||||||
throw new TypeError('Private key network mismatch');
|
throw new TypeError('Private key network mismatch');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,7 +48,31 @@ describe('Networks', function() {
|
|||||||
var net = networks.get('customnet');
|
var net = networks.get('customnet');
|
||||||
should.equal(net, undefined);
|
should.equal(net, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can return custom networks', function() {
|
||||||
|
var custom = {
|
||||||
|
name: 'customnet',
|
||||||
|
alias: 'mynet',
|
||||||
|
pubkeyhash: 0x10,
|
||||||
|
privatekey: 0x90,
|
||||||
|
scripthash: 0x08,
|
||||||
|
xpubkey: 0x0278b20e,
|
||||||
|
xprivkey: 0x0278ade4,
|
||||||
|
networkMagic: 0xe7beb4d4,
|
||||||
|
port: 20001,
|
||||||
|
dnsSeeds: [
|
||||||
|
'localhost',
|
||||||
|
'mynet.localhost'
|
||||||
|
]
|
||||||
|
};
|
||||||
|
networks.add(custom);
|
||||||
|
customnet = networks.get('customnet');
|
||||||
|
var allNetworks = networks.all();
|
||||||
|
var customInOutput = allNetworks.indexOf(customnet) > -1;
|
||||||
|
should.equal(customInOutput, true);
|
||||||
|
networks.remove(customnet);
|
||||||
|
});
|
||||||
|
|
||||||
it('should not set a network map for an undefined value', function() {
|
it('should not set a network map for an undefined value', function() {
|
||||||
var custom = {
|
var custom = {
|
||||||
name: 'somenet',
|
name: 'somenet',
|
||||||
|
|||||||
@ -44,6 +44,35 @@ describe('PrivateKey', function() {
|
|||||||
should.exist(a.bn);
|
should.exist(a.bn);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should create a private key from a custom network WIF string', function() {
|
||||||
|
var wifNamecoin = '74pxNKNpByQ2kMow4d9kF6Z77BYeKztQNLq3dSyU4ES1K5KLNiz';
|
||||||
|
var nmc = {
|
||||||
|
name: 'namecoin',
|
||||||
|
alias: 'namecoin',
|
||||||
|
pubkeyhash: 0x34,
|
||||||
|
privatekey: 0xB4,
|
||||||
|
// these below aren't the real NMC version numbers
|
||||||
|
scripthash: 0x08,
|
||||||
|
xpubkey: 0x0278b20e,
|
||||||
|
xprivkey: 0x0278ade4,
|
||||||
|
networkMagic: 0xf9beb4fe,
|
||||||
|
port: 20001,
|
||||||
|
dnsSeeds: [
|
||||||
|
'localhost',
|
||||||
|
'mynet.localhost'
|
||||||
|
]
|
||||||
|
};
|
||||||
|
Networks.add(nmc);
|
||||||
|
var nmcNet = Networks.get('namecoin');
|
||||||
|
var a = new PrivateKey(
|
||||||
|
'74pxNKNpByQ2kMow4d9kF6Z77BYeKztQNLq3dSyU4ES1K5KLNiz',
|
||||||
|
nmcNet
|
||||||
|
);
|
||||||
|
should.exist(a);
|
||||||
|
should.exist(a.bn);
|
||||||
|
Networks.remove(nmcNet);
|
||||||
|
});
|
||||||
|
|
||||||
it('should create a new random testnet private key with empty data', function() {
|
it('should create a new random testnet private key with empty data', function() {
|
||||||
var a = new PrivateKey(null, Networks.testnet);
|
var a = new PrivateKey(null, Networks.testnet);
|
||||||
should.exist(a);
|
should.exist(a);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user