option parsing.
This commit is contained in:
parent
158a35881c
commit
d445fb7943
@ -35,7 +35,7 @@ function Address(options) {
|
|||||||
this.label = options.label || '';
|
this.label = options.label || '';
|
||||||
this.derived = !!options.derived;
|
this.derived = !!options.derived;
|
||||||
|
|
||||||
this.key = bcoin.keypair(options);
|
this.key = options.pair || options.key || bcoin.keypair(options);
|
||||||
this.path = options.path;
|
this.path = options.path;
|
||||||
this.change = !!options.change;
|
this.change = !!options.change;
|
||||||
this.index = options.index;
|
this.index = options.index;
|
||||||
|
|||||||
@ -46,8 +46,6 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var hd = exports;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modules
|
* Modules
|
||||||
*/
|
*/
|
||||||
@ -55,6 +53,7 @@ var hd = exports;
|
|||||||
var bcoin = require('../bcoin');
|
var bcoin = require('../bcoin');
|
||||||
var hash = require('hash.js');
|
var hash = require('hash.js');
|
||||||
var bn = require('bn.js');
|
var bn = require('bn.js');
|
||||||
|
var inherits = require('inherits');
|
||||||
var elliptic = require('elliptic');
|
var elliptic = require('elliptic');
|
||||||
var utils = bcoin.utils;
|
var utils = bcoin.utils;
|
||||||
var assert = utils.assert;
|
var assert = utils.assert;
|
||||||
@ -104,6 +103,14 @@ HDSeed._mnemonic = function _mnemonic(entropy) {
|
|||||||
return mnemonic.join(' ');
|
return mnemonic.join(' ');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract
|
||||||
|
*/
|
||||||
|
|
||||||
|
function HD(options) {
|
||||||
|
return new HDPrivateKey(options);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HD Private Key
|
* HD Private Key
|
||||||
*/
|
*/
|
||||||
@ -114,15 +121,30 @@ function HDPrivateKey(options) {
|
|||||||
if (!(this instanceof HDPrivateKey))
|
if (!(this instanceof HDPrivateKey))
|
||||||
return new HDPrivateKey(options);
|
return new HDPrivateKey(options);
|
||||||
|
|
||||||
|
assert(!(options instanceof HDPrivateKey));
|
||||||
|
assert(!(options instanceof HDPublicKey));
|
||||||
|
|
||||||
|
if (options instanceof bcoin.hd.seed)
|
||||||
|
options = { seed: options };
|
||||||
|
|
||||||
|
if (options) {
|
||||||
|
if (options.xprivkey)
|
||||||
|
options.xkey = options.xprivkey;
|
||||||
|
if (options.xpubkey)
|
||||||
|
options.xkey = options.xpubkey;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options) {
|
||||||
|
if (HDPublicKey.isExtended(options) || HDPublicKey.isExtended(options.xkey))
|
||||||
|
return new HDPublicKey(options);
|
||||||
|
}
|
||||||
|
|
||||||
if (!options)
|
if (!options)
|
||||||
options = { seed: bcoin.hd.seed() };
|
options = { seed: bcoin.hd.seed() };
|
||||||
|
|
||||||
if (HDPrivateKey.isExtended(options))
|
if (HDPrivateKey.isExtended(options))
|
||||||
options = { xkey: options };
|
options = { xkey: options };
|
||||||
|
|
||||||
if (HDPublicKey.isExtended(options))
|
|
||||||
return new HDPublicKey(options);
|
|
||||||
|
|
||||||
if (options.passphrase !== undefined
|
if (options.passphrase !== undefined
|
||||||
|| options.bits
|
|| options.bits
|
||||||
|| options.entropy
|
|| options.entropy
|
||||||
@ -161,6 +183,8 @@ function HDPrivateKey(options) {
|
|||||||
this.isPublic = false;
|
this.isPublic = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inherits(HDPrivateKey, HD);
|
||||||
|
|
||||||
HDPrivateKey.prototype.scan44 = function scan44(options, txByAddress, callback) {
|
HDPrivateKey.prototype.scan44 = function scan44(options, txByAddress, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var accounts = [];
|
var accounts = [];
|
||||||
@ -837,9 +861,20 @@ function HDPublicKey(options) {
|
|||||||
if (!options)
|
if (!options)
|
||||||
throw new Error('No options for HDPublicKey');
|
throw new Error('No options for HDPublicKey');
|
||||||
|
|
||||||
|
assert(!(options instanceof HDPrivateKey));
|
||||||
|
assert(!(options instanceof HDPublicKey));
|
||||||
|
|
||||||
|
assert(!options.xprivkey);
|
||||||
|
|
||||||
|
if (options.xpubkey)
|
||||||
|
options.xkey = options.xpubkey;
|
||||||
|
|
||||||
if (HDPublicKey.isExtended(options))
|
if (HDPublicKey.isExtended(options))
|
||||||
options = { xkey: options };
|
options = { xkey: options };
|
||||||
|
|
||||||
|
if (HDPrivateKey.isExtended(options) || HDPrivateKey.isExtended(options.xkey))
|
||||||
|
throw new Error('Cannot pass xprivkey into HDPublicKey');
|
||||||
|
|
||||||
data = options.xkey
|
data = options.xkey
|
||||||
? this._unbuild(options.xkey)
|
? this._unbuild(options.xkey)
|
||||||
: options;
|
: options;
|
||||||
@ -859,6 +894,8 @@ function HDPublicKey(options) {
|
|||||||
this.isPublic = true;
|
this.isPublic = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inherits(HDPublicKey, HD);
|
||||||
|
|
||||||
HDPublicKey.prototype.scan44 = HDPrivateKey.prototype.scan44;
|
HDPublicKey.prototype.scan44 = HDPrivateKey.prototype.scan44;
|
||||||
HDPublicKey.prototype.deriveAccount44 = HDPrivateKey.prototype.deriveAccount44;
|
HDPublicKey.prototype.deriveAccount44 = HDPrivateKey.prototype.deriveAccount44;
|
||||||
HDPublicKey.prototype.deriveBIP44 = HDPrivateKey.prototype.deriveBIP44;
|
HDPublicKey.prototype.deriveBIP44 = HDPrivateKey.prototype.deriveBIP44;
|
||||||
@ -1174,10 +1211,14 @@ cache.get = function(key, index) {
|
|||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|
||||||
hd.seed = HDSeed;
|
exports = HD;
|
||||||
hd.priv = HDPrivateKey;
|
|
||||||
hd.pub = HDPublicKey;
|
exports.seed = HDSeed;
|
||||||
hd.privateKey = HDPrivateKey;
|
exports.priv = HDPrivateKey;
|
||||||
hd.publicKey = HDPublicKey;
|
exports.pub = HDPublicKey;
|
||||||
hd.pbkdf2 = pbkdf2;
|
exports.privateKey = HDPrivateKey;
|
||||||
hd.fromJSON = HDPrivateKey.fromJSON;
|
exports.publicKey = HDPublicKey;
|
||||||
|
exports.pbkdf2 = pbkdf2;
|
||||||
|
exports.fromJSON = HDPrivateKey.fromJSON;
|
||||||
|
|
||||||
|
module.exports = HD;
|
||||||
|
|||||||
@ -31,39 +31,17 @@ function Wallet(options) {
|
|||||||
|
|
||||||
options = utils.merge({}, options);
|
options = utils.merge({}, options);
|
||||||
|
|
||||||
if (!options.master) {
|
if (typeof options.master === 'string')
|
||||||
options.master = options.hd
|
options.master = { xkey: options.master };
|
||||||
? bcoin.hd.privateKey(options.hd)
|
|
||||||
: bcoin.hd.privateKey();
|
if (options.master
|
||||||
delete options.hd;
|
&& typeof options.master === 'object'
|
||||||
|
&& !(options.master instanceof bcoin.hd)) {
|
||||||
|
options.master = bcoin.hd(options.master);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.key) {
|
if (!options.master)
|
||||||
options.pair = options.key;
|
options.master = bcoin.hd.privateKey();
|
||||||
delete options.key;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.priv) {
|
|
||||||
options.privateKey = options.priv;
|
|
||||||
delete options.priv;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.pub) {
|
|
||||||
options.publicKey = options.pub;
|
|
||||||
delete options.pub;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((options.pair instanceof bcoin.hd.privateKey)
|
|
||||||
|| (options.pair instanceof bcoin.hd.publicKey)) {
|
|
||||||
options.master = options.pair;
|
|
||||||
delete options.pair;
|
|
||||||
} else if (options.privateKey instanceof bcoin.hd.privateKey) {
|
|
||||||
options.master = options.privateKey;
|
|
||||||
delete options.privateKey;
|
|
||||||
} else if (options.publicKey instanceof bcoin.hd.publicKey) {
|
|
||||||
options.master = options.publicKey;
|
|
||||||
delete options.publicKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.addresses = [];
|
this.addresses = [];
|
||||||
@ -81,7 +59,7 @@ function Wallet(options) {
|
|||||||
this.cosignerIndex = -1;
|
this.cosignerIndex = -1;
|
||||||
|
|
||||||
this.type = options.type || 'pubkeyhash';
|
this.type = options.type || 'pubkeyhash';
|
||||||
this.derivation = options.derivation || null;
|
this.derivation = options.derivation || 'bip44';
|
||||||
this.compressed = options.compressed !== false;
|
this.compressed = options.compressed !== false;
|
||||||
this.keys = [];
|
this.keys = [];
|
||||||
this.m = options.m || 1;
|
this.m = options.m || 1;
|
||||||
@ -93,13 +71,6 @@ function Wallet(options) {
|
|||||||
assert(this.type === 'pubkeyhash' || this.type === 'multisig');
|
assert(this.type === 'pubkeyhash' || this.type === 'multisig');
|
||||||
this.prefixType = this.type === 'multisig' ? 'scripthash' : 'pubkeyhash';
|
this.prefixType = this.type === 'multisig' ? 'scripthash' : 'pubkeyhash';
|
||||||
|
|
||||||
if (!this.derivation) {
|
|
||||||
if (this.type === 'multisig')
|
|
||||||
this.derivation = 'bip45';
|
|
||||||
else
|
|
||||||
this.derivation = 'bip44';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (network.prefixes[this.prefixType] == null)
|
if (network.prefixes[this.prefixType] == null)
|
||||||
throw new Error('Unknown prefix: ' + this.prefixType);
|
throw new Error('Unknown prefix: ' + this.prefixType);
|
||||||
|
|
||||||
@ -778,7 +749,6 @@ Wallet.prototype.__defineGetter__('address', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Wallet.prototype.toJSON = function toJSON(encrypt) {
|
Wallet.prototype.toJSON = function toJSON(encrypt) {
|
||||||
assert(this._initialized);
|
|
||||||
return {
|
return {
|
||||||
v: 3,
|
v: 3,
|
||||||
name: 'wallet',
|
name: 'wallet',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user