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
* @constructor
* @param {Object} options
* @param {HDPrivateKey|HDPublicKey} options.key
* @param {String?} options.path
* @param {Boolean?} options.change
* @param {HDPrivateKey|HDPublicKey|KeyPair|Buffer} options.key
* @param {String?} options.name
* @param {Number?} options.account
* @param {Number?} options.change
* @param {Number?} options.index
* @param {String?} options.type - `"pubkeyhash"` or `"multisig"`.
* @param {Buffer[]} options.keys - Shared multisig keys.
@ -45,12 +46,27 @@ function KeyRing(options) {
this.key = null;
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)
this.fromOptions(options);
}
/**
* Inject properties from options object.
* @private
* @param {Object} options
*/
KeyRing.prototype.fromOptions = function fromOptions(options) {
var i;
@ -84,6 +100,12 @@ KeyRing.prototype.fromOptions = function fromOptions(options) {
return this;
};
/**
* Instantiate key ring from options.
* @param {Object} options
* @returns {KeyRing}
*/
KeyRing.fromOptions = function fromOptions(options) {
return new KeyRing().fromOptions(options);
};
@ -302,12 +324,12 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress() {
*/
KeyRing.prototype.getKeyHash = function getKeyHash(enc) {
if (!this._hash)
this._hash = utils.hash160(this.getPublicKey());
if (!this._keyHash)
this._keyHash = utils.hash160(this.getPublicKey());
return enc === 'hex'
? this._hash.toString('hex')
: this._hash;
? this._keyHash.toString('hex')
: this._keyHash;
};
/**
@ -318,16 +340,16 @@ KeyRing.prototype.getKeyHash = function getKeyHash(enc) {
KeyRing.prototype.getKeyAddress = function getKeyAddress() {
var hash, address;
if (!this._address) {
if (!this._keyAddress) {
hash = this.getKeyHash();
if (this.witness)
address = this.compile(hash, 'witnesspubkeyhash', 0);
else
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() {
if (!this.addressMap) {
this.addressMap = {};
if (!this._addressMap) {
this._addressMap = {};
this.addressMap[this.getKeyHash('hex')] = true;
this._addressMap[this.getKeyHash('hex')] = true;
if (this.type === 'multisig')
this.addressMap[this.getScriptHash('hex')] = true;
this._addressMap[this.getScriptHash('hex')] = true;
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.
* @param {String?} passphrase - KeyRing passphrase
* @returns {Object}
*/
@ -570,10 +591,9 @@ KeyRing.prototype.toJSON = function toJSON() {
};
/**
* Instantiate an KeyRing from a jsonified transaction object.
* @param {Object} json - The jsonified transaction object.
* @param {String?} passphrase - KeyRing passphrase
* @returns {KeyRing}
* Inject properties from json object.
* @private
* @param {Object} json
*/
KeyRing.prototype.fromJSON = function fromJSON(json) {
@ -591,6 +611,12 @@ KeyRing.prototype.fromJSON = function fromJSON(json) {
return this;
};
/**
* Instantiate an KeyRing from a jsonified transaction object.
* @param {Object} json - The jsonified transaction object.
* @returns {KeyRing}
*/
KeyRing.fromJSON = function fromJSON(json) {
return new KeyRing().fromJSON(json);
};
@ -626,8 +652,9 @@ KeyRing.prototype.toRaw = function toRaw(writer) {
};
/**
* Instantiate a keyring from serialized data.
* @returns {KeyRing}
* Inject properties from serialized data.
* @private
* @param {Buffer} data
*/
KeyRing.prototype.fromRaw = function fromRaw(data) {
@ -654,6 +681,12 @@ KeyRing.prototype.fromRaw = function fromRaw(data) {
return this;
};
/**
* Instantiate a keyring from serialized data.
* @param {Buffer} data
* @returns {KeyRing}
*/
KeyRing.fromRaw = function 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) {
var key, decipher;
var decipher;
if (!crypto)
return aes.cbc.decrypt(data, key, iv);

View File

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