remove storage from Wallet

This commit is contained in:
Matias Alejo Garcia 2014-10-24 09:36:28 -03:00
commit 567cb8c713
8 changed files with 92 additions and 116 deletions

View file

@ -55,6 +55,7 @@ module.exports = function(grunt) {
scripts: { scripts: {
files: [ files: [
'js/models/*.js', 'js/models/*.js',
'js/util/*.js',
'plugins/*.js', 'plugins/*.js',
'js/*.js', 'js/*.js',
'!js/copayBundle.js', '!js/copayBundle.js',

View file

@ -56,7 +56,7 @@ var defaultConfig = {
plugins: { plugins: {
LocalStorage: true, LocalStorage: true,
//GoogleDrive: true, //GoogleDrive: true,
InsightStorage: true //InsightStorage: true
}, },
InsightStorage: { InsightStorage: {

View file

@ -64,10 +64,6 @@ Identity._walletFromObj = function(o, readOpts) {
return Wallet.fromObj(o, readOpts); return Wallet.fromObj(o, readOpts);
}; };
Identity._walletRead = function(id, r, cb) {
return Wallet.read(id, r, cb);
};
Identity._walletDelete = function(id, s, cb) { Identity._walletDelete = function(id, s, cb) {
return Wallet.delete(id, s, cb); return Wallet.delete(id, s, cb);
}; };
@ -105,7 +101,11 @@ Identity._getStorage = function(opts, password) {
*/ */
Identity.anyProfile = function(opts, cb) { Identity.anyProfile = function(opts, cb) {
var storage = Identity._getStorage(opts); var storage = Identity._getStorage(opts);
Profile.any(storage, cb); storage.getFirst(Profile.key(''), {
onlyKey: true
}, function(err, v, k) {
return cb(k ? true : false);
});
}; };
/** /**
@ -116,7 +116,11 @@ Identity.anyProfile = function(opts, cb) {
*/ */
Identity.anyWallet = function(opts, cb) { Identity.anyWallet = function(opts, cb) {
var storage = Identity._getStorage(opts); var storage = Identity._getStorage(opts);
Wallet.any(storage, cb); storage.getFirst(Wallet.getStorageKey(''), {
onlyKey: true
}, function(err, v, k) {
return cb(k ? true : false);
});
}; };
/** /**
@ -249,12 +253,41 @@ Identity.isAvailable = function(email, opts, cb) {
return cb(); return cb();
}; };
Identity.prototype.readWallet = function(walletId, readOpts, cb) {
preconditions.checkArgument(cb);
var self = this,
err;
var obj = {};
this.storage.getFirst(Wallet.getStorageKey(walletId), {}, function(err, obj) {
if (err) return cb(err);
if (!obj)
return cb(new Error('WNOTFOUND: Wallet not found'));
var w, err;
obj.id = walletId;
try {
log.debug('## OPENING Wallet: ' + walletId);
w = Wallet.fromUntrustedObj(obj, readOpts);
} catch (e) {
log.debug("ERROR: ", e.message);
if (e && e.message && e.message.indexOf('MISSOPTS')) {
err = new Error('WERROR: Could not read: ' + walletId + ': ' + e.message);
} else {
err = e;
}
w = null;
}
return cb(err, w);
});
};
Identity.prototype.storeWallet = function(w, cb) { Identity.prototype.storeWallet = function(w, cb) {
preconditions.checkArgument(w && _.isObject(w)); preconditions.checkArgument(w && _.isObject(w));
var id = w.getId(); var id = w.getId();
var val = w.toObj(); var val = w.toObj();
var key = Wallet.getStorageKey(id + '_' + w.getName()); var key = Wallet.getStorageKey(id + '_' + w.getName());
@ -264,7 +297,6 @@ Identity.prototype.storeWallet = function(w, cb) {
if (cb) if (cb)
cb(err); cb(err);
}); });
}; };
@ -343,7 +375,6 @@ Identity.prototype.importWallet = function(base64, password, skipFields, cb) {
var obj = this.storage.decrypt(base64, password); var obj = this.storage.decrypt(base64, password);
var readOpts = { var readOpts = {
storage: this.storage,
networkOpts: this.networkOpts, networkOpts: this.networkOpts,
blockchainOpts: this.blockchainOpts, blockchainOpts: this.blockchainOpts,
skipFields: skipFields skipFields: skipFields
@ -509,7 +540,6 @@ Identity.prototype.createWallet = function(opts, cb) {
log.debug('\t### TxProposals Initialized'); log.debug('\t### TxProposals Initialized');
opts.storage = this.storage;
opts.networkOpts = this.networkOpts; opts.networkOpts = this.networkOpts;
opts.blockchainOpts = this.blockchainOpts; opts.blockchainOpts = this.blockchainOpts;
@ -519,9 +549,6 @@ Identity.prototype.createWallet = function(opts, cb) {
opts.totalCopayers = totalCopayers; opts.totalCopayers = totalCopayers;
opts.version = opts.version || this.version; opts.version = opts.version || this.version;
if (opts.password && !this.storage.hasPassphrase())
this.storage.setPassword(opts.password);
var self = this; var self = this;
var w = Identity._newWallet(opts); var w = Identity._newWallet(opts);
this.addWallet(w, function(err) { this.addWallet(w, function(err) {
@ -592,8 +619,7 @@ Identity.prototype.openWallet = function(walletId, cb) {
// self.migrateWallet(walletId, password, function() { // self.migrateWallet(walletId, password, function() {
// //
Identity._walletRead(walletId, { self.readWallet(walletId, {
storage: self.storage,
networkOpts: this.networkOpts, networkOpts: this.networkOpts,
blockchainOpts: this.blockchainOpts blockchainOpts: this.blockchainOpts
}, function(err, w) { }, function(err, w) {

View file

@ -6,8 +6,6 @@
// this line throws a warning on Chrome Desktop // this line throws a warning on Chrome Desktop
var sjcl = require('../../lib/sjcl'); var sjcl = require('../../lib/sjcl');
console.log('[Passphrase.js.8]'); //TODO
var preconditions = require('preconditions').instance(); var preconditions = require('preconditions').instance();
var _ = require('underscore'); var _ = require('underscore');

View file

@ -46,17 +46,12 @@ Profile.create = function(email, password, storage, cb) {
}; };
Profile.any = function(storage, cb) {
storage.getFirst(Profile.key(''), { onlyKey: true}, function(err, v, k) {
return cb(k ? true : false);
});
};
Profile.open = function(email, password, storage, cb) { Profile.open = function(email, password, storage, cb) {
preconditions.checkArgument(cb); preconditions.checkArgument(cb);
preconditions.checkState(storage.hasPassphrase()); preconditions.checkState(storage.hasPassphrase());
var key = Profile.key(Profile.hash(email, password)); var key = Profile.key(Profile.hash(email, password));
console.log('[Profile.js.59:key:]',key); //TODO
storage.get(key, function(err, val) { storage.get(key, function(err, val) {
if (err || !val) if (err || !val)
return cb(new Error('PNOTFOUND: Profile not found')); return cb(new Error('PNOTFOUND: Profile not found'));

View file

@ -37,7 +37,6 @@ var copayConfig = require('../../config');
* @TODO: Split this leviathan. * @TODO: Split this leviathan.
* *
* @param {Object} opts * @param {Object} opts
* @param {Storage} opts.storage - an object that can persist the wallet
* @param {Network} opts.network - used to send and retrieve messages from * @param {Network} opts.network - used to send and retrieve messages from
* copayers * copayers
* @param {Blockchain} opts.blockchain - source of truth for what happens in * @param {Blockchain} opts.blockchain - source of truth for what happens in
@ -70,7 +69,7 @@ function Wallet(opts) {
opts.blockchain = opts.blockchain || Wallet._newInsight(opts.blockchainOpts[networkName]);; opts.blockchain = opts.blockchain || Wallet._newInsight(opts.blockchainOpts[networkName]);;
//required params //required params
['storage', 'network', 'blockchain', ['network', 'blockchain',
'requiredCopayers', 'totalCopayers', 'spendUnconfirmed', 'requiredCopayers', 'totalCopayers', 'spendUnconfirmed',
'publicKeyRing', 'txProposals', 'privateKey', 'version', 'publicKeyRing', 'txProposals', 'privateKey', 'version',
'reconnectDelay' 'reconnectDelay'
@ -82,7 +81,8 @@ function Wallet(opts) {
this.id = opts.id || Wallet.getRandomId(); this.id = opts.id || Wallet.getRandomId();
this.secretNumber = opts.secretNumber || Wallet.getRandomSecretNumber(); this.secretNumber = opts.secretNumber || Wallet.getRandomSecretNumber();
this.lock = new WalletLock(this.storage, this.id, opts.lockTimeOutMin); // TODO
// this.lock = new WalletLock(this.storage, this.id, opts.lockTimeOutMin);
this.settings = opts.settings || copayConfig.wallet.settings; this.settings = opts.settings || copayConfig.wallet.settings;
this.name = opts.name; this.name = opts.name;
@ -121,9 +121,9 @@ function Wallet(opts) {
inherits(Wallet, events.EventEmitter); inherits(Wallet, events.EventEmitter);
Wallet.prototype.emitAndKeepAlive = function(args) { Wallet.prototype.emitAndKeepAlive = function(args) {
log.debug('Wallet Emitting:',arguments); log.debug('Wallet Emitting:', arguments);
this.keepAlive(); this.keepAlive();
this.emit.apply(this,arguments); this.emit.apply(this, arguments);
}; };
/** /**
@ -179,13 +179,6 @@ Wallet.getStorageKey = function(str) {
return 'wallet::' + str; return 'wallet::' + str;
}; };
Wallet.any = function(storage, cb) {
storage.getFirst(Wallet.getStorageKey(''), { onlyKey: true}, function(err, v, k) {
return cb(k ? true : false);
});
};
/* for stubbing */ /* for stubbing */
Wallet._newInsight = function(opts) { Wallet._newInsight = function(opts) {
return new Insight(opts); return new Insight(opts);
@ -234,65 +227,17 @@ Wallet.getMaxRequiredCopayers = function(totalCopayers) {
* @param cb * @param cb
* @return {undefined} * @return {undefined}
*/ */
Wallet.delete = function(walletId, storage, cb) { // Wallet.delete = function(walletId, storage, cb) {
preconditions.checkArgument(cb); // preconditions.checkArgument(cb);
storage.deletePrefix(Wallet.getStorageKey(walletId), function(err) { // storage.deletePrefix(Wallet.getStorageKey(walletId), function(err) {
if (err && err.message != 'not found') return cb(err); // if (err && err.message != 'not found') return cb(err);
storage.deletePrefix(walletId + '::', function(err) { // storage.deletePrefix(walletId + '::', function(err) {
if (err && err.message != 'not found') return cb(err); // if (err && err.message != 'not found') return cb(err);
return cb(); // return cb();
}); // });
}); // });
}; // };
//
/**
* @desc Retrieve a wallet from storage
*
* @param {string} walletId - the wallet id
* @param readOpts (see fromObj)
* @param {function} callback - {err, Wallet}
* @return {undefined}
*/
Wallet.read = function(walletId, readOpts, cb) {
preconditions.checkArgument(readOpts);
preconditions.checkArgument(readOpts.storage);
preconditions.checkArgument(readOpts.storage.setPassword);
preconditions.checkArgument(cb);
var storage = readOpts.storage;
var self = this,
err;
var obj = {};
storage.getFirst(Wallet.getStorageKey(walletId), {}, function(err, ret) {
if (err) return cb(err);
if (!ret)
return cb(new Error('WNOTFOUND: Wallet not found'));
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
obj[p] = ret[p];
});
var w, err;
obj.id = walletId;
try {
log.debug('## OPENING Wallet: ' + walletId);
w = self.fromObj(obj, readOpts);
} catch (e) {
log.debug("ERROR: ", e.message);
if (e && e.message && e.message.indexOf('MISSOPTS')) {
err = new Error('WERROR: Could not read: ' + walletId + ': ' + e.message);
} else {
err = e;
}
w = null;
}
return cb(err, w);
});
};
/** /**
@ -1024,12 +969,13 @@ Wallet.prototype.getRegisteredPeerIds = function() {
Wallet.prototype.keepAlive = function() { Wallet.prototype.keepAlive = function() {
var self = this; var self = this;
this.lock.keepAlive(function(err) {
if (err) { // this.lock.keepAlive(function(err) {
log.debug(err); // if (err) {
self.emitAndKeepAlive('locked', null, 'Wallet appears to be openned on other browser instance. Closing this one.'); // log.debug(err);
} // self.emitAndKeepAlive('locked', null, 'Wallet appears to be openned on other browser instance. Closing this one.');
}); // }
// });
}; };
@ -1061,6 +1007,19 @@ Wallet.prototype.toObj = function() {
return walletObj; return walletObj;
}; };
Wallet.fromUntrustedObj = function(obj, readOpts) {
obj = _.clone(obj);
var o = {};
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
o[p] = obj[p];
});
return Wallet.fromObj(o,readOpts);
};
/** /**
* @desc Retrieve the wallet state from a trusted object * @desc Retrieve the wallet state from a trusted object
* *
@ -1073,7 +1032,6 @@ Wallet.prototype.toObj = function() {
* @param {Object} o.txProposals - TxProposals to be deserialized by {@link TxProposals#fromObj} * @param {Object} o.txProposals - TxProposals to be deserialized by {@link TxProposals#fromObj}
* @param {string} o.nickname - user's nickname * @param {string} o.nickname - user's nickname
* *
* @param readOpts.storage
* @param readOpts.network * @param readOpts.network
* @param readOpts.blockchain * @param readOpts.blockchain
* @param readOpts.isImported {boolean} - tag wallet as 'imported' (skip forced backup step) * @param readOpts.isImported {boolean} - tag wallet as 'imported' (skip forced backup step)
@ -1083,9 +1041,7 @@ Wallet.fromObj = function(o, readOpts) {
preconditions.checkArgument(readOpts.networkOpts); preconditions.checkArgument(readOpts.networkOpts);
preconditions.checkArgument(readOpts.blockchainOpts); preconditions.checkArgument(readOpts.blockchainOpts);
preconditions.checkArgument(readOpts.storage.setPassword);
var storage = readOpts.storage;
var networkOpts = readOpts.networkOpts; var networkOpts = readOpts.networkOpts;
var blockchainOpts = readOpts.blockchainOpts; var blockchainOpts = readOpts.blockchainOpts;
var skipFields = readOpts.skipFields || []; var skipFields = readOpts.skipFields || [];
@ -1147,7 +1103,6 @@ Wallet.fromObj = function(o, readOpts) {
opts.lastTimestamp = o.lastTimestamp || 0; opts.lastTimestamp = o.lastTimestamp || 0;
opts.storage = storage;
opts.blockchainOpts = readOpts.blockchainOpts; opts.blockchainOpts = readOpts.blockchainOpts;
opts.networkOpts = readOpts.networkOpts; opts.networkOpts = readOpts.networkOpts;
opts.isImported = readOpts.isImported || false; opts.isImported = readOpts.isImported || false;
@ -1159,10 +1114,10 @@ Wallet.fromObj = function(o, readOpts) {
* @desc Return a base64 encrypted version of the wallet * @desc Return a base64 encrypted version of the wallet
* @return {string} base64 encoded string * @return {string} base64 encoded string
*/ */
Wallet.prototype.export = function() { // Wallet.prototype.export = function() {
var walletObj = this.toObj(); // var walletObj = this.toObj();
return this.storage.encrypt(walletObj); // return this.storage.encrypt(walletObj);
}; // };
/** /**
* @desc Send a message to other peers * @desc Send a message to other peers
@ -1817,7 +1772,7 @@ Wallet.prototype.sendPaymentTx = function(ntxid, options, cb) {
log.debug('XHR status: ' + status); log.debug('XHR status: ' + status);
return self._checkSentTx(ntxid, function(txid) { return self._checkSentTx(ntxid, function(txid) {
log.debug('[Wallet.js.1581:txid:%s]', txid); log.debug('[Wallet.js.1581:txid:%s]', txid);
if (txid) if (txid)
self.emitAndKeepAlive('hasChange'); self.emitAndKeepAlive('hasChange');
return cb(txid, txp.merchant); return cb(txid, txp.merchant);
}); });
@ -1850,7 +1805,7 @@ Wallet.prototype.receivePaymentRequestACK = function(ntxid, tx, txp, ack, cb) {
log.debug('Sending to server was not met with a returned tx.'); log.debug('Sending to server was not met with a returned tx.');
return this._checkSentTx(ntxid, function(txid) { return this._checkSentTx(ntxid, function(txid) {
log.debug('[Wallet.js.1613:txid:%s]', txid); log.debug('[Wallet.js.1613:txid:%s]', txid);
if (txid) if (txid)
self.emitAndKeepAlive('hasChange'); self.emitAndKeepAlive('hasChange');
return cb(txid, txp.merchant); return cb(txid, txp.merchant);
}); });
@ -2613,9 +2568,10 @@ Wallet.prototype.close = function(cb) {
this.blockchain.destroy(); this.blockchain.destroy();
log.debug('## CLOSING Wallet: ' + this.id); log.debug('## CLOSING Wallet: ' + this.id);
this.lock.release(function() { // TODO
// this.lock.release(function() {
if (cb) return cb(); if (cb) return cb();
}); // });
}; };
/** /**

View file

@ -47,7 +47,7 @@ factory('notification', ['$timeout',
}; };
function html5Notify(icon, title, content, ondisplay, onclose) { function html5Notify(icon, title, content, ondisplay, onclose) {
if (window.webkitNotifications.checkPermission() === 0) { if (window.webkitNotifications && window.webkitNotifications.checkPermission() === 0) {
if (!icon) { if (!icon) {
icon = 'img/favicon.ico'; icon = 'img/favicon.ico';
} }

View file

@ -1,7 +1,7 @@
/** /**
* Small module for some helpers that wrap CryptoJS with some good practices. * Small module for some helpers that wrap sjcl with some good practices.
*/ */
var sjcl = require('sjcl'); var sjcl = require('../../lib/sjcl');
var log = require('../log.js'); var log = require('../log.js');
var _ = require('underscore'); var _ = require('underscore');