diff --git a/lib/model/wallet.js b/lib/model/wallet.js index b629309..3c992d2 100644 --- a/lib/model/wallet.js +++ b/lib/model/wallet.js @@ -2,6 +2,8 @@ var _ = require('lodash'); +var Copayer = require('./copayer'); + function Wallet(opts) { opts = opts || {}; @@ -13,6 +15,7 @@ function Wallet(opts) { this.status = 'pending'; this.publicKeyRing = []; this.addressIndex = 0; + this.copayers = []; }; Wallet.fromObj = function (obj) { @@ -26,7 +29,24 @@ Wallet.fromObj = function (obj) { x.status = obj.status; x.publicKeyRing = obj.publicKeyRing; x.addressIndex = obj.addressIndex; + x.copayers = _.map(obj.copayers, function (copayer) { + return new Copayer(copayer); + }); + return x; }; +Wallet.prototype.addCopayer = function (copayer) { + this.copayers.push(copayer); + + if (this.copayers.length < this.n) return; + + this.status = 'complete'; + this.publicKeyRing = _.pluck(this.copayers, 'xPubKey'); +}; + +Wallet.prototype.getCopayer = function (copayerId) { + return _.find(this.copayers, { id: copayerId }); +}; + module.exports = Wallet; diff --git a/lib/server.js b/lib/server.js index 9a45b5e..244f636 100644 --- a/lib/server.js +++ b/lib/server.js @@ -62,7 +62,6 @@ CopayServer.prototype.createWallet = function (opts, cb) { * Retrieves a wallet from storage. * @param {Object} opts * @param {string} opts.id - The wallet id. - * @param {truthy} opts.includeCopayers - Fetch wallet along with list of copayers. * @returns {Object} wallet */ CopayServer.prototype.getWallet = function (opts, cb) { @@ -72,15 +71,7 @@ CopayServer.prototype.createWallet = function (opts, cb) { if (err) return cb(err); if (!wallet) return cb('Wallet not found'); - if (opts.includeCopayers) { - self.storage.fetchCopayers(wallet.id, function (err, copayers) { - if (err) return cb(err); - wallet.copayers = copayers || []; - return cb(null, wallet); - }); - } else { - return cb(null, wallet); - } + return cb(null, wallet); }); }; @@ -110,7 +101,7 @@ CopayServer.prototype._runLocked = function (walletId, cb, task) { var self = this; self._runLocked(opts.walletId, cb, function (cb) { - self.getWallet({ id: opts.walletId, includeCopayers: true }, function (err, wallet) { + self.getWallet({ id: opts.walletId }, function (err, wallet) { if (err || !wallet) return cb(err); if (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return cb('Copayer already in wallet'); if (wallet.copayers.length == wallet.n) return cb('Wallet full'); @@ -124,14 +115,12 @@ CopayServer.prototype._runLocked = function (walletId, cb, task) { xPubKeySignature: opts.xPubKeySignature, }); - self.storage.storeCopayer(wallet.id, copayer, function (err) { - if (err) return cb(err); - if ((wallet.copayers.length + 1) < wallet.n) return cb(); + wallet.addCopayer(copayer); - wallet.status = 'complete'; - wallet.publicKeyRing = _.pluck(wallet.copayers, 'xPubKey'); - wallet.publicKeyRing.push(copayer.xPubKey); - self.storage.storeWallet(wallet, cb); + self.storage.storeWallet(wallet, function (err) { + if (err) return cb(err); + + return cb(); }); }); }); @@ -182,8 +171,10 @@ CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) { CopayServer.prototype.verifyMessageSignature = function (opts, cb) { var self = this; - self.storage.fetchCopayer(opts.walletId, opts.copayerId, function (err, copayer) { - if (err) return cb(err); + self.getWallet({ id: opts.walletId }, function (err, wallet) { + if (err || !wallet) return cb(err); + + var copayer = wallet.getCopayer(opts.copayerId); if (!copayer) return cb('Copayer not found'); var isValid = self._doVerifyMessageSignature(copayer.xPubKey, opts.message, opts.signature); diff --git a/lib/storage.js b/lib/storage.js index 03aab38..4ce1364 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -27,32 +27,6 @@ Storage.prototype.fetchWallet = function (id, cb) { }); }; -Storage.prototype.fetchCopayer = function (walletId, copayerId, cb) { - this.db.get('wallet-' + walletId + '-copayer-' + copayerId, function (err, data) { - if (err) { - if (err.notFound) return cb(); - return cb(err); - } - return cb(null, Copayer.fromObj(data)); - }); -}; - -Storage.prototype.fetchCopayers = function (walletId, cb) { - var copayers = []; - var key = 'wallet-' + walletId + '-copayer-'; - this.db.createReadStream({ gte: key, lt: key + '~' }) - .on('data', function (data) { - copayers.push(Copayer.fromObj(data.value)); - }) - .on('error', function (err) { - if (err.notFound) return cb(); - return cb(err); - }) - .on('end', function () { - return cb(null, copayers); - }); -}; - Storage.prototype.fetchTx = function (walletId, txProposalId, cb) { this.db.get('wallet-' + walletId + '-tx-' + txProposalId, function (err, data) { if (err) { @@ -67,10 +41,6 @@ Storage.prototype.storeWallet = function (wallet, cb) { this.db.put('wallet-' + wallet.id, wallet, cb); }; -Storage.prototype.storeCopayer = function (walletId, copayer, cb) { - this.db.put('wallet-' + walletId + '-copayer-' + copayer.id, copayer, cb); -}; - Storage.prototype.storeAddress = function (walletId, address, cb) { this.db.put('wallet-' + walletId + '-address-' + address.address, address, cb); };