add error on incorrect password while importing

This commit is contained in:
Manuel Araoz 2014-06-02 16:41:57 -03:00
commit 6feb2a2176
3 changed files with 49 additions and 44 deletions

View file

@ -6,7 +6,15 @@ angular.module('copay.import').controller('ImportController',
var reader = new FileReader(); var reader = new FileReader();
var _importBackup = function(encryptedObj) { var _importBackup = function(encryptedObj) {
Passphrase.getBase64Async($scope.password, function(passphrase){ Passphrase.getBase64Async($scope.password, function(passphrase){
$rootScope.wallet = walletFactory.fromEncryptedObj(encryptedObj, passphrase); var w = walletFactory.fromEncryptedObj(encryptedObj, passphrase);
if (!w) {
$scope.loading = false;
$rootScope.$flashMessage = { message: 'Wrong password', type: 'error'};
$rootScope.$digest();
return;
}
$rootScope.wallet = w;
controllerUtils.startNetwork($rootScope.wallet); controllerUtils.startNetwork($rootScope.wallet);
}); });
}; };
@ -23,6 +31,7 @@ angular.module('copay.import').controller('ImportController',
$scope.import = function(form) { $scope.import = function(form) {
if (form.$invalid) { if (form.$invalid) {
$scope.loading = false;
$rootScope.$flashMessage = { message: 'There is an error in the form. Please, try again', type: 'error'}; $rootScope.$flashMessage = { message: 'There is an error in the form. Please, try again', type: 'error'};
return; return;
} }
@ -32,6 +41,7 @@ angular.module('copay.import').controller('ImportController',
var password = form.password.$modelValue; var password = form.password.$modelValue;
if (!backupFile && !backupText) { if (!backupFile && !backupText) {
$scope.loading = false;
$rootScope.$flashMessage = { message: 'Please, select your backup file or paste the text', type: 'error'}; $rootScope.$flashMessage = { message: 'Please, select your backup file or paste the text', type: 'error'};
return; return;
} }

View file

@ -39,14 +39,11 @@ WalletFactory.prototype.log = function(){
WalletFactory.prototype._checkRead = function(walletId) { WalletFactory.prototype._checkRead = function(walletId) {
var s = this.storage; var s = this.storage;
var ret = var ret =
(
s.get(walletId, 'publicKeyRing') && s.get(walletId, 'publicKeyRing') &&
s.get(walletId, 'txProposals') && s.get(walletId, 'txProposals') &&
s.get(walletId, 'opts') && s.get(walletId, 'opts') &&
s.get(walletId, 'privateKey') s.get(walletId, 'privateKey');
)?true:false; return ret;
;
return ret?true:false;
}; };
WalletFactory.prototype.fromObj = function(obj) { WalletFactory.prototype.fromObj = function(obj) {
@ -60,14 +57,15 @@ WalletFactory.prototype.fromObj = function(obj) {
WalletFactory.prototype.fromEncryptedObj = function(base64, password) { WalletFactory.prototype.fromEncryptedObj = function(base64, password) {
this.storage._setPassphrase(password); this.storage._setPassphrase(password);
var walletObj = this.storage.import(base64); var walletObj = this.storage.import(base64);
if (!walletObj) return null;
var w = this.fromObj(walletObj); var w = this.fromObj(walletObj);
w.store(); if (!w) return null;
return w; return w;
}; };
WalletFactory.prototype.read = function(walletId) { WalletFactory.prototype.read = function(walletId) {
if (! this._checkRead(walletId)) if (! this._checkRead(walletId))
return false; return null;
var obj = {}; var obj = {};
var s = this.storage; var s = this.storage;
@ -149,7 +147,6 @@ WalletFactory.prototype.open = function(walletId, opts) {
var w = this.read(walletId); var w = this.read(walletId);
if (w) { if (w) {
this._checkVersion(w.version); this._checkVersion(w.version);
w.store(); w.store();

View file

@ -3,6 +3,7 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var id = 0; var id = 0;
function Storage(opts) { function Storage(opts) {
opts = opts || {}; opts = opts || {};
@ -34,11 +35,15 @@ Storage.prototype._encryptObj = function(obj) {
Storage.prototype._decrypt = function(base64) { Storage.prototype._decrypt = function(base64) {
var decryptedStr = null; var decryptedStr = null;
try {
var decrypted = CryptoJS.AES.decrypt(base64, this._getPassphrase()); var decrypted = CryptoJS.AES.decrypt(base64, this._getPassphrase());
if (decrypted) if (decrypted)
decryptedStr = decrypted.toString(CryptoJS.enc.Utf8); decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
} catch (e) {
console.log('Error while decrypting ' + base64);
return null;
}
return decryptedStr; return decryptedStr;
}; };
@ -49,18 +54,12 @@ Storage.prototype._decryptObj = function(base64) {
Storage.prototype._read = function(k) { Storage.prototype._read = function(k) {
var ret; var ret;
try {
ret = localStorage.getItem(k); ret = localStorage.getItem(k);
if (ret){ if (!ret) return null;
ret = this._decrypt(ret); ret = this._decrypt(ret);
if (!ret) return null;
ret = ret.toString(CryptoJS.enc.Utf8); ret = ret.toString(CryptoJS.enc.Utf8);
ret = JSON.parse(ret); ret = JSON.parse(ret);
}
} catch (e) {
console.log('Error while decrypting: '+e);
return null;
};
return ret; return ret;
}; };
@ -93,7 +92,6 @@ Storage.prototype._key = function(walletId, k) {
// get value by key // get value by key
Storage.prototype.get = function(walletId, k) { Storage.prototype.get = function(walletId, k) {
var ret = this._read(this._key(walletId, k)); var ret = this._read(this._key(walletId, k));
return ret; return ret;
}; };