Browse Source

Fix bug on hdprivate key generation

patch-2
eordano 10 years ago
parent
commit
89d78019d8
  1. 5
      lib/hdprivatekey.js
  2. 21
      test/hdprivatekey.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,

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);
});

Loading…
Cancel
Save