This commit is contained in:
Christopher Jeffrey 2016-06-25 01:29:08 -07:00
parent f784c25d01
commit a1943bcb5e
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 122 additions and 64 deletions

View File

@ -18,9 +18,10 @@ var BufferWriter = require('./writer');
* @exports KeyRing * @exports KeyRing
* @constructor * @constructor
* @param {Object} options * @param {Object} options
* @param {HDPrivateKey|HDPublicKey} options.key * @param {HDPrivateKey|HDPublicKey|KeyPair|Buffer} options.key
* @param {String?} options.path * @param {String?} options.name
* @param {Boolean?} options.change * @param {Number?} options.account
* @param {Number?} options.change
* @param {Number?} options.index * @param {Number?} options.index
* @param {String?} options.type - `"pubkeyhash"` or `"multisig"`. * @param {String?} options.type - `"pubkeyhash"` or `"multisig"`.
* @param {Buffer[]} options.keys - Shared multisig keys. * @param {Buffer[]} options.keys - Shared multisig keys.
@ -45,12 +46,27 @@ function KeyRing(options) {
this.key = null; this.key = null;
this.keys = []; this.keys = [];
this.addressMap = null; this._keyHash = null;
this._keyAddress = null;
this._program = null;
this._programHash = null;
this._programAddress = null;
this._script = null;
this._scriptHash160 = null;
this._scriptHash256 = null;
this._scriptAddress = null;
this._addressMap = null;
if (options) if (options)
this.fromOptions(options); this.fromOptions(options);
} }
/**
* Inject properties from options object.
* @private
* @param {Object} options
*/
KeyRing.prototype.fromOptions = function fromOptions(options) { KeyRing.prototype.fromOptions = function fromOptions(options) {
var i; var i;
@ -84,6 +100,12 @@ KeyRing.prototype.fromOptions = function fromOptions(options) {
return this; return this;
}; };
/**
* Instantiate key ring from options.
* @param {Object} options
* @returns {KeyRing}
*/
KeyRing.fromOptions = function fromOptions(options) { KeyRing.fromOptions = function fromOptions(options) {
return new KeyRing().fromOptions(options); return new KeyRing().fromOptions(options);
}; };
@ -302,12 +324,12 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress() {
*/ */
KeyRing.prototype.getKeyHash = function getKeyHash(enc) { KeyRing.prototype.getKeyHash = function getKeyHash(enc) {
if (!this._hash) if (!this._keyHash)
this._hash = utils.hash160(this.getPublicKey()); this._keyHash = utils.hash160(this.getPublicKey());
return enc === 'hex' return enc === 'hex'
? this._hash.toString('hex') ? this._keyHash.toString('hex')
: this._hash; : this._keyHash;
}; };
/** /**
@ -318,16 +340,16 @@ KeyRing.prototype.getKeyHash = function getKeyHash(enc) {
KeyRing.prototype.getKeyAddress = function getKeyAddress() { KeyRing.prototype.getKeyAddress = function getKeyAddress() {
var hash, address; var hash, address;
if (!this._address) { if (!this._keyAddress) {
hash = this.getKeyHash(); hash = this.getKeyHash();
if (this.witness) if (this.witness)
address = this.compile(hash, 'witnesspubkeyhash', 0); address = this.compile(hash, 'witnesspubkeyhash', 0);
else else
address = this.compile(hash, 'pubkeyhash'); address = this.compile(hash, 'pubkeyhash');
this._address = address; this._keyAddress = address;
} }
return this._address; return this._keyAddress;
}; };
/** /**
@ -372,19 +394,19 @@ KeyRing.prototype.getAddress = function getAddress() {
*/ */
KeyRing.prototype.getAddressMap = function getAddressMap() { KeyRing.prototype.getAddressMap = function getAddressMap() {
if (!this.addressMap) { if (!this._addressMap) {
this.addressMap = {}; this._addressMap = {};
this.addressMap[this.getKeyHash('hex')] = true; this._addressMap[this.getKeyHash('hex')] = true;
if (this.type === 'multisig') if (this.type === 'multisig')
this.addressMap[this.getScriptHash('hex')] = true; this._addressMap[this.getScriptHash('hex')] = true;
if (this.witness) if (this.witness)
this.addressMap[this.getProgramHash('hex')] = true; this._addressMap[this.getProgramHash('hex')] = true;
} }
return this.addressMap; return this._addressMap;
}; };
/** /**
@ -546,7 +568,6 @@ KeyRing.prototype.__defineGetter__('address', function() {
/** /**
* Convert an KeyRing to a more json-friendly object. * Convert an KeyRing to a more json-friendly object.
* @param {String?} passphrase - KeyRing passphrase
* @returns {Object} * @returns {Object}
*/ */
@ -570,10 +591,9 @@ KeyRing.prototype.toJSON = function toJSON() {
}; };
/** /**
* Instantiate an KeyRing from a jsonified transaction object. * Inject properties from json object.
* @param {Object} json - The jsonified transaction object. * @private
* @param {String?} passphrase - KeyRing passphrase * @param {Object} json
* @returns {KeyRing}
*/ */
KeyRing.prototype.fromJSON = function fromJSON(json) { KeyRing.prototype.fromJSON = function fromJSON(json) {
@ -591,6 +611,12 @@ KeyRing.prototype.fromJSON = function fromJSON(json) {
return this; return this;
}; };
/**
* Instantiate an KeyRing from a jsonified transaction object.
* @param {Object} json - The jsonified transaction object.
* @returns {KeyRing}
*/
KeyRing.fromJSON = function fromJSON(json) { KeyRing.fromJSON = function fromJSON(json) {
return new KeyRing().fromJSON(json); return new KeyRing().fromJSON(json);
}; };
@ -626,8 +652,9 @@ KeyRing.prototype.toRaw = function toRaw(writer) {
}; };
/** /**
* Instantiate a keyring from serialized data. * Inject properties from serialized data.
* @returns {KeyRing} * @private
* @param {Buffer} data
*/ */
KeyRing.prototype.fromRaw = function fromRaw(data) { KeyRing.prototype.fromRaw = function fromRaw(data) {
@ -654,6 +681,12 @@ KeyRing.prototype.fromRaw = function fromRaw(data) {
return this; return this;
}; };
/**
* Instantiate a keyring from serialized data.
* @param {Buffer} data
* @returns {KeyRing}
*/
KeyRing.fromRaw = function fromRaw(data) { KeyRing.fromRaw = function fromRaw(data) {
return new KeyRing().fromRaw(data); return new KeyRing().fromRaw(data);
}; };

View File

@ -501,7 +501,7 @@ utils.decrypt = function decrypt(data, passphrase, iv, callback) {
*/ */
utils.decipher = function decipher(data, key, iv) { utils.decipher = function decipher(data, key, iv) {
var key, decipher; var decipher;
if (!crypto) if (!crypto)
return aes.cbc.decrypt(data, key, iv); return aes.cbc.decrypt(data, key, iv);

View File

@ -64,9 +64,11 @@ function Wallet(db, options) {
utils.inherits(Wallet, EventEmitter); utils.inherits(Wallet, EventEmitter);
Wallet.fromOptions = function fromOptions(db, options) { /**
return new Wallet(db).fromOptions(options); * Inject properties from options object.
}; * @private
* @param {Object} options
*/
Wallet.prototype.fromOptions = function fromOptions(options) { Wallet.prototype.fromOptions = function fromOptions(options) {
var master = options.master; var master = options.master;
@ -88,6 +90,25 @@ Wallet.prototype.fromOptions = function fromOptions(options) {
return this; return this;
}; };
/**
* Instantiate wallet from options.
* @param {WalletDB} db
* @param {Object} options
* @returns {Wallet}
*/
Wallet.fromOptions = function fromOptions(db, options) {
return new Wallet(db).fromOptions(options);
};
/**
* Attempt to intialize the wallet (generating
* the first addresses along with the lookahead
* addresses). Called automatically from the
* walletdb.
* @param {Function} callback
*/
Wallet.prototype.init = function init(options, callback) { Wallet.prototype.init = function init(options, callback) {
var self = this; var self = this;
@ -111,8 +132,14 @@ Wallet.prototype.init = function init(options, callback) {
}); });
}; };
/**
* Open wallet (done after retrieval).
* @param {Function} callback
*/
Wallet.prototype.open = function open(callback) { Wallet.prototype.open = function open(callback) {
var self = this; var self = this;
assert(this.initialized); assert(this.initialized);
this.getAccount(0, function(err, account) { this.getAccount(0, function(err, account) {
@ -148,14 +175,6 @@ Wallet.prototype.destroy = function destroy(callback) {
return utils.nextTick(callback); return utils.nextTick(callback);
}; };
/**
* Attempt to intialize the wallet (generating
* the first addresses along with the lookahead
* addresses). Called automatically from the
* walletdb and open().
* @param {Function} callback
*/
/** /**
* Add a public account key to the wallet (multisig). * Add a public account key to the wallet (multisig).
* Saves the key in the wallet database. * Saves the key in the wallet database.
@ -227,7 +246,7 @@ Wallet.prototype.removeKey = function removeKey(account, key, callback) {
/** /**
* Change or set master key's passphrase. * Change or set master key's passphrase.
* @param {(String|Buffer)?} old * @param {(String|Buffer)?} old
* @param {(String|Buffer)?} new_ * @param {String|Buffer} new_
* @param {Function} callback * @param {Function} callback
*/ */
@ -1407,14 +1426,9 @@ Wallet.prototype.toJSON = function toJSON() {
}; };
/** /**
* Handle a deserialized JSON wallet object. * Inject properties from json object.
* @returns {Object} A "naked" wallet (a * @private
* plain javascript object which is suitable
* for passing to the Wallet constructor).
* @param {Object} json * @param {Object} json
* @param {String?} passphrase
* @returns {Object}
* @throws Error on bad decrypt
*/ */
Wallet.prototype.fromJSON = function fromJSON(json) { Wallet.prototype.fromJSON = function fromJSON(json) {
@ -1447,11 +1461,9 @@ Wallet.prototype.toRaw = function toRaw(writer) {
}; };
/** /**
* Parse a serialized wallet. Return a "naked" * Inject properties from serialized data.
* wallet object, suitable for passing into * @private
* the wallet constructor.
* @param {Buffer} data * @param {Buffer} data
* @returns {Object}
*/ */
Wallet.prototype.fromRaw = function fromRaw(data) { Wallet.prototype.fromRaw = function fromRaw(data) {
@ -1559,9 +1571,11 @@ function Account(db, options) {
utils.inherits(Account, EventEmitter); utils.inherits(Account, EventEmitter);
Account.fromOptions = function fromOptions(db, options) { /**
return new Account(db).fromOptions(options); * Inject properties from options object.
}; * @private
* @param {Object} options
*/
Account.prototype.fromOptions = function fromOptions(options) { Account.prototype.fromOptions = function fromOptions(options) {
assert(options, 'Options are required.'); assert(options, 'Options are required.');
@ -1606,6 +1620,17 @@ Account.prototype.fromOptions = function fromOptions(options) {
return this; return this;
}; };
/**
* Instantiate account from options.
* @param {WalletDB} db
* @param {Object} options
* @returns {Account}
*/
Account.fromOptions = function fromOptions(db, options) {
return new Account(db).fromOptions(options);
};
/* /*
* Default address lookahead. * Default address lookahead.
* @const {Number} * @const {Number}
@ -1617,7 +1642,7 @@ Account.LOOKAHEAD = 5;
* Attempt to intialize the account (generating * Attempt to intialize the account (generating
* the first addresses along with the lookahead * the first addresses along with the lookahead
* addresses). Called automatically from the * addresses). Called automatically from the
* walletdb and open(). * walletdb.
* @param {Function} callback * @param {Function} callback
*/ */
@ -1636,6 +1661,11 @@ Account.prototype.init = function init(callback) {
this.setDepth(1, 1, callback); this.setDepth(1, 1, callback);
}; };
/**
* Open the account (done after retrieval).
* @param {Function} callback
*/
Account.prototype.open = function open(callback) { Account.prototype.open = function open(callback) {
if (!this.initialized) if (!this.initialized)
return callback(); return callback();
@ -2108,8 +2138,7 @@ Account.prototype.inspect = function inspect() {
/** /**
* Convert the account to an object suitable for * Convert the account to an object suitable for
* serialization. Will automatically encrypt the * serialization.
* master key based on the `passphrase` option.
* @returns {Object} * @returns {Object}
*/ */
@ -2143,14 +2172,9 @@ Account.prototype.toJSON = function toJSON() {
}; };
/** /**
* Handle a deserialized JSON account object. * Inject properties from json object.
* @returns {Object} A "naked" account (a * @private
* plain javascript object which is suitable
* for passing to the Account constructor).
* @param {Object} json * @param {Object} json
* @param {String?} passphrase
* @returns {Object}
* @throws Error on bad decrypt
*/ */
Account.prototype.fromJSON = function fromJSON(json) { Account.prototype.fromJSON = function fromJSON(json) {
@ -2209,9 +2233,8 @@ Account.prototype.toRaw = function toRaw(writer) {
}; };
/** /**
* Parse a serialized account. Return a "naked" * Inject properties from serialized data.
* account object, suitable for passing into * @private
* the account constructor.
* @param {Buffer} data * @param {Buffer} data
* @returns {Object} * @returns {Object}
*/ */
@ -2244,6 +2267,7 @@ Account.prototype.fromRaw = function fromRaw(data) {
/** /**
* Instantiate a account from serialized data. * Instantiate a account from serialized data.
* @param {WalletDB} data
* @param {Buffer} data * @param {Buffer} data
* @returns {Account} * @returns {Account}
*/ */
@ -2255,6 +2279,7 @@ Account.fromRaw = function fromRaw(db, data) {
/** /**
* Instantiate a Account from a * Instantiate a Account from a
* jsonified account object. * jsonified account object.
* @param {WalletDB} db
* @param {Object} json - The jsonified account object. * @param {Object} json - The jsonified account object.
* @returns {Account} * @returns {Account}
*/ */