Wallet/js/controllers/import.js

83 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-04-25 17:34:24 -03:00
'use strict';
2014-06-03 17:42:36 -03:00
angular.module('copayApp.controllers').controller('ImportController',
2014-05-01 18:33:36 -03:00
function($scope, $rootScope, walletFactory, controllerUtils, Passphrase) {
2014-04-25 17:34:24 -03:00
$scope.title = 'Import a backup';
2014-05-01 19:39:01 -03:00
var reader = new FileReader();
2014-05-01 19:08:35 -03:00
var _importBackup = function(encryptedObj) {
2014-05-07 18:48:56 -03:00
Passphrase.getBase64Async($scope.password, function(passphrase){
2014-06-04 16:15:03 -03:00
var w, errMsg;
try {
w = walletFactory.fromEncryptedObj(encryptedObj, passphrase);
} catch(e) {
errMsg = e.message;
}
if (!w) {
$scope.loading = false;
2014-06-04 16:15:03 -03:00
$rootScope.$flashMessage = { message: errMsg || 'Wrong password', type: 'error'};
$rootScope.$digest();
return;
}
$rootScope.wallet = w;
2014-05-07 18:48:56 -03:00
controllerUtils.startNetwork($rootScope.wallet);
$rootScope.wallet.on('connectionError', function() {
var message = "Looks like you are already connected to this wallet, please logout from it and try importing it again.";
$rootScope.$flashMessage = { message: message, type: 'error'};
});
$rootScope.wallet.on('serverError', function() {
$rootScope.$flashMessage = { message: 'The PeerJS server is not responding, please try again', type: 'error'};
controllerUtils.onErrorDigest();
});
2014-05-07 18:48:56 -03:00
});
2014-05-01 19:08:35 -03:00
};
2014-06-03 15:48:27 -04:00
$scope.openFileDialog = function() {
if (window.cshell) {
return cshell.send('backup:import');
}
$scope.choosefile = !$scope.choosefile;
};
$scope.openPasteArea = function() {
$scope.pastetext = !$scope.pastetext;
};
2014-04-25 19:12:13 -03: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
2014-05-01 18:33:36 -03:00
var encryptedObj = evt.target.result;
2014-05-01 19:08:35 -03:00
_importBackup(encryptedObj);
2014-04-25 19:12:13 -03:00
}
};
2014-05-01 19:08:35 -03:00
};
2014-04-25 19:12:13 -03:00
2014-05-05 13:16:56 -03:00
$scope.import = function(form) {
if (form.$invalid) {
2014-06-02 18:04:13 -03:00
$scope.loading = false;
2014-05-20 08:30:20 -07:00
$rootScope.$flashMessage = { message: 'There is an error in the form. Please, try again', type: 'error'};
2014-05-05 13:16:56 -03:00
return;
2014-05-01 19:08:35 -03:00
}
2014-05-05 13:16:56 -03:00
var backupFile = $scope.file;
var backupText = form.backupText.$modelValue;
var password = form.password.$modelValue;
if (!backupFile && !backupText) {
$scope.loading = false;
2014-05-20 08:30:20 -07:00
$rootScope.$flashMessage = { message: 'Please, select your backup file or paste the text', type: 'error'};
2014-06-02 18:04:13 -03:00
$scope.loading = false;
2014-05-05 13:16:56 -03:00
return;
}
$scope.loading = true;
2014-06-03 15:48:27 -04:00
2014-05-05 13:16:56 -03:00
if (backupFile) {
reader.readAsBinaryString(backupFile);
}
else {
_importBackup(backupText);
}
2014-04-25 19:12:13 -03:00
};
2014-04-25 17:34:24 -03:00
});