|
|
@ -3,7 +3,6 @@ |
|
|
|
var _ = require('lodash'); |
|
|
|
var levelup = require('levelup'); |
|
|
|
var $ = require('preconditions').singleton(); |
|
|
|
var async = require('async'); |
|
|
|
var log = require('npmlog'); |
|
|
|
log.debug = log.verbose; |
|
|
|
|
|
|
@ -12,14 +11,48 @@ var Copayer = require('./model/copayer'); |
|
|
|
var Address = require('./model/address'); |
|
|
|
var TxProposal = require('./model/txproposal'); |
|
|
|
|
|
|
|
var Storage = function (opts) { |
|
|
|
var Storage = function(opts) { |
|
|
|
opts = opts || {}; |
|
|
|
this.db = opts.db || levelup(opts.dbPath || './db/copay.db', { valueEncoding: 'json' }); |
|
|
|
this.db = opts.db || levelup(opts.dbPath || './db/copay.db', { |
|
|
|
valueEncoding: 'json' |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var opKey = function(key) { |
|
|
|
return key ? '!' + key : ''; |
|
|
|
}; |
|
|
|
|
|
|
|
var MAX_TS = '999999999999'; |
|
|
|
var opKeyTs = function(key) { |
|
|
|
return key ? '!' + ('000000000000'+key).slice(-12) : ''; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Storage.prototype.fetchWallet = function (id, cb) { |
|
|
|
this.db.get('wallet-' + id, function (err, data) { |
|
|
|
var KEY = { |
|
|
|
WALLET: function(id) { |
|
|
|
return 'wallet!' + id; |
|
|
|
}, |
|
|
|
COPAYER: function(id) { |
|
|
|
return 'copayer!' + id; |
|
|
|
}, |
|
|
|
TXP: function(walletId, txProposalId) { |
|
|
|
return 'txp!' + walletId + opKey(txProposalId); |
|
|
|
}, |
|
|
|
TXP_BY_TS: function(walletId, ts, txProposalId) { |
|
|
|
return 'txp-ts!' + walletId + opKeyTs(ts) + opKey(txProposalId); |
|
|
|
}, |
|
|
|
PENDING_TXP_BY_TS: function(walletId, ts, txProposalId) { |
|
|
|
return 'pending-txp-ts!' + walletId + opKey(ts) + opKey(txProposalId); |
|
|
|
}, |
|
|
|
ADDRESS: function(walletId, address) { |
|
|
|
return 'address!' + walletId + opKey(address); |
|
|
|
}, |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.fetchWallet = function(id, cb) { |
|
|
|
this.db.get(KEY.WALLET(id), function(err, data) { |
|
|
|
if (err) { |
|
|
|
if (err.notFound) return cb(); |
|
|
|
return cb(err); |
|
|
@ -28,25 +61,33 @@ Storage.prototype.fetchWallet = function (id, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.storeWallet = function (wallet, cb) { |
|
|
|
this.db.put('wallet-' + wallet.id, wallet, cb); |
|
|
|
Storage.prototype.storeWallet = function(wallet, cb) { |
|
|
|
this.db.put(KEY.WALLET(wallet.id), wallet, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.storeWalletAndUpdateCopayersLookup = function (wallet, cb) { |
|
|
|
Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) { |
|
|
|
var ops = []; |
|
|
|
ops.push({ type: 'put', key: 'wallet-' + wallet.id, value: wallet }); |
|
|
|
_.each(wallet.copayers, function (copayer) { |
|
|
|
var value = { |
|
|
|
walletId: wallet.id, |
|
|
|
ops.push({ |
|
|
|
type: 'put', |
|
|
|
key: KEY.WALLET(wallet.id), |
|
|
|
value: wallet |
|
|
|
}); |
|
|
|
_.each(wallet.copayers, function(copayer) { |
|
|
|
var value = { |
|
|
|
walletId: wallet.id, |
|
|
|
signingPubKey: copayer.signingPubKey, |
|
|
|
}; |
|
|
|
ops.push({ type: 'put', key: 'copayer-' + copayer.id, value: value }); |
|
|
|
ops.push({ |
|
|
|
type: 'put', |
|
|
|
key: KEY.COPAYER(copayer.id), |
|
|
|
value: value |
|
|
|
}); |
|
|
|
}); |
|
|
|
this.db.batch(ops, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.fetchCopayerLookup = function (copayerId, cb) { |
|
|
|
this.db.get('copayer-' + copayerId, function (err, data) { |
|
|
|
Storage.prototype.fetchCopayerLookup = function(copayerId, cb) { |
|
|
|
this.db.get(KEY.COPAYER(copayerId), function(err, data) { |
|
|
|
if (err) { |
|
|
|
if (err.notFound) return cb(); |
|
|
|
return cb(err); |
|
|
@ -55,8 +96,8 @@ Storage.prototype.fetchCopayerLookup = function (copayerId, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.fetchTx = function (walletId, txProposalId, cb) { |
|
|
|
this.db.get('wallet-' + walletId + '-txp-' + txProposalId, function (err, data) { |
|
|
|
Storage.prototype.fetchTx = function(walletId, txProposalId, cb) { |
|
|
|
this.db.get(KEY.TXP(walletId, txProposalId), function(err, data) { |
|
|
|
if (err) { |
|
|
|
if (err.notFound) return cb(); |
|
|
|
return cb(err); |
|
|
@ -65,55 +106,124 @@ Storage.prototype.fetchTx = function (walletId, txProposalId, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.fetchTxs = function (walletId, cb) { |
|
|
|
Storage.prototype.fetchPendingTxs = function(walletId, cb) { |
|
|
|
var txs = []; |
|
|
|
var key = KEY.PENDING_TXP_BY_TS(walletId); |
|
|
|
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); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* fetchTxs |
|
|
|
* |
|
|
|
* @param walletId |
|
|
|
* @param opts.minTs |
|
|
|
* @param opts.maxTs |
|
|
|
* @param opts.limit |
|
|
|
*/ |
|
|
|
Storage.prototype.fetchTxs = function(walletId, opts, cb) { |
|
|
|
var txs = []; |
|
|
|
var key = 'wallet-' + walletId + '-txp-'; |
|
|
|
this.db.createReadStream({ gte: key, lt: key + '~' }) |
|
|
|
.on('data', function (data) { |
|
|
|
opts = opts || {}; |
|
|
|
opts.limit = opts.limit || -1; |
|
|
|
opts.minTs = opts.minTs || 0; |
|
|
|
opts.maxTs = opts.maxTs || MAX_TS; |
|
|
|
|
|
|
|
var key = KEY.TXP_BY_TS(walletId, opts.minTs); |
|
|
|
var endkey = KEY.TXP_BY_TS(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) { |
|
|
|
.on('error', function(err) { |
|
|
|
if (err.notFound) return cb(); |
|
|
|
return cb(err); |
|
|
|
}) |
|
|
|
.on('end', function () { |
|
|
|
.on('end', function() { |
|
|
|
return cb(null, txs); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.storeTx = function (walletId, txp, cb) { |
|
|
|
this.db.put('wallet-' + walletId + '-txp-' + txp.id, txp, 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)
|
|
|
|
Storage.prototype.storeTx = function(walletId, txp, cb) { |
|
|
|
var ops = [{ |
|
|
|
type: 'put', |
|
|
|
key: KEY.TXP(walletId, txp.id), |
|
|
|
value: txp, |
|
|
|
}, { |
|
|
|
type: 'put', |
|
|
|
key: KEY.TXP_BY_TS(walletId, txp.createdOn, txp.id), |
|
|
|
value: txp, |
|
|
|
}]; |
|
|
|
|
|
|
|
if (txp.isPending()) { |
|
|
|
ops.push({ |
|
|
|
type: 'put', |
|
|
|
key: KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id), |
|
|
|
value: txp, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
ops.push({ |
|
|
|
type: 'del', |
|
|
|
key: KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id), |
|
|
|
}); |
|
|
|
} |
|
|
|
this.db.batch(ops, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.fetchAddresses = function (walletId, cb) { |
|
|
|
Storage.prototype.fetchAddresses = function(walletId, cb) { |
|
|
|
var addresses = []; |
|
|
|
var key = 'wallet-' + walletId + '-address-'; |
|
|
|
this.db.createReadStream({ gte: key, lt: key + '~' }) |
|
|
|
.on('data', function (data) { |
|
|
|
var key = KEY.ADDRESS(walletId); |
|
|
|
this.db.createReadStream({ |
|
|
|
gte: key, |
|
|
|
lt: key + '~' |
|
|
|
}) |
|
|
|
.on('data', function(data) { |
|
|
|
addresses.push(Address.fromObj(data.value)); |
|
|
|
}) |
|
|
|
.on('error', function (err) { |
|
|
|
.on('error', function(err) { |
|
|
|
if (err.notFound) return cb(); |
|
|
|
return cb(err); |
|
|
|
}) |
|
|
|
.on('end', function () { |
|
|
|
.on('end', function() { |
|
|
|
return cb(null, addresses); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.storeAddress = function (walletId, address, cb) { |
|
|
|
this.db.put('wallet-' + walletId + '-address-' + address.address, address, cb); |
|
|
|
Storage.prototype.storeAddress = function(walletId, address, cb) { |
|
|
|
this.db.put(KEY.ADDRESS(walletId, address.address), address, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.removeAddress = function (walletId, address, cb) { |
|
|
|
this.db.del('wallet-' + walletId + '-address-' + address.address, cb); |
|
|
|
Storage.prototype.removeAddress = function(walletId, address, cb) { |
|
|
|
this.db.del(KEY.ADDRESS(walletId, address.address), cb); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Storage.prototype._dump = function (cb) { |
|
|
|
Storage.prototype._dump = function(cb) { |
|
|
|
this.db.readStream() |
|
|
|
.on('data', console.log) |
|
|
|
.on('end', function () { if (cb) return cb(); }); |
|
|
|
.on('end', function() { |
|
|
|
if (cb) return cb(); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = Storage; |
|
|
|