|
|
@ -12,8 +12,6 @@ var MASTER_SECRET = new Buffer('Bitcoin seed') |
|
|
|
var HARDENED_OFFSET = 0x80000000 |
|
|
|
var LEN = 78 |
|
|
|
|
|
|
|
var N = ecparams.params.n |
|
|
|
|
|
|
|
//Bitcoin hardcoded by default, can use package `coininfo` for others
|
|
|
|
var BITCOIN_VERSIONS = {private: 0x0488ADE4, public: 0x0488B21E} |
|
|
|
|
|
|
@ -56,13 +54,13 @@ Object.defineProperty(HDKey.prototype, 'publicKey', { |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
Object.defineProperty(HDKey.prototype, 'privateOld', { |
|
|
|
Object.defineProperty(HDKey.prototype, 'privateExtendedKey', { |
|
|
|
get: function() { |
|
|
|
return serialize(this, this.versions.private, Buffer.concat([new Buffer([0]), this.privateKey])) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
Object.defineProperty(HDKey.prototype, 'publicOld', { |
|
|
|
Object.defineProperty(HDKey.prototype, 'publicExtendedKey', { |
|
|
|
get: function() { |
|
|
|
return serialize(this, this.versions.public, this.publicKey) |
|
|
|
} |
|
|
@ -74,7 +72,7 @@ HDKey.prototype.derive = function(path) { |
|
|
|
return this |
|
|
|
|
|
|
|
var entries = path.split('/') |
|
|
|
var hkey = this |
|
|
|
var hdkey = this |
|
|
|
entries.forEach(function(c, i) { |
|
|
|
if (i == 0) { |
|
|
|
assert(c, 'm', 'Invalid path') |
|
|
@ -86,10 +84,10 @@ HDKey.prototype.derive = function(path) { |
|
|
|
assert(childIndex < HARDENED_OFFSET, 'Invalid index') |
|
|
|
if (hardened) childIndex += HARDENED_OFFSET |
|
|
|
|
|
|
|
hkey = hkey.deriveChild(childIndex) |
|
|
|
hdkey = hdkey.deriveChild(childIndex) |
|
|
|
}) |
|
|
|
|
|
|
|
return hkey |
|
|
|
return hdkey |
|
|
|
} |
|
|
|
|
|
|
|
HDKey.prototype.deriveChild = function(index) { |
|
|
@ -124,10 +122,10 @@ HDKey.prototype.deriveChild = function(index) { |
|
|
|
// Private parent key -> private child key
|
|
|
|
if (this.privateKey) { |
|
|
|
// ki = parse256(IL) + kpar (mod n)
|
|
|
|
var ki = pIL.add(BigInteger.fromBuffer(this.privateKey)).mod(N) |
|
|
|
var ki = pIL.add(BigInteger.fromBuffer(this.privateKey)).mod(ecparams.params.n) |
|
|
|
|
|
|
|
// In case parse256(IL) >= n or ki == 0, one should proceed with the next value for i
|
|
|
|
if (pIL.compareTo(N) >= 0 || ki.signum() === 0) { |
|
|
|
if (pIL.compareTo(ecparams.params.n) >= 0 || ki.signum() === 0) { |
|
|
|
return this.derive(index + 1) |
|
|
|
} |
|
|
|
|
|
|
|