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