walletdb: shared keys check.

This commit is contained in:
Christopher Jeffrey 2016-11-03 23:07:00 -07:00
parent 7faba4a23b
commit 3baddeebb6
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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;
};
/**