|
@ -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) { |
|
@ -150,7 +150,7 @@ HDPublicKey.prototype._deriveWithNumber = function (index) { |
|
|
return derived; |
|
|
return derived; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
HDPublicKey.prototype._deriveFromString = function (path) { |
|
|
HDPublicKey.prototype._deriveFromString = function(path) { |
|
|
/* jshint maxcomplexity: 8 */ |
|
|
/* jshint maxcomplexity: 8 */ |
|
|
if (_.contains(path, "'")) { |
|
|
if (_.contains(path, "'")) { |
|
|
throw new hdErrors.InvalidIndexCantDeriveHardened(); |
|
|
throw new hdErrors.InvalidIndexCantDeriveHardened(); |
|
@ -175,7 +175,7 @@ HDPublicKey.prototype._deriveFromString = function (path) { |
|
|
* network provided matches the network serialized. |
|
|
* network provided matches the network serialized. |
|
|
* @return {boolean} |
|
|
* @return {boolean} |
|
|
*/ |
|
|
*/ |
|
|
HDPublicKey.isValidSerialized = function (data, network) { |
|
|
HDPublicKey.isValidSerialized = function(data, network) { |
|
|
return _.isNull(HDPublicKey.getSerializedError(data, network)); |
|
|
return _.isNull(HDPublicKey.getSerializedError(data, network)); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -188,7 +188,7 @@ HDPublicKey.isValidSerialized = function (data, network) { |
|
|
* network provided matches the network serialized. |
|
|
* network provided matches the network serialized. |
|
|
* @return {errors|null} |
|
|
* @return {errors|null} |
|
|
*/ |
|
|
*/ |
|
|
HDPublicKey.getSerializedError = function (data, network) { |
|
|
HDPublicKey.getSerializedError = function(data, network) { |
|
|
/* jshint maxcomplexity: 10 */ |
|
|
/* jshint maxcomplexity: 10 */ |
|
|
/* jshint maxstatements: 20 */ |
|
|
/* jshint maxstatements: 20 */ |
|
|
if (!(_.isString(data) || BufferUtil.isBuffer(data))) { |
|
|
if (!(_.isString(data) || BufferUtil.isBuffer(data))) { |
|
@ -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); |
|
@ -218,7 +218,7 @@ HDPublicKey.getSerializedError = function (data, network) { |
|
|
return null; |
|
|
return null; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
HDPublicKey._validateNetwork = function (data, networkArg) { |
|
|
HDPublicKey._validateNetwork = function(data, networkArg) { |
|
|
var network = Network.get(networkArg); |
|
|
var network = Network.get(networkArg); |
|
|
if (!network) { |
|
|
if (!network) { |
|
|
return new errors.InvalidNetworkArgument(networkArg); |
|
|
return new errors.InvalidNetworkArgument(networkArg); |
|
@ -241,7 +241,7 @@ HDPublicKey.prototype._buildFromPrivate = function (arg) { |
|
|
return this._buildFromBuffers(args); |
|
|
return this._buildFromBuffers(args); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
HDPublicKey.prototype._buildFromObject = function (arg) { |
|
|
HDPublicKey.prototype._buildFromObject = function(arg) { |
|
|
/* jshint maxcomplexity: 10 */ |
|
|
/* jshint maxcomplexity: 10 */ |
|
|
// TODO: Type validation
|
|
|
// TODO: Type validation
|
|
|
var buffers = { |
|
|
var buffers = { |
|
@ -257,7 +257,7 @@ HDPublicKey.prototype._buildFromObject = function (arg) { |
|
|
return this._buildFromBuffers(buffers); |
|
|
return this._buildFromBuffers(buffers); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
HDPublicKey.prototype._buildFromSerialized = function (arg) { |
|
|
HDPublicKey.prototype._buildFromSerialized = function(arg) { |
|
|
var decoded = Base58Check.decode(arg); |
|
|
var decoded = Base58Check.decode(arg); |
|
|
var buffers = { |
|
|
var buffers = { |
|
|
version: decoded.slice(HDPublicKey.VersionStart, HDPublicKey.VersionEnd), |
|
|
version: decoded.slice(HDPublicKey.VersionStart, HDPublicKey.VersionEnd), |
|
@ -289,7 +289,7 @@ HDPublicKey.prototype._buildFromSerialized = function (arg) { |
|
|
* representation |
|
|
* representation |
|
|
* @return {HDPublicKey} this |
|
|
* @return {HDPublicKey} this |
|
|
*/ |
|
|
*/ |
|
|
HDPublicKey.prototype._buildFromBuffers = function (arg) { |
|
|
HDPublicKey.prototype._buildFromBuffers = function(arg) { |
|
|
/* jshint maxcomplexity: 8 */ |
|
|
/* jshint maxcomplexity: 8 */ |
|
|
/* jshint maxstatements: 20 */ |
|
|
/* jshint maxstatements: 20 */ |
|
|
|
|
|
|
|
@ -333,7 +333,7 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) { |
|
|
return this; |
|
|
return this; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
HDPublicKey._validateBufferArguments = function (arg) { |
|
|
HDPublicKey._validateBufferArguments = function(arg) { |
|
|
var checkBuffer = function(name, size) { |
|
|
var checkBuffer = function(name, size) { |
|
|
var buff = arg[name]; |
|
|
var buff = arg[name]; |
|
|
assert(BufferUtil.isBuffer(buff), name + ' argument is not a buffer, it\'s ' + typeof buff); |
|
|
assert(BufferUtil.isBuffer(buff), name + ' argument is not a buffer, it\'s ' + typeof buff); |
|
@ -367,7 +367,7 @@ HDPublicKey.fromObject = function(arg) { |
|
|
* Returns the base58 checked representation of the public key |
|
|
* Returns the base58 checked representation of the public key |
|
|
* @return {string} a string starting with "xpub..." in livenet |
|
|
* @return {string} a string starting with "xpub..." in livenet |
|
|
*/ |
|
|
*/ |
|
|
HDPublicKey.prototype.toString = function () { |
|
|
HDPublicKey.prototype.toString = function() { |
|
|
return this.xpubkey; |
|
|
return this.xpubkey; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|