|
|
@ -17,6 +17,7 @@ var collections = { |
|
|
|
TXS: 'txs', |
|
|
|
ADDRESSES: 'addresses', |
|
|
|
NOTIFICATIONS: 'notifications', |
|
|
|
COPAYERS_LOOKUP: 'copayers_lookup', |
|
|
|
}; |
|
|
|
|
|
|
|
var Storage = function(opts) { |
|
|
@ -74,44 +75,38 @@ Storage.prototype.storeWallet = function(wallet, cb) { |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) { |
|
|
|
return this.storeWallet(wallet, cb); |
|
|
|
}; |
|
|
|
var self = this; |
|
|
|
|
|
|
|
Storage.prototype.fetchCopayerLookup2 = function(copayerId, cb) { |
|
|
|
this.db.collection(collections.WALLETS).findOne({ |
|
|
|
'copayers.id': copayerId |
|
|
|
}, function(err, result) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!result) return cb(); |
|
|
|
var copayer = _.find(result.copayers, { |
|
|
|
id: copayerId |
|
|
|
}); |
|
|
|
return cb(null, { |
|
|
|
walletId: result.id, |
|
|
|
var copayerLookups = _.map(wallet.copayers, function(copayer) { |
|
|
|
return { |
|
|
|
copayerId: copayer.id, |
|
|
|
walletId: wallet.id, |
|
|
|
requestPubKey: copayer.requestPubKey, |
|
|
|
}; |
|
|
|
}); |
|
|
|
|
|
|
|
this.db.collection(collections.COPAYERS_LOOKUP).remove({ |
|
|
|
walletId: wallet.id |
|
|
|
}, { |
|
|
|
w: 1 |
|
|
|
}, function(err) { |
|
|
|
if (err) return cb(err); |
|
|
|
self.db.collection(collections.COPAYERS_LOOKUP).insert(copayerLookups, { |
|
|
|
w: 1 |
|
|
|
}, function(err) { |
|
|
|
if (err) return cb(err); |
|
|
|
return self.storeWallet(wallet, cb); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.fetchCopayerLookup = function(copayerId, cb) { |
|
|
|
this.db.collection(collections.WALLETS).find({}).toArray(function(err, result) { |
|
|
|
this.db.collection(collections.COPAYERS_LOOKUP).findOne({ |
|
|
|
copayerId: copayerId |
|
|
|
}, function(err, result) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
result = _.find(result, function(w) { |
|
|
|
return _.any(w.copayers, { |
|
|
|
id: copayerId |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (!result) return cb(); |
|
|
|
var copayer = _.find(result.copayers, { |
|
|
|
id: copayerId |
|
|
|
}); |
|
|
|
return cb(null, { |
|
|
|
walletId: result.id, |
|
|
|
requestPubKey: copayer.requestPubKey, |
|
|
|
}); |
|
|
|
return cb(null, result); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
@ -278,18 +273,11 @@ Storage.prototype.removeWallet = function(walletId, cb) { |
|
|
|
}, next); |
|
|
|
}, |
|
|
|
function(next) { |
|
|
|
self.db.collection(collections.ADDRESSES).remove({ |
|
|
|
walletId: walletId |
|
|
|
}, next); |
|
|
|
}, |
|
|
|
function(next) { |
|
|
|
self.db.collection(collections.TXS).remove({ |
|
|
|
walletId: walletId |
|
|
|
}, next); |
|
|
|
}, |
|
|
|
function(next) { |
|
|
|
self.db.collection(collections.NOTIFICATIONS).remove({ |
|
|
|
walletId: walletId |
|
|
|
var otherCollections = _.without(_.values(collections), collections.WALLETS); |
|
|
|
async.each(otherCollections, function(col, next) { |
|
|
|
self.db.collection(col).remove({ |
|
|
|
walletId: walletId |
|
|
|
}, next); |
|
|
|
}, next); |
|
|
|
}, |
|
|
|
], cb); |
|
|
|