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

View File

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