wallet: refactor masterkey.

This commit is contained in:
Christopher Jeffrey 2016-10-10 00:55:36 -07:00
parent 5a353d1592
commit 6761122580
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 24 additions and 15 deletions

View File

@ -279,7 +279,7 @@ MasterKey.prototype.decipher = function decipher(data, iv) {
* the timer if there is one.
*/
MasterKey.prototype.destroy = function destroy() {
MasterKey.prototype.lock = function lock() {
if (!this.encrypted) {
assert(this.timer == null);
assert(this.key);
@ -299,6 +299,15 @@ MasterKey.prototype.destroy = function destroy() {
}
};
/**
* Destroy the key permanently.
*/
MasterKey.prototype.destroy = function destroy() {
this.lock();
this.locker.destroy();
};
/**
* Decrypt the key permanently.
* @param {Buffer|String} passphrase - Zero this yourself.
@ -332,7 +341,7 @@ MasterKey.prototype._decrypt = co(function* decrypt(passphrase) {
if (!passphrase)
return;
this.destroy();
this.lock();
key = yield this.derive(passphrase);
data = crypto.decipher(this.ciphertext, key, this.iv);

View File

@ -81,7 +81,7 @@ function Wallet(db, options) {
this.accountDepth = 0;
this.token = constants.ZERO_HASH;
this.tokenDepth = 0;
this.master = null;
this.master = new MasterKey();
this.txdb = new TXDB(this);
this.account = null;
@ -99,27 +99,27 @@ utils.inherits(Wallet, EventEmitter);
*/
Wallet.prototype.fromOptions = function fromOptions(options) {
var master = options.master;
var key = options.master;
var id, token;
if (!MasterKey.isMasterKey(master)) {
if (!master)
master = HD.fromMnemonic(null, this.network);
if (MasterKey.isMasterKey(key)) {
this.master.fromOptions(key);
} else {
if (!key)
key = HD.fromMnemonic(null, this.network);
if (HD.isExtended(master))
master = HD.fromBase58(master);
if (HD.isExtended(key))
key = HD.fromBase58(key);
assert(HD.isPrivate(master),
assert(HD.isPrivate(key),
'Must create wallet with hd private key.');
assert(master.network === this.network,
assert(key.network === this.network,
'Network mismatch for master key.');
master = MasterKey.fromKey(master);
this.master.fromKey(key);
}
this.master = master;
if (options.wid != null) {
assert(utils.isNumber(options.wid));
this.wid = options.wid;
@ -589,7 +589,7 @@ Wallet.prototype.lock = co(function* lock() {
var unlock1 = yield this.writeLock.lock();
var unlock2 = yield this.fundLock.lock();
try {
this.master.destroy();
this.master.lock();
} finally {
unlock2();
unlock1();