modularize network version check/tests issues/1265

This commit is contained in:
Brandon Robertz 2015-06-08 19:40:58 -05:00 committed by Brandon Roberts
parent 49e621f4f2
commit a38c0c2d93
4 changed files with 74 additions and 7 deletions

View File

@ -18,6 +18,15 @@ Network.prototype.toString = function toString() {
return this.name;
};
/**
* @member Networks#all
* Retrieves all networks registered.
* @return Array
*/
function all() {
return networks;
}
/**
* @function
* @member Networks#get
@ -172,5 +181,6 @@ module.exports = {
livenet: livenet,
mainnet: livenet,
testnet: testnet,
get: get
get: get,
all: all
};

View File

@ -157,14 +157,18 @@ PrivateKey._transformBuffer = function(buf, network) {
}
info.network = Networks.get(buf[0], 'privatekey');
if (buf[0] === Networks.livenet.privatekey) {
info.network = Networks.livenet;
} else if (buf[0] === Networks.testnet.privatekey) {
info.network = Networks.testnet;
} else {
var allNetworks = Networks.all();
var matches = _.filter( allNetworks, function( network) {
return buf[0] === network.privatekey;
});
if (matches.length !== 1) {
throw new Error('Invalid network');
}
info.network = matches[0];
if (network && info.network !== Networks.get(network)) {
throw new TypeError('Private key network mismatch');
}

View File

@ -48,7 +48,31 @@ describe('Networks', function() {
var net = networks.get('customnet');
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() {
var custom = {
name: 'somenet',

View File

@ -44,6 +44,35 @@ describe('PrivateKey', function() {
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() {
var a = new PrivateKey(null, Networks.testnet);
should.exist(a);