|
|
@ -11,6 +11,7 @@ var Wallet = require('./model/wallet'); |
|
|
|
var Copayer = require('./model/copayer'); |
|
|
|
var Address = require('./model/address'); |
|
|
|
var TxProposal = require('./model/txproposal'); |
|
|
|
var Notification = require('./model/notification'); |
|
|
|
|
|
|
|
var Storage = function(opts) { |
|
|
|
opts = opts || {}; |
|
|
@ -44,6 +45,9 @@ var KEY = { |
|
|
|
TXP: function(walletId, txProposalId) { |
|
|
|
return walletPrefix(walletId) + '!txp' + opKey(txProposalId); |
|
|
|
}, |
|
|
|
NOTIFICATION: function(walletId, notificationId) { |
|
|
|
return walletPrefix(walletId) + '!not' + opKey(notificationId); |
|
|
|
}, |
|
|
|
PENDING_TXP: function(walletId, txProposalId) { |
|
|
|
return walletPrefix(walletId) + '!ptxp' + opKey(txProposalId); |
|
|
|
}, |
|
|
@ -107,6 +111,19 @@ Storage.prototype.fetchTx = function(walletId, txProposalId, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Storage.prototype.fetchNotification = function(walletId, notificationId, cb) { |
|
|
|
this.db.get(KEY.NOTIFICATION(walletId, notificationId), function(err, data) { |
|
|
|
if (err) { |
|
|
|
if (err.notFound) return cb(); |
|
|
|
return cb(err); |
|
|
|
} |
|
|
|
return cb(null, Notification.fromObj(data)); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Storage.prototype.fetchPendingTxs = function(walletId, cb) { |
|
|
|
var txs = []; |
|
|
|
var key = KEY.PENDING_TXP(walletId); |
|
|
@ -162,6 +179,49 @@ Storage.prototype.fetchTxs = function(walletId, opts, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* fetchNotifications |
|
|
|
* |
|
|
|
* @param walletId |
|
|
|
* @param opts.minTs |
|
|
|
* @param opts.maxTs |
|
|
|
* @param opts.limit |
|
|
|
*/ |
|
|
|
Storage.prototype.fetchNotifications = function(walletId, opts, cb) { |
|
|
|
var txs = []; |
|
|
|
opts = opts || {}; |
|
|
|
opts.limit = _.isNumber(opts.limit) ? parseInt(opts.limit) : -1; |
|
|
|
opts.minTs = _.isNumber(opts.minTs) ? ('000000000000' + parseInt(opts.minTs)).slice(-12) : 0; |
|
|
|
opts.maxTs = _.isNumber(opts.maxTs) ? ('000000000000' + parseInt(opts.maxTs)).slice(-12) : MAX_TS; |
|
|
|
|
|
|
|
var key = KEY.NOTIFICATION(walletId, opts.minTs); |
|
|
|
var endkey = KEY.NOTIFICATION(walletId, opts.maxTs); |
|
|
|
|
|
|
|
this.db.createReadStream({ |
|
|
|
gt: key, |
|
|
|
lt: endkey + '~', |
|
|
|
reverse: true, |
|
|
|
limit: opts.limit, |
|
|
|
}) |
|
|
|
.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.storeNotification = function(walletId, notification, cb) { |
|
|
|
this.db.put(KEY.NOTIFICATION(walletId, notification.id), notification, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// TODO should we store only txp.id on keys for indexing
|
|
|
|
// or the whole txp? For now, the entire record makes sense
|
|
|
|
// (faster + easier to access)
|
|
|
@ -261,6 +321,7 @@ Storage.prototype.removeWallet = function(walletId, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
async.series([ |
|
|
|
|
|
|
|
function(next) { |
|
|
|
self._removeCopayers(walletId, next); |
|
|
|
}, |
|
|
|