copay/js/controllers/importProfile.js

69 lines
2.0 KiB
JavaScript
Raw Normal View History

2014-10-31 15:39:01 -07:00
'use strict';
angular.module('copayApp.controllers').controller('ImportProfileController',
2014-11-29 22:23:15 -08:00
function($scope, $rootScope, $location, notification, isMobile, identityService) {
2014-10-31 15:39:01 -07:00
$scope.title = 'Import a backup';
$scope.importStatus = 'Importing wallet - Reading backup...';
$scope.hideAdv = true;
$scope.is_iOS = isMobile.iOS();
var reader = new FileReader();
var updateStatus = function(status) {
$scope.importStatus = status;
$scope.$digest();
}
var _importBackup = function(str) {
var password = $scope.password;
updateStatus('Importing profile - Setting things up...');
2014-11-29 23:42:39 -08:00
identityService.importProfile(str,password, function(err, iden) {
$scope.loading = false;
2014-11-18 07:12:09 -08:00
if (err) {
2014-11-29 23:42:39 -08:00
copay.logger.warn(err);
2014-11-18 07:12:09 -08:00
if ((err.toString() || '').match('BADSTR')) {
$scope.error = 'Bad password or corrupt profile file';
2014-11-18 07:12:09 -08:00
} else if ((err.toString() || '').match('EEXISTS')) {
$scope.error = 'Profile already exists';
} else {
$scope.error = 'Unknown error';
}
2014-11-29 23:42:39 -08:00
}
2014-10-31 15:39:01 -07:00
});
};
$scope.getFile = function() {
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
var encryptedObj = evt.target.result;
_importBackup(encryptedObj);
}
};
};
$scope.import = function(form) {
if (form.$invalid) {
2014-11-11 08:58:48 -08:00
$scope.error = 'Please enter the required fields';
2014-10-31 15:39:01 -07:00
return;
}
var backupFile = $scope.file;
var backupText = form.backupText.$modelValue;
var password = form.password.$modelValue;
if (!backupFile && !backupText) {
2014-11-11 08:58:48 -08:00
$scope.error = 'Please, select your backup file';
2014-10-31 15:39:01 -07:00
return;
}
2014-11-29 22:23:15 -08:00
$scope.loading = true;
2014-10-31 15:39:01 -07:00
if (backupFile) {
reader.readAsBinaryString(backupFile);
} else {
_importBackup(backupText);
}
};
});