Wallet/js/controllers/import.js

133 lines
4.1 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-10-28 12:01:09 -03:00
function($scope, $rootScope, $location, controllerUtils, notification, isMobile, Compatibility) {
2014-08-04 11:27:46 -03:00
2014-10-27 16:13:06 -03:00
$rootScope.title = 'Import a backup';
2014-06-25 15:49:02 -03:00
$scope.importStatus = 'Importing wallet - Reading backup...';
$scope.hideAdv = true;
$scope.is_iOS = isMobile.iOS();
2014-06-25 15:49:02 -03:00
2014-10-30 16:13:56 -03:00
Compatibility.check($scope);
2014-05-01 19:39:01 -03:00
var reader = new FileReader();
2014-06-25 15:49:02 -03:00
var updateStatus = function(status) {
$scope.importStatus = status;
$scope.$digest();
}
2014-05-01 19:08:35 -03:00
var _importBackup = function(encryptedObj) {
2014-10-16 17:39:22 -03:00
var password = $scope.password;
updateStatus('Importing wallet - Setting things up...');
var skipFields = [];
if ($scope.skipPublicKeyRing)
skipFields.push('publicKeyRing');
2014-10-16 17:39:22 -03:00
if ($scope.skipTxProposals)
skipFields.push('txProposals');
2014-11-07 17:35:32 -03:00
$rootScope.iden.importEncryptedWallet(encryptedObj, password, skipFields, opts, function(err, w) {
if (!w) {
$scope.loading = false;
2014-11-11 14:51:01 -03:00
$scope.error = 'Wrong password';
$rootScope.$digest();
return;
}
2014-07-25 10:50:18 -03:00
// if wallet was never used, we're done
2014-07-08 16:47:00 -03:00
if (!w.isReady()) {
2014-10-16 17:39:22 -03:00
controllerUtils.installWalletHandlers($scope, w);
controllerUtils.setFocusedWallet(w);
2014-07-08 16:47:00 -03:00
return;
}
2014-10-28 15:15:42 -03:00
2014-07-25 10:50:18 -03: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;
2014-11-11 14:51:01 -03:00
$scope.error = 'Error updating indexes: ' + err;
}
2014-10-16 17:39:22 -03:00
controllerUtils.installWalletHandlers($scope, w);
controllerUtils.setFocusedWallet(w);
});
2014-10-28 15:15:42 -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;
};
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-11-07 17:35:32 -03:00
2014-10-30 16:13:56 -03:00
copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj, $scope.password, {},
2014-11-07 17:35:32 -03:00
function(err, wallet) {
2014-10-28 15:15:42 -03:00
if (err) {
2014-11-07 17:35:32 -03:00
$scope.loading = false;
2014-11-11 14:51:01 -03:00
$scope.error = 'Could not read wallet. Please check your password';
2014-10-28 15:15:42 -03:00
} else {
controllerUtils.installWalletHandlers($scope, wallet);
controllerUtils.setFocusedWallet(wallet);
return;
}
}
);
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) {
$scope.loading = true;
2014-05-05 13:16:56 -03:00
if (form.$invalid) {
2014-06-02 18:04:13 -03:00
$scope.loading = false;
2014-11-11 14:51:01 -03:00
$scope.error = 'There is an error in the form';
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;
2014-10-30 16:13:56 -03:00
var backupOldWallet = form.backupOldWallet.$modelValue;
2014-05-05 13:16:56 -03:00
var password = form.password.$modelValue;
2014-10-30 16:13:56 -03:00
if (backupOldWallet) {
backupText = backupOldWallet.value;
}
if (!backupFile && !backupText) {
$scope.loading = false;
2014-11-11 14:51:01 -03:00
$scope.error = 'Please, select your backup file';
2014-05-05 13:16:56 -03:00
return;
}
if (backupFile) {
reader.readAsBinaryString(backupFile);
2014-11-07 17:35:32 -03:00
} else {
copay.Compatibility.importEncryptedWallet($rootScope.iden, backupText, $scope.password, {},
function(err, wallet) {
if (err) {
2014-11-11 14:51:01 -03:00
$scope.error = 'Could not read wallet. Please check your password';
2014-11-07 17:35:32 -03:00
} else {
copay.Compatibility.deleteOldWallet(backupOldWallet);
controllerUtils.installWalletHandlers($scope, wallet);
controllerUtils.setFocusedWallet(wallet);
return;
2014-10-28 15:15:42 -03:00
}
2014-11-07 17:35:32 -03:00
}
);
2014-10-27 21:05:22 -03:00
try {
_importBackup(backupText);
2014-11-07 17:35:32 -03:00
} catch (e) {
2014-10-30 16:13:56 -03:00
copay.Compatibility.importEncryptedWallet(backupText, $scope.password, $scope.skipPublicKeyRing, $scope.skipTxProposals);
2014-10-27 21:05:22 -03:00
}
}
2014-04-25 19:12:13 -03:00
};
2014-04-25 17:34:24 -03:00
});