Browse Source

Merge pull request #1163 from eordano/fix/selectNetwork

Improvements to Networks.get
patch-2
Manuel Aráoz 10 years ago
parent
commit
d9d088a0fc
  1. 2
      lib/hdprivatekey.js
  2. 11
      lib/networks.js
  3. 6
      test/networks.js

2
lib/hdprivatekey.js

@ -512,7 +512,7 @@ HDPrivateKey.prototype.inspect = function() {
*/ */
HDPrivateKey.prototype.toObject = function toObject() { HDPrivateKey.prototype.toObject = function toObject() {
return { return {
network: Network.get(BufferUtil.integerFromBuffer(this._buffers.version)).name, network: Network.get(BufferUtil.integerFromBuffer(this._buffers.version), 'xprivkey').name,
depth: BufferUtil.integerFromSingleByteBuffer(this._buffers.depth), depth: BufferUtil.integerFromSingleByteBuffer(this._buffers.depth),
fingerPrint: BufferUtil.integerFromBuffer(this.fingerPrint), fingerPrint: BufferUtil.integerFromBuffer(this.fingerPrint),
parentFingerPrint: BufferUtil.integerFromBuffer(this._buffers.parentFingerPrint), parentFingerPrint: BufferUtil.integerFromBuffer(this._buffers.parentFingerPrint),

11
lib/networks.js

@ -22,16 +22,19 @@ Network.prototype.toString = function toString() {
* @member Networks#get * @member Networks#get
* Retrieves the network associated with a magic number or string. * Retrieves the network associated with a magic number or string.
* @param {string|number|Network} arg * @param {string|number|Network} arg
* @param {string} key - if set, only check if the magic number associated with this name matches * @param {string|Array} keys - if set, only check if the magic number associated with this name matches
* @return Network * @return Network
*/ */
function getNetwork(arg, key) { function getNetwork(arg, keys) {
if (~networks.indexOf(arg)) { if (~networks.indexOf(arg)) {
return arg; return arg;
} }
if (key) { if (keys) {
if (!_.isArray(keys)) {
keys = [keys];
}
for (var index in networks) { for (var index in networks) {
if (networks[index][key] === arg) { if (_.any(keys, function(key) { return networks[index][key] === arg; })) {
return networks[index]; return networks[index];
} }
} }

6
test/networks.js

@ -83,6 +83,12 @@ describe('Networks', function() {
expect(networks.get(0x6f, 'privatekey')).to.equal(undefined); expect(networks.get(0x6f, 'privatekey')).to.equal(undefined);
}); });
it('can test for multiple keys', function() {
expect(networks.get(0x6f, ['pubkeyhash', 'scripthash'])).to.equal(networks.testnet);
expect(networks.get(0xc4, ['pubkeyhash', 'scripthash'])).to.equal(networks.testnet);
expect(networks.get(0x6f, ['privatekey', 'port'])).to.equal(undefined);
});
it('converts to string using the "name" property', function() { it('converts to string using the "name" property', function() {
networks.livenet.toString().should.equal('livenet'); networks.livenet.toString().should.equal('livenet');
}); });

Loading…
Cancel
Save