diff --git a/lib/server.js b/lib/server.js index 81535dd..d9d9e07 100644 --- a/lib/server.js +++ b/lib/server.js @@ -102,7 +102,7 @@ CopayServer.prototype._runLocked = function (walletId, cb, task) { self._runLocked(opts.walletId, cb, function (cb) { self.getWallet({ id: opts.walletId }, function (err, wallet) { - if (err || !wallet) return cb(err); + if (err) 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'); // TODO: validate copayer's extended public key using the public key from this wallet @@ -142,7 +142,7 @@ CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) { self._runLocked(opts.walletId, cb, function (cb) { self.getWallet({ id: opts.walletId }, function (err, wallet) { - if (err || !wallet) return cb(err); + if (err) return cb(err); var index = wallet.addressIndex++; self.storage.storeWallet(wallet, function (err) { @@ -251,7 +251,7 @@ CopayServer.prototype.createTx = function (opts, cb) { var self = this; self.getWallet({ id: opts.walletId }, function (err, wallet) { - if (err || !wallet) return cb(err); + if (err) return cb(err); self._getUtxos({ walletId: wallet.id }, function (err, utxos) { if (err) return cb(err); @@ -277,7 +277,7 @@ CopayServer.prototype.createTx = function (opts, cb) { }; CopayServer.prototype._broadcastTx = function (rawTx, cb) { - // TODO: this should attempt to broadcast _all_ accepted txps? + // TODO: this should attempt to broadcast _all_ accepted and not-yet broadcasted (status=='accepted') txps? cb = cb || function () {}; throw 'not implemented'; @@ -295,7 +295,7 @@ CopayServer.prototype.signTx = function (opts, cb) { var self = this; self.getWallet({ id: opts.walletId }, function (err, wallet) { - if (err || !wallet) return cb(err); + if (err) return cb(err); self.fetchTx(wallet.id, opts.txProposalId, function (err, txp) { if (err) return cb(err); @@ -336,7 +336,17 @@ CopayServer.prototype.signTx = function (opts, cb) { CopayServer.prototype.getPendingTxs = function (opts, cb) { var self = this; - throw 'not implemented'; + self.getWallet({ id: opts.walletId }, function (err, wallet) { + if (err) return cb(err); + + self.storage.fetchTxs(wallet.id, function (err, txps) { + if (err) return cb(err); + + var pending = _.filter(txps, { status: 'pending' }); + + return cb(null, pending); + }); + }); }; diff --git a/lib/storage.js b/lib/storage.js index f8b7a03..dc17834 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -10,6 +10,7 @@ log.debug = log.verbose; var Wallet = require('./model/wallet'); var Copayer = require('./model/copayer'); var Address = require('./model/address'); +var TxProposal = require('./model/txproposal'); var Storage = function (opts) { opts = opts || {}; @@ -65,6 +66,22 @@ Storage.prototype.fetchAddresses = function (walletId, cb) { }); }; +Storage.prototype.fetchTxs = function (walletId, cb) { + var txs = []; + var key = 'wallet-' + walletId + '-txp-'; + this.db.createReadStream({ gte: key, lt: key + '~' }) + .on('data', function (data) { + txs.push(TxProposal.fromObj(data.value)); + }) + .on('error', function (err) { + if (err.notFound) return cb(); + return cb(err); + }) + .on('end', function () { + return cb(null, txs); + }); +}; + Storage.prototype._dump = function (cb) { this.db.readStream() .on('data', console.log) diff --git a/test/integration.js b/test/integration.js index 203f5f9..cca8ca8 100644 --- a/test/integration.js +++ b/test/integration.js @@ -420,6 +420,10 @@ describe('Copay server', function() { tx.rawTx.should.equal('raw'); tx.isAccepted().should.equal.false; tx.isRejected().should.equal.false; + server.getPendingTxs({ walletId: '123' }, function (err, txs) { + should.not.exist(err); + txs.length.should.equal(1); + }); done(); }); });