Browse Source

Merge pull request #1104 from eordano/fix/1095

Fix bug on HDKeys not returning the correct network when requesting a PrivateKey/PublicKey from the HDKey
patch-2
Manuel Aráoz 10 years ago
parent
commit
0031ae8089
  1. 5
      lib/hdprivatekey.js
  2. 5
      lib/hdpublickey.js
  3. 6
      lib/publickey.js
  4. 21
      test/hdprivatekey.js
  5. 12
      test/hdpublickey.js

5
lib/hdprivatekey.js

@ -404,18 +404,19 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) {
}
}
var network = Network.get(BufferUtil.integerFromBuffer(arg.version));
var xprivkey;
xprivkey = Base58Check.encode(buffer.Buffer.concat(sequence));
arg.xprivkey = new Buffer(xprivkey);
var privateKey = new PrivateKey(BN.fromBuffer(arg.privateKey));
var privateKey = new PrivateKey(BN.fromBuffer(arg.privateKey), network);
var publicKey = privateKey.toPublicKey();
var size = HDPrivateKey.ParentFingerPrintSize;
var fingerPrint = Hash.sha256ripemd160(publicKey.toBuffer()).slice(0, size);
JSUtil.defineImmutable(this, {
xprivkey: xprivkey,
network: Network.get(BufferUtil.integerFromBuffer(arg.version)),
network: network,
depth: BufferUtil.integerFromSingleByteBuffer(arg.depth),
privateKey: privateKey,
publicKey: publicKey,

5
lib/hdpublickey.js

@ -318,18 +318,19 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) {
throw new errors.InvalidB58Checksum(concat, checksum);
}
}
var network = Network.get(BufferUtil.integerFromBuffer(arg.version));
var xpubkey;
xpubkey = Base58Check.encode(BufferUtil.concat(sequence));
arg.xpubkey = new Buffer(xpubkey);
var publicKey = PublicKey.fromString(arg.publicKey);
var publicKey = new PublicKey(arg.publicKey, {network: network});
var size = HDPublicKey.ParentFingerPrintSize;
var fingerPrint = Hash.sha256ripemd160(publicKey.toBuffer()).slice(0, size);
JSUtil.defineImmutable(this, {
xpubkey: xpubkey,
network: Network.get(BufferUtil.integerFromBuffer(arg.version)),
network: network,
depth: BufferUtil.integerFromSingleByteBuffer(arg.depth),
publicKey: publicKey,
fingerPrint: fingerPrint

6
lib/publickey.js

@ -70,8 +70,7 @@ var PublicKey = function PublicKey(data, extra) {
PublicKey.prototype._classifyArgs = function(data, extra) {
/* jshint maxcomplexity: 10 */
var info = {
compressed: _.isUndefined(extra.compressed) || extra.compressed,
network: _.isUndefined(extra.network) ? undefined : Network.get(extra.network)
compressed: _.isUndefined(extra.compressed) || extra.compressed
};
// detect type of data
@ -88,6 +87,9 @@ PublicKey.prototype._classifyArgs = function(data, extra) {
} else {
throw new TypeError('First argument is an unrecognized data format.');
}
if (!info.network) {
info.network = _.isUndefined(extra.network) ? undefined : Network.get(extra.network);
}
return info;
};

21
test/hdprivatekey.js

@ -8,6 +8,7 @@ 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;
var Base58Check = bitcore.encoding.Base58Check;
@ -85,17 +86,25 @@ describe('HDPrivate key interface', function() {
it('allows no-new calling', function() {
HDPrivateKey(xprivkey).toString().should.equal(xprivkey);
});
it('allows the use of a copy constructor', function() {
HDPrivateKey(HDPrivateKey(xprivkey))
.xprivkey.should.equal(xprivkey);
});
});
it('inspect() displays correctly', function() {
HDPrivateKey(xprivkey).inspect().should.equal('<HDPrivateKey: ' + xprivkey + '>');
});
describe('public key', function() {
var testnetKey = new HDPrivateKey('tprv8ZgxMBicQKsPdEeU2KiGFnUgRGriMnQxrwrg6FWCBg4jeiidHRyCCdA357kfkZiGaXEapWZsGDKikeeEbvgXo3UmEdbEKNdQH9VXESmGuUK');
var livenetKey = new HDPrivateKey('xprv9s21ZrQH143K3e39bnn1vyS7YFa1EAJAFGDoeHaSBsgBxgAkTEXeSx7xLvhNQNJxJwhzziWcK3znUFKRPRwWBPkKZ8ijUBa5YYpYPQmeBDX');
it('allows the use of a copy constructor', function() {
HDPrivateKey(HDPrivateKey(xprivkey))
.xprivkey.should.equal(xprivkey);
it('matches the network', function() {
testnetKey.publicKey.network.should.equal(Networks.testnet);
livenetKey.publicKey.network.should.equal(Networks.livenet);
});
});
it('inspect() displays correctly', function() {
HDPrivateKey(xprivkey).inspect().should.equal('<HDPrivateKey: ' + xprivkey + '>');
});
it('fails when trying to derive with an invalid argument', function() {
expectDerivationFail([], hdErrors.InvalidDerivationArgument);
});

12
test/hdpublickey.js

@ -13,9 +13,11 @@ var BufferUtil = bitcore.util.buffer;
var HDPrivateKey = bitcore.HDPrivateKey;
var HDPublicKey = bitcore.HDPublicKey;
var Base58Check = bitcore.encoding.Base58Check;
var Networks = bitcore.Networks;
var xprivkey = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
var xpubkey = 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8';
var xpubkeyTestnet = 'tpubD6NzVbkrYhZ4WZaiWHz59q5EQ61bd6dUYfU4ggRWAtNAyyYRNWT6ktJ7UHJEXURvTfTfskFQmK7Ff4FRkiRN5wQH8nkGAb6aKB4Yyeqsw5m';
var json = '{"network":"livenet","depth":0,"fingerPrint":876747070,"parentFingerPrint":0,"childIndex":0,"chainCode":"873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508","publicKey":"0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2","checksum":-1421395167,"xpubkey":"xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"}';
var derived_0_1_200000 = 'xpub6BqyndF6rkBNTV6LXwiY8Pco8aqctqq7tGEUdA8fmGDTnDJphn2fmxr3eM8Lm3m8TrNUsLbEjHvpa3adBU18YpEx4tp2Zp6nqax3mQkudhX';
@ -31,7 +33,7 @@ describe('HDPublicKey interface', function() {
got = e instanceof errorType;
}
if (!error instanceof errorType) {
console.log('Adsasd', typeof error);
console.log('Error', typeof error);
}
// expect(got).to.equal(true);
};
@ -147,6 +149,14 @@ describe('HDPublicKey interface', function() {
pubKey.toString().should.equal(pubKey.xpubkey);
});
it('publicKey property matches network', function() {
var livenet = new HDPublicKey(xpubkey);
var testnet = new HDPublicKey(xpubkeyTestnet);
livenet.publicKey.network.should.equal(Networks.livenet);
testnet.publicKey.network.should.equal(Networks.testnet);
});
it('inspect() displays correctly', function() {
var pubKey = new HDPublicKey(xpubkey);
pubKey.inspect().should.equal('<HDPublicKey: ' + pubKey.xpubkey + '>');

Loading…
Cancel
Save