bitcore-wallet-service/lib/storage.js

120 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-01-27 05:18:45 -08:00
'use strict';
var _ = require('lodash');
var levelup = require('levelup');
var $ = require('preconditions').singleton();
var async = require('async');
var log = require('npmlog');
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-01-27 05:18:45 -08:00
var Storage = function (opts) {
2015-02-02 12:07:18 -08:00
opts = opts || {};
this.db = opts.db || levelup(opts.dbPath || './db/copay.db', { valueEncoding: 'json' });
2015-01-27 05:18:45 -08:00
};
Storage.prototype.fetchWallet = function (id, cb) {
2015-02-02 12:07:18 -08:00
this.db.get('wallet-' + id, function (err, data) {
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-01-29 10:00:35 -08:00
Storage.prototype.storeWallet = function (wallet, cb) {
2015-02-02 12:07:18 -08:00
this.db.put('wallet-' + wallet.id, wallet, cb);
2015-01-29 10:00:35 -08:00
};
2015-02-06 05:46:41 -08:00
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,
signingPubKey: copayer.signingPubKey,
};
ops.push({ type: 'put', 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) {
if (err) {
if (err.notFound) return cb();
return cb(err);
}
return cb(null, data);
});
};
2015-01-28 08:28:18 -08:00
Storage.prototype.fetchTx = function (walletId, txProposalId, cb) {
2015-02-02 12:07:18 -08:00
this.db.get('wallet-' + walletId + '-txp-' + txProposalId, function (err, data) {
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-01-29 10:00:35 -08:00
Storage.prototype.fetchTxs = function (walletId, cb) {
2015-02-02 12:07:18 -08:00
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);
});
2015-01-27 05:18:45 -08:00
};
2015-01-28 12:06:29 -08:00
Storage.prototype.storeTx = function (walletId, txp, cb) {
2015-02-03 18:17:06 -08:00
this.db.put('wallet-' + walletId + '-txp-' + txp.id, txp, cb);
2015-01-28 05:36:49 -08:00
};
2015-01-28 08:28:18 -08:00
Storage.prototype.fetchAddresses = function (walletId, cb) {
2015-02-02 12:07:18 -08:00
var addresses = [];
var key = 'wallet-' + walletId + '-address-';
this.db.createReadStream({ gte: key, lt: key + '~' })
.on('data', function (data) {
addresses.push(Address.fromObj(data.value));
})
.on('error', function (err) {
if (err.notFound) return cb();
return cb(err);
})
.on('end', function () {
return cb(null, addresses);
});
2015-01-27 05:18:45 -08:00
};
2015-01-29 10:00:35 -08:00
Storage.prototype.storeAddress = function (walletId, address, cb) {
2015-02-02 12:07:18 -08:00
this.db.put('wallet-' + walletId + '-address-' + address.address, address, cb);
2015-01-28 12:40:37 -08:00
};
2015-02-03 12:32:40 -08:00
Storage.prototype.removeAddress = function (walletId, address, cb) {
this.db.del('wallet-' + walletId + '-address-' + address.address, cb);
};
2015-01-29 10:00:35 -08:00
2015-01-27 05:18:45 -08:00
Storage.prototype._dump = function (cb) {
this.db.readStream()
.on('data', console.log)
.on('end', function () { if (cb) return cb(); });
};
module.exports = Storage;