From 3baddeebb695250345519831e6de256224e66027 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 3 Nov 2016 23:07:00 -0700 Subject: [PATCH] walletdb: shared keys check. --- lib/wallet/account.js | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 7b7a71f0..e6c0cb90 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -289,6 +289,9 @@ Account.prototype.pushKey = function pushKey(key) { if (!key.isAccount44()) throw new Error('Must add HD account keys to BIP44 wallet.'); + if (this.type !== Account.types.MULTISIG) + throw new Error('Cannot add keys to non-multisig wallet.'); + if (key.equal(this.accountKey)) throw new Error('Cannot add own key.'); @@ -326,8 +329,8 @@ Account.prototype.spliceKey = function spliceKey(key) { if (!key.isAccount44()) throw new Error('Must add HD account keys to BIP44 wallet.'); - if (key.equal(this.accountKey)) - throw new Error('Cannot remove own key.'); + if (this.type !== Account.types.MULTISIG) + throw new Error('Cannot remove keys from non-multisig wallet.'); if (this.keys.length === this.n - 1) throw new Error('Cannot remove key.'); @@ -343,16 +346,8 @@ Account.prototype.spliceKey = function spliceKey(key) { */ Account.prototype.addSharedKey = co(function* addSharedKey(key) { - var result = false; - var exists; - - try { - result = this.pushKey(key); - } catch (e) { - throw e; - } - - exists = yield this._checkKeys(); + var result = this.pushKey(key); + var exists = yield this._hasDuplicate(); if (exists) { this.spliceKey(key); @@ -371,20 +366,17 @@ Account.prototype.addSharedKey = co(function* addSharedKey(key) { * @returns {Promise} */ -Account.prototype._checkKeys = co(function* _checkKeys() { +Account.prototype._hasDuplicate = function _hasDuplicate() { var ring, hash; - if (this.initialized || this.type !== Account.types.MULTISIG) - return false; - if (this.keys.length !== this.n - 1) return false; ring = this.deriveReceive(0); hash = ring.getScriptHash('hex'); - return yield this.wallet.hasAddress(hash); -}); + return this.wallet.hasAddress(hash); +}; /** * Remove a public account key from the account (multisig). @@ -394,17 +386,14 @@ Account.prototype._checkKeys = co(function* _checkKeys() { */ Account.prototype.removeSharedKey = function removeSharedKey(key) { - var result = false; + var result = this.spliceKey(key); - try { - result = this.spliceKey(key); - } catch (e) { - return Promise.reject(e); - } + if (!result) + return false; this.save(); - return Promise.resolve(result); + return true; }; /**