wallet listing working

This commit is contained in:
Matias Alejo Garcia 2014-09-03 01:25:08 -03:00
commit 6efa4f86de
14 changed files with 538 additions and 240 deletions

View file

@ -43,6 +43,7 @@ module.exports = function(grunt) {
scripts: { scripts: {
files: [ files: [
'js/models/**/*.js', 'js/models/**/*.js',
'js/models/*.js',
'plugins/*.js', 'plugins/*.js',
'copay.js', 'copay.js',
'utils/*.js' 'utils/*.js'

View file

@ -53,12 +53,13 @@ var defaultConfig = {
verbose: 1, verbose: 1,
plugins: { plugins: {
LocalStorage: true, // LocalStorage: true,
// GoogleDrive: true, GoogleDrive: true,
}, },
GoogleDrive: { GoogleDrive: {
clientId: '1', clientId: '232630733383-29u1khqf5i8qubhf0homhpb2m14b5lja.apps.googleusercontent.com',
home: 'copay'
}, },
}; };
if (typeof module !== 'undefined') if (typeof module !== 'undefined')

View file

@ -37,11 +37,10 @@ var modules = [
'copayApp.directives', 'copayApp.directives',
]; ];
if (config.plugins.length) if (Object.keys(config.plugins).length)
modules.push('angularLoad'); modules.push('angularLoad');
var copayApp = window.copayApp = angular.module('copayApp', modules); var copayApp = window.copayApp = angular.module('copayApp', modules);
copayApp.config(function($sceDelegateProvider) { copayApp.config(function($sceDelegateProvider) {

View file

@ -1,10 +1,13 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('HomeController', angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, walletFactory, notification, controllerUtils) {
function($scope, $rootScope, $location, walletFactory, notification, controllerUtils) {
controllerUtils.redirIfLogged();
controllerUtils.redirIfLogged();
$scope.loading = true;
walletFactory.getWallets(function(ret) {
$scope.loading = false; $scope.loading = false;
$scope.hasWallets = (walletFactory.getWallets() && walletFactory.getWallets().length > 0) ? true : false; $scope.hasWallets = (ret && ret.length > 0) ? true : false;
}); });
});

View file

@ -14,12 +14,18 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc
}; };
$rootScope.fromSetup = false; $rootScope.fromSetup = false;
$scope.loading = false; $scope.loading = false;
$scope.wallets = walletFactory.getWallets().sort(cmp); walletFactory.getWallets(function(wallets) {
$scope.selectedWalletId = walletFactory.storage.getLastOpened() || ($scope.wallets[0] && $scope.wallets[0].id); $scope.wallets = wallets.sort(cmp);
});
walletFactory.storage.getLastOpened(function(ret) {
$scope.selectedWalletId = ret || ($scope.wallets[0] && $scope.wallets[0].id);
});
$scope.openPassword = ''; $scope.openPassword = '';
$scope.isMobile = !!window.cordova; $scope.isMobile = !!window.cordova;
if (!$scope.wallets.length){ if (!$scope.wallets.length) {
$location.path('/'); $location.path('/');
} }
@ -34,19 +40,15 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc
Passphrase.getBase64Async(password, function(passphrase) { Passphrase.getBase64Async(password, function(passphrase) {
var w, errMsg; var w, errMsg;
try { walletFactory.open($scope.selectedWalletId, passphrase, function(err, w) {
w = walletFactory.open($scope.selectedWalletId, passphrase); if (!w) {
} catch (e) { $scope.loading = false;
errMsg = e.message; notification.error('Error', err.errMsg || 'Wrong password');
}; $rootScope.$digest();
if (!w) { }
$scope.loading = false; $rootScope.updatingBalance = true;
notification.error('Error', errMsg || 'Wrong password'); controllerUtils.startNetwork(w, $scope);
$rootScope.$digest(); });
return;
}
$rootScope.updatingBalance = true;
controllerUtils.startNetwork(w, $scope);
}); });
}; };

View file

@ -1,5 +1,5 @@
'use strict'; 'use strict';
var preconditions = require('preconditions').singleton();
var CryptoJS = require('node-cryptojs-aes').CryptoJS; var CryptoJS = require('node-cryptojs-aes').CryptoJS;
var bitcore = require('bitcore'); var bitcore = require('bitcore');
var preconditions = require('preconditions').instance(); var preconditions = require('preconditions').instance();
@ -55,38 +55,50 @@ Storage.prototype._decrypt = function(base64) {
}; };
Storage.prototype._read = function(k) { Storage.prototype._read = function(k, cb) {
var ret; preconditions.checkArgument(cb);
ret = this.storage.getItem(k);
if (!ret) return null; var self = this;
ret = this._decrypt(ret); this.storage.getItem(k, function(ret) {
if (!ret) return null; if (!ret) return cb(null);
ret = ret.toString(CryptoJS.enc.Utf8); var ret = self._decrypt(ret);
ret = JSON.parse(ret); if (!ret) return cb(null);
return ret;
ret = ret.toString(CryptoJS.enc.Utf8);
ret = JSON.parse(ret);
return cb(ret);
});
}; };
Storage.prototype._write = function(k, v) { Storage.prototype._write = function(k, v, cb) {
preconditions.checkArgument(cb);
v = JSON.stringify(v); v = JSON.stringify(v);
v = this._encrypt(v); v = this._encrypt(v);
this.storage.setItem(k, v, cb);
this.storage.setItem(k, v);
}; };
// get value by key // get value by key
Storage.prototype.getGlobal = function(k) { Storage.prototype.getGlobal = function(k, cb) {
var item = this.storage.getItem(k); preconditions.checkArgument(cb);
return item == 'undefined' ? undefined : item;
this.storage.getItem(k, function(item) {
cb(item == 'undefined' ? undefined : item);
});
}; };
// set value for key // set value for key
Storage.prototype.setGlobal = function(k, v) { Storage.prototype.setGlobal = function(k, v, cb) {
this.storage.setItem(k, typeof v === 'object' ? JSON.stringify(v) : v); preconditions.checkArgument(cb);
this.storage.setItem(k, typeof v === 'object' ? JSON.stringify(v) : v, cb);
}; };
// remove value for key // remove value for key
Storage.prototype.removeGlobal = function(k) { Storage.prototype.removeGlobal = function(k, cb) {
this.storage.removeItem(k); preconditions.checkArgument(cb);
this.storage.removeItem(k, cb);
}; };
Storage.prototype.getSessionId = function() { Storage.prototype.getSessionId = function() {
@ -102,23 +114,38 @@ Storage.prototype._key = function(walletId, k) {
return walletId + '::' + k; return walletId + '::' + k;
}; };
// get value by key // get value by key
Storage.prototype.get = function(walletId, k) { Storage.prototype.get = function(walletId, k, cb) {
var ret = this._read(this._key(walletId, k)); this._read(this._key(walletId, k), cb);
return ret; };
Storage.prototype.getMany = function(walletId, keys, cb) {
preconditions.checkArgument(cb);
var self = this;
var ret = {};
var l = keys.length - 1;
for (var ii in keys) {
var k = keys[ii];
this._read(this._key(walletId, k), function(v) {
ret[k] = v;
if (ii == l) return cb(ret);
});
}
}; };
// set value for key // set value for key
Storage.prototype.set = function(walletId, k, v) { Storage.prototype.set = function(walletId, k, v, cb) {
this._write(this._key(walletId, k), v); this._write(this._key(walletId, k), v, cb);
}; };
// remove value for key // remove value for key
Storage.prototype.remove = function(walletId, k) { Storage.prototype.remove = function(walletId, k, cb) {
this.removeGlobal(this._key(walletId, k)); this.removeGlobal(this._key(walletId, k), cb);
}; };
Storage.prototype.setName = function(walletId, name) { Storage.prototype.setName = function(walletId, name, cb) {
this.setGlobal('nameFor::' + walletId, name); this.setGlobal('nameFor::' + walletId, name, cb);
}; };
Storage.prototype.getName = function(walletId) { Storage.prototype.getName = function(walletId) {
@ -126,41 +153,54 @@ Storage.prototype.getName = function(walletId) {
return ret; return ret;
}; };
Storage.prototype.getWalletIds = function() { Storage.prototype.getWalletIds = function(cb) {
var walletIds = []; var walletIds = [];
var uniq = {}; var uniq = {};
console.log('[Encrypted.js.144]', this.storage); //TODO this.storage.allKeys(function(keys) {
console.log('[Encrypted.js.144]', this.storage.length); //TODO console.log('[Storage.js.170:keys:]',keys); //TODO
for (var i = 0; i < this.storage.length; i++) {
var key = this.storage.key(i);
var split = key.split('::');
if (split.length == 2) {
var walletId = split[0];
if (!walletId || walletId === 'nameFor' || walletId === 'lock') for (var ii in keys) {
continue; var key = keys[ii];
console.log('[Storage.js.174:key:]',key); //TODO
var split = key.split('::');
if (split.length == 2) {
var walletId = split[0];
if (typeof uniq[walletId] === 'undefined') { if (!walletId || walletId === 'nameFor' || walletId === 'lock')
walletIds.push(walletId); continue;
uniq[walletId] = 1;
if (typeof uniq[walletId] === 'undefined') {
walletIds.push(walletId);
uniq[walletId] = 1;
}
} }
} }
} return cb(walletIds);
return walletIds; });
}; };
Storage.prototype.getWallets = function() { Storage.prototype.getWallets = function(cb) {
var wallets = []; var wallets = [];
var ids = this.getWalletIds(); var self = this;
for (var i in ids) { this.getWalletIds(function(ids) {
wallets.push({ var l = ids.length - 1;
id: ids[i], if (!l)
name: this.getName(ids[i]), return cb([]);
});
} for (var i in ids) {
return wallets; self.getName(ids[i], function(name) {
wallets.push({
id: ids[i],
name: name,
});
if (i == l) {
return cb(wallets);
}
})
}
});
}; };
Storage.prototype.deleteWallet = function(walletId) { Storage.prototype.deleteWallet = function(walletId) {
@ -179,25 +219,35 @@ Storage.prototype.deleteWallet = function(walletId) {
} }
}; };
Storage.prototype.setLastOpened = function(walletId) { Storage.prototype.setLastOpened = function(walletId, cb) {
this.setGlobal('lastOpened', walletId); this.setGlobal('lastOpened', walletId, cb);
} }
Storage.prototype.getLastOpened = function() { Storage.prototype.getLastOpened = function(cb) {
return this.getGlobal('lastOpened'); this.getGlobal('lastOpened', cb);
} }
//obj contains keys to be set //obj contains keys to be set
Storage.prototype.setFromObj = function(walletId, obj) { Storage.prototype.setFromObj = function(walletId, obj, cb) {
preconditions.checkArgument(cb);
var self = this;
console.log('[Storage.js.241]'); //TODO
var l = Object.keys(obj).length,
i = 0;
for (var k in obj) { for (var k in obj) {
this.set(walletId, k, obj[k]); console.log('[Storage.js.247]', k, i, l); //TODO
self.set(walletId, k, obj[k], function() {
if (++i == l) {
self.setName(walletId, obj.opts.name, cb);
}
});
} }
this.setName(walletId, obj.opts.name);
}; };
// remove all values // remove all values
Storage.prototype.clearAll = function() { Storage.prototype.clearAll = function(cb) {
this.storage.clear(); this.storage.clear(cb);
}; };
Storage.prototype.import = function(base64) { Storage.prototype.import = function(base64) {

View file

@ -3,8 +3,9 @@ var preconditions = require('preconditions').singleton();
function PluginManager(config) { function PluginManager(config) {
this.registered = {}; this.registered = {};
this.scripts = [];
for(var ii in config.plugins){ for (var ii in config.plugins) {
var pluginName = ii; var pluginName = ii;
if (!config.plugins[pluginName]) if (!config.plugins[pluginName])
@ -12,7 +13,7 @@ function PluginManager(config) {
console.log('Loading plugin: ' + pluginName); console.log('Loading plugin: ' + pluginName);
var pluginClass = require('../plugins/' + pluginName); var pluginClass = require('../plugins/' + pluginName);
var pluginObj = new pluginClass(); var pluginObj = new pluginClass(config[pluginName]);
pluginObj.init(); pluginObj.init();
this._register(pluginObj, pluginName); this._register(pluginObj, pluginName);
} }
@ -25,9 +26,10 @@ PluginManager.TYPE = {};
PluginManager.TYPE['STORAGE'] = KIND_UNIQUE; PluginManager.TYPE['STORAGE'] = KIND_UNIQUE;
PluginManager.prototype._register = function(obj, name) { PluginManager.prototype._register = function(obj, name) {
preconditions.checkArgument(obj.type,'Plugin has not type:' + name); preconditions.checkArgument(obj.type, 'Plugin has not type:' + name);
var type = obj.type; var type = obj.type;
var kind = PluginManager.TYPE[type]; var kind = PluginManager.TYPE[type];
preconditions.checkArgument(kind, 'Plugin has unkown type' + name); preconditions.checkArgument(kind, 'Plugin has unkown type' + name);
preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered: ' + name); preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered: ' + name);
@ -37,8 +39,11 @@ PluginManager.prototype._register = function(obj, name) {
this.registered[type] = this.registered[type] || []; this.registered[type] = this.registered[type] || [];
this.registered[type].push(obj); this.registered[type].push(obj);
} }
this.scripts = this.scripts.concat(obj.scripts || []);
}; };
PluginManager.prototype.get = function(type) { PluginManager.prototype.get = function(type) {
return this.registered[type]; return this.registered[type];
}; };

View file

@ -821,21 +821,28 @@ Wallet.prototype.getRegisteredPeerIds = function() {
* @emits locked - in case the wallet is opened in another instance * @emits locked - in case the wallet is opened in another instance
*/ */
Wallet.prototype.keepAlive = function() { Wallet.prototype.keepAlive = function() {
try { var self = this;
this.lock.keepAlive();
} catch (e) { this.lock.keepAlive(function(err) {
log.debug(e); if (err) {
this.emit('locked', null, 'Wallet appears to be openned on other browser instance. Closing this one.'); log.debug(err);
} self.emit('locked', null, 'Wallet appears to be openned on other browser instance. Closing this one.');
}
});
}; };
/** /**
* @desc Store the wallet's state * @desc Store the wallet's state
* @param {function} callback (err)
*/ */
Wallet.prototype.store = function() { Wallet.prototype.store = function(cb) {
var self = this;
this.keepAlive(); this.keepAlive();
this.storage.setFromObj(this.id, this.toObj()); this.storage.setFromObj(this.id, this.toObj(), function(err) {
log.debug('Wallet stored'); log.debug('Wallet stored');
if (cb)
cb(err);
});
}; };
/** /**
@ -2339,11 +2346,14 @@ Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb)
/** /**
* @desc Closes the wallet and disconnects all services * @desc Closes the wallet and disconnects all services
*/ */
Wallet.prototype.close = function() { Wallet.prototype.close = function(cb) {
var self =this;
log.debug('## CLOSING'); log.debug('## CLOSING');
this.lock.release(); this.lock.release(function() {
this.network.cleanUp(); self.network.cleanUp();
this.blockchain.destroy(); self.blockchain.destroy();
if (cb) return cb();
});
}; };
/** /**

View file

@ -1,4 +1,5 @@
'use strict'; 'use strict';
var preconditions = require('preconditions').singleton();
var TxProposals = require('./TxProposals'); var TxProposals = require('./TxProposals');
var PublicKeyRing = require('./PublicKeyRing'); var PublicKeyRing = require('./PublicKeyRing');
@ -6,7 +7,7 @@ var PrivateKey = require('./PrivateKey');
var Wallet = require('./Wallet'); var Wallet = require('./Wallet');
var _ = require('underscore'); var _ = require('underscore');
var log = require('../../log'); var log = require('../../log');
var PluginManager = require('./PluginManager'); var PluginManager = require('./PluginManager');
var Async = module.exports.Async = require('../network/Async'); var Async = module.exports.Async = require('../network/Async');
var Insight = module.exports.Insight = require('../blockchain/Insight'); var Insight = module.exports.Insight = require('../blockchain/Insight');
var preconditions = require('preconditions').singleton(); var preconditions = require('preconditions').singleton();
@ -33,16 +34,24 @@ var Storage = module.exports.Storage = require('../Storage');
* @param {string} version - the version of copay for which this wallet was generated (for example, 0.4.7) * @param {string} version - the version of copay for which this wallet was generated (for example, 0.4.7)
* @constructor * @constructor
*/ */
function WalletFactory(config, version) {
function WalletFactory(config, version, pluginManager) {
var self = this; var self = this;
config = config || {}; config = config || {};
this.pluginManager = new PluginManager(config); this.Storage = config.Storage || Storage;
this.Storage = config.Storage || Storage;
this.Network = config.Network || Async; this.Network = config.Network || Async;
this.Blockchain = config.Blockchain || Insight; this.Blockchain = config.Blockchain || Insight;
this.storage = new this.Storage({storage: this.pluginManager.get('STORAGE')});
var storageOpts = {};
if (pluginManager) {
storageOpts = {
storage: pluginManager.get('STORAGE')
};
}
this.storage = new this.Storage(storageOpts);
this.networks = { this.networks = {
'livenet': new this.Network(config.network.livenet), 'livenet': new this.Network(config.network.livenet),
@ -57,27 +66,6 @@ function WalletFactory(config, version) {
this.version = version; this.version = version;
}; };
/**
* @desc
* Returns true if the storage instance can retrieve the following keys using a given walletId
* <ul>
* <li><tt>publicKeyRing</tt></li>
* <li><tt>txProposals</tt></li>
* <li><tt>opts</tt></li>
* <li><tt>privateKey</tt></li>
* </ul>
* @param {string} walletId
* @return {boolean} true if all the keys are present in the storage instance
*/
WalletFactory.prototype._checkRead = function(walletId) {
var s = this.storage;
var ret =
s.get(walletId, 'publicKeyRing') &&
s.get(walletId, 'txProposals') &&
s.get(walletId, 'opts') &&
s.get(walletId, 'privateKey');
return !!ret;
};
/** /**
* @desc obtain network name from serialized wallet * @desc obtain network name from serialized wallet
@ -100,9 +88,14 @@ WalletFactory.prototype.obtainNetworkName = function(obj) {
WalletFactory.prototype.fromObj = function(obj, skipFields) { WalletFactory.prototype.fromObj = function(obj, skipFields) {
var networkName = this.obtainNetworkName(obj); var networkName = this.obtainNetworkName(obj);
preconditions.checkState(networkName); preconditions.checkState(networkName);
preconditions.checkArgument(obj);
obj.opts.reconnectDelay = this.walletDefaults.reconnectDelay; obj.opts.reconnectDelay = this.walletDefaults.reconnectDelay;
// this is only used if private key or public key ring is skipped
obj.opts.networkName = this.networkName;
skipFields = skipFields || []; skipFields = skipFields || [];
skipFields.forEach(function(k) { skipFields.forEach(function(k) {
if (obj[k]) { if (obj[k]) {
@ -155,20 +148,17 @@ WalletFactory.prototype.import = function(base64, password, skipFields) {
* @param {string[]} skipFields - parameters to ignore when importing * @param {string[]} skipFields - parameters to ignore when importing
* @return {Wallet} * @return {Wallet}
*/ */
WalletFactory.prototype.read = function(walletId, skipFields) { WalletFactory.prototype.read = function(walletId, skipFields, cb) {
if (!this._checkRead(walletId)) var self = this;
return false;
var obj = {}; var obj = {};
var s = this.storage;
obj.id = walletId; obj.id = walletId;
_.each(Wallet.PERSISTED_PROPERTIES, function(value) {
obj[value] = s.get(walletId, value);
});
var w = this.fromObj(obj, skipFields); this.storage.getMany(walletId, Wallet.PERSISTED_PROPERTIES, function(ret) {
return w; for (var ii in ret) {
obj[ii] = ret[ii];
}
return cb(self.fromObj(obj, skipFields));
});
}; };
/** /**
@ -269,29 +259,29 @@ WalletFactory.prototype._checkVersion = function(inVersion) {
* @desc Retrieve a wallet from the storage * @desc Retrieve a wallet from the storage
* @param {string} walletId - the id of the wallet * @param {string} walletId - the id of the wallet
* @param {string} passphrase - the passphrase to decode it * @param {string} passphrase - the passphrase to decode it
* @return {Wallet} * @param {function} callback (err, {Wallet})
* @return
*/ */
WalletFactory.prototype.open = function(walletId, passphrase) { WalletFactory.prototype.open = function(walletId, passphrase, cb) {
this.storage._setPassphrase(passphrase); var self = this,
var w = this.read(walletId); err;
if (w) { self.storage._setPassphrase(passphrase);
w.store(); self.read(walletId, null, function(w) {
} w.store(function() {
self.storage.setLastOpened(walletId, function() {
this.storage.setLastOpened(walletId); return cb(err, w);
return w; });
});
});
}; };
/** WalletFactory.prototype.getWallets = function(cb) {
* @desc Retrieve all wallets stored without encription in the storage instance var ret = this.storage.getWallets(function(ret) {
* @returns {Wallet[]} ret.forEach(function(i) {
*/ i.show = i.name ? ((i.name + ' <' + i.id + '>')) : i.id;
WalletFactory.prototype.getWallets = function() { });
var ret = this.storage.getWallets(); return cb(ret);
ret.forEach(function(i) {
i.show = i.name ? ((i.name + ' <' + i.id + '>')) : i.id;
}); });
return ret;
}; };
/** /**
@ -374,30 +364,32 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
connectedOnce = true; connectedOnce = true;
}); });
joinNetwork.on('serverError', function() { << << << < HEAD
return cb('joinError'); joinNetwork.on('serverError', function() { === === =
}); self.network.on('serverError', function() { >>> >>> > wallet listing working
return cb('joinError');
});
joinNetwork.start(opts, function() { joinNetwork.start(opts, function() {
joinNetwork.greet(decodedSecret.pubKey, opts.secretNumber); joinNetwork.greet(decodedSecret.pubKey, opts.secretNumber);
joinNetwork.on('data', function(sender, data) { joinNetwork.on('data', function(sender, data) {
if (data.type === 'walletId') { if (data.type === 'walletId') {
if (data.networkName !== decodedSecret.networkName) { if (data.networkName !== decodedSecret.networkName) {
return cb('badNetwork'); return cb('badNetwork');
} }
data.opts.privateKey = privateKey; data.opts.privateKey = privateKey;
data.opts.nickname = nickname; data.opts.nickname = nickname;
data.opts.passphrase = passphrase; data.opts.passphrase = passphrase;
data.opts.id = data.walletId; data.opts.id = data.walletId;
var w = self.create(data.opts); var w = self.create(data.opts);
w.sendWalletReady(decodedSecret.pubKey); w.sendWalletReady(decodedSecret.pubKey);
return cb(null, w); return cb(null, w);
} else { } else {
return cb('walletFull', w); return cb('walletFull', w);
} }
}); });
}); });
}; };
module.exports = WalletFactory; module.exports = WalletFactory;

View file

@ -10,46 +10,51 @@ function WalletLock(storage, walletId, timeoutMin) {
this.storage = storage; this.storage = storage;
this.timeoutMin = timeoutMin || 5; this.timeoutMin = timeoutMin || 5;
this.key = WalletLock._keyFor(walletId); this.key = WalletLock._keyFor(walletId);
this._setLock(); this._setLock(function() {});
} }
WalletLock._keyFor = function(walletId) { WalletLock._keyFor = function(walletId) {
return 'lock' + '::' + walletId; return 'lock' + '::' + walletId;
}; };
WalletLock.prototype._isLockedByOther = function() { WalletLock.prototype._isLockedByOther = function(cb) {
var json = this.storage.getGlobal(this.key); var self=this;
var wl = json ? JSON.parse(json) : null; this.storage.getGlobal(this.key, function(json) {
var t = wl ? (Date.now() - wl.expireTs) : false; var wl = json ? JSON.parse(json) : null;
// is not locked? var t = wl ? (Date.now() - wl.expireTs) : false;
if (!wl || t > 0 || wl.sessionId === this.sessionId) // is not locked?
return false; if (!wl || t > 0 || wl.sessionId === self.sessionId)
return cb(false);
// Seconds remainding // Seconds remainding
return parseInt(-t/1000.); return cb(parseInt(-t / 1000.));
};
WalletLock.prototype._setLock = function() {
this.storage.setGlobal(this.key, {
sessionId: this.sessionId,
expireTs: Date.now() + this.timeoutMin * 60 * 1000,
}); });
}; };
WalletLock.prototype.keepAlive = function() { WalletLock.prototype._setLock = function(cb) {
preconditions.checkState(this.sessionId); this.storage.setGlobal(this.key, {
sessionId: this.sessionId,
var t = this._isLockedByOther(); expireTs: Date.now() + this.timeoutMin * 60 * 1000,
if (t) }, cb);
throw new Error('Wallet is already open. Close it to proceed or wait '+ t + ' seconds if you close it already' );
this._setLock();
}; };
WalletLock.prototype.release = function() { WalletLock.prototype.keepAlive = function(cb) {
this.storage.removeGlobal(this.key); preconditions.checkState(this.sessionId);
var self = this;
this._isLockedByOther(function(t) {
if (t)
return cb(new Error('Wallet is already open. Close it to proceed or wait ' + t + ' seconds if you close it already'));
self._setLock(cb);
});
};
WalletLock.prototype.release = function(cb) {
this.storage.removeGlobal(this.key, cb);
}; };

View file

@ -1,5 +1,19 @@
'use strict'; 'use strict';
angular.module('copayApp.services').factory('pluginManager', function(angularLoad){ angular.module('copayApp.services').factory('pluginManager', function(angularLoad){
return new copay.PluginManager(config); var pm = new copay.PluginManager(config);
var scripts = pm.scripts;
for(var ii in scripts){
var src = scripts[ii].src;
console.log('\tLoading ',src); //TODO
angularLoad.loadScript(src)
.then(scripts[ii].then || null)
.catch(function() {
throw new Error('Loading ' + src);
})
}
return pm;
}); });

View file

@ -1,3 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.services').factory('walletFactory', function(pluginManager){
console.log('[walletFactory.js.3]'); //TODO
return new copay.WalletFactory(config, copay.version, pluginManager);
});
angular.module('copayApp.services').value('walletFactory', new copay.WalletFactory(config, copay.version));

View file

@ -9,33 +9,33 @@ LocalStorage.prototype.init = function() {
}; };
LocalStorage.prototype.getItem = function(k) { LocalStorage.prototype.getItem = function(k,cb) {
return localStorage.getItem(k); return cb(localStorage.getItem(k));
}; };
LocalStorage.prototype.setItem = function(k,v) { LocalStorage.prototype.setItem = function(k,v,cb) {
localStorage.setItem(k,v); localStorage.setItem(k,v);
return cb();
}; };
LocalStorage.prototype.removeItem = function(k) { LocalStorage.prototype.removeItem = function(k,cb) {
localStorage.removeItem(k); localStorage.removeItem(k);
return cb();
}; };
LocalStorage.prototype.clear = function() { LocalStorage.prototype.clear = function(cb) {
localStorage.clear(); localStorage.clear();
return cb();
}; };
delete LocalStorage.prototype.length; LocalStorage.prototype.allKeys = function(cb) {
var l = localStorage.length;
var ret = [];
Object.defineProperty(LocalStorage.prototype, 'length', { for(var i=0; i<l; i++)
get: function() { ret.push(localStorage.key(i));
return localStorage.length;
}
});
LocalStorage.prototype.key = function(k) { return cb(ret);
var v = localStorage.key(k);
return v;
}; };

View file

@ -1,37 +1,249 @@
'use strict'; 'use strict';
function GoogleDrive() { var preconditions = require('preconditions').singleton();
var loaded = 0;
var SCOPES = 'https://www.googleapis.com/auth/drive';
function GoogleDrive(config) {
preconditions.checkArgument(config && config.clientId, 'No clientId at GoogleDrive config');
this.clientId = config.clientId;
this.home = config.home || 'copay';
this.idCache = {};
this.type = 'STORAGE'; this.type = 'STORAGE';
this.scripts = [{
then: this.initLoaded.bind(this),
src: 'https://apis.google.com/js/client.js?onload=InitGoogleDrive'
}];
this.isReady = false;
this.useImmediate = true;
this.ts = 100;
}; };
GoogleDrive.prototype.init = function() { window.InitGoogleDrive = function() {
console.log('[googleDrive.js.3] init GoogleDrive'); //TODO
console.log('[googleDrive.js.18] setting loaded'); //TODO
loaded = 1;
}; };
GoogleDrive.prototype.init = function() {};
/**
* Called when the client library is loaded to start the auth flow.
*/
GoogleDrive.prototype.initLoaded = function() {
if (!loaded) {
window.setTimeout(this.initLoaded.bind(this), 500);
} else {
window.setTimeout(this.checkAuth.bind(this), 1);
}
}
/**
* Check if the current user has authorized the application.
*/
GoogleDrive.prototype.checkAuth = function() {
console.log('\tChecking google Auth'); //TODO
gapi.auth.authorize({
'client_id': this.clientId,
'scope': SCOPES,
'immediate': this.useImmediate,
},
this.handleAuthResult.bind(this));
};
/**
* Called when authorization server replies.
*/
GoogleDrive.prototype.handleAuthResult = function(authResult) {
var self = this;
console.log('[googleDrive.js.39:authResult:]', authResult); //TODO
if (authResult.error) {
if (authResult.error) {
self.useImmediate = false;
return this.checkAuth();
};
throw new Error(authResult.error);
}
gapi.client.load('drive', 'v2', function() {
self.isReady = true;
});
}
GoogleDrive.prototype.checkReady = function() {
if (!this.isReady)
throw new Error('goggle drive is not ready!');
};
GoogleDrive.prototype.getItem = function(k) { GoogleDrive.prototype.getItem = function(k) {
return localStorage.getItem(k); this.checkReady();
throw new Error('getItem not implemented');
}; };
GoogleDrive.prototype.setItem = function(k,v) { GoogleDrive.prototype.setItem = function(k, v, cb) {
localStorage.setItem(k,v); var self = this;
this.checkReady();
var parendId = self._idForName[this.home];
preconditions.checkState(parentId);
var boundary = '-------314159265358979323846';
var delimiter = "\r\n--" + boundary + "\r\n";
var close_delim = "\r\n--" + boundary + "--";
var metadata = {
'title': k,
'mimeType': 'application/octet-stream',
'parents': [parentId],
};
var base64Data = btoa(v);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: application/octet-stream \r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
console.log('[googleDrive.js.105:multipartRequestBody:]',multipartRequestBody); //TODO
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {
'uploadType': 'multipart',
},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
request.execute(cb);
}; };
GoogleDrive.prototype.removeItem = function(k) { GoogleDrive.prototype.removeItem = function(k) {
localStorage.removeItem(k); this.checkReady();
throw new Error('removeItem not implemented');
}; };
GoogleDrive.prototype.clear = function() { GoogleDrive.prototype.clear = function() {
localStorage.clear(); this.checkReady();
throw new Error('clear not implemented');
}; };
delete GoogleDrive.prototype.length;
Object.defineProperty(GoogleDrive.prototype, 'length', { GoogleDrive.prototype._mkdir = function(cb) {
get: function() { preconditions.checkArgument(cb);
return localStorage.length; var self = this;
console.log('Creating drive folder ' + this.home);
var request = gapi.client.request({
'path': '/drive/v2/files',
'method': 'POST',
'body': JSON.stringify({
'title': this.home,
'mimeType': "application/vnd.google-apps.folder",
}),
});
request.execute(function(){
self._idForName(this.home, cb);
});
};
GoogleDrive.prototype._idForName = function(name, cb) {
preconditions.checkArgument(name);
preconditions.checkArgument(cb);
var self = this;
if (!self.isReady) {
console.log('\tWaiting for Drive');
self.ts = self.ts * 1.5;
return setTimeout(self._idForName.bind(self, name, cb), self.ts);
} }
}); console.log('[googleDrive.js.178:name:]',name); //TODO
if (self.idCache[name])
return cb(self.idCache[name]);
console.log('Querying for: ', name); //TODO
var args;
var idParent = name == this.home ? 'root' : self.idCache[this.home] ;
console.log('[googleDrive.js.177:idParent:]',idParent); //TODO
preconditions.checkState(idParent);
args = {
'path': '/drive/v2/files',
'method': 'GET',
'params': {
'q': "title='" + name + "' and trashed = false and '" + idParent + "' in parents",
}
};
console.log('[googleDrive.js.196:args:]', args); //TODO
var request = gapi.client.request(args);
request.execute(function(res) {
console.log('[googleDrive.js.175:res:]', res); //TODO
var i = res.items && res.items[0] ? res.items[0].id : false;
if (i)
self.idCache[name] = i;
return cb(self.idCache[name]);
});
};
GoogleDrive.prototype._checkHomeDir = function(cb) {
var self = this;
this._idForName(this.home, function(homeId) {
if (!homeId)
return self._mkdir(cb);
return cb(homeId);
});
};
GoogleDrive.prototype.allKeys = function(cb) {
var self = this;
this._checkHomeDir(function(homeId) {
preconditions.checkState(homeId);
var request = gapi.client.request({
'path': '/drive/v2/files',
'method': 'GET',
'params': {
'q': "'" + homeId + "' in parents and trashed = false",
'fields': 'items(id,title)'
},
});
request.execute(function(res) {
console.log('[googleDrive.js.152:res:]', res); //TODO
if (res.error)
throw new Error(res.error.message);
var ret = [];
for (var ii in res.items) {
ret.push(res[ii].id);
}
return cb(ret);
});
});
};
GoogleDrive.prototype.key = function(k) { GoogleDrive.prototype.key = function(k) {
var v = localStorage.key(k); var v = localStorage.key(k);