bitcore-wallet-service/lib/storage.js

395 lines
9.0 KiB
JavaScript
Raw Normal View History

2015-01-27 05:18:45 -08:00
'use strict';
var _ = require('lodash');
var levelup = require('levelup');
2015-02-09 13:07:15 -08:00
var async = require('async');
2015-01-27 05:18:45 -08:00
var $ = require('preconditions').singleton();
var log = require('npmlog');
2015-02-12 18:57:16 -08:00
var util = require('util');
2015-01-27 05:18:45 -08:00
log.debug = log.verbose;
var Wallet = require('./model/wallet');
var Copayer = require('./model/copayer');
var Address = require('./model/address');
2015-01-28 12:40:37 -08:00
var TxProposal = require('./model/txproposal');
2015-02-11 10:42:49 -08:00
var Notification = require('./model/notification');
2015-01-27 05:18:45 -08:00
2015-02-06 12:04:10 -08:00
var Storage = function(opts) {
2015-02-02 12:07:18 -08:00
opts = opts || {};
2015-02-06 12:04:10 -08:00
this.db = opts.db || levelup(opts.dbPath || './db/copay.db', {
valueEncoding: 'json'
});
2015-01-27 05:18:45 -08:00
};
2015-02-12 06:39:27 -08:00
var zeroPad = function(x, length) {
return (Array(length).join('0') + parseInt(x)).slice(-length);
};
2015-01-27 05:18:45 -08:00
2015-02-09 13:07:15 -08:00
var walletPrefix = function(id) {
return 'w!' + id;
};
2015-02-06 12:51:21 -08:00
var opKey = function(key) {
return key ? '!' + key : '';
};
2015-02-12 06:39:27 -08:00
var MAX_TS = Array(14).join('9');
2015-02-06 23:09:45 -08:00
var opKeyTs = function(key) {
2015-02-12 06:39:27 -08:00
return key ? '!' + zeroPad(key, 14) : '';
2015-02-06 23:09:45 -08:00
};
2015-02-06 12:26:43 -08:00
var KEY = {
2015-02-09 13:07:15 -08:00
WALLET: function(walletId) {
return walletPrefix(walletId) + '!main';
2015-02-06 12:26:43 -08:00
},
COPAYER: function(id) {
2015-02-06 23:14:53 -08:00
return 'copayer!' + id;
2015-02-06 12:26:43 -08:00
},
2015-02-06 12:51:21 -08:00
TXP: function(walletId, txProposalId) {
2015-02-09 13:07:15 -08:00
return walletPrefix(walletId) + '!txp' + opKey(txProposalId);
2015-02-06 12:26:43 -08:00
},
2015-02-11 10:42:49 -08:00
NOTIFICATION: function(walletId, notificationId) {
return walletPrefix(walletId) + '!not' + opKey(notificationId);
},
2015-02-07 09:15:04 -08:00
PENDING_TXP: function(walletId, txProposalId) {
2015-02-09 13:07:15 -08:00
return walletPrefix(walletId) + '!ptxp' + opKey(txProposalId);
2015-02-06 12:26:43 -08:00
},
ADDRESS: function(walletId, address) {
2015-02-09 13:07:15 -08:00
return walletPrefix(walletId) + '!addr' + opKey(address);
2015-02-06 12:26:43 -08:00
},
};
2015-02-06 12:04:10 -08:00
Storage.prototype.fetchWallet = function(id, cb) {
2015-02-06 12:26:43 -08:00
this.db.get(KEY.WALLET(id), function(err, data) {
2015-02-02 12:07:18 -08:00
if (err) {
if (err.notFound) return cb();
return cb(err);
}
return cb(null, Wallet.fromObj(data));
});
2015-01-27 05:18:45 -08:00
};
2015-02-06 12:04:10 -08:00
Storage.prototype.storeWallet = function(wallet, cb) {
2015-02-06 12:26:43 -08:00
this.db.put(KEY.WALLET(wallet.id), wallet, cb);
2015-01-29 10:00:35 -08:00
};
2015-02-06 12:04:10 -08:00
Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) {
2015-02-06 05:46:41 -08:00
var ops = [];
2015-02-06 12:04:10 -08:00
ops.push({
type: 'put',
2015-02-06 12:26:43 -08:00
key: KEY.WALLET(wallet.id),
2015-02-06 12:04:10 -08:00
value: wallet
});
_.each(wallet.copayers, function(copayer) {
var value = {
walletId: wallet.id,
2015-02-06 05:46:41 -08:00
signingPubKey: copayer.signingPubKey,
};
2015-02-06 12:04:10 -08:00
ops.push({
type: 'put',
2015-02-06 12:26:43 -08:00
key: KEY.COPAYER(copayer.id),
2015-02-06 12:04:10 -08:00
value: value
});
2015-02-06 05:46:41 -08:00
});
this.db.batch(ops, cb);
};
2015-02-06 12:04:10 -08:00
Storage.prototype.fetchCopayerLookup = function(copayerId, cb) {
2015-02-06 12:26:43 -08:00
this.db.get(KEY.COPAYER(copayerId), function(err, data) {
2015-02-06 05:46:41 -08:00
if (err) {
if (err.notFound) return cb();
return cb(err);
}
return cb(null, data);
});
};
2015-02-06 12:04:10 -08:00
Storage.prototype.fetchTx = function(walletId, txProposalId, cb) {
2015-02-06 12:51:21 -08:00
this.db.get(KEY.TXP(walletId, txProposalId), function(err, data) {
2015-02-02 12:07:18 -08:00
if (err) {
if (err.notFound) return cb();
return cb(err);
}
return cb(null, TxProposal.fromObj(data));
});
2015-01-28 08:28:18 -08:00
};
2015-01-27 05:18:45 -08:00
2015-02-11 10:42:49 -08:00
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));
});
};
2015-02-06 12:26:43 -08:00
Storage.prototype.fetchPendingTxs = function(walletId, cb) {
var txs = [];
2015-02-07 09:15:04 -08:00
var key = KEY.PENDING_TXP(walletId);
2015-02-06 12:26:43 -08:00
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);
});
};
2015-02-06 23:09:45 -08:00
/**
2015-02-12 05:26:13 -08:00
* fetchTxs. Times are in UNIX EPOCH (seconds)
2015-02-06 23:09:45 -08:00
*
* @param walletId
* @param opts.minTs
* @param opts.maxTs
* @param opts.limit
*/
Storage.prototype.fetchTxs = function(walletId, opts, cb) {
2015-02-02 12:07:18 -08:00
var txs = [];
2015-02-06 23:09:45 -08:00
opts = opts || {};
2015-02-07 09:15:04 -08:00
opts.limit = _.isNumber(opts.limit) ? parseInt(opts.limit) : -1;
2015-02-12 06:39:27 -08:00
opts.minTs = _.isNumber(opts.minTs) ? zeroPad(opts.minTs, 11) : 0;
opts.maxTs = _.isNumber(opts.maxTs) ? zeroPad(opts.maxTs, 11) : MAX_TS;
2015-02-06 23:09:45 -08:00
2015-02-07 09:15:04 -08:00
var key = KEY.TXP(walletId, opts.minTs);
var endkey = KEY.TXP(walletId, opts.maxTs);
2015-02-06 23:09:45 -08:00
2015-02-06 12:04:10 -08:00
this.db.createReadStream({
2015-02-06 23:09:45 -08:00
gt: key,
lt: endkey + '~',
reverse: true,
limit: opts.limit,
2015-02-06 12:04:10 -08:00
})
.on('data', function(data) {
2015-02-02 12:07:18 -08:00
txs.push(TxProposal.fromObj(data.value));
})
2015-02-06 12:04:10 -08:00
.on('error', function(err) {
2015-02-02 12:07:18 -08:00
if (err.notFound) return cb();
return cb(err);
})
2015-02-06 12:04:10 -08:00
.on('end', function() {
2015-02-02 12:07:18 -08:00
return cb(null, txs);
});
2015-01-27 05:18:45 -08:00
};
2015-02-11 10:42:49 -08:00
/**
* 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;
2015-02-12 18:57:16 -08:00
opts.minTs = _.isNumber(opts.minTs) ? zeroPad(opts.minTs, 11) : 0;
opts.maxTs = _.isNumber(opts.maxTs) ? zeroPad(opts.maxTs, 11) : MAX_TS;
2015-02-11 10:42:49 -08:00
var key = KEY.NOTIFICATION(walletId, opts.minTs);
var endkey = KEY.NOTIFICATION(walletId, opts.maxTs);
this.db.createReadStream({
gt: key,
lt: endkey + '~',
2015-02-12 05:26:13 -08:00
reverse: opts.reverse,
2015-02-11 10:42:49 -08:00
limit: opts.limit,
})
.on('data', function(data) {
2015-02-12 05:26:13 -08:00
txs.push(Notification.fromObj(data.value));
2015-02-11 10:42:49 -08:00
})
.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);
};
2015-02-06 12:26:43 -08:00
// 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)
2015-02-06 12:04:10 -08:00
Storage.prototype.storeTx = function(walletId, txp, cb) {
2015-02-06 14:57:52 -08:00
var ops = [{
type: 'put',
key: KEY.TXP(walletId, txp.id),
value: txp,
}];
if (txp.isPending()) {
ops.push({
type: 'put',
2015-02-07 09:15:04 -08:00
key: KEY.PENDING_TXP(walletId, txp.id),
2015-02-06 14:57:52 -08:00
value: txp,
});
} else {
ops.push({
type: 'del',
2015-02-07 09:15:04 -08:00
key: KEY.PENDING_TXP(walletId, txp.id),
2015-02-06 14:57:52 -08:00
});
}
this.db.batch(ops, cb);
2015-01-28 05:36:49 -08:00
};
2015-02-07 17:31:37 -08:00
Storage.prototype.removeTx = function(walletId, txProposalId, cb) {
var ops = [{
type: 'del',
2015-02-10 13:04:50 -08:00
key: KEY.TXP(walletId, txProposalId),
2015-02-07 17:31:37 -08:00
}, {
type: 'del',
2015-02-10 13:04:50 -08:00
key: KEY.PENDING_TXP(walletId, txProposalId),
2015-02-07 17:31:37 -08:00
}];
this.db.batch(ops, cb);
};
2015-02-09 13:07:15 -08:00
Storage.prototype._delByKey = function(key, cb) {
var self = this;
var keys = [];
this.db.createKeyStream({
gte: key,
2015-02-07 17:31:37 -08:00
lt: key + '~',
})
2015-02-09 13:07:15 -08:00
.on('data', function(key) {
keys.push(key);
})
2015-02-07 17:31:37 -08:00
.on('error', function(err) {
if (err.notFound) return cb();
2015-02-09 13:07:15 -08:00
console.log('[storage.js.252]'); //TODO
2015-02-07 17:31:37 -08:00
return cb(err);
})
2015-02-09 13:07:15 -08:00
.on('end', function(err) {
self.db.batch(_.map(keys, function(k) {
return {
key: k,
type: 'del'
};
}), function(err) {
return cb(err);
});
2015-02-07 17:31:37 -08:00
});
};
2015-02-09 13:07:15 -08:00
Storage.prototype.removeAllPendingTxs = function(walletId, cb) {
this._delByKey(KEY.PENDING_TXP(walletId), cb);
};
2015-02-07 17:31:37 -08:00
Storage.prototype.removeAllTxs = function(walletId, cb) {
2015-02-09 13:07:15 -08:00
this._delByKey(KEY.TXP(walletId), cb);
2015-02-07 17:31:37 -08:00
};
2015-02-09 13:07:15 -08:00
Storage.prototype._removeCopayers = function(walletId, cb) {
var self = this;
this.fetchWallet(walletId, function(err, w) {
2015-02-09 13:26:25 -08:00
if (err || !w) return cb(err);
2015-02-09 13:07:15 -08:00
self.db.batch(_.map(w.copayers, function(c) {
return {
type: 'del',
key: KEY.COPAYER(c.id),
};
}), cb);
});
};
2015-02-11 18:11:30 -08:00
Storage.prototype._removeAllNotifications = function(walletId, cb) {
this._delByKey(KEY.NOTIFICATION(walletId), cb);
};
2015-02-09 13:07:15 -08:00
2015-02-07 17:31:37 -08:00
Storage.prototype._removeAllAddresses = function(walletId, cb) {
2015-02-09 13:07:15 -08:00
this._delByKey(KEY.ADDRESS(walletId), cb);
2015-02-07 17:31:37 -08:00
};
Storage.prototype.removeWallet = function(walletId, cb) {
var self = this;
async.series([
2015-02-11 10:42:49 -08:00
2015-02-09 13:26:25 -08:00
function(next) {
2015-02-11 18:11:30 -08:00
// This should be the first step. Will check the wallet exists
2015-02-09 13:26:25 -08:00
self._removeCopayers(walletId, next);
},
2015-02-07 17:31:37 -08:00
function(next) {
2015-02-11 18:11:30 -08:00
self._delByKey(walletPrefix(walletId), cb);
2015-02-07 17:31:37 -08:00
},
], cb);
};
2015-02-06 12:04:10 -08:00
Storage.prototype.fetchAddresses = function(walletId, cb) {
2015-02-02 12:07:18 -08:00
var addresses = [];
2015-02-06 12:51:21 -08:00
var key = KEY.ADDRESS(walletId);
2015-02-06 12:04:10 -08:00
this.db.createReadStream({
gte: key,
lt: key + '~'
})
.on('data', function(data) {
2015-02-02 12:07:18 -08:00
addresses.push(Address.fromObj(data.value));
})
2015-02-06 12:04:10 -08:00
.on('error', function(err) {
2015-02-02 12:07:18 -08:00
if (err.notFound) return cb();
return cb(err);
})
2015-02-06 12:04:10 -08:00
.on('end', function() {
2015-02-02 12:07:18 -08:00
return cb(null, addresses);
});
2015-01-27 05:18:45 -08:00
};
2015-02-08 15:46:02 -08:00
Storage.prototype.storeAddressAndWallet = function(wallet, address, cb) {
var ops = [{
type: 'put',
key: KEY.WALLET(wallet.id),
value: wallet,
}, {
type: 'put',
key: KEY.ADDRESS(wallet.id, address.address),
value: address,
}, ];
this.db.batch(ops, cb);
2015-01-28 12:40:37 -08:00
};
2015-02-06 12:04:10 -08:00
Storage.prototype.removeAddress = function(walletId, address, cb) {
2015-02-06 14:57:52 -08:00
this.db.del(KEY.ADDRESS(walletId, address.address), cb);
2015-02-03 12:32:40 -08:00
};
2015-01-29 10:00:35 -08:00
2015-02-09 13:07:15 -08:00
Storage.prototype._dump = function(cb, fn) {
fn = fn || console.log;
2015-01-27 05:18:45 -08:00
this.db.readStream()
2015-02-12 18:57:16 -08:00
.on('data', function(data) {
fn(util.inspect(data, {
depth: 10
}));
})
2015-02-06 12:04:10 -08:00
.on('end', function() {
if (cb) return cb();
});
2015-01-27 05:18:45 -08:00
};
module.exports = Storage;