copay/js/controllers/import.js

100 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-04-25 13:34:24 -07:00
'use strict';
2014-06-03 13:42:36 -07:00
angular.module('copayApp.controllers').controller('ImportController',
2014-06-26 14:15:27 -07:00
function($scope, $rootScope, walletFactory, controllerUtils, Passphrase, notification) {
2014-04-25 13:34:24 -07:00
$scope.title = 'Import a backup';
2014-06-25 11:49:02 -07:00
$scope.importStatus = 'Importing wallet - Reading backup...';
2014-05-01 15:39:01 -07:00
var reader = new FileReader();
2014-06-25 11:49:02 -07:00
var updateStatus = function(status) {
$scope.importStatus = status;
$scope.$digest();
}
2014-05-01 15:08:35 -07:00
var _importBackup = function(encryptedObj) {
Passphrase.getBase64Async($scope.password, function(passphrase) {
2014-06-25 11:49:02 -07:00
updateStatus('Importing wallet - Setting things up...');
var w, errMsg;
2014-07-25 06:50:18 -07:00
// try to import encrypted wallet with passphrase
try {
w = walletFactory.import(encryptedObj, passphrase);
} catch (e) {
errMsg = e.message;
}
if (!w) {
$scope.loading = false;
notification.error('Error', errMsg || 'Wrong password');
$rootScope.$digest();
return;
}
2014-07-25 06:50:18 -07:00
// if wallet was never used, we're done
2014-07-08 12:47:00 -07:00
if (!w.isReady()) {
$rootScope.wallet = w;
controllerUtils.startNetwork($rootScope.wallet, $scope);
return;
}
2014-07-25 06:50:18 -07:00
// if it was used, we need to scan for indices
w.updateIndexes(function(err) {
updateStatus('Importing wallet - We are almost there...');
if (err) {
$scope.loading = false;
notification.error('Error', 'Error updating indexes: ' + err);
}
$rootScope.wallet = w;
controllerUtils.startNetwork($rootScope.wallet, $scope);
});
2014-05-07 14:48:56 -07:00
});
2014-05-01 15:08:35 -07:00
};
2014-06-03 12:48:27 -07: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 15:12:13 -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
2014-05-01 14:33:36 -07:00
var encryptedObj = evt.target.result;
2014-05-01 15:08:35 -07:00
_importBackup(encryptedObj);
2014-04-25 15:12:13 -07:00
}
};
2014-05-01 15:08:35 -07:00
};
2014-04-25 15:12:13 -07:00
2014-05-05 09:16:56 -07:00
$scope.import = function(form) {
$scope.loading = true;
2014-05-05 09:16:56 -07:00
if (form.$invalid) {
2014-06-02 14:04:13 -07:00
$scope.loading = false;
2014-06-26 13:27:46 -07:00
notification.error('Error', 'There is an error in the form.');
2014-05-05 09:16:56 -07:00
return;
2014-05-01 15:08:35 -07:00
}
2014-05-05 09:16:56 -07:00
var backupFile = $scope.file;
var password = form.password.$modelValue;
2014-07-07 06:34:53 -07:00
if (!backupFile) {
$scope.loading = false;
2014-06-26 13:27:46 -07:00
notification.error('Error', 'Please, select your backup file or paste the file contents');
2014-06-02 14:04:13 -07:00
$scope.loading = false;
2014-05-05 09:16:56 -07:00
return;
}
if (backupFile) {
reader.readAsBinaryString(backupFile);
2014-07-07 06:34:53 -07:00
}
2014-04-25 15:12:13 -07:00
};
2014-04-25 13:34:24 -07:00
});