Browse Source

.

activeAddress
Ivan Socolsky 10 years ago
parent
commit
792d576bbf
  1. 22
      lib/server.js
  2. 17
      lib/storage.js
  3. 4
      test/integration.js

22
lib/server.js

@ -102,7 +102,7 @@ CopayServer.prototype._runLocked = function (walletId, cb, task) {
self._runLocked(opts.walletId, cb, function (cb) { self._runLocked(opts.walletId, cb, function (cb) {
self.getWallet({ id: opts.walletId }, function (err, wallet) { 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 (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return cb('Copayer already in wallet');
if (wallet.copayers.length == wallet.n) return cb('Wallet full'); if (wallet.copayers.length == wallet.n) return cb('Wallet full');
// TODO: validate copayer's extended public key using the public key from this wallet // 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._runLocked(opts.walletId, cb, function (cb) {
self.getWallet({ id: opts.walletId }, function (err, wallet) { self.getWallet({ id: opts.walletId }, function (err, wallet) {
if (err || !wallet) return cb(err); if (err) return cb(err);
var index = wallet.addressIndex++; var index = wallet.addressIndex++;
self.storage.storeWallet(wallet, function (err) { self.storage.storeWallet(wallet, function (err) {
@ -251,7 +251,7 @@ CopayServer.prototype.createTx = function (opts, cb) {
var self = this; var self = this;
self.getWallet({ id: opts.walletId }, function (err, wallet) { 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) { self._getUtxos({ walletId: wallet.id }, function (err, utxos) {
if (err) return cb(err); if (err) return cb(err);
@ -277,7 +277,7 @@ CopayServer.prototype.createTx = function (opts, cb) {
}; };
CopayServer.prototype._broadcastTx = function (rawTx, 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 () {}; cb = cb || function () {};
throw 'not implemented'; throw 'not implemented';
@ -295,7 +295,7 @@ CopayServer.prototype.signTx = function (opts, cb) {
var self = this; var self = this;
self.getWallet({ id: opts.walletId }, function (err, wallet) { 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) { self.fetchTx(wallet.id, opts.txProposalId, function (err, txp) {
if (err) return cb(err); if (err) return cb(err);
@ -336,7 +336,17 @@ CopayServer.prototype.signTx = function (opts, cb) {
CopayServer.prototype.getPendingTxs = function (opts, cb) { CopayServer.prototype.getPendingTxs = function (opts, cb) {
var self = this; 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);
});
});
}; };

17
lib/storage.js

@ -10,6 +10,7 @@ log.debug = log.verbose;
var Wallet = require('./model/wallet'); var Wallet = require('./model/wallet');
var Copayer = require('./model/copayer'); var Copayer = require('./model/copayer');
var Address = require('./model/address'); var Address = require('./model/address');
var TxProposal = require('./model/txproposal');
var Storage = function (opts) { var Storage = function (opts) {
opts = 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) { Storage.prototype._dump = function (cb) {
this.db.readStream() this.db.readStream()
.on('data', console.log) .on('data', console.log)

4
test/integration.js

@ -420,6 +420,10 @@ describe('Copay server', function() {
tx.rawTx.should.equal('raw'); tx.rawTx.should.equal('raw');
tx.isAccepted().should.equal.false; tx.isAccepted().should.equal.false;
tx.isRejected().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(); done();
}); });
}); });

Loading…
Cancel
Save