Merge pull request #1317 from braydonf/hdpubkey-hardened-error
Restore error when trying to derive a hardened path with HDPublicKey.
This commit is contained in:
commit
095cf32966
|
@ -164,6 +164,9 @@ module.exports = [{
|
||||||
}, {
|
}, {
|
||||||
name: 'InvalidPath',
|
name: 'InvalidPath',
|
||||||
message: 'Invalid derivation path, it should look like: "m/1/100", got "{0}"'
|
message: 'Invalid derivation path, it should look like: "m/1/100", got "{0}"'
|
||||||
|
}, {
|
||||||
|
name: 'InvalidIndexCantDeriveHardened',
|
||||||
|
message: 'Invalid argument: creating a hardened path requires an HDPrivateKey'
|
||||||
}, {
|
}, {
|
||||||
name: 'MustSupplyArgument',
|
name: 'MustSupplyArgument',
|
||||||
message: 'Must supply an argument to create a HDPublicKey'
|
message: 'Must supply an argument to create a HDPublicKey'
|
||||||
|
|
|
@ -108,9 +108,9 @@ HDPublicKey.isValidPath = function(arg) {
|
||||||
*
|
*
|
||||||
* @param {string|number} arg
|
* @param {string|number} arg
|
||||||
*/
|
*/
|
||||||
HDPublicKey.prototype.derive = function (arg) {
|
HDPublicKey.prototype.derive = function(arg, hardened) {
|
||||||
if (_.isNumber(arg)) {
|
if (_.isNumber(arg)) {
|
||||||
return this._deriveWithNumber(arg);
|
return this._deriveWithNumber(arg, hardened);
|
||||||
} else if (_.isString(arg)) {
|
} else if (_.isString(arg)) {
|
||||||
return this._deriveFromString(arg);
|
return this._deriveFromString(arg);
|
||||||
} else {
|
} else {
|
||||||
|
@ -118,8 +118,8 @@ HDPublicKey.prototype.derive = function (arg) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
HDPublicKey.prototype._deriveWithNumber = function (index) {
|
HDPublicKey.prototype._deriveWithNumber = function(index, hardened) {
|
||||||
if (index >= HDPublicKey.Hardened) {
|
if (index >= HDPublicKey.Hardened || hardened) {
|
||||||
throw new hdErrors.InvalidIndexCantDeriveHardened();
|
throw new hdErrors.InvalidIndexCantDeriveHardened();
|
||||||
}
|
}
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
|
@ -203,7 +203,7 @@ HDPublicKey.getSerializedError = function (data, network) {
|
||||||
return new errors.InvalidB58Checksum(data);
|
return new errors.InvalidB58Checksum(data);
|
||||||
}
|
}
|
||||||
if (data.length !== HDPublicKey.DataSize) {
|
if (data.length !== HDPublicKey.DataSize) {
|
||||||
return new errors.InvalidLength(data);
|
return new hdErrors.InvalidLength(data);
|
||||||
}
|
}
|
||||||
if (!_.isUndefined(network)) {
|
if (!_.isUndefined(network)) {
|
||||||
var error = HDPublicKey._validateNetwork(data, network);
|
var error = HDPublicKey._validateNetwork(data, network);
|
||||||
|
|
|
@ -24,31 +24,22 @@ var derived_0_1_200000 = 'xpub6BqyndF6rkBNTV6LXwiY8Pco8aqctqq7tGEUdA8fmGDTnDJphn
|
||||||
describe('HDPublicKey interface', function() {
|
describe('HDPublicKey interface', function() {
|
||||||
|
|
||||||
var expectFail = function(func, errorType) {
|
var expectFail = function(func, errorType) {
|
||||||
var got = null;
|
(function() {
|
||||||
var error = null;
|
|
||||||
try {
|
|
||||||
func();
|
func();
|
||||||
} catch (e) {
|
}).should.throw(errorType);
|
||||||
error = e;
|
|
||||||
got = e instanceof errorType;
|
|
||||||
}
|
|
||||||
if (!error instanceof errorType) {
|
|
||||||
console.log('Error', typeof error);
|
|
||||||
}
|
|
||||||
// expect(got).to.equal(true);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var expectDerivationFail = function(argument, error) {
|
var expectDerivationFail = function(argument, error) {
|
||||||
return expectFail(function() {
|
(function() {
|
||||||
var pubkey = new HDPublicKey(xpubkey);
|
var pubkey = new HDPublicKey(xpubkey);
|
||||||
xpubkey.derive(argument);
|
pubkey.derive(argument);
|
||||||
}, error);
|
}).should.throw(error);
|
||||||
};
|
};
|
||||||
|
|
||||||
var expectFailBuilding = function(argument, error) {
|
var expectFailBuilding = function(argument, error) {
|
||||||
return expectFail(function() {
|
(function() {
|
||||||
return new HDPublicKey(argument);
|
return new HDPublicKey(argument);
|
||||||
}, error);
|
}).should.throw(error);
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('creation formats', function() {
|
describe('creation formats', function() {
|
||||||
|
@ -225,16 +216,22 @@ describe('HDPublicKey interface', function() {
|
||||||
|
|
||||||
it('doesn\'t allow other parameters like m\' or M\' or "s"', function() {
|
it('doesn\'t allow other parameters like m\' or M\' or "s"', function() {
|
||||||
/* jshint quotmark: double */
|
/* jshint quotmark: double */
|
||||||
expectDerivationFail("m'", hdErrors.InvalidDerivationArgument);
|
expectDerivationFail("m'", hdErrors.InvalidIndexCantDeriveHardened);
|
||||||
expectDerivationFail("M'", hdErrors.InvalidDerivationArgument);
|
expectDerivationFail("M'", hdErrors.InvalidIndexCantDeriveHardened);
|
||||||
expectDerivationFail("1", hdErrors.InvalidDerivationArgument);
|
expectDerivationFail("1", hdErrors.InvalidPath);
|
||||||
expectDerivationFail("S", hdErrors.InvalidDerivationArgument);
|
expectDerivationFail("S", hdErrors.InvalidPath);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can\'t derive hardened keys', function() {
|
it('can\'t derive hardened keys', function() {
|
||||||
expectFail(function() {
|
expectFail(function() {
|
||||||
return new HDPublicKey(xpubkey).derive(HDPublicKey.Hardened);
|
return new HDPublicKey(xpubkey).derive(HDPublicKey.Hardened);
|
||||||
}, hdErrors.InvalidDerivationArgument);
|
}, hdErrors.InvalidIndexCantDeriveHardened);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can\'t derive hardened keys via second argument', function() {
|
||||||
|
expectFail(function() {
|
||||||
|
return new HDPublicKey(xpubkey).derive(5, true);
|
||||||
|
}, hdErrors.InvalidIndexCantDeriveHardened);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('validates correct paths', function() {
|
it('validates correct paths', function() {
|
||||||
|
|
Loading…
Reference in New Issue