Merge pull request #1307 from matiu/feature/drive
Feature/ Async storage + Google Drive example
This commit is contained in:
commit
3a79f039cd
38 changed files with 1980 additions and 1192 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -79,3 +79,5 @@ dist/*.tar.gz
|
|||
dist/*.exe
|
||||
|
||||
doc/
|
||||
/node_modules
|
||||
/*-cov
|
||||
|
|
|
|||
12
Gruntfile.js
12
Gruntfile.js
|
|
@ -64,7 +64,9 @@ module.exports = function(grunt) {
|
|||
},
|
||||
scripts: {
|
||||
files: [
|
||||
'js/models/**/*.js'
|
||||
'js/models/**/*.js',
|
||||
'js/models/*.js',
|
||||
'plugins/*.js',
|
||||
],
|
||||
tasks: ['shell:dev']
|
||||
},
|
||||
|
|
@ -136,7 +138,9 @@ module.exports = function(grunt) {
|
|||
'lib/ng-idle/angular-idle.min.js',
|
||||
'lib/angular-foundation/mm-foundation.min.js',
|
||||
'lib/angular-foundation/mm-foundation-tpls.min.js',
|
||||
'lib/angular-gettext/dist/angular-gettext.min.js'
|
||||
'lib/angular-gettext/dist/angular-gettext.min.js',
|
||||
'lib/angular-load/angular-load.min.js'
|
||||
// If you add libs here, remember to add it too to karma.conf
|
||||
],
|
||||
dest: 'lib/angularjs-all.js'
|
||||
},
|
||||
|
|
@ -150,7 +154,7 @@ module.exports = function(grunt) {
|
|||
'js/controllers/*.js',
|
||||
'js/translations.js',
|
||||
'js/mobile.js', // PLACEHOLDER: CORDOVA SRIPT
|
||||
'js/init.js'
|
||||
'js/init.js',
|
||||
],
|
||||
dest: 'js/copayMain.js'
|
||||
}
|
||||
|
|
@ -198,7 +202,7 @@ module.exports = function(grunt) {
|
|||
},
|
||||
jsdoc: {
|
||||
dist : {
|
||||
src: ['js/models/core/*.js'],
|
||||
src: ['js/models/core/*.js', 'js/models/*.js', 'plugins/*.js'],
|
||||
options: {
|
||||
destination: 'doc',
|
||||
configure: 'jsdoc.conf.json',
|
||||
|
|
|
|||
|
|
@ -24,7 +24,8 @@
|
|||
"zeroclipboard": "~1.3.5",
|
||||
"ng-idle": "*",
|
||||
"underscore": "~1.7.0",
|
||||
"inherits": "~0.0.1"
|
||||
"inherits": "~0.0.1",
|
||||
"angular-load": "0.2.0"
|
||||
},
|
||||
"resolutions": {
|
||||
"angular": "=1.2.19"
|
||||
|
|
|
|||
22
config.js
22
config.js
|
|
@ -49,6 +49,28 @@ var defaultConfig = {
|
|||
updateFrequencySeconds: 60 * 60
|
||||
},
|
||||
|
||||
verbose: 1,
|
||||
|
||||
plugins: {
|
||||
LocalStorage: true,
|
||||
//GoogleDrive: true,
|
||||
},
|
||||
|
||||
GoogleDrive: {
|
||||
home: 'copay',
|
||||
|
||||
/*
|
||||
* This clientId was generated at:
|
||||
* https://console.developers.google.com/project
|
||||
* To run Copay with Google Drive at your domain you need
|
||||
* to generata your own Id.
|
||||
*/
|
||||
// for localhost:3001 you can use you can:
|
||||
clientId: '232630733383-a35gcnovnkgka94394i88gq60vtjb4af.apps.googleusercontent.com',
|
||||
|
||||
// for copay.io:
|
||||
// clientId: '1036948132229-biqm3b8sirik9lt5rtvjo9kjjpotn4ac.apps.googleusercontent.com',
|
||||
},
|
||||
};
|
||||
if (typeof module !== 'undefined')
|
||||
module.exports = defaultConfig;
|
||||
|
|
|
|||
3
copay.js
3
copay.js
|
|
@ -11,11 +11,12 @@ module.exports.HDParams = require('./js/models/core/HDParams');
|
|||
// components
|
||||
var Async = module.exports.Async = require('./js/models/network/Async');
|
||||
var Insight = module.exports.Insight = require('./js/models/blockchain/Insight');
|
||||
var StorageLocalEncrypted = module.exports.StorageLocalEncrypted = require('./js/models/storage/LocalEncrypted');
|
||||
var Storage = module.exports.Storage = require('./js/models/Storage');
|
||||
|
||||
module.exports.WalletFactory = require('./js/models/core/WalletFactory');
|
||||
module.exports.Wallet = require('./js/models/core/Wallet');
|
||||
module.exports.WalletLock = require('./js/models/core/WalletLock');
|
||||
module.exports.PluginManager = require('./js/models/core/PluginManager');
|
||||
module.exports.version = require('./version').version;
|
||||
module.exports.commitHash = require('./version').commitHash;
|
||||
|
||||
|
|
|
|||
14
js/app.js
14
js/app.js
|
|
@ -15,7 +15,11 @@ if (localConfig) {
|
|||
}
|
||||
}
|
||||
|
||||
var copayApp = window.copayApp = angular.module('copayApp', [
|
||||
var log = function() {
|
||||
if (config.verbose) console.log(arguments);
|
||||
}
|
||||
|
||||
var modules = [
|
||||
'ngRoute',
|
||||
'angularMoment',
|
||||
'mm.foundation',
|
||||
|
|
@ -26,7 +30,13 @@ var copayApp = window.copayApp = angular.module('copayApp', [
|
|||
'copayApp.services',
|
||||
'copayApp.controllers',
|
||||
'copayApp.directives',
|
||||
]);
|
||||
];
|
||||
|
||||
if (Object.keys(config.plugins).length)
|
||||
modules.push('angularLoad');
|
||||
|
||||
|
||||
var copayApp = window.copayApp = angular.module('copayApp', modules);
|
||||
|
||||
copayApp.config(function($sceDelegateProvider) {
|
||||
$sceDelegateProvider.resourceUrlWhitelist([
|
||||
|
|
|
|||
|
|
@ -86,8 +86,9 @@ angular.module('copayApp.controllers').controller('CreateController',
|
|||
privateKeyHex: $scope.private,
|
||||
networkName: $scope.networkName,
|
||||
};
|
||||
var w = walletFactory.create(opts);
|
||||
controllerUtils.startNetwork(w, $scope);
|
||||
walletFactory.create(opts, function(err, w) {
|
||||
controllerUtils.startNetwork(w, $scope);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('HomeController',
|
||||
function($scope, $rootScope, $location, walletFactory, notification, controllerUtils) {
|
||||
|
||||
controllerUtils.redirIfLogged();
|
||||
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, walletFactory, notification, controllerUtils) {
|
||||
|
||||
$scope.loading = false;
|
||||
$scope.hasWallets = (walletFactory.getWallets() && walletFactory.getWallets().length > 0) ? true : false;
|
||||
controllerUtils.redirIfLogged();
|
||||
|
||||
$scope.retreiving = true;
|
||||
walletFactory.getWallets(function(err,ret) {
|
||||
$scope.retreiving = false;
|
||||
$scope.hasWallets = (ret && ret.length > 0) ? true : false;
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -119,7 +119,12 @@ angular.module('copayApp.controllers').controller('JoinController',
|
|||
$scope.loading = true;
|
||||
|
||||
Passphrase.getBase64Async($scope.joinPassword, function(passphrase) {
|
||||
walletFactory.joinCreateSession($scope.connectionId, $scope.nickname, passphrase, $scope.private, function(err, w) {
|
||||
walletFactory.joinCreateSession({
|
||||
secret: $scope.connectionId,
|
||||
nickname: $scope.nickname,
|
||||
passphrase: passphrase,
|
||||
privateHex: $scope.private,
|
||||
}, function(err, w) {
|
||||
$scope.loading = false;
|
||||
if (err || !w) {
|
||||
if (err === 'joinError')
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ angular.module('copayApp.controllers').controller('MoreController',
|
|||
value: 100000000,
|
||||
decimals: 8
|
||||
}];
|
||||
|
||||
$scope.selectedAlternative = {
|
||||
name: w.settings.alternativeName,
|
||||
isoCode: w.settings.alternativeIsoCode
|
||||
|
|
|
|||
|
|
@ -14,15 +14,32 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc
|
|||
};
|
||||
$rootScope.fromSetup = false;
|
||||
$scope.loading = false;
|
||||
$scope.wallets = walletFactory.getWallets().sort(cmp);
|
||||
$scope.selectedWalletId = walletFactory.storage.getLastOpened() || ($scope.wallets[0] && $scope.wallets[0].id);
|
||||
$scope.retreiving = true;
|
||||
|
||||
walletFactory.getWallets(function(err, wallets) {
|
||||
|
||||
if (err || !wallets || !wallets.length) {
|
||||
$location.path('/');
|
||||
} else {
|
||||
$scope.retreiving = false;
|
||||
$scope.wallets = wallets.sort(cmp);
|
||||
|
||||
walletFactory.storage.getLastOpened(function(ret) {
|
||||
if (ret && _.indexOf(_.pluck($scope.wallets, 'id')) == -1)
|
||||
ret = null;
|
||||
|
||||
$scope.selectedWalletId = ret || ($scope.wallets[0] && $scope.wallets[0].id);
|
||||
|
||||
setTimeout(function() {
|
||||
$rootScope.$digest();
|
||||
}, 0);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
$scope.openPassword = '';
|
||||
$scope.isMobile = !!window.cordova;
|
||||
|
||||
if (!$scope.wallets.length){
|
||||
$location.path('/');
|
||||
}
|
||||
|
||||
$scope.open = function(form) {
|
||||
if (form && form.$invalid) {
|
||||
notification.error('Error', 'Please enter the required fields');
|
||||
|
|
@ -34,19 +51,16 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc
|
|||
|
||||
Passphrase.getBase64Async(password, function(passphrase) {
|
||||
var w, errMsg;
|
||||
try {
|
||||
w = walletFactory.open($scope.selectedWalletId, passphrase);
|
||||
} catch (e) {
|
||||
errMsg = e.message;
|
||||
};
|
||||
if (!w) {
|
||||
$scope.loading = false;
|
||||
notification.error('Error', errMsg || 'Wrong password');
|
||||
$rootScope.$digest();
|
||||
return;
|
||||
}
|
||||
$rootScope.updatingBalance = true;
|
||||
controllerUtils.startNetwork(w, $scope);
|
||||
walletFactory.open($scope.selectedWalletId, passphrase, function(err, w) {
|
||||
if (!w) {
|
||||
$scope.loading = false;
|
||||
notification.error('Error', err.errMsg || 'Wrong password');
|
||||
$rootScope.$digest();
|
||||
} else {
|
||||
$rootScope.updatingBalance = true;
|
||||
controllerUtils.startNetwork(w, $scope);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
|||
320
js/models/Storage.js
Normal file
320
js/models/Storage.js
Normal file
|
|
@ -0,0 +1,320 @@
|
|||
'use strict';
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
|
||||
var bitcore = require('bitcore');
|
||||
var preconditions = require('preconditions').instance();
|
||||
var _ = require('underscore');
|
||||
var CACHE_DURATION = 1000 * 60 * 5;
|
||||
var id = 0;
|
||||
|
||||
function Storage(opts) {
|
||||
opts = opts || {};
|
||||
|
||||
this.wListCache = {};
|
||||
this.__uniqueid = ++id;
|
||||
if (opts.password)
|
||||
this.setPassphrase(opts.password);
|
||||
|
||||
try {
|
||||
this.storage = opts.storage || localStorage;
|
||||
this.sessionStorage = opts.sessionStorage || sessionStorage;
|
||||
} catch (e) {
|
||||
console.log('Error in storage:', e); //TODO
|
||||
};
|
||||
|
||||
preconditions.checkState(this.storage, 'No storage defined');
|
||||
preconditions.checkState(this.sessionStorage, 'No sessionStorage defined');
|
||||
}
|
||||
|
||||
var pps = {};
|
||||
Storage.prototype._getPassphrase = function() {
|
||||
|
||||
if (!pps[this.__uniqueid])
|
||||
throw new Error('NOPASSPHRASE: No passphrase set');
|
||||
|
||||
return pps[this.__uniqueid];
|
||||
}
|
||||
|
||||
Storage.prototype.setPassphrase = function(password) {
|
||||
pps[this.__uniqueid] = password;
|
||||
}
|
||||
|
||||
Storage.prototype._encrypt = function(string) {
|
||||
var encrypted = CryptoJS.AES.encrypt(string, this._getPassphrase());
|
||||
var encryptedBase64 = encrypted.toString();
|
||||
return encryptedBase64;
|
||||
};
|
||||
|
||||
Storage.prototype._decrypt = function(base64) {
|
||||
var decryptedStr = null;
|
||||
try {
|
||||
var decrypted = CryptoJS.AES.decrypt(base64, this._getPassphrase());
|
||||
if (decrypted)
|
||||
decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
|
||||
} catch (e) {
|
||||
// Error while decrypting
|
||||
return null;
|
||||
}
|
||||
return decryptedStr;
|
||||
};
|
||||
|
||||
|
||||
Storage.prototype._read = function(k, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
var self = this;
|
||||
this.storage.getItem(k, function(ret) {
|
||||
if (!ret) return cb(null);
|
||||
var ret = self._decrypt(ret);
|
||||
if (!ret) return cb(null);
|
||||
|
||||
ret = ret.toString(CryptoJS.enc.Utf8);
|
||||
ret = JSON.parse(ret);
|
||||
return cb(ret);
|
||||
});
|
||||
};
|
||||
|
||||
Storage.prototype._write = function(k, v, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
v = JSON.stringify(v);
|
||||
v = this._encrypt(v);
|
||||
this.storage.setItem(k, v, cb);
|
||||
};
|
||||
|
||||
// get value by key
|
||||
Storage.prototype.getGlobal = function(k, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
this.storage.getItem(k, function(item) {
|
||||
cb(item == 'undefined' ? undefined : item);
|
||||
});
|
||||
};
|
||||
|
||||
// set value for key
|
||||
Storage.prototype.setGlobal = function(k, v, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
this.storage.setItem(k, typeof v === 'object' ? JSON.stringify(v) : v, cb);
|
||||
};
|
||||
|
||||
// remove value for key
|
||||
Storage.prototype.removeGlobal = function(k, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
this.storage.removeItem(k, cb);
|
||||
};
|
||||
|
||||
Storage.prototype.getSessionId = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
|
||||
self.sessionStorage.getItem('sessionId', function(sessionId) {
|
||||
if (sessionId)
|
||||
return cb(sessionId);
|
||||
|
||||
sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex');
|
||||
self.sessionStorage.setItem('sessionId', sessionId, function() {
|
||||
return cb(sessionId);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Storage.prototype.setSessionId = function(sessionId, cb) {
|
||||
this.sessionStorage.setItem('sessionId', sessionId, cb);
|
||||
};
|
||||
|
||||
Storage.prototype._key = function(walletId, k) {
|
||||
return walletId + '::' + k;
|
||||
};
|
||||
// get value by key
|
||||
Storage.prototype.get = function(walletId, k, cb) {
|
||||
preconditions.checkArgument(walletId, k, cb);
|
||||
this._read(this._key(walletId, k), cb);
|
||||
};
|
||||
|
||||
|
||||
Storage.prototype._readHelper = function(walletId, k, cb) {
|
||||
var wk = this._key(walletId, k);
|
||||
this._read(wk, function(v) {
|
||||
return cb(v, k);
|
||||
});
|
||||
};
|
||||
|
||||
Storage.prototype.getMany = function(walletId, keys, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
var self = this;
|
||||
var ret = {};
|
||||
|
||||
var l = keys.length,
|
||||
i = 0;
|
||||
|
||||
for (var ii in keys) {
|
||||
this._readHelper(walletId, keys[ii], function(v, k) {
|
||||
ret[k] = v;
|
||||
if (++i == l) {
|
||||
return cb(ret);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// set value for key
|
||||
Storage.prototype.set = function(walletId, k, v, cb) {
|
||||
preconditions.checkArgument(walletId && k && cb);
|
||||
|
||||
if (_.isUndefined(v)) return cb();
|
||||
|
||||
this._write(this._key(walletId, k), v, cb);
|
||||
};
|
||||
|
||||
// remove value for key
|
||||
Storage.prototype.remove = function(walletId, k, cb) {
|
||||
preconditions.checkArgument(walletId && k && cb);
|
||||
this.removeGlobal(this._key(walletId, k), cb);
|
||||
};
|
||||
|
||||
Storage.prototype.setName = function(walletId, name, cb) {
|
||||
preconditions.checkArgument(walletId && name && cb);
|
||||
this.setGlobal('nameFor::' + walletId, name, cb);
|
||||
};
|
||||
|
||||
Storage.prototype.getName = function(walletId, cb) {
|
||||
preconditions.checkArgument(walletId && cb);
|
||||
this.getGlobal('nameFor::' + walletId, cb);
|
||||
};
|
||||
|
||||
Storage.prototype.getWalletIds = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
var walletIds = [];
|
||||
var uniq = {};
|
||||
this.storage.allKeys(function(keys) {
|
||||
for (var ii in keys) {
|
||||
var key = keys[ii];
|
||||
var split = key.split('::');
|
||||
if (split.length == 2) {
|
||||
var walletId = split[0];
|
||||
|
||||
if (!walletId || walletId === 'nameFor' || walletId === 'lock')
|
||||
continue;
|
||||
|
||||
if (typeof uniq[walletId] === 'undefined') {
|
||||
walletIds.push(walletId);
|
||||
uniq[walletId] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cb(walletIds);
|
||||
});
|
||||
};
|
||||
|
||||
Storage.prototype.getWallets = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
if (this.wListCache.ts > Date.now())
|
||||
return cb(this.wListCache.data)
|
||||
|
||||
var wallets = [];
|
||||
var self = this;
|
||||
|
||||
this.getWalletIds(function(ids) {
|
||||
var l = ids.length,
|
||||
i = 0;
|
||||
if (!l)
|
||||
return cb([]);
|
||||
|
||||
_.each(ids, function(id) {
|
||||
self.getName(id, function(name) {
|
||||
wallets.push({
|
||||
id: id,
|
||||
name: name,
|
||||
});
|
||||
if (++i == l) {
|
||||
self.wListCache.data = wallets;
|
||||
self.wListCache.ts = Date.now() + CACHE_DURATION;
|
||||
return cb(wallets);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Storage.prototype.deleteWallet = function(walletId, cb) {
|
||||
preconditions.checkArgument(walletId);
|
||||
preconditions.checkArgument(cb);
|
||||
var err;
|
||||
var self = this;
|
||||
|
||||
var toDelete = {};
|
||||
|
||||
this.storage.allKeys(function(allKeys) {
|
||||
for (var ii in allKeys) {
|
||||
var key = allKeys[ii];
|
||||
var split = key.split('::');
|
||||
if (split.length == 2 && split[0] === walletId) {
|
||||
toDelete[key] = 1;
|
||||
};
|
||||
}
|
||||
var l = Object.keys(toDelete).length,
|
||||
j = 0;
|
||||
if (!l)
|
||||
return cb(new Error('WNOTFOUND: Wallet not found'));
|
||||
|
||||
toDelete['nameFor::' + walletId] = 1;
|
||||
l++;
|
||||
|
||||
for (var i in toDelete) {
|
||||
self.removeGlobal(i, function() {
|
||||
if (++j == l)
|
||||
return cb(err);
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
};
|
||||
|
||||
Storage.prototype.setLastOpened = function(walletId, cb) {
|
||||
this.setGlobal('lastOpened', walletId, cb);
|
||||
}
|
||||
|
||||
Storage.prototype.getLastOpened = function(cb) {
|
||||
this.getGlobal('lastOpened', cb);
|
||||
}
|
||||
|
||||
//obj contains keys to be set
|
||||
Storage.prototype.setFromObj = function(walletId, obj, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
|
||||
var l = Object.keys(obj).length,
|
||||
i = 0;
|
||||
for (var k in obj) {
|
||||
self.set(walletId, k, obj[k], function() {
|
||||
if (++i == l) {
|
||||
if (obj.opts.name)
|
||||
self.setName(walletId, obj.opts.name, cb);
|
||||
else
|
||||
return cb();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// remove all values
|
||||
Storage.prototype.clearAll = function(cb) {
|
||||
this.storage.clear(cb);
|
||||
};
|
||||
|
||||
Storage.prototype.import = function(base64) {
|
||||
var decryptedStr = this._decrypt(base64);
|
||||
return JSON.parse(decryptedStr);
|
||||
};
|
||||
|
||||
Storage.prototype.export = function(obj) {
|
||||
var string = JSON.stringify(obj);
|
||||
return this._encrypt(string);
|
||||
};
|
||||
|
||||
module.exports = Storage;
|
||||
52
js/models/core/PluginManager.js
Normal file
52
js/models/core/PluginManager.js
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
'use strict';
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var log = require('../../log');
|
||||
|
||||
function PluginManager(config) {
|
||||
this.registered = {};
|
||||
this.scripts = [];
|
||||
|
||||
for (var ii in config.plugins) {
|
||||
var pluginName = ii;
|
||||
|
||||
if (!config.plugins[pluginName])
|
||||
continue;
|
||||
|
||||
log.info('Loading plugin: ' + pluginName);
|
||||
var pluginClass = require('../plugins/' + pluginName);
|
||||
var pluginObj = new pluginClass(config[pluginName]);
|
||||
pluginObj.init();
|
||||
this._register(pluginObj, pluginName);
|
||||
}
|
||||
};
|
||||
|
||||
var KIND_UNIQUE = PluginManager.KIND_UNIQUE = 1;
|
||||
var KIND_MULTIPLE = PluginManager.KIND_MULTIPLE = 2;
|
||||
|
||||
PluginManager.TYPE = {};
|
||||
PluginManager.TYPE['STORAGE'] = KIND_UNIQUE;
|
||||
|
||||
PluginManager.prototype._register = function(obj, name) {
|
||||
preconditions.checkArgument(obj.type, 'Plugin has not type:' + name);
|
||||
var type = obj.type;
|
||||
var kind = PluginManager.TYPE[type];
|
||||
|
||||
preconditions.checkArgument(kind, 'Plugin has unknown type' + name);
|
||||
preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered: ' + name);
|
||||
|
||||
if (kind === PluginManager.KIND_UNIQUE) {
|
||||
this.registered[type] = obj;
|
||||
} else {
|
||||
this.registered[type] = this.registered[type] || [];
|
||||
this.registered[type].push(obj);
|
||||
}
|
||||
|
||||
this.scripts = this.scripts.concat(obj.scripts || []);
|
||||
};
|
||||
|
||||
|
||||
PluginManager.prototype.get = function(type) {
|
||||
return this.registered[type];
|
||||
};
|
||||
|
||||
module.exports = PluginManager;
|
||||
|
|
@ -50,7 +50,6 @@ TxProposals.prototype.getNtxidsSince = function(sinceTs) {
|
|||
if (txp.createdTs >= sinceTs)
|
||||
ret.push(ii);
|
||||
}
|
||||
console.log('[TxProposals.js.52:ret:]',ret); //TODO
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ function Wallet(opts) {
|
|||
'publicKeyRing', 'txProposals', 'privateKey', 'version',
|
||||
'reconnectDelay'
|
||||
].forEach(function(k) {
|
||||
preconditions.checkArgument(!_.isUndefined(opts[k]), 'missing required option for Wallet: ' + k);
|
||||
preconditions.checkArgument(!_.isUndefined(opts[k]), 'MISSOPT: missing required option for Wallet: ' + k);
|
||||
self[k] = opts[k];
|
||||
});
|
||||
|
||||
|
|
@ -822,21 +822,28 @@ Wallet.prototype.getRegisteredPeerIds = function() {
|
|||
* @emits locked - in case the wallet is opened in another instance
|
||||
*/
|
||||
Wallet.prototype.keepAlive = function() {
|
||||
try {
|
||||
this.lock.keepAlive();
|
||||
} catch (e) {
|
||||
log.debug(e);
|
||||
this.emit('locked', null, 'Wallet appears to be openned on other browser instance. Closing this one.');
|
||||
}
|
||||
var self = this;
|
||||
|
||||
this.lock.keepAlive(function(err) {
|
||||
if (err) {
|
||||
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
|
||||
* @param {function} callback (err)
|
||||
*/
|
||||
Wallet.prototype.store = function() {
|
||||
Wallet.prototype.store = function(cb) {
|
||||
var self = this;
|
||||
this.keepAlive();
|
||||
this.storage.setFromObj(this.id, this.toObj());
|
||||
log.debug('Wallet stored');
|
||||
this.storage.setFromObj(this.id, this.toObj(), function(err) {
|
||||
log.debug('Wallet stored');
|
||||
if (cb)
|
||||
cb(err);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -878,7 +885,7 @@ Wallet.prototype.toObj = function() {
|
|||
*/
|
||||
Wallet.fromObj = function(o, storage, network, blockchain) {
|
||||
|
||||
// TODO: What is this supposed to do?
|
||||
// clone opts
|
||||
var opts = JSON.parse(JSON.stringify(o.opts));
|
||||
|
||||
opts.addressBook = o.addressBook;
|
||||
|
|
@ -2342,11 +2349,14 @@ Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb)
|
|||
/**
|
||||
* @desc Closes the wallet and disconnects all services
|
||||
*/
|
||||
Wallet.prototype.close = function() {
|
||||
Wallet.prototype.close = function(cb) {
|
||||
var self =this;
|
||||
log.debug('## CLOSING');
|
||||
this.lock.release();
|
||||
this.network.cleanUp();
|
||||
this.blockchain.destroy();
|
||||
this.lock.release(function() {
|
||||
self.network.cleanUp();
|
||||
self.blockchain.destroy();
|
||||
if (cb) return cb();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
'use strict';
|
||||
var preconditions = require('preconditions').singleton();
|
||||
|
||||
var TxProposals = require('./TxProposals');
|
||||
var PublicKeyRing = require('./PublicKeyRing');
|
||||
|
|
@ -6,10 +7,11 @@ var PrivateKey = require('./PrivateKey');
|
|||
var Wallet = require('./Wallet');
|
||||
var _ = require('underscore');
|
||||
var log = require('../../log');
|
||||
var PluginManager = require('./PluginManager');
|
||||
var Async = module.exports.Async = require('../network/Async');
|
||||
var Insight = module.exports.Insight = require('../blockchain/Insight');
|
||||
var StorageLocalEncrypted = module.exports.StorageLocalEncrypted = require('../storage/LocalEncrypted');
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var Storage = module.exports.Storage = require('../Storage');
|
||||
|
||||
/**
|
||||
* @desc
|
||||
|
|
@ -32,15 +34,26 @@ var preconditions = require('preconditions').singleton();
|
|||
* @param {string} version - the version of copay for which this wallet was generated (for example, 0.4.7)
|
||||
* @constructor
|
||||
*/
|
||||
function WalletFactory(config, version) {
|
||||
var self = this;
|
||||
config = config || {};
|
||||
|
||||
this.Storage = config.Storage || StorageLocalEncrypted;
|
||||
function WalletFactory(config, version, pluginManager) {
|
||||
var self = this;
|
||||
preconditions.checkArgument(config);
|
||||
preconditions.checkArgument(config.network);
|
||||
|
||||
this.Storage = config.Storage || Storage;
|
||||
this.Network = config.Network || Async;
|
||||
this.Blockchain = config.Blockchain || Insight;
|
||||
|
||||
this.storage = new this.Storage(config.storage);
|
||||
var storageOpts = {};
|
||||
|
||||
if (pluginManager) {
|
||||
storageOpts = {
|
||||
storage: pluginManager.get('STORAGE')
|
||||
};
|
||||
}
|
||||
|
||||
this.storage = new this.Storage(storageOpts);
|
||||
|
||||
this.networks = {
|
||||
'livenet': new this.Network(config.network.livenet),
|
||||
'testnet': new this.Network(config.network.testnet),
|
||||
|
|
@ -50,31 +63,10 @@ function WalletFactory(config, version) {
|
|||
'testnet': new this.Blockchain(config.network.testnet),
|
||||
};
|
||||
|
||||
this.walletDefaults = config.wallet;
|
||||
this.walletDefaults = config.wallet || {};
|
||||
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
|
||||
|
|
@ -94,10 +86,15 @@ WalletFactory.prototype.obtainNetworkName = function(obj) {
|
|||
* @param {string[]} skipFields - fields to skip when importing
|
||||
* @return {Wallet}
|
||||
*/
|
||||
WalletFactory.prototype.fromObj = function(obj, skipFields) {
|
||||
var networkName = this.obtainNetworkName(obj);
|
||||
WalletFactory.prototype.fromObj = function(inObj, skipFields) {
|
||||
var networkName = this.obtainNetworkName(inObj);
|
||||
preconditions.checkState(networkName);
|
||||
preconditions.checkArgument(inObj);
|
||||
|
||||
var obj = JSON.parse(JSON.stringify(inObj));
|
||||
|
||||
// not stored options
|
||||
obj.opts = obj.opts || {};
|
||||
obj.opts.reconnectDelay = this.walletDefaults.reconnectDelay;
|
||||
|
||||
skipFields = skipFields || [];
|
||||
|
|
@ -117,16 +114,15 @@ WalletFactory.prototype.fromObj = function(obj, skipFields) {
|
|||
/**
|
||||
* @desc Imports a wallet from an encrypted base64 object
|
||||
* @param {string} base64 - the base64 encoded object
|
||||
* @param {string} password - password to decrypt it
|
||||
* @param {string} passphrase - passphrase to decrypt it
|
||||
* @param {string[]} skipFields - fields to ignore when importing
|
||||
* @return {Wallet}
|
||||
*/
|
||||
WalletFactory.prototype.fromEncryptedObj = function(base64, password, skipFields) {
|
||||
this.storage._setPassphrase(password);
|
||||
WalletFactory.prototype.fromEncryptedObj = function(base64, passphrase, skipFields) {
|
||||
this.storage.setPassphrase(passphrase);
|
||||
var walletObj = this.storage.import(base64);
|
||||
if (!walletObj) return false;
|
||||
var w = this.fromObj(walletObj, skipFields);
|
||||
return w;
|
||||
return this.fromObj(walletObj, skipFields);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -134,15 +130,15 @@ WalletFactory.prototype.fromEncryptedObj = function(base64, password, skipFields
|
|||
* @TODO: this is essentialy the same method as {@link WalletFactory#fromEncryptedObj}!
|
||||
* @desc Imports a wallet from an encrypted base64 object
|
||||
* @param {string} base64 - the base64 encoded object
|
||||
* @param {string} password - password to decrypt it
|
||||
* @param {string} passphrase - passphrase to decrypt it
|
||||
* @param {string[]} skipFields - fields to ignore when importing
|
||||
* @return {Wallet}
|
||||
*/
|
||||
WalletFactory.prototype.import = function(base64, password, skipFields) {
|
||||
WalletFactory.prototype.import = function(base64, passphrase, skipFields) {
|
||||
var self = this;
|
||||
var w = self.fromEncryptedObj(base64, password, skipFields);
|
||||
var w = self.fromEncryptedObj(base64, passphrase, skipFields);
|
||||
|
||||
if (!w) throw new Error('Wrong password');
|
||||
if (!w) throw new Error('Wrong passphrase');
|
||||
return w;
|
||||
};
|
||||
|
||||
|
|
@ -150,26 +146,52 @@ WalletFactory.prototype.import = function(base64, password, skipFields) {
|
|||
* @desc Retrieve a wallet from storage
|
||||
* @param {string} walletId - the wallet id
|
||||
* @param {string[]} skipFields - parameters to ignore when importing
|
||||
* @return {Wallet}
|
||||
* @param {function} callback - {err, Wallet}
|
||||
*/
|
||||
WalletFactory.prototype.read = function(walletId, skipFields) {
|
||||
if (!this._checkRead(walletId))
|
||||
return false;
|
||||
|
||||
WalletFactory.prototype.read = function(walletId, skipFields, cb) {
|
||||
var self = this,
|
||||
err;
|
||||
var obj = {};
|
||||
var s = this.storage;
|
||||
|
||||
obj.id = walletId;
|
||||
_.each(Wallet.PERSISTED_PROPERTIES, function(value) {
|
||||
obj[value] = s.get(walletId, value);
|
||||
this.storage.getMany(walletId, Wallet.PERSISTED_PROPERTIES, function(ret) {
|
||||
for (var ii in ret) {
|
||||
obj[ii] = ret[ii];
|
||||
}
|
||||
|
||||
if (!_.any(_.values(obj)))
|
||||
return cb(new Error('Wallet not found'));
|
||||
|
||||
var w, err;
|
||||
obj.id = walletId;
|
||||
try {
|
||||
w = self.fromObj(obj, skipFields);
|
||||
} catch (e) {
|
||||
if (e && e.message && e.message.indexOf('MISSOPTS')) {
|
||||
err = new Error('Could not read: ' + walletId);
|
||||
} else {
|
||||
err = e;
|
||||
}
|
||||
w = null;
|
||||
}
|
||||
return cb(err, w);
|
||||
});
|
||||
};
|
||||
|
||||
var w = this.fromObj(obj, skipFields);
|
||||
return w;
|
||||
|
||||
/**
|
||||
* @desc This method instantiates a wallet. Usefull for stubbing.
|
||||
*
|
||||
* @param {opts} opts, ready for new Wallet(opts)
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
WalletFactory.prototype._getWallet = function(opts) {
|
||||
return new Wallet(opts);
|
||||
};
|
||||
|
||||
/**
|
||||
* @desc This method instantiates a wallet
|
||||
* @desc This method prepares options for a new Wallet
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {string} opts.id
|
||||
|
|
@ -185,9 +207,11 @@ WalletFactory.prototype.read = function(walletId, skipFields) {
|
|||
* @TODO: Figure out in what unit is this reconnect delay.
|
||||
* @param {number} opts.reconnectDelay milliseconds?
|
||||
* @param {number=} opts.version
|
||||
* @param {callback} opts.version
|
||||
* @return {Wallet}
|
||||
*/
|
||||
WalletFactory.prototype.create = function(opts) {
|
||||
WalletFactory.prototype.create = function(opts, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
opts = opts || {};
|
||||
opts.networkName = opts.networkName || 'testnet';
|
||||
|
|
@ -224,7 +248,6 @@ WalletFactory.prototype.create = function(opts) {
|
|||
});
|
||||
log.debug('\t### TxProposals Initialized');
|
||||
|
||||
this.storage._setPassphrase(opts.passphrase);
|
||||
|
||||
opts.storage = this.storage;
|
||||
opts.network = this.networks[opts.networkName];
|
||||
|
|
@ -236,10 +259,15 @@ WalletFactory.prototype.create = function(opts) {
|
|||
opts.totalCopayers = totalCopayers;
|
||||
opts.version = opts.version || this.version;
|
||||
|
||||
var w = new Wallet(opts);
|
||||
w.store();
|
||||
this.storage.setLastOpened(w.id);
|
||||
return w;
|
||||
this.storage.setPassphrase(opts.passphrase);
|
||||
var w = this._getWallet(opts);
|
||||
var self = this;
|
||||
w.store(function(err) {
|
||||
if (err) return cb(err);
|
||||
self.storage.setLastOpened(w.id, function(err) {
|
||||
return cb(err, w);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -266,29 +294,31 @@ WalletFactory.prototype._checkVersion = function(inVersion) {
|
|||
* @desc Retrieve a wallet from the storage
|
||||
* @param {string} walletId - the id of the wallet
|
||||
* @param {string} passphrase - the passphrase to decode it
|
||||
* @return {Wallet}
|
||||
* @param {function} callback (err, {Wallet})
|
||||
* @return
|
||||
*/
|
||||
WalletFactory.prototype.open = function(walletId, passphrase) {
|
||||
this.storage._setPassphrase(passphrase);
|
||||
var w = this.read(walletId);
|
||||
if (w) {
|
||||
w.store();
|
||||
}
|
||||
WalletFactory.prototype.open = function(walletId, passphrase, cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
self.storage.setPassphrase(passphrase);
|
||||
self.read(walletId, null, function(err, w) {
|
||||
if (err) return cb(err);
|
||||
|
||||
this.storage.setLastOpened(walletId);
|
||||
return w;
|
||||
w.store(function(err) {
|
||||
self.storage.setLastOpened(walletId, function() {
|
||||
return cb(err, w);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @desc Retrieve all wallets stored without encription in the storage instance
|
||||
* @returns {Wallet[]}
|
||||
*/
|
||||
WalletFactory.prototype.getWallets = function() {
|
||||
var ret = this.storage.getWallets();
|
||||
ret.forEach(function(i) {
|
||||
i.show = i.name ? ((i.name + ' <' + i.id + '>')) : i.id;
|
||||
WalletFactory.prototype.getWallets = function(cb) {
|
||||
this.storage.getWallets(function(ret) {
|
||||
ret.forEach(function(i) {
|
||||
i.show = i.name ? ((i.name + ' <' + i.id + '>')) : i.id;
|
||||
});
|
||||
return cb(null, ret);
|
||||
});
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -301,9 +331,12 @@ WalletFactory.prototype.getWallets = function() {
|
|||
*/
|
||||
WalletFactory.prototype.delete = function(walletId, cb) {
|
||||
var s = this.storage;
|
||||
s.deleteWallet(walletId);
|
||||
s.setLastOpened(undefined);
|
||||
return cb();
|
||||
s.deleteWallet(walletId, function(err) {
|
||||
if (err) return cb(err);
|
||||
s.setLastOpened(null, function(err) {
|
||||
return cb(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -331,15 +364,21 @@ WalletFactory.prototype.decodeSecret = function(secret) {
|
|||
* information locally using <tt>passphrase</tt>. <tt>privateHex</tt> is the
|
||||
* private extended master key. <tt>cb</tt> has two params: error and wallet.
|
||||
*
|
||||
* @param {string} secret - the wallet secret
|
||||
* @param {string} nickname - a nickname for the current user
|
||||
* @param {string} passphrase - a passphrase to use to encrypt the wallet for persistance
|
||||
* @param {string} privateHex - the private extended master key
|
||||
* @param {object} opts
|
||||
* @param {string} opts.secret - the wallet secret
|
||||
* @param {string} opts.passphrase - a passphrase to use to encrypt the wallet for persistance
|
||||
* @param {string} opts.nickname - a nickname for the current user
|
||||
* @param {string} opts.privateHex - the private extended master key
|
||||
* @param {walletCreationCallback} cb - a callback
|
||||
*/
|
||||
WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphrase, privateHex, cb) {
|
||||
WalletFactory.prototype.joinCreateSession = function(opts, cb) {
|
||||
preconditions.checkArgument(opts);
|
||||
preconditions.checkArgument(opts.secret);
|
||||
preconditions.checkArgument(opts.passphrase);
|
||||
preconditions.checkArgument(opts.nickname);
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
var decodedSecret = this.decodeSecret(secret);
|
||||
var decodedSecret = this.decodeSecret(opts.secret);
|
||||
if (!decodedSecret || !decodedSecret.networkName || !decodedSecret.pubKey) {
|
||||
return cb('badSecret');
|
||||
}
|
||||
|
|
@ -348,14 +387,14 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
|
|||
networkName: decodedSecret.networkName,
|
||||
};
|
||||
|
||||
if (privateHex && privateHex.length > 1) {
|
||||
if (opts.privateHex && opts.privateHex.length > 1) {
|
||||
privOpts.extendedPrivateKeyString = privateHex;
|
||||
}
|
||||
|
||||
//Create our PrivateK
|
||||
var privateKey = new PrivateKey(privOpts);
|
||||
log.debug('\t### PrivateKey Initialized');
|
||||
var opts = {
|
||||
var joinOpts = {
|
||||
copayerId: privateKey.getId(),
|
||||
privkey: privateKey.getIdPriv(),
|
||||
key: privateKey.getIdKey(),
|
||||
|
|
@ -379,23 +418,32 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
|
|||
return cb('joinError');
|
||||
});
|
||||
|
||||
joinNetwork.start(opts, function() {
|
||||
joinNetwork.greet(decodedSecret.pubKey, opts.secretNumber);
|
||||
joinNetwork.start(joinOpts, function() {
|
||||
|
||||
joinNetwork.greet(decodedSecret.pubKey, joinOpts.secretNumber);
|
||||
joinNetwork.on('data', function(sender, data) {
|
||||
if (data.type === 'walletId') {
|
||||
if (data.type === 'walletId' && data.opts) {
|
||||
if (data.networkName !== decodedSecret.networkName) {
|
||||
return cb('badNetwork');
|
||||
}
|
||||
|
||||
data.opts.privateKey = privateKey;
|
||||
data.opts.nickname = nickname;
|
||||
data.opts.passphrase = passphrase;
|
||||
data.opts.id = data.walletId;
|
||||
var w = self.create(data.opts);
|
||||
w.sendWalletReady(decodedSecret.pubKey);
|
||||
return cb(null, w);
|
||||
} else {
|
||||
return cb('walletFull', w);
|
||||
var walletOpts = _.clone(data.opts);
|
||||
walletOpts.id = data.walletId;
|
||||
|
||||
walletOpts.privateKey = privateKey;
|
||||
walletOpts.nickname = opts.nickname;
|
||||
walletOpts.passphrase = opts.passphrase;
|
||||
|
||||
self.create(walletOpts, function(err, w) {
|
||||
|
||||
if (w) {
|
||||
w.sendWalletReady(decodedSecret.pubKey);
|
||||
} else {
|
||||
if (!err) err = 'walletFull';
|
||||
log.info(err);
|
||||
}
|
||||
return cb(err, w);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,50 +6,95 @@ function WalletLock(storage, walletId, timeoutMin) {
|
|||
preconditions.checkArgument(storage);
|
||||
preconditions.checkArgument(walletId);
|
||||
|
||||
this.sessionId = storage.getSessionId();
|
||||
this.storage = storage;
|
||||
this.timeoutMin = timeoutMin || 5;
|
||||
this.key = WalletLock._keyFor(walletId);
|
||||
this._setLock();
|
||||
}
|
||||
|
||||
|
||||
WalletLock.prototype.init = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
|
||||
self.storage.getSessionId(function(sid) {
|
||||
preconditions.checkState(sid);
|
||||
|
||||
self.sessionId = sid;
|
||||
cb();
|
||||
});
|
||||
};
|
||||
|
||||
WalletLock._keyFor = function(walletId) {
|
||||
return 'lock' + '::' + walletId;
|
||||
};
|
||||
|
||||
WalletLock.prototype._isLockedByOther = function() {
|
||||
var json = this.storage.getGlobal(this.key);
|
||||
var wl = json ? JSON.parse(json) : null;
|
||||
var t = wl ? (Date.now() - wl.expireTs) : false;
|
||||
// is not locked?
|
||||
if (!wl || t > 0 || wl.sessionId === this.sessionId)
|
||||
return false;
|
||||
WalletLock.prototype._isLockedByOther = function(cb) {
|
||||
var self = this;
|
||||
|
||||
// Seconds remainding
|
||||
return parseInt(-t/1000.);
|
||||
};
|
||||
this.storage.getGlobal(this.key, function(json) {
|
||||
var wl = json ? JSON.parse(json) : null;
|
||||
if (!wl || !wl.expireTs)
|
||||
return cb(false);
|
||||
|
||||
var expiredSince = Date.now() - wl.expireTs;
|
||||
if (expiredSince >= 0)
|
||||
return cb(false);
|
||||
|
||||
WalletLock.prototype._setLock = function() {
|
||||
this.storage.setGlobal(this.key, {
|
||||
sessionId: this.sessionId,
|
||||
expireTs: Date.now() + this.timeoutMin * 60 * 1000,
|
||||
var isMyself = wl.sessionId === self.sessionId;
|
||||
|
||||
if (isMyself)
|
||||
return cb(false);
|
||||
|
||||
// Seconds remainding
|
||||
return cb(parseInt(-expiredSince / 1000));
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
WalletLock.prototype.keepAlive = function() {
|
||||
WalletLock.prototype._setLock = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
preconditions.checkState(this.sessionId);
|
||||
var self = this;
|
||||
|
||||
var t = this._isLockedByOther();
|
||||
if (t)
|
||||
throw new Error('Wallet is already open. Close it to proceed or wait '+ t + ' seconds if you close it already' );
|
||||
this._setLock();
|
||||
this.storage.setGlobal(this.key, {
|
||||
sessionId: this.sessionId,
|
||||
expireTs: Date.now() + this.timeoutMin * 60 * 1000,
|
||||
}, function() {
|
||||
|
||||
cb(null);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
WalletLock.prototype.release = function() {
|
||||
this.storage.removeGlobal(this.key);
|
||||
WalletLock.prototype._doKeepAlive = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
preconditions.checkState(this.sessionId);
|
||||
|
||||
var self = this;
|
||||
|
||||
this._isLockedByOther(function(t) {
|
||||
if (t)
|
||||
return cb(new Error('LOCKED: Wallet is locked for ' + t + ' srcs'));
|
||||
|
||||
self._setLock(cb);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
WalletLock.prototype.keepAlive = function(cb) {
|
||||
var self = this;
|
||||
|
||||
if (!self.sessionId) {
|
||||
return self.init(self._doKeepAlive.bind(self, cb));
|
||||
};
|
||||
|
||||
return this._doKeepAlive(cb);
|
||||
};
|
||||
|
||||
|
||||
WalletLock.prototype.release = function(cb) {
|
||||
this.storage.removeGlobal(this.key, cb);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,208 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
|
||||
var bitcore = require('bitcore');
|
||||
var preconditions = require('preconditions').instance();
|
||||
var id = 0;
|
||||
|
||||
function Storage(opts) {
|
||||
opts = opts || {};
|
||||
|
||||
this.__uniqueid = ++id;
|
||||
if (opts.password)
|
||||
this._setPassphrase(opts.password);
|
||||
|
||||
try {
|
||||
this.localStorage = opts.localStorage || localStorage;
|
||||
this.sessionStorage = opts.sessionStorage || sessionStorage;
|
||||
} catch (e) {}
|
||||
preconditions.checkState(this.localStorage, 'No localstorage found');
|
||||
preconditions.checkState(this.sessionStorage, 'No sessionStorage found');
|
||||
}
|
||||
|
||||
var pps = {};
|
||||
Storage.prototype._getPassphrase = function() {
|
||||
if (!pps[this.__uniqueid])
|
||||
throw new Error('No passprase set');
|
||||
|
||||
return pps[this.__uniqueid];
|
||||
}
|
||||
|
||||
Storage.prototype._setPassphrase = function(password) {
|
||||
pps[this.__uniqueid] = password;
|
||||
}
|
||||
|
||||
Storage.prototype._encrypt = function(string) {
|
||||
var encrypted = CryptoJS.AES.encrypt(string, this._getPassphrase());
|
||||
var encryptedBase64 = encrypted.toString();
|
||||
return encryptedBase64;
|
||||
};
|
||||
|
||||
Storage.prototype._decrypt = function(base64) {
|
||||
var decryptedStr = null;
|
||||
try {
|
||||
var decrypted = CryptoJS.AES.decrypt(base64, this._getPassphrase());
|
||||
if (decrypted)
|
||||
decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
|
||||
} catch (e) {
|
||||
// Error while decrypting
|
||||
return null;
|
||||
}
|
||||
return decryptedStr;
|
||||
};
|
||||
|
||||
|
||||
Storage.prototype._read = function(k) {
|
||||
var ret;
|
||||
ret = this.localStorage.getItem(k);
|
||||
if (!ret) return null;
|
||||
ret = this._decrypt(ret);
|
||||
if (!ret) return null;
|
||||
ret = ret.toString(CryptoJS.enc.Utf8);
|
||||
ret = JSON.parse(ret);
|
||||
return ret;
|
||||
};
|
||||
|
||||
Storage.prototype._write = function(k, v) {
|
||||
v = JSON.stringify(v);
|
||||
v = this._encrypt(v);
|
||||
|
||||
this.localStorage.setItem(k, v);
|
||||
};
|
||||
|
||||
// get value by key
|
||||
Storage.prototype.getGlobal = function(k) {
|
||||
var item = this.localStorage.getItem(k);
|
||||
return item == 'undefined' ? undefined : item;
|
||||
};
|
||||
|
||||
// set value for key
|
||||
Storage.prototype.setGlobal = function(k, v) {
|
||||
this.localStorage.setItem(k, typeof v === 'object' ? JSON.stringify(v) : v);
|
||||
};
|
||||
|
||||
// remove value for key
|
||||
Storage.prototype.removeGlobal = function(k) {
|
||||
this.localStorage.removeItem(k);
|
||||
};
|
||||
|
||||
Storage.prototype.getSessionId = function() {
|
||||
var sessionId = this.sessionStorage.getItem('sessionId');
|
||||
if (!sessionId) {
|
||||
sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex');
|
||||
this.sessionStorage.setItem('sessionId', sessionId);
|
||||
}
|
||||
return sessionId;
|
||||
};
|
||||
|
||||
Storage.prototype._key = function(walletId, k) {
|
||||
return walletId + '::' + k;
|
||||
};
|
||||
// get value by key
|
||||
Storage.prototype.get = function(walletId, k) {
|
||||
var ret = this._read(this._key(walletId, k));
|
||||
return ret;
|
||||
};
|
||||
|
||||
// set value for key
|
||||
Storage.prototype.set = function(walletId, k, v) {
|
||||
this._write(this._key(walletId, k), v);
|
||||
};
|
||||
|
||||
// remove value for key
|
||||
Storage.prototype.remove = function(walletId, k) {
|
||||
this.removeGlobal(this._key(walletId, k));
|
||||
};
|
||||
|
||||
Storage.prototype.setName = function(walletId, name) {
|
||||
this.setGlobal('nameFor::' + walletId, name);
|
||||
};
|
||||
|
||||
Storage.prototype.getName = function(walletId) {
|
||||
var ret = this.getGlobal('nameFor::' + walletId);
|
||||
return ret;
|
||||
};
|
||||
|
||||
Storage.prototype.getWalletIds = function() {
|
||||
var walletIds = [];
|
||||
var uniq = {};
|
||||
|
||||
for (var i = 0; i < this.localStorage.length; i++) {
|
||||
var key = this.localStorage.key(i);
|
||||
var split = key.split('::');
|
||||
if (split.length == 2) {
|
||||
var walletId = split[0];
|
||||
|
||||
if (!walletId || walletId === 'nameFor' || walletId === 'lock')
|
||||
continue;
|
||||
|
||||
if (typeof uniq[walletId] === 'undefined') {
|
||||
walletIds.push(walletId);
|
||||
uniq[walletId] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return walletIds;
|
||||
};
|
||||
|
||||
Storage.prototype.getWallets = function() {
|
||||
var wallets = [];
|
||||
var ids = this.getWalletIds();
|
||||
|
||||
for (var i in ids) {
|
||||
wallets.push({
|
||||
id: ids[i],
|
||||
name: this.getName(ids[i]),
|
||||
});
|
||||
}
|
||||
return wallets;
|
||||
};
|
||||
|
||||
Storage.prototype.deleteWallet = function(walletId) {
|
||||
var toDelete = {};
|
||||
toDelete['nameFor::' + walletId] = 1;
|
||||
|
||||
for (var i = 0; i < this.localStorage.length; i++) {
|
||||
var key = this.localStorage.key(i);
|
||||
var split = key.split('::');
|
||||
if (split.length == 2 && split[0] === walletId) {
|
||||
toDelete[key] = 1;
|
||||
}
|
||||
}
|
||||
for (var i in toDelete) {
|
||||
this.removeGlobal(i);
|
||||
}
|
||||
};
|
||||
|
||||
Storage.prototype.setLastOpened = function(walletId) {
|
||||
this.setGlobal('lastOpened', walletId);
|
||||
}
|
||||
|
||||
Storage.prototype.getLastOpened = function() {
|
||||
return this.getGlobal('lastOpened');
|
||||
}
|
||||
|
||||
//obj contains keys to be set
|
||||
Storage.prototype.setFromObj = function(walletId, obj) {
|
||||
for (var k in obj) {
|
||||
this.set(walletId, k, obj[k]);
|
||||
}
|
||||
this.setName(walletId, obj.opts.name);
|
||||
};
|
||||
|
||||
// remove all values
|
||||
Storage.prototype.clearAll = function() {
|
||||
this.localStorage.clear();
|
||||
};
|
||||
|
||||
Storage.prototype.import = function(base64) {
|
||||
var decryptedStr = this._decrypt(base64);
|
||||
return JSON.parse(decryptedStr);
|
||||
};
|
||||
|
||||
Storage.prototype.export = function(obj) {
|
||||
var string = JSON.stringify(obj);
|
||||
return this._encrypt(string);
|
||||
};
|
||||
|
||||
module.exports = Storage;
|
||||
|
|
@ -76,8 +76,8 @@ angular
|
|||
// IDLE timeout
|
||||
var timeout = config.wallet.idleDurationMin * 60 || 300;
|
||||
$idleProvider.idleDuration(timeout); // in seconds
|
||||
$idleProvider.warningDuration(20); // in seconds
|
||||
$keepaliveProvider.interval(2); // in seconds
|
||||
$idleProvider.warningDuration(40); // in seconds
|
||||
$keepaliveProvider.interval(30); // in seconds
|
||||
})
|
||||
.run(function($rootScope, $location, $idle, gettextCatalog) {
|
||||
gettextCatalog.currentLanguage = config.defaultLanguage;
|
||||
|
|
|
|||
18
js/services/pluginManager.js
Normal file
18
js/services/pluginManager.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services').factory('pluginManager', function(angularLoad){
|
||||
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;
|
||||
});
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
'use strict';
|
||||
angular.module('copayApp.services').factory('walletFactory', function(pluginManager){
|
||||
return new copay.WalletFactory(config, copay.version, pluginManager);
|
||||
});
|
||||
|
||||
angular.module('copayApp.services').value('walletFactory', new copay.WalletFactory(config, copay.version));
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ module.exports = function(config) {
|
|||
'lib/angular-route/angular-route.min.js',
|
||||
'lib/angular-foundation/mm-foundation.min.js',
|
||||
'lib/angular-foundation/mm-foundation-tpls.min.js',
|
||||
'lib/angular-load/angular-load.min.js',
|
||||
'lib/angular-gettext/dist/angular-gettext.min.js',
|
||||
'lib/inherits/inherits.js',
|
||||
'lib/bitcore.js',
|
||||
|
|
@ -60,6 +61,7 @@ module.exports = function(config) {
|
|||
'test/mocks/FakeWallet.js',
|
||||
'test/mocks/FakeBlockchainSocket.js',
|
||||
'test/mocks/FakePayProServer.js',
|
||||
'test/mocks/FakeLocalStorage.js',
|
||||
|
||||
'test/mocha.conf.js',
|
||||
|
||||
|
|
|
|||
322
plugins/GoogleDrive.js
Normal file
322
plugins/GoogleDrive.js
Normal file
|
|
@ -0,0 +1,322 @@
|
|||
'use strict';
|
||||
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var loaded = 0;
|
||||
var SCOPES = 'https://www.googleapis.com/auth/drive';
|
||||
var log = require('../js/log');
|
||||
|
||||
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.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;
|
||||
};
|
||||
|
||||
window.InitGoogleDrive = function() {
|
||||
log.debug('googleDrive loadeded'); //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() {
|
||||
|
||||
log.debug('Google Drive: Checking Auth');
|
||||
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;
|
||||
log.debug('Google Drive: 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._httpGet = function(theUrl) {
|
||||
var accessToken = gapi.auth.getToken().access_token;
|
||||
var xmlHttp = null;
|
||||
|
||||
xmlHttp = new XMLHttpRequest();
|
||||
xmlHttp.open("GET", theUrl, false);
|
||||
xmlHttp.setRequestHeader('Authorization', 'Bearer ' + accessToken);
|
||||
xmlHttp.send(null);
|
||||
return xmlHttp.responseText;
|
||||
}
|
||||
|
||||
GoogleDrive.prototype.getItem = function(k, cb) {
|
||||
//console.log('[googleDrive.js.95:getItem:]', k); //TODO
|
||||
var self = this;
|
||||
|
||||
self.checkReady();
|
||||
self._idForName(k, function(kId) {
|
||||
// console.log('[googleDrive.js.89:kId:]', kId); //TODO
|
||||
if (!kId)
|
||||
return cb(null);
|
||||
|
||||
|
||||
var args = {
|
||||
'path': '/drive/v2/files/' + kId,
|
||||
'method': 'GET',
|
||||
};
|
||||
// console.log('[googleDrive.js.95:args:]', args); //TODO
|
||||
|
||||
var request = gapi.client.request(args);
|
||||
request.execute(function(res) {
|
||||
// console.log('[googleDrive.js.175:res:]', res); //TODO
|
||||
if (!res || !res.downloadUrl)
|
||||
return cb(null);
|
||||
|
||||
return cb(self._httpGet(res.downloadUrl));
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
GoogleDrive.prototype.setItem = function(k, v, cb) {
|
||||
// console.log('[googleDrive.js.111:setItem:]', k, v); //TODO
|
||||
var self = this;
|
||||
|
||||
self.checkReady();
|
||||
self._idForName(this.home, function(parentId) {
|
||||
preconditions.checkState(parentId);
|
||||
// console.log('[googleDrive.js.118:parentId:]', parentId); //TODO
|
||||
self._idForName(k, function(kId) {
|
||||
|
||||
// console.log('[googleDrive.js.105]', parentId, kId); //TODO
|
||||
|
||||
|
||||
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': [{
|
||||
'id': 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;
|
||||
|
||||
var args = {
|
||||
'path': '/upload/drive/v2/files' + (kId ? '/' + kId : ''),
|
||||
'method': kId ? 'PUT' : 'POST',
|
||||
'params': {
|
||||
'uploadType': 'multipart',
|
||||
},
|
||||
'headers': {
|
||||
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
|
||||
},
|
||||
'body': multipartRequestBody
|
||||
}
|
||||
// console.log('[googleDrive.js.148:args:]', args); //TODO
|
||||
|
||||
var request = gapi.client.request(args);
|
||||
request.execute(function(ret) {
|
||||
return cb(ret.kind === 'drive#file' ? null : new Error('error saving file on drive'));
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
GoogleDrive.prototype.removeItem = function(k, cb) {
|
||||
var self = this;
|
||||
|
||||
self.checkReady();
|
||||
self._idForName(this.home, function(parentId) {
|
||||
preconditions.checkState(parentId);
|
||||
self._idForName(k, function(kId) {
|
||||
|
||||
var args = {
|
||||
'path': '/drive/v2/files/' + kId,
|
||||
'method': 'DELETE',
|
||||
};
|
||||
var request = gapi.client.request(args);
|
||||
request.execute(function() {
|
||||
if (cb)
|
||||
cb();
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
GoogleDrive.prototype.clear = function() {
|
||||
this.checkReady();
|
||||
throw new Error('clear not implemented');
|
||||
};
|
||||
|
||||
|
||||
GoogleDrive.prototype._mkdir = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
|
||||
log.debug('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(self.home, cb);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
GoogleDrive.prototype._idForName = function(name, cb) {
|
||||
// console.log('[googleDrive.js.199:_idForName:]', name); //TODO
|
||||
preconditions.checkArgument(name);
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
|
||||
if (!self.isReady) {
|
||||
log.debug('Waiting for Google Drive');
|
||||
self.ts = self.ts * 1.5;
|
||||
return setTimeout(self._idForName.bind(self, name, cb), self.ts);
|
||||
}
|
||||
|
||||
if (self.idCache[name]) {
|
||||
// console.log('[googleDrive.js.212:] FROM CACHE', name, self.idCache[name]); //TODO
|
||||
return cb(self.idCache[name]);
|
||||
}
|
||||
|
||||
log.debug('GoogleDrive Querying for: ', name); //TODO
|
||||
var args;
|
||||
|
||||
var idParent = name == this.home ? 'root' : self.idCache[this.home];
|
||||
|
||||
if (!idParent) {
|
||||
return self._mkdir(function() {
|
||||
self._idForName(name, cb);
|
||||
});
|
||||
}
|
||||
// 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",
|
||||
}
|
||||
};
|
||||
|
||||
var request = gapi.client.request(args);
|
||||
request.execute(function(res) {
|
||||
var i = res.items && res.items[0] ? res.items[0].id : false;
|
||||
if (i)
|
||||
self.idCache[name] = i;
|
||||
// console.log('[googleDrive.js.238] CACHING ' + name + ':' + i); //TODO
|
||||
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.items[ii].title);
|
||||
}
|
||||
return cb(ret);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
GoogleDrive.prototype.key = function(k) {
|
||||
var v = localStorage.key(k);
|
||||
return v;
|
||||
};
|
||||
|
||||
|
||||
module.exports = GoogleDrive;
|
||||
41
plugins/LocalStorage.js
Normal file
41
plugins/LocalStorage.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
'use strict';
|
||||
|
||||
function LocalStorage() {
|
||||
this.type = 'STORAGE';
|
||||
};
|
||||
|
||||
LocalStorage.prototype.init = function() {
|
||||
};
|
||||
|
||||
|
||||
LocalStorage.prototype.getItem = function(k,cb) {
|
||||
return cb(localStorage.getItem(k));
|
||||
};
|
||||
|
||||
LocalStorage.prototype.setItem = function(k,v,cb) {
|
||||
localStorage.setItem(k,v);
|
||||
return cb();
|
||||
};
|
||||
|
||||
LocalStorage.prototype.removeItem = function(k,cb) {
|
||||
localStorage.removeItem(k);
|
||||
return cb();
|
||||
};
|
||||
|
||||
LocalStorage.prototype.clear = function(cb) {
|
||||
localStorage.clear();
|
||||
return cb();
|
||||
};
|
||||
|
||||
LocalStorage.prototype.allKeys = function(cb) {
|
||||
var l = localStorage.length;
|
||||
var ret = [];
|
||||
|
||||
for(var i=0; i<l; i++)
|
||||
ret.push(localStorage.key(i));
|
||||
|
||||
return cb(ret);
|
||||
};
|
||||
|
||||
|
||||
module.exports = LocalStorage;
|
||||
|
|
@ -1,27 +1,33 @@
|
|||
//localstorage Mock
|
||||
ls = {};
|
||||
function LocalStorage(opts) {}
|
||||
|
||||
FakeLocalStorage = {};
|
||||
FakeLocalStorage.length = 0;
|
||||
FakeLocalStorage.removeItem = function(key) {
|
||||
delete ls[key];
|
||||
this.length = Object.keys(ls).length;
|
||||
function FakeLocalStorage() {
|
||||
this.ls = {};
|
||||
};
|
||||
FakeLocalStorage.prototype.removeItem = function(key, cb) {
|
||||
delete this.ls[key];
|
||||
cb();
|
||||
};
|
||||
|
||||
FakeLocalStorage.getItem = function(k) {
|
||||
return ls[k];
|
||||
FakeLocalStorage.prototype.getItem = function(k, cb) {
|
||||
return cb(this.ls[k]);
|
||||
};
|
||||
|
||||
|
||||
FakeLocalStorage.key = function(i) {
|
||||
return Object.keys(ls)[i];
|
||||
FakeLocalStorage.prototype.allKeys = function(cb) {
|
||||
return cb(Object.keys(this.ls));
|
||||
};
|
||||
|
||||
FakeLocalStorage.setItem = function(k, v) {
|
||||
ls[k] = v;
|
||||
this.key[this.length] = k;
|
||||
this.length = Object.keys(ls).length;
|
||||
FakeLocalStorage.prototype.setItem = function(k, v, cb) {
|
||||
this.ls[k] = v;
|
||||
return cb();
|
||||
};
|
||||
FakeLocalStorage.prototype.clear = function() {
|
||||
this.ls = {};
|
||||
}
|
||||
|
||||
module.exports = FakeLocalStorage;
|
||||
|
||||
module.exports.storageParams = {
|
||||
storage: new FakeLocalStorage(),
|
||||
sessionStorage: new FakeLocalStorage(),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,131 +0,0 @@
|
|||
var FakeStorage = function() {
|
||||
this.reset();
|
||||
};
|
||||
|
||||
|
||||
FakeStorage.prototype.reset = function(password) {
|
||||
this.storage = {};
|
||||
};
|
||||
|
||||
FakeStorage.prototype._setPassphrase = function(password) {
|
||||
this.storage.passphrase = password;
|
||||
};
|
||||
|
||||
FakeStorage.prototype.setGlobal = function(id, v) {
|
||||
this.storage[id] = typeof v === 'object' ? JSON.stringify(v) : v;
|
||||
};
|
||||
|
||||
FakeStorage.prototype.getGlobal = function(id) {
|
||||
return this.storage[id];
|
||||
};
|
||||
|
||||
FakeStorage.prototype.setLastOpened = function(val) {
|
||||
this.storage['lastOpened'] = val;
|
||||
};
|
||||
|
||||
FakeStorage.prototype.getLastOpened = function() {
|
||||
return this.storage['lastOpened'];
|
||||
};
|
||||
|
||||
FakeStorage.prototype.setLock = function(id) {
|
||||
this.storage[id + '::lock'] = true;
|
||||
}
|
||||
|
||||
FakeStorage.prototype.getLock = function(id) {
|
||||
return this.storage[id + '::lock'];
|
||||
}
|
||||
|
||||
FakeStorage.prototype.getSessionId = function() {
|
||||
return this.sessionId || 'aSessionId';
|
||||
};
|
||||
|
||||
|
||||
FakeStorage.prototype.removeLock = function(id) {
|
||||
delete this.storage[id + '::lock'];
|
||||
}
|
||||
|
||||
FakeStorage.prototype.removeGlobal = function(id) {
|
||||
delete this.storage[id];
|
||||
};
|
||||
|
||||
|
||||
FakeStorage.prototype.set = function(wid, id, payload) {
|
||||
this.storage[wid + '::' + id] = payload;
|
||||
};
|
||||
|
||||
FakeStorage.prototype.get = function(wid, id) {
|
||||
return this.storage[wid + '::' + id];
|
||||
};
|
||||
|
||||
FakeStorage.prototype.clear = function() {
|
||||
delete this['storage'];
|
||||
};
|
||||
|
||||
FakeStorage.prototype.getWalletIds = function() {
|
||||
var walletIds = [];
|
||||
var uniq = {};
|
||||
|
||||
for (var ii in this.storage) {
|
||||
var split = ii.split('::');
|
||||
if (split.length == 2) {
|
||||
var walletId = split[0];
|
||||
|
||||
if (!walletId || walletId === 'nameFor' || walletId ==='lock')
|
||||
continue;
|
||||
|
||||
if (typeof uniq[walletId] === 'undefined') {
|
||||
walletIds.push(walletId);
|
||||
uniq[walletId] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return walletIds;
|
||||
};
|
||||
|
||||
FakeStorage.prototype.deleteWallet = function(walletId) {
|
||||
var toDelete = {};
|
||||
toDelete['nameFor::' + walletId] = 1;
|
||||
|
||||
for (var key in this.storage) {
|
||||
var split = key.split('::');
|
||||
if (split.length == 2 && split[0] === walletId) {
|
||||
toDelete[key] = 1;
|
||||
}
|
||||
}
|
||||
for (var i in toDelete) {
|
||||
this.removeGlobal(i);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
FakeStorage.prototype.getName = function(walletId) {
|
||||
return this.getGlobal('nameFor::' + walletId);
|
||||
};
|
||||
|
||||
|
||||
FakeStorage.prototype.setName = function(walletId, name) {
|
||||
this.setGlobal('nameFor::' + walletId, name);
|
||||
};
|
||||
|
||||
|
||||
FakeStorage.prototype.getWallets = function() {
|
||||
var wallets = [];
|
||||
var ids = this.getWalletIds();
|
||||
|
||||
for (var i in ids) {
|
||||
wallets.push({
|
||||
id: ids[i],
|
||||
name: this.getName(ids[i]),
|
||||
});
|
||||
}
|
||||
return wallets;
|
||||
};
|
||||
|
||||
FakeStorage.prototype.setFromObj = function(walletId, obj) {
|
||||
for (var k in obj) {
|
||||
this.set(walletId, k, obj[k]);
|
||||
}
|
||||
this.setName(walletId, obj.opts.name);
|
||||
};
|
||||
|
||||
module.exports = FakeStorage;
|
||||
|
|
@ -1,249 +0,0 @@
|
|||
'use strict';
|
||||
var chai = chai || require('chai');
|
||||
var should = chai.should();
|
||||
var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined';
|
||||
var copay = copay || require('../copay');
|
||||
var LocalEncrypted = copay.StorageLocalEncrypted;
|
||||
|
||||
var fakeWallet = 'fake-wallet-id';
|
||||
var timeStamp = Date.now();
|
||||
var localMock = require('./mocks/FakeLocalStorage');
|
||||
var sessionMock = require('./mocks/FakeLocalStorage');
|
||||
|
||||
|
||||
describe('Storage/LocalEncrypted model', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
});
|
||||
s._setPassphrase('mysupercoolpassword');
|
||||
|
||||
it('should create an instance', function() {
|
||||
var s2 = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
});
|
||||
should.exist(s2);
|
||||
});
|
||||
it('should fail when encrypting without a password', function() {
|
||||
var s2 = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
});
|
||||
(function() {
|
||||
s2.set(fakeWallet, timeStamp, 1);
|
||||
}).should.throw();
|
||||
});
|
||||
it('should be able to encrypt and decrypt', function() {
|
||||
s._write(fakeWallet + timeStamp, 'value');
|
||||
s._read(fakeWallet + timeStamp).should.equal('value');
|
||||
localMock.removeItem(fakeWallet + timeStamp);
|
||||
});
|
||||
it('should be able to set a value', function() {
|
||||
s.set(fakeWallet, timeStamp, 1);
|
||||
localMock.removeItem(fakeWallet + '::' + timeStamp);
|
||||
});
|
||||
var getSetData = [
|
||||
1, 1000, -15, -1000,
|
||||
0.1, -0.5, -0.5e-10, Math.PI,
|
||||
'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b',
|
||||
'1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], {
|
||||
x: 1,
|
||||
y: 2
|
||||
}, {
|
||||
x: 'hi',
|
||||
y: null
|
||||
}, {
|
||||
a: {},
|
||||
b: [],
|
||||
c: [1, 2, 'hi']
|
||||
},
|
||||
null
|
||||
];
|
||||
getSetData.forEach(function(obj) {
|
||||
it('should be able to set a value and get it for ' + JSON.stringify(obj), function() {
|
||||
s.set(fakeWallet, timeStamp, obj);
|
||||
var obj2 = s.get(fakeWallet, timeStamp);
|
||||
JSON.stringify(obj2).should.equal(JSON.stringify(obj));
|
||||
localMock.removeItem(fakeWallet + '::' + timeStamp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#export', function() {
|
||||
it('should export the encrypted wallet', function() {
|
||||
var storage = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password',
|
||||
});
|
||||
storage.set(fakeWallet, timeStamp, 'testval');
|
||||
var obj = {
|
||||
test: 'testval'
|
||||
};
|
||||
var encrypted = storage.export(obj);
|
||||
encrypted.length.should.be.greaterThan(10);
|
||||
localMock.removeItem(fakeWallet + '::' + timeStamp);
|
||||
//encrypted.slice(0,6).should.equal("53616c");
|
||||
});
|
||||
});
|
||||
|
||||
describe('#remove', function() {
|
||||
it('should remove an item', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
s.set('1', "hola", 'juan');
|
||||
s.get('1', 'hola').should.equal('juan');
|
||||
s.remove('1', 'hola');
|
||||
|
||||
should.not.exist(s.get('1', 'hola'));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#getWalletIds', function() {
|
||||
it('should get wallet ids', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
s.set('1', "hola", 'juan');
|
||||
s.set('2', "hola", 'juan');
|
||||
s.getWalletIds().should.deep.equal(['1', '2']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getName #setName', function() {
|
||||
it('should get/set names', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
s.setName(1, 'hola');
|
||||
s.getName(1).should.equal('hola');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getLastOpened #setLastOpened', function() {
|
||||
it('should get/set names', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
s.setLastOpened('hey');
|
||||
s.getLastOpened().should.equal('hey');
|
||||
});
|
||||
});
|
||||
|
||||
if (is_browser) {
|
||||
describe('#getSessionId', function() {
|
||||
it('should get SessionId', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
var sid = s.getSessionId();
|
||||
should.exist(sid);
|
||||
var sid2 = s.getSessionId();
|
||||
sid2.should.equal(sid);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('#getWallets', function() {
|
||||
it('should retreive wallets from storage', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
s.set('1', "hola", 'juan');
|
||||
s.set('2', "hola", 'juan');
|
||||
s.setName(1, 'hola');
|
||||
s.getWallets()[0].should.deep.equal({
|
||||
id: '1',
|
||||
name: 'hola',
|
||||
});
|
||||
s.getWallets()[1].should.deep.equal({
|
||||
id: '2',
|
||||
name: undefined
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('#deleteWallet', function() {
|
||||
it('should delete a wallet', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
s.set('1', "hola", 'juan');
|
||||
s.set('2', "hola", 'juan');
|
||||
s.setName(1, 'hola');
|
||||
|
||||
s.deleteWallet('1');
|
||||
s.getWallets().length.should.equal(1);
|
||||
s.getWallets()[0].should.deep.equal({
|
||||
id: '2',
|
||||
name: undefined
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#setFromObj', function() {
|
||||
it('set localstorage from an object', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
s.setFromObj('id1', {
|
||||
'key': 'val',
|
||||
'opts': {
|
||||
'name': 'nameid1'
|
||||
},
|
||||
});
|
||||
|
||||
s.get('id1', 'key').should.equal('val');
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#globals', function() {
|
||||
it('should set, get and remove keys', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
s.setGlobal('a', {
|
||||
b: 1
|
||||
});
|
||||
JSON.parse(s.getGlobal('a')).should.deep.equal({
|
||||
b: 1
|
||||
});
|
||||
s.removeGlobal('a');
|
||||
should.not.exist(s.getGlobal('a'));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('session storage', function() {
|
||||
it('should get a session ID', function() {
|
||||
var s = new LocalEncrypted({
|
||||
localStorage: localMock,
|
||||
sessionStorage: sessionMock,
|
||||
password: 'password'
|
||||
});
|
||||
s.getSessionId().length.should.equal(16);
|
||||
(new Buffer(s.getSessionId(),'hex')).length.should.equal(8);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -36,5 +36,4 @@ describe('Passphrase model', function() {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ if (is_browser) {
|
|||
}
|
||||
var Wallet = copay.Wallet;
|
||||
var PrivateKey = copay.PrivateKey;
|
||||
var Storage = require('./mocks/FakeStorage');
|
||||
var Network = require('./mocks/FakeNetwork');
|
||||
var Blockchain = require('./mocks/FakeBlockchain');
|
||||
var bitcore = bitcore || require('bitcore');
|
||||
|
|
@ -21,6 +20,10 @@ var Address = bitcore.Address;
|
|||
var PayPro = bitcore.PayPro;
|
||||
var bignum = bitcore.Bignum;
|
||||
var startServer = copay.FakePayProServer; // TODO should be require('./mocks/FakePayProServer');
|
||||
var localMock = require('./mocks/FakeLocalStorage');
|
||||
var sessionMock = require('./mocks/FakeLocalStorage');
|
||||
var Storage = copay.Storage;
|
||||
|
||||
|
||||
var server;
|
||||
|
||||
|
|
@ -30,6 +33,7 @@ var walletConfig = {
|
|||
spendUnconfirmed: true,
|
||||
reconnectDelay: 100,
|
||||
networkName: 'testnet',
|
||||
storage: require('./mocks/FakeLocalStorage').storageParams,
|
||||
};
|
||||
|
||||
var getNewEpk = function() {
|
||||
|
|
@ -41,6 +45,7 @@ var getNewEpk = function() {
|
|||
};
|
||||
|
||||
describe('PayPro (in Wallet) model', function() {
|
||||
|
||||
if (!is_browser) {
|
||||
var createW = function(N, conf) {
|
||||
var c = JSON.parse(JSON.stringify(conf || walletConfig));
|
||||
|
|
@ -64,6 +69,7 @@ describe('PayPro (in Wallet) model', function() {
|
|||
});
|
||||
|
||||
var storage = new Storage(walletConfig.storage);
|
||||
storage.setPassphrase('xxx');
|
||||
var network = new Network(walletConfig.network);
|
||||
var blockchain = new Blockchain(walletConfig.blockchain);
|
||||
c.storage = storage;
|
||||
|
|
|
|||
321
test/test.Storage.js
Normal file
321
test/test.Storage.js
Normal file
|
|
@ -0,0 +1,321 @@
|
|||
'use strict';
|
||||
var chai = chai || require('chai');
|
||||
var sinon = require('sinon');
|
||||
var should = chai.should();
|
||||
var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined';
|
||||
var copay = copay || require('../copay');
|
||||
var Storage = copay.Storage;
|
||||
|
||||
var fakeWallet = 'fake-wallet-id';
|
||||
var timeStamp = Date.now();
|
||||
|
||||
describe('Storage model', function() {
|
||||
|
||||
var s;
|
||||
beforeEach(function() {
|
||||
s = new Storage(require('./mocks/FakeLocalStorage').storageParams);
|
||||
s.setPassphrase('mysupercoolpassword');
|
||||
s.storage.clear();
|
||||
s.sessionStorage.clear();
|
||||
});
|
||||
|
||||
|
||||
it('should create an instance', function() {
|
||||
var s2 = new Storage(require('./mocks/FakeLocalStorage').storageParams);
|
||||
should.exist(s2);
|
||||
});
|
||||
it('should fail when encrypting without a password', function() {
|
||||
var s2 = new Storage(require('./mocks/FakeLocalStorage').storageParams);
|
||||
(function() {
|
||||
s2.set(fakeWallet, timeStamp, 1, function() {});
|
||||
}).should.throw('NOPASSPHRASE');
|
||||
});
|
||||
it('should be able to encrypt and decrypt', function(done) {
|
||||
s._write(fakeWallet + timeStamp, 'value', function() {
|
||||
s._read(fakeWallet + timeStamp, function(v) {
|
||||
v.should.equal('value');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should be able to set a value', function(done) {
|
||||
s.set(fakeWallet, timeStamp, 1, function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
var getSetData = [
|
||||
1, 1000, -15, -1000,
|
||||
0.1, -0.5, -0.5e-10, Math.PI,
|
||||
'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b',
|
||||
'1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], {
|
||||
x: 1,
|
||||
y: 2
|
||||
}, {
|
||||
x: 'hi',
|
||||
y: null
|
||||
}, {
|
||||
a: {},
|
||||
b: [],
|
||||
c: [1, 2, 'hi']
|
||||
},
|
||||
null
|
||||
];
|
||||
getSetData.forEach(function(obj) {
|
||||
it('should be able to set a value and get it for ' + JSON.stringify(obj), function(done) {
|
||||
s.set(fakeWallet, timeStamp, obj, function() {
|
||||
s.get(fakeWallet, timeStamp, function(obj2) {
|
||||
JSON.stringify(obj2).should.equal(JSON.stringify(obj));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#export', function() {
|
||||
it('should export the encrypted wallet', function(done) {
|
||||
s.set(fakeWallet, timeStamp, 'testval', function() {
|
||||
var obj = {
|
||||
test: 'testval'
|
||||
};
|
||||
var encrypted = s.export(obj);
|
||||
encrypted.length.should.be.greaterThan(10);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#remove', function() {
|
||||
it('should remove an item', function(done) {
|
||||
s.set('1', "hola", 'juan', function() {
|
||||
s.get('1', 'hola', function(v) {
|
||||
v.should.equal('juan');
|
||||
s.remove('1', 'hola', function() {
|
||||
s.get('1', 'hola', function(v) {
|
||||
should.not.exist(v);
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#getWalletIds', function() {
|
||||
it('should get wallet ids', function(done) {
|
||||
s.set('1', "hola", 'juan', function() {
|
||||
s.set('2', "hola", 'juan', function() {
|
||||
s.getWalletIds(function(v) {
|
||||
v.should.deep.equal(['1', '2']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getName #setName', function() {
|
||||
it('should get/set names', function(done) {
|
||||
s.setName(1, 'hola', function() {
|
||||
s.getName(1, function(v) {
|
||||
v.should.equal('hola');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getLastOpened #setLastOpened', function() {
|
||||
it('should get/set last opened', function() {
|
||||
s.setLastOpened('hey', function() {
|
||||
s.getLastOpened(function(v) {
|
||||
v.should.equal('hey');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
if (is_browser) {
|
||||
describe('#getSessionId', function() {
|
||||
it('should get SessionId', function(done) {
|
||||
s.getSessionId(function(sid) {
|
||||
should.exist(sid);
|
||||
s.getSessionId(function(sid2) {
|
||||
sid2.should.equal(sid);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('#getWallets', function() {
|
||||
it('should retreive wallets from storage', function(done) {
|
||||
s.set('1', "hola", 'juan', function() {
|
||||
s.set('2', "hola", 'juan', function() {
|
||||
s.setName(1, 'hola', function() {
|
||||
|
||||
s.getWallets(function(ws) {
|
||||
ws[0].should.deep.equal({
|
||||
id: '1',
|
||||
name: 'hola',
|
||||
});
|
||||
ws[1].should.deep.equal({
|
||||
id: '2',
|
||||
name: undefined
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should retreive wallets from storage (with delay)', function(done) {
|
||||
s.set('1', "hola", 'juan', function() {
|
||||
s.set('2', "hola", 'juan', function() {
|
||||
s.setName(1, 'hola', function() {
|
||||
|
||||
var orig = s.getName.bind(s);
|
||||
s.getName = function(wid, cb) {
|
||||
setTimeout(function() {
|
||||
orig(wid, cb);
|
||||
},1);
|
||||
};
|
||||
|
||||
s.getWallets(function(ws) {
|
||||
ws[0].should.deep.equal({
|
||||
id: '1',
|
||||
name: 'hola',
|
||||
});
|
||||
ws[1].should.deep.equal({
|
||||
id: '2',
|
||||
name: undefined
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#deleteWallet', function() {
|
||||
it('should fail to delete a unexisting wallet', function(done) {
|
||||
s.set('1', "hola", 'juan', function() {
|
||||
s.set('2', "hola", 'juan', function() {
|
||||
s.deleteWallet('3', function(err) {
|
||||
err.toString().should.include('WNOTFOUND');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should delete a wallet', function(done) {
|
||||
s.set('1', "hola", 'juan', function() {
|
||||
s.set('2', "hola", 'juan', function() {
|
||||
s.deleteWallet('1', function(err) {
|
||||
should.not.exist(err);
|
||||
s.getWallets(function(ws) {
|
||||
ws.length.should.equal(1);
|
||||
ws[0].should.deep.equal({
|
||||
id: '2',
|
||||
name: undefined
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#setFromObj', function() {
|
||||
it('set localstorage from an object', function(done) {
|
||||
s.setFromObj('id1', {
|
||||
'key': 'val',
|
||||
'opts': {
|
||||
'name': 'nameid1'
|
||||
},
|
||||
}, function() {
|
||||
s.get('id1', 'key', function(v) {
|
||||
v.should.equal('val');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#globals', function() {
|
||||
it('should set, get and remove keys', function(done) {
|
||||
s.setGlobal('a', {
|
||||
b: 1
|
||||
}, function() {
|
||||
s.getGlobal('a', function(v) {
|
||||
|
||||
JSON.parse(v).should.deep.equal({
|
||||
b: 1
|
||||
});
|
||||
s.removeGlobal('a', function() {
|
||||
s.getGlobal('a', function(v) {
|
||||
should.not.exist(v);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('session storage', function() {
|
||||
it('should get a session ID', function(done) {
|
||||
s.getSessionId(function(s) {
|
||||
should.exist(s);
|
||||
s.length.should.equal(16);
|
||||
(new Buffer(s, 'hex')).length.should.equal(8);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#import', function() {
|
||||
it('should not be able to decrypt with wrong password', function() {
|
||||
s.setPassphrase('xxx');
|
||||
var wo = s.import(encryptedLegacy1);
|
||||
should.not.exist(wo);
|
||||
});
|
||||
|
||||
it('should be able to decrypt an old backup', function() {
|
||||
s.setPassphrase(legacyPassword1);
|
||||
var wo = s.import(encryptedLegacy1);
|
||||
should.exist(wo);
|
||||
wo.opts.id.should.equal('48ba2f1ffdfe9708');
|
||||
wo.opts.spendUnconfirmed.should.equal(true);
|
||||
wo.opts.requiredCopayers.should.equal(1);
|
||||
wo.opts.totalCopayers.should.equal(1);
|
||||
wo.opts.name.should.equal('pepe wallet');
|
||||
wo.opts.version.should.equal('0.4.7');
|
||||
wo.publicKeyRing.walletId.should.equal('48ba2f1ffdfe9708');
|
||||
wo.publicKeyRing.networkName.should.equal('testnet');
|
||||
wo.publicKeyRing.requiredCopayers.should.equal(1);
|
||||
wo.publicKeyRing.totalCopayers.should.equal(1);
|
||||
wo.publicKeyRing.indexes.length.should.equal(2);
|
||||
JSON.stringify(wo.publicKeyRing.indexes[0]).should.equal('{"copayerIndex":2147483647,"changeIndex":0,"receiveIndex":1}');
|
||||
JSON.stringify(wo.publicKeyRing.indexes[1]).should.equal('{"copayerIndex":0,"changeIndex":0,"receiveIndex":1}');
|
||||
wo.publicKeyRing.copayersBackup.length.should.equal(1);
|
||||
wo.publicKeyRing.copayersBackup[0].should.equal('0298f65b2694c55f9048bc05f10368242727c7f9d2065cbd788c3ecde1ec57f33f');
|
||||
wo.publicKeyRing.copayersExtPubKeys.length.should.equal(1);
|
||||
wo.publicKeyRing.copayersExtPubKeys[0].should.equal('tpubD9SGoP7CXsqSKTiQxCZSCpicDcophqnE4yuqjfw5M9tAR3fSjT9GDGwPEUFCN7SSmRKGDLZgKQePYFaLWyK32akeSan45TNTd8sgef9Ymh6');
|
||||
wo.privateKey.extendedPrivateKeyString.should.equal('tprv8ZgxMBicQKsPfQCscb7CtJKzixxcVSyrCVcfr3WCFbtT8kYTzNubhjQ5R7AuYJgPCcSH4R8T34YVxeohKGhAB9wbB4eFBbQFjUpjGCqptHm');
|
||||
wo.privateKey.networkName.should.equal('testnet');
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
var legacyPassword1 = '1DUpLRbuVpgLkcEY8gY8iod/SmA7+OheGZJ9PtvmTlvNE0FkEWpCKW9STdzXYJqbn0wiAapE4ojHNYj2hjYYAQ==';
|
||||
var encryptedLegacy1 = 'U2FsdGVkX19yGM1uBAIzQa8Po/dvUicmxt1YyRk/S97PcZ6I6rHMp9dMagIrehg4Qd6JHn/ustmFHS7vmBYj0EBpf6rdXiQezaWnVAJS9/xYjAO36EFUbl+NmUanuwujAxgYdSP/sNssRLeInvExmZYW993EEclxkwL6YUyX66kKsxGQo2oWng0NreBJNhFmrbOEWeFje2PiWP57oUjKsurFzwpluAAarUTYSLud+nXeabC7opzOP5yqniWBMJz0Ou8gpNCWCMhG/P9F9ccVPY7juyd0Hf41FVse8nd2++axKB57+paozLdO+HRfV6zkMqC3h8gWY7LkS75j3bvqcTw9LhXmzE0Sz21n9yDnRpA4chiAvtwQvvBGgj1pFMKhNQU6Obac9ZwKYzUTgdDn3Uzg1UlDzgyOh9S89rbRTV84WB+hXwhuVluWzbNNYV3vXe5PFrocVktIrtS3xQh+k/7my4A6/gRRrzNYpKrUASJqDS/9u9WBkG35xD63J/qXjtG2M0YPwbI57BK1IK4K510b8V72lz5U2XQrIC4ldBwni1rpSavwCJV9xF6hUdOmNV8fZsVHP0NeN1PYlLkSb2QgfuoWnkcsJerwuFR7GZC/i6efrswtpO0wMEQr/J0CLbeXlHAru6xxjCBhWoJvZpMGw72zgnDLoyMNsEVglNhx/VlV9ZMYkkdaEYAxPOEIyZdQ5MS+2jEAlXf818n/xzJSVrniCn9be8EPePvkw35pivprvy09vbW4cKsWBKvgIyoT6A3OhUOCCS8E9cg0WAjjav2EymrbKmGWRHaiD+EoJqaDg6s20zhHn1YEa/YwvGGSB5+Hg8baLHD8ZASvxz4cFFAAVZrBUedRFgHzqwaMUlFXLgueivWUj7RXlIw6GuNhLoo1QkhZMacf23hrFxxQYvGBRw1hekBuDmcsGWljA28udBxBd5f9i+3gErttMLJ6IPaud590uvrxRIclu0Sz9R2EQX64YJxqDtLpMY0PjddSMu8vaDRpK9/ZSrnz/xrXsyabaafz4rE/ItFXjwFUFkvtmuauHTz6nmuKjVfxvNLNAiKb/gI7vQyUhnTbKIApe7XyJsjedNDtZqsPoJRIzdDmrZYxGStbAZ7HThqFJlSJ9NPNhH+E2jm3TwL5mwt0fFZ5h+p497lHMtIcKffESo7KNa2juSVNMDREk0NcyxGXGiVB2FWl4sLdvyhcsVq0I7tmW6OGZKRf8W49GCJXq6Ie69DJ9LB1DO67NV1jsYbsLx9uhE2yEmpWZ3jkoCV/Eas4grxt0CGN6EavzQ==';
|
||||
|
|
@ -13,7 +13,7 @@ if (is_browser) {
|
|||
var copayConfig = require('../config');
|
||||
var Wallet = copay.Wallet;
|
||||
var PrivateKey = copay.PrivateKey;
|
||||
var Storage = require('./mocks/FakeStorage');
|
||||
var Storage = copay.Storage;
|
||||
var Network = require('./mocks/FakeNetwork');
|
||||
var Blockchain = require('./mocks/FakeBlockchain');
|
||||
var Builder = require('./mocks/FakeBuilder');
|
||||
|
|
@ -28,6 +28,7 @@ var walletConfig = {
|
|||
spendUnconfirmed: true,
|
||||
reconnectDelay: 100,
|
||||
networkName: 'testnet',
|
||||
storage: require('./mocks/FakeLocalStorage').storageParams,
|
||||
};
|
||||
|
||||
var getNewEpk = function() {
|
||||
|
|
@ -81,6 +82,7 @@ describe('Wallet model', function() {
|
|||
});
|
||||
|
||||
var storage = new Storage(walletConfig.storage);
|
||||
storage.setPassphrase('xxx');
|
||||
var network = new Network(walletConfig.network);
|
||||
var blockchain = new Blockchain(walletConfig.blockchain);
|
||||
c.storage = storage;
|
||||
|
|
@ -341,8 +343,10 @@ describe('Wallet model', function() {
|
|||
// non stored options
|
||||
o.opts.reconnectDelay = 100;
|
||||
|
||||
var s = new Storage(walletConfig.storage);
|
||||
s.setPassphrase('xxx');
|
||||
var w2 = Wallet.fromObj(o,
|
||||
new Storage(walletConfig.storage),
|
||||
s,
|
||||
new Network(walletConfig.network),
|
||||
new Blockchain(walletConfig.blockchain));
|
||||
should.exist(w2);
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -11,12 +11,20 @@ if (is_browser) {
|
|||
}
|
||||
var copayConfig = require('../config');
|
||||
var WalletLock = copay.WalletLock;
|
||||
|
||||
var PrivateKey = copay.PrivateKey;
|
||||
var Storage = require('./mocks/FakeStorage');
|
||||
var Storage = copay.Storage;
|
||||
|
||||
|
||||
|
||||
var storage;
|
||||
describe('WalletLock model', function() {
|
||||
var storage = new Storage();
|
||||
|
||||
beforeEach(function() {
|
||||
storage = new Storage(require('./mocks/FakeLocalStorage').storageParams);
|
||||
storage.setPassphrase('mysupercoolpassword');
|
||||
storage.storage.clear();
|
||||
storage.sessionStorage.clear();
|
||||
});
|
||||
|
||||
it('should fail with missing args', function() {
|
||||
(function() {
|
||||
|
|
@ -36,45 +44,68 @@ describe('WalletLock model', function() {
|
|||
should.exist(w);
|
||||
});
|
||||
|
||||
it('should NOT fail if locked already', function() {
|
||||
|
||||
it('should generate a sessionId with init', function(done) {
|
||||
var w = new WalletLock(storage, 'id');
|
||||
var spy = sinon.spy(storage, 'getSessionId');
|
||||
w.init(function() {
|
||||
spy.calledOnce.should.equal(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('#keepAlive should call getsessionId if not called before', function(done) {
|
||||
var w = new WalletLock(storage, 'id');
|
||||
var spy = sinon.spy(storage, 'getSessionId');
|
||||
w.keepAlive(function() {
|
||||
spy.calledOnce.should.equal(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should NOT fail if locked already by me', function(done) {
|
||||
var w = new WalletLock(storage, 'walletId2');
|
||||
w.keepAlive(function() {
|
||||
var w2 = new WalletLock(storage, 'walletId2');
|
||||
w2.init(function() {
|
||||
w2.keepAlive(function() {
|
||||
w.sessionId.should.equal(w2.sessionId);
|
||||
should.exist(w2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
it('should FAIL if locked by someone else', function(done) {
|
||||
var w = new WalletLock(storage, 'walletId');
|
||||
storage.sessionId = 'xxx';
|
||||
var w2= new WalletLock(storage, 'walletId');
|
||||
should.exist(w2);
|
||||
});
|
||||
w.keepAlive(function() {
|
||||
storage.setSessionId('session2', function() {
|
||||
var w2 = new WalletLock(storage, 'walletId');
|
||||
w2.keepAlive(function(locked) {
|
||||
should.exist(locked);
|
||||
locked.message.should.contain('LOCKED');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
it('should change status of previously openned wallet', function() {
|
||||
storage.sessionId = 'session1';
|
||||
it('should FAIL if locked by someone else but expired', function(done) {
|
||||
var w = new WalletLock(storage, 'walletId');
|
||||
storage.sessionId = 'xxx';
|
||||
var w2= new WalletLock(storage, 'walletId');
|
||||
w2.keepAlive();
|
||||
(function() {w.keepAlive();}).should.throw('already open');
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('should not fail if locked by me', function() {
|
||||
var s = new Storage();
|
||||
var w = new WalletLock(s, 'walletId');
|
||||
var w2 = new WalletLock(s, 'walletId')
|
||||
w2.keepAlive();
|
||||
should.exist(w2);
|
||||
});
|
||||
|
||||
it('should not fail if expired', function() {
|
||||
var s = new Storage();
|
||||
var w = new WalletLock(s, 'walletId');
|
||||
var k = Object.keys(s.storage)[0];
|
||||
var v = JSON.parse(s.storage[k]);
|
||||
v.expireTs = Date.now() - 60 * 6 * 1000;
|
||||
s.storage[k] = JSON.stringify(v);
|
||||
|
||||
s.sessionId = 'xxx';
|
||||
var w2 = new WalletLock(s, 'walletId')
|
||||
should.exist(w2);
|
||||
});
|
||||
|
||||
|
||||
w.keepAlive(function() {
|
||||
storage.setSessionId('session2', function() {
|
||||
|
||||
var json = JSON.parse(storage.storage.ls['lock::walletId']);
|
||||
json.expireTs -= 3600 * 1000;
|
||||
storage.storage.ls['lock::walletId'] = JSON.stringify(json);
|
||||
var w2 = new WalletLock(storage, 'walletId');
|
||||
w2.keepAlive(function(locked) {
|
||||
w2.sessionId.should.equal('session2');
|
||||
should.not.exist(locked);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
});
|
||||
|
|
|
|||
|
|
@ -15,16 +15,19 @@ saveAs = function(blob, filename) {
|
|||
var startServer = require('../../mocks/FakePayProServer');
|
||||
|
||||
describe("Unit: Controllers", function() {
|
||||
config.plugins.LocalStorage=true;
|
||||
config.plugins.GoogleDrive=null;
|
||||
|
||||
var invalidForm = {
|
||||
$invalid: true
|
||||
};
|
||||
|
||||
var scope;
|
||||
|
||||
var server;
|
||||
|
||||
beforeEach(module('copayApp.services'));
|
||||
beforeEach(module('copayApp.controllers'));
|
||||
beforeEach(angular.mock.module('copayApp'));
|
||||
|
||||
var walletConfig = {
|
||||
requiredCopayers: 3,
|
||||
|
|
@ -36,6 +39,7 @@ describe("Unit: Controllers", function() {
|
|||
alternativeIsoCode: 'LOL'
|
||||
};
|
||||
|
||||
|
||||
describe('More Controller', function() {
|
||||
var ctrl;
|
||||
beforeEach(inject(function($controller, $rootScope) {
|
||||
|
|
@ -72,11 +76,6 @@ describe("Unit: Controllers", function() {
|
|||
expect(saveAsLastCall.filename).equal('myTESTwullet-testID-keybackup.json.aes');
|
||||
});
|
||||
|
||||
it('Backup controller #delete', function() {
|
||||
expect(scope.wallet).not.equal(undefined);
|
||||
scope.deleteWallet();
|
||||
expect(scope.wallet).equal(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Create Controller', function() {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
var sinon = require('sinon');
|
||||
var preconditions = require('preconditions').singleton();
|
||||
|
||||
beforeEach(angular.mock.module('copayApp'));
|
||||
|
||||
describe("Unit: Walletfactory Service", function() {
|
||||
beforeEach(angular.mock.module('copayApp.services'));
|
||||
it('should contain a walletFactory service', inject(function(walletFactory) {
|
||||
|
|
|
|||
|
|
@ -83,6 +83,19 @@ var createBundle = function(opts) {
|
|||
b.require('./js/models/core/HDPath', {
|
||||
expose: '../js/models/core/HDPath'
|
||||
});
|
||||
b.require('./js/models/core/PluginManager', {
|
||||
expose: '../js/models/core/PluginManager'
|
||||
});
|
||||
|
||||
if (!opts.disablePlugins) {
|
||||
b.require('./plugins/GoogleDrive', {
|
||||
expose: '../plugins/GoogleDrive'
|
||||
});
|
||||
b.require('./plugins/LocalStorage', {
|
||||
expose: '../plugins/LocalStorage'
|
||||
});
|
||||
}
|
||||
|
||||
b.require('./config', {
|
||||
expose: '../config'
|
||||
});
|
||||
|
|
@ -91,9 +104,6 @@ var createBundle = function(opts) {
|
|||
//include dev dependencies
|
||||
b.require('sinon');
|
||||
b.require('blanket');
|
||||
b.require('./test/mocks/FakeStorage', {
|
||||
expose: './mocks/FakeStorage'
|
||||
});
|
||||
b.require('./test/mocks/FakeLocalStorage', {
|
||||
expose: './mocks/FakeLocalStorage'
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
<div class="home" ng-controller="HomeController">
|
||||
<div class="row">
|
||||
<div data-alert class="loading-screen" ng-show="retreiving">
|
||||
<i class="size-60 fi-bitcoin-circle icon-rotate spinner"></i>
|
||||
Retreiving information from storage...
|
||||
</div>
|
||||
<div class="row" ng-show="!loading && !retreiving">
|
||||
<div class="large-4 columns logo-setup">
|
||||
<img src="img/logo-negative-beta.svg" alt="Copay" width="146" height="59">
|
||||
<div ng-include="'views/includes/version.html'"></div>
|
||||
|
|
|
|||
|
|
@ -1,9 +1,13 @@
|
|||
<div class="open" ng-controller="OpenController">
|
||||
<div data-alert class="loading-screen" ng-show="loading && !failure">
|
||||
<div data-alert class="loading-screen" ng-show="retreiving">
|
||||
<i class="size-60 fi-bitcoin-circle icon-rotate spinner"></i>
|
||||
Retreiving information from storage...
|
||||
</div>
|
||||
<div data-alert class="loading-screen" ng-show="loading && !failure && !retreiving">
|
||||
<i class="size-60 fi-bitcoin-circle icon-rotate spinner"></i>
|
||||
<span translate>Connecting...</span>
|
||||
</div>
|
||||
<div class="row" ng-show="!loading">
|
||||
<div class="row" ng-show="!loading && !retreiving">
|
||||
<div class="large-4 columns logo-setup">
|
||||
<img src="img/logo-negative-beta.svg" alt="Copay" width="146" height="59">
|
||||
<div ng-include="'views/includes/version.html'"></div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue