|
|
@ -23,10 +23,13 @@ function HDKey(versions) { |
|
|
|
this._privateKeyInteger = BigInteger.ZERO |
|
|
|
this._publicKey = null |
|
|
|
this.chainCode = null |
|
|
|
this._fingerprint = 0 |
|
|
|
this.parentFingerprint = 0 |
|
|
|
} |
|
|
|
|
|
|
|
Object.defineProperty(HDKey.prototype, 'fingerprint', {get: function() { return this.identifier.slice(0, 4) } }) |
|
|
|
Object.defineProperty(HDKey.prototype, 'fingerprint', { get: function() { return this._fingerprint } }) |
|
|
|
Object.defineProperty(HDKey.prototype, 'identifier', {get: function() { return this._identifier } }) |
|
|
|
Object.defineProperty(HDKey.prototype, 'pubKeyHash', {get: function() { return this.identifier }}) |
|
|
|
|
|
|
|
Object.defineProperty(HDKey.prototype, 'privateKey', { |
|
|
|
get: function() { |
|
|
@ -38,6 +41,7 @@ Object.defineProperty(HDKey.prototype, 'privateKey', { |
|
|
|
this._privateKeyInteger = BigInteger.fromBuffer(this._privateKey) |
|
|
|
this._publicKey = ecparams.params.G.multiply(this._privateKeyInteger).getEncoded(true) //force compressed point
|
|
|
|
this._identifier = hash160(this.publicKey) |
|
|
|
this._fingerprint = this._identifier.slice(0, 4).readUInt32BE(0) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
@ -49,6 +53,8 @@ Object.defineProperty(HDKey.prototype, 'publicKey', { |
|
|
|
assert(value.length === 33 || value.length === 65, 'Public key must be 33 or 65 bytes.') |
|
|
|
var pt = Point.decodeFrom(ecparams, value) |
|
|
|
this._publicKey = pt.getEncoded(true) //force compressed point
|
|
|
|
this._identifier = hash160(this.publicKey) |
|
|
|
this._fingerprint = this._identifier.slice(0, 4).readUInt32BE(0) |
|
|
|
this._privateKey = null |
|
|
|
this._privateKeyInteger = null |
|
|
|
} |
|
|
@ -147,7 +153,7 @@ HDKey.prototype.deriveChild = function(index) { |
|
|
|
|
|
|
|
hd.chainCode = IR |
|
|
|
hd.depth = this.depth + 1 |
|
|
|
hd.parentFingerprint = this.fingerprint.readUInt32BE(0) |
|
|
|
hd.parentFingerprint = this.fingerprint//.readUInt32BE(0)
|
|
|
|
hd.index = index |
|
|
|
|
|
|
|
return hd |
|
|
@ -165,6 +171,31 @@ HDKey.fromMasterSeed = function(seedBuffer, versions) { |
|
|
|
return hdkey |
|
|
|
} |
|
|
|
|
|
|
|
HDKey.fromExtendedKey = function(keyBuffer, versions) { |
|
|
|
// => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33)
|
|
|
|
versions = versions || BITCOIN_VERSIONS |
|
|
|
var hdkey = new HDKey(versions) |
|
|
|
|
|
|
|
var version = keyBuffer.readUInt32BE(0) |
|
|
|
assert(version === versions.private || version === versions.public, 'Version mismatch: does not match private or public') |
|
|
|
|
|
|
|
hdkey.depth = keyBuffer.readUInt8(4) |
|
|
|
hdkey.parentFingerprint = keyBuffer.readUInt32BE(5) |
|
|
|
hdkey.index = keyBuffer.readUInt32BE(9) |
|
|
|
hdkey.chainCode = keyBuffer.slice(13, 45) |
|
|
|
|
|
|
|
var key = keyBuffer.slice(45) |
|
|
|
if (key.readUInt8(0) === 0) { //private
|
|
|
|
assert(version === versions.private, 'Version mismatch: version does not match private') |
|
|
|
hdkey.privateKey = key.slice(1) //cut off first 0x0 byte
|
|
|
|
} else { |
|
|
|
assert(version === versions.public, 'Version mismatch: version does not match public') |
|
|
|
hdkey.publicKey = key |
|
|
|
} |
|
|
|
|
|
|
|
return hdkey |
|
|
|
} |
|
|
|
|
|
|
|
function serialize(hdkey, version, key) { |
|
|
|
// => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33)
|
|
|
|
var buffer = new Buffer(LEN) |
|
|
|