Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit 1c8fe1e

Browse files
Braydon Fullermatiu
authored andcommitted
Handle invalid public key derivation rather than throw error
1 parent f42bbd3 commit 1c8fe1e

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/hdpublickey.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,12 @@ HDPublicKey.prototype._deriveWithNumber = function(index, hardened) {
136136
var leftPart = BN.fromBuffer(hash.slice(0, 32), {size: 32});
137137
var chainCode = hash.slice(32, 64);
138138

139-
var publicKey = PublicKey.fromPoint(Point.getG().mul(leftPart).add(this.publicKey.point));
139+
var publicKey;
140+
try {
141+
publicKey = PublicKey.fromPoint(Point.getG().mul(leftPart).add(this.publicKey.point));
142+
} catch (e) {
143+
return this._deriveWithNumber(index + 1);
144+
}
140145

141146
var derived = new HDPublicKey({
142147
network: this.network,

test/hdkeys.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,27 @@ describe('BIP32 compliance', function() {
288288
derived.privateKey.toString().should.equal('b15bce3608d607ee3a49069197732c656bca942ee59f3e29b4d56914c1de6825');
289289
bitcore.PrivateKey.isValid.callCount.should.equal(2);
290290
});
291+
it('will handle edge case that a derive public key is invalid', function() {
292+
var publicKeyBuffer = new Buffer('029e58b241790284ef56502667b15157b3fc58c567f044ddc35653860f9455d099', 'hex');
293+
var chainCodeBuffer = new Buffer('39816057bba9d952fe87fe998b7fd4d690a1bb58c2ff69141469e4d1dffb4b91', 'hex');
294+
var key = new HDPublicKey({
295+
network: 'testnet',
296+
depth: 0,
297+
parentFingerPrint: 0,
298+
childIndex: 0,
299+
chainCode: chainCodeBuffer,
300+
publicKey: publicKeyBuffer
301+
});
302+
var unstubbed = bitcore.PublicKey.fromPoint;
303+
bitcore.PublicKey.fromPoint = function() {
304+
bitcore.PublicKey.fromPoint = unstubbed;
305+
throw new Error('Point cannot be equal to Infinity');
306+
};
307+
sandbox.spy(key, '_deriveWithNumber');
308+
var derived = key.derive("m/44");
309+
key._deriveWithNumber.callCount.should.equal(2);
310+
key.publicKey.toString().should.equal('029e58b241790284ef56502667b15157b3fc58c567f044ddc35653860f9455d099');
311+
});
291312
});
292313

293314
describe('seed', function() {

0 commit comments

Comments
 (0)