Merge pull request #1609 from cmgustavo/feature/import-from-localstorage

Import Old wallets from localStorage
This commit is contained in:
Matias Alejo Garcia 2014-10-30 16:54:53 -03:00
commit fcb2401fff
13 changed files with 67 additions and 81 deletions

View file

@ -2,9 +2,7 @@
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager, identityService) {
controllerUtils.redirIfLogged();
$scope.retreiving = true;
identityService.check($scope);
$scope.retreiving = false;
$scope.createProfile = function(form) {
if (form && form.$invalid) {

View file

@ -1,11 +1,11 @@
'use strict';
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager, identityService) {
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager, identityService, Compatibility) {
controllerUtils.redirIfLogged();
$scope.retreiving = true;
identityService.check($scope);
$scope.confirmedEmail = getParam('confirmed');
$scope.retreiving = false;
Compatibility.check($scope);
$scope.openProfile = function(form) {
if (form && form.$invalid) {
notification.error('Error', 'Please enter the required fields');

View file

@ -8,6 +8,8 @@ angular.module('copayApp.controllers').controller('ImportController',
$scope.hideAdv = true;
$scope.is_iOS = isMobile.iOS();
Compatibility.check($scope);
var reader = new FileReader();
var updateStatus = function(status) {
@ -65,7 +67,7 @@ angular.module('copayApp.controllers').controller('ImportController',
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
var encryptedObj = evt.target.result;
Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj, $scope.password, {},
copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj, $scope.password, {},
function(err, wallet){
if (err) {
notification.error('Error', 'Could not read wallet. Please check your password');
@ -91,8 +93,13 @@ angular.module('copayApp.controllers').controller('ImportController',
var backupFile = $scope.file;
var backupText = form.backupText.$modelValue;
var backupOldWallet = form.backupOldWallet.$modelValue;
var password = form.password.$modelValue;
if (backupOldWallet) {
backupText = backupOldWallet.value;
}
if (!backupFile && !backupText) {
$scope.loading = false;
notification.error('Error', 'Please, select your backup file');
@ -104,11 +111,12 @@ angular.module('copayApp.controllers').controller('ImportController',
reader.readAsBinaryString(backupFile);
}
else {
Compatibility.importEncryptedWallet($rootScope.iden, backupText, $scope.password, {},
copay.Compatibility.importEncryptedWallet($rootScope.iden, backupText, $scope.password, {},
function(err, wallet){
if (err) {
notification.error('Error', 'Could not read wallet. Please check your password');
} else {
copay.Compatibility.deleteOldWallet(backupOldWallet);
controllerUtils.installWalletHandlers($scope, wallet);
controllerUtils.setFocusedWallet(wallet);
return;
@ -118,7 +126,7 @@ angular.module('copayApp.controllers').controller('ImportController',
try {
_importBackup(backupText);
} catch(e) {
Compatibility.importEncryptedWallet(backupText, $scope.password, $scope.skipPublicKeyRing, $scope.skipTxProposals);
copay.Compatibility.importEncryptedWallet(backupText, $scope.password, $scope.skipPublicKeyRing, $scope.skipTxProposals);
}
}
};

View file

@ -20,6 +20,7 @@ Compatibility._getWalletIds = function(cb) {
preconditions.checkArgument(cb);
var walletIds = [];
var uniq = {};
var key;
for (key in localStorage) {
var split = key.split('::');
if (split.length == 2) {
@ -111,7 +112,9 @@ Compatibility.getWallets_Old = function(cb) {
Compatibility.getWallets2 = function(cb) {
var self = this;
var re = /wallet::([^_]+)(_?(.*))/;
var va = /^{+/;
var key;
var keys = [];
for (key in localStorage) {
keys.push(key);
@ -120,11 +123,15 @@ Compatibility.getWallets2 = function(cb) {
if (key.indexOf('wallet::') !== 0)
return null;
var match = key.match(re);
var matchValue = localStorage[key].match(va);
if (match.length != 4)
return null;
if (matchValue)
return null;
return {
id: match[1],
name: match[3] ? match[3] : undefined,
value: localStorage[key]
};
}));
@ -186,7 +193,7 @@ Compatibility.readWalletPre8 = function(walletId, password, cb) {
};
Compatibility.importEncryptedWallet = function(identity, cypherText, password, opts, cb) {
var crypto = opts.cryptoUtil || cryptoUtils;
var crypto = (opts && opts.cryptoUtil) || cryptoUtils;
var key = crypto.kdf(password);
var obj = crypto.decrypt(key, cypherText);
if (!obj) {
@ -227,5 +234,10 @@ Compatibility.kdf = function(password) {
return sbase64;
};
Compatibility.deleteOldWallet = function(walletObj) {
localStorage.removeItem('wallet::'+walletObj.id+'_'+walletObj.name);
log.info('Old wallet ' + walletObj.name + ' deleted: ' + walletObj.id);
};
module.exports = Compatibility;

View file

@ -490,20 +490,6 @@ Identity.prototype.addWallet = function(wallet, cb) {
this.storage.setItem(wallet.getStorageKey(), wallet.toObj(), cb);
};
/**
* check if any profile exists on storage
* @param opts.storageOpts
* @param cb
*/
Identity.checkIfExistsAny = function(opts, cb) {
var storage = opts.storage || opts.pluginManager.get('DB');
storage.getFirst(Identity.getStoragePrefix(), {
onlyKey: true
}, function(err, v, k) {
return cb(k ? true : false);
});
};
/**
* @desc Checks if a version is compatible with the current version
* @param {string} inVersion - a version, with major, minor, and revision, period-separated (x.y.z)

View file

@ -178,20 +178,6 @@ Wallet.prototype.getStorageKey = function() {
return Wallet.getStorageKey(this.getId());
};
/**
* check if any wallet exists on storage
* @param opts.storageOpts
* @param cb
*/
Wallet.checkIfExistsAny = function(opts, cb) {
var storage = opts.storage || opts.pluginManager.get('DB');
storage.getFirst(Wallet.getStoragePrefix(), {
onlyKey: true
}, function(err, v, k) {
return cb(k ? true : false);
});
};
/* for stubbing */
Wallet._newInsight = function(opts) {
return new Insight(opts);

View file

@ -1,5 +1,13 @@
'use strict';
angular.module('copayApp.services').factory('Compatibility', function() {
return require('copay').Compatibility;
angular.module('copayApp.services').factory('Compatibility', function($rootScope) {
var root = {};
root.check = function (scope) {
copay.Compatibility.listWalletsPre8(function(wallets) {
scope.anyWallet = wallets.length > 0 ? true : false;
scope.oldWallets = wallets;
});
};
return root;
});

View file

@ -4,24 +4,6 @@ angular.module('copayApp.services')
.factory('identityService', function($rootScope, $location, pluginManager, controllerUtils) {
var root = {};
root.check = function (scope) {
copay.Identity.checkIfExistsAny({
pluginManager: pluginManager,
}, function(anyProfile) {
copay.Wallet.checkIfExistsAny({
pluginManager: pluginManager,
}, function(anyWallet) {
scope.retreiving = false;
scope.anyProfile = anyProfile ? true : false;
scope.anyWallet = anyWallet ? true : false;
if (!scope.anyProfile) {
$location.path('/createProfile');
}
});
});
};
root.create = function(scope, form) {
var iden = copay.Identity.create({
email: form.email.$modelValue,