hd: cleanup some methods.
This commit is contained in:
parent
11a660aeab
commit
7089735fb0
@ -70,11 +70,11 @@ function HDPrivateKey(options) {
|
|||||||
HDPrivateKey.prototype.fromOptions = function fromOptions(options) {
|
HDPrivateKey.prototype.fromOptions = function fromOptions(options) {
|
||||||
assert(options, 'No options for HD private key.');
|
assert(options, 'No options for HD private key.');
|
||||||
assert(util.isNumber(options.depth));
|
assert(util.isNumber(options.depth));
|
||||||
|
assert(options.depth >= 0 && options.depth <= 0xff);
|
||||||
assert(Buffer.isBuffer(options.parentFingerPrint));
|
assert(Buffer.isBuffer(options.parentFingerPrint));
|
||||||
assert(util.isNumber(options.childIndex));
|
assert(util.isNumber(options.childIndex));
|
||||||
assert(Buffer.isBuffer(options.chainCode));
|
assert(Buffer.isBuffer(options.chainCode));
|
||||||
assert(Buffer.isBuffer(options.privateKey));
|
assert(Buffer.isBuffer(options.privateKey));
|
||||||
assert(options.depth <= 0xff, 'Depth is too high.');
|
|
||||||
|
|
||||||
if (options.network)
|
if (options.network)
|
||||||
this.network = Network.get(options.network);
|
this.network = Network.get(options.network);
|
||||||
@ -179,15 +179,12 @@ HDPrivateKey.prototype.destroy = function destroy(pub) {
|
|||||||
HDPrivateKey.prototype.derive = function derive(index, hardened) {
|
HDPrivateKey.prototype.derive = function derive(index, hardened) {
|
||||||
var bw, id, data, hash, left, right, key, child;
|
var bw, id, data, hash, left, right, key, child;
|
||||||
|
|
||||||
if (typeof index === 'string')
|
assert(typeof index === 'number');
|
||||||
return this.derivePath(index);
|
|
||||||
|
|
||||||
hardened = index >= common.HARDENED ? true : hardened;
|
if (hardened && index < common.HARDENED)
|
||||||
|
|
||||||
if (index < common.HARDENED && hardened)
|
|
||||||
index += common.HARDENED;
|
index += common.HARDENED;
|
||||||
|
|
||||||
if (!(index >= 0 && index < common.MAX_INDEX))
|
if (index < 0 || index >= common.MAX_INDEX)
|
||||||
throw new Error('Index out of range.');
|
throw new Error('Index out of range.');
|
||||||
|
|
||||||
if (this.depth >= 0xff)
|
if (this.depth >= 0xff)
|
||||||
@ -201,7 +198,7 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) {
|
|||||||
|
|
||||||
bw = new StaticWriter(37);
|
bw = new StaticWriter(37);
|
||||||
|
|
||||||
if (hardened) {
|
if (index >= common.HARDENED) {
|
||||||
bw.writeU8(0);
|
bw.writeU8(0);
|
||||||
bw.writeBytes(this.privateKey);
|
bw.writeBytes(this.privateKey);
|
||||||
bw.writeU32BE(index);
|
bw.writeU32BE(index);
|
||||||
@ -422,8 +419,7 @@ HDPrivateKey.prototype.equal = function equal(obj) {
|
|||||||
HDPrivateKey.prototype.compare = function compare(key) {
|
HDPrivateKey.prototype.compare = function compare(key) {
|
||||||
var cmp;
|
var cmp;
|
||||||
|
|
||||||
if (!HDPrivateKey.isHDPrivateKey(key))
|
assert(HDPrivateKey.isHDPrivateKey(key));
|
||||||
return 1;
|
|
||||||
|
|
||||||
cmp = this.depth - key.depth;
|
cmp = this.depth - key.depth;
|
||||||
|
|
||||||
@ -471,7 +467,6 @@ HDPrivateKey.prototype.fromSeed = function fromSeed(seed, network) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hash = crypto.hmac('sha512', seed, common.SEED_SALT);
|
hash = crypto.hmac('sha512', seed, common.SEED_SALT);
|
||||||
|
|
||||||
left = hash.slice(0, 32);
|
left = hash.slice(0, 32);
|
||||||
right = hash.slice(32, 64);
|
right = hash.slice(32, 64);
|
||||||
|
|
||||||
@ -609,7 +604,7 @@ HDPrivateKey.prototype.fromBase58 = function fromBase58(xkey, network) {
|
|||||||
/**
|
/**
|
||||||
* Inject properties from serialized data.
|
* Inject properties from serialized data.
|
||||||
* @private
|
* @private
|
||||||
* @param {Buffer} raw
|
* @param {BufferReader} br
|
||||||
* @param {(Network|NetworkType)?} network
|
* @param {(Network|NetworkType)?} network
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -621,7 +616,7 @@ HDPrivateKey.prototype.fromReader = function fromReader(br, network) {
|
|||||||
this.parentFingerPrint = br.readBytes(4);
|
this.parentFingerPrint = br.readBytes(4);
|
||||||
this.childIndex = br.readU32BE();
|
this.childIndex = br.readU32BE();
|
||||||
this.chainCode = br.readBytes(32);
|
this.chainCode = br.readBytes(32);
|
||||||
br.readU8();
|
assert(br.readU8() === 0);
|
||||||
this.privateKey = br.readBytes(32);
|
this.privateKey = br.readBytes(32);
|
||||||
this.publicKey = ec.publicKeyCreate(this.privateKey, true);
|
this.publicKey = ec.publicKeyCreate(this.privateKey, true);
|
||||||
|
|
||||||
@ -633,12 +628,12 @@ HDPrivateKey.prototype.fromReader = function fromReader(br, network) {
|
|||||||
/**
|
/**
|
||||||
* Inject properties from serialized data.
|
* Inject properties from serialized data.
|
||||||
* @private
|
* @private
|
||||||
* @param {Buffer} raw
|
* @param {Buffer} data
|
||||||
* @param {(Network|NetworkType)?} network
|
* @param {(Network|NetworkType)?} network
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HDPrivateKey.prototype.fromRaw = function fromRaw(raw, network) {
|
HDPrivateKey.prototype.fromRaw = function fromRaw(data, network) {
|
||||||
return this.fromReader(new BufferReader(raw), network);
|
return this.fromReader(new BufferReader(data), network);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -718,13 +713,13 @@ HDPrivateKey.fromReader = function fromReader(br, network) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate key from serialized data.
|
* Instantiate key from serialized data.
|
||||||
* @param {Buffer} raw
|
* @param {Buffer} data
|
||||||
* @param {(Network|NetworkType)?} network
|
* @param {(Network|NetworkType)?} network
|
||||||
* @returns {HDPrivateKey}
|
* @returns {HDPrivateKey}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HDPrivateKey.fromRaw = function fromRaw(raw, network) {
|
HDPrivateKey.fromRaw = function fromRaw(data, network) {
|
||||||
return new HDPrivateKey().fromRaw(raw, network);
|
return new HDPrivateKey().fromRaw(data, network);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -64,6 +64,7 @@ function HDPublicKey(options) {
|
|||||||
HDPublicKey.prototype.fromOptions = function fromOptions(options) {
|
HDPublicKey.prototype.fromOptions = function fromOptions(options) {
|
||||||
assert(options, 'No options for HDPublicKey');
|
assert(options, 'No options for HDPublicKey');
|
||||||
assert(util.isNumber(options.depth));
|
assert(util.isNumber(options.depth));
|
||||||
|
assert(options.depth >= 0 && options.depth <= 0xff);
|
||||||
assert(Buffer.isBuffer(options.parentFingerPrint));
|
assert(Buffer.isBuffer(options.parentFingerPrint));
|
||||||
assert(util.isNumber(options.childIndex));
|
assert(util.isNumber(options.childIndex));
|
||||||
assert(Buffer.isBuffer(options.chainCode));
|
assert(Buffer.isBuffer(options.chainCode));
|
||||||
@ -152,11 +153,10 @@ HDPublicKey.prototype.destroy = function destroy() {
|
|||||||
HDPublicKey.prototype.derive = function derive(index, hardened) {
|
HDPublicKey.prototype.derive = function derive(index, hardened) {
|
||||||
var bw, id, data, hash, left, right, key, child;
|
var bw, id, data, hash, left, right, key, child;
|
||||||
|
|
||||||
if (typeof index === 'string')
|
assert(typeof index === 'number');
|
||||||
return this.derivePath(index);
|
|
||||||
|
|
||||||
if (index >= common.HARDENED || hardened)
|
if (index >= common.HARDENED || hardened)
|
||||||
throw new Error('Index out of range.');
|
throw new Error('Cannot derive hardened.');
|
||||||
|
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
throw new Error('Index out of range.');
|
throw new Error('Index out of range.');
|
||||||
@ -335,8 +335,7 @@ HDPublicKey.prototype.equal = function equal(obj) {
|
|||||||
HDPublicKey.prototype.compare = function compare(key) {
|
HDPublicKey.prototype.compare = function compare(key) {
|
||||||
var cmp;
|
var cmp;
|
||||||
|
|
||||||
if (!HDPublicKey.isHDPublicKey(key))
|
assert(HDPublicKey.isHDPublicKey(key));
|
||||||
return 1;
|
|
||||||
|
|
||||||
cmp = this.depth - key.depth;
|
cmp = this.depth - key.depth;
|
||||||
|
|
||||||
@ -469,7 +468,7 @@ HDPublicKey.prototype.fromBase58 = function fromBase58(xkey, network) {
|
|||||||
/**
|
/**
|
||||||
* Inject properties from serialized data.
|
* Inject properties from serialized data.
|
||||||
* @private
|
* @private
|
||||||
* @param {Buffer} raw
|
* @param {BufferReader} br
|
||||||
* @param {(Network|NetworkType)?} network
|
* @param {(Network|NetworkType)?} network
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -491,12 +490,12 @@ HDPublicKey.prototype.fromReader = function fromReader(br, network) {
|
|||||||
/**
|
/**
|
||||||
* Inject properties from serialized data.
|
* Inject properties from serialized data.
|
||||||
* @private
|
* @private
|
||||||
* @param {Buffer} raw
|
* @param {Buffer} data
|
||||||
* @param {(Network|NetworkType)?} network
|
* @param {(Network|NetworkType)?} network
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HDPublicKey.prototype.fromRaw = function fromRaw(raw, network) {
|
HDPublicKey.prototype.fromRaw = function fromRaw(data, network) {
|
||||||
return this.fromReader(new BufferReader(raw), network);
|
return this.fromReader(new BufferReader(data), network);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -575,7 +574,7 @@ HDPublicKey.fromReader = function fromReader(br, network) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate key from serialized data.
|
* Instantiate key from serialized data.
|
||||||
* @param {Buffer} raw
|
* @param {Buffer} data
|
||||||
* @param {(Network|NetworkType)?} network
|
* @param {(Network|NetworkType)?} network
|
||||||
* @returns {HDPublicKey}
|
* @returns {HDPublicKey}
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -107,7 +107,7 @@ describe('HD', function() {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
it('should derive ' + path + ' from master', function() {
|
it('should derive ' + path + ' from master', function() {
|
||||||
var key = master.derive(path);
|
var key = master.derivePath(path);
|
||||||
equal(key.toBase58(), kp.prv);
|
equal(key.toBase58(), kp.prv);
|
||||||
equal(key.toPublic().toBase58(), kp.pub);
|
equal(key.toPublic().toBase58(), kp.pub);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -159,7 +159,7 @@ describe('Wallet', function() {
|
|||||||
|
|
||||||
script = Script.fromMultisig(1, 2, [
|
script = Script.fromMultisig(1, 2, [
|
||||||
w.account.receive.getPublicKey(),
|
w.account.receive.getPublicKey(),
|
||||||
k.derive('m/0/0').publicKey
|
k.derivePath('m/0/0').publicKey
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Input transaction (bare 1-of-2 multisig)
|
// Input transaction (bare 1-of-2 multisig)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user