refactor on import wallet controllers
This commit is contained in:
parent
16e2194377
commit
45e44fba19
7 changed files with 105 additions and 144 deletions
|
|
@ -3,10 +3,11 @@
|
|||
angular.module('copayApp.controllers').controller('ImportController',
|
||||
function($scope, $rootScope, $location, controllerUtils, notification, isMobile, Compatibility) {
|
||||
|
||||
$rootScope.title = 'Import a backup';
|
||||
$rootScope.title = 'Import wallet';
|
||||
$scope.importStatus = 'Importing wallet - Reading backup...';
|
||||
$scope.hideAdv = true;
|
||||
$scope.is_iOS = isMobile.iOS();
|
||||
$scope.importOpts = {};
|
||||
|
||||
Compatibility.check($scope);
|
||||
|
||||
|
|
@ -17,44 +18,6 @@ angular.module('copayApp.controllers').controller('ImportController',
|
|||
$scope.$digest();
|
||||
}
|
||||
|
||||
var _importBackup = function(encryptedObj) {
|
||||
var password = $scope.password;
|
||||
updateStatus('Importing wallet - Setting things up...');
|
||||
var skipFields = [];
|
||||
if ($scope.skipPublicKeyRing)
|
||||
skipFields.push('publicKeyRing');
|
||||
|
||||
if ($scope.skipTxProposals)
|
||||
skipFields.push('txProposals');
|
||||
|
||||
$rootScope.iden.importEncryptedWallet(encryptedObj, password, skipFields, opts, function(err, w) {
|
||||
if (!w) {
|
||||
$scope.loading = false;
|
||||
$scope.error = 'Wrong password';
|
||||
$rootScope.$digest();
|
||||
return;
|
||||
}
|
||||
|
||||
// if wallet was never used, we're done
|
||||
if (!w.isReady()) {
|
||||
controllerUtils.installWalletHandlers($scope, w);
|
||||
controllerUtils.setFocusedWallet(w);
|
||||
return;
|
||||
}
|
||||
|
||||
// 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;
|
||||
$scope.error = 'Error updating indexes: ' + err;
|
||||
}
|
||||
controllerUtils.installWalletHandlers($scope, w);
|
||||
controllerUtils.setFocusedWallet(w);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$scope.openFileDialog = function() {
|
||||
if (window.cshell) {
|
||||
return cshell.send('backup:import');
|
||||
|
|
@ -62,72 +25,79 @@ angular.module('copayApp.controllers').controller('ImportController',
|
|||
$scope.choosefile = !$scope.choosefile;
|
||||
};
|
||||
|
||||
$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;
|
||||
|
||||
copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj, $scope.password, {},
|
||||
function(err, wallet) {
|
||||
if (err) {
|
||||
$scope.loading = false;
|
||||
$scope.error = 'Could not read wallet. Please check your password';
|
||||
} else {
|
||||
controllerUtils.installWalletHandlers($scope, wallet);
|
||||
controllerUtils.setFocusedWallet(wallet);
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
};
|
||||
$scope._doImport = function(encryptedObj, password) {
|
||||
updateStatus('Importing wallet - Procesing backup...');
|
||||
|
||||
$scope.import = function(form) {
|
||||
$scope.loading = true;
|
||||
|
||||
if (form.$invalid) {
|
||||
$scope.loading = false;
|
||||
$scope.error = 'There is an error in the form';
|
||||
return;
|
||||
}
|
||||
|
||||
var backupFile = $scope.file;
|
||||
var backupText = form.backupText.$modelValue;
|
||||
var backupOldWallet = form.backupOldWallet.$modelValue;
|
||||
var password = form.password.$modelValue;
|
||||
|
||||
if (backupOldWallet) {
|
||||
backupText = backupOldWallet.value;
|
||||
}
|
||||
|
||||
if (!backupFile && !backupText) {
|
||||
$scope.loading = false;
|
||||
$scope.error = 'Please, select your backup file';
|
||||
return;
|
||||
}
|
||||
|
||||
if (backupFile) {
|
||||
reader.readAsBinaryString(backupFile);
|
||||
} else {
|
||||
copay.Compatibility.importEncryptedWallet($rootScope.iden, backupText, $scope.password, {},
|
||||
function(err, wallet) {
|
||||
if (err) {
|
||||
$scope.error = 'Could not read wallet. Please check your password';
|
||||
} else {
|
||||
copay.Compatibility.deleteOldWallet(backupOldWallet);
|
||||
controllerUtils.installWalletHandlers($scope, wallet);
|
||||
copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj,
|
||||
$scope.password, $scope.importOpts, function(err, wallet) {
|
||||
if (err) {
|
||||
$scope.loading = false;
|
||||
$scope.error = 'Could not read wallet. Please check your password';
|
||||
} else {
|
||||
controllerUtils.installWalletHandlers($scope, wallet);
|
||||
updateStatus('Importing wallet - Scanning for transactions...');
|
||||
wallet.updateIndexes(function(err) {
|
||||
controllerUtils.setFocusedWallet(wallet);
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
try {
|
||||
_importBackup(backupText);
|
||||
} catch (e) {
|
||||
copay.Compatibility.importEncryptedWallet(backupText, $scope.password, $scope.skipPublicKeyRing, $scope.skipTxProposals);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
$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;
|
||||
$scope._doImport(encryptedObj, $scope.password);
|
||||
}
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
$scope.import = function(form) {
|
||||
$scope.loading = true;
|
||||
|
||||
if (form.$invalid) {
|
||||
$scope.loading = false;
|
||||
$scope.error = 'There is an error in the form';
|
||||
return;
|
||||
}
|
||||
|
||||
var backupFile = $scope.file;
|
||||
var backupText = form.backupText.$modelValue;
|
||||
var backupOldWallet = form.backupOldWallet.$modelValue;
|
||||
var password = form.password.$modelValue;
|
||||
|
||||
if (backupOldWallet) {
|
||||
backupText = backupOldWallet.value;
|
||||
}
|
||||
|
||||
if (!backupFile && !backupText) {
|
||||
$scope.loading = false;
|
||||
$scope.error = 'Please, select your backup file';
|
||||
return;
|
||||
}
|
||||
|
||||
$scope.importOpts = {};
|
||||
|
||||
var skipFields = [];
|
||||
|
||||
if ($scope.skipPublicKeyRing)
|
||||
skipFields.push('publicKeyRing');
|
||||
|
||||
if ($scope.skipTxProposals)
|
||||
skipFields.push('txProposals');
|
||||
|
||||
if (skipFields)
|
||||
$scope.importOpts.skipFields = skipFields;
|
||||
|
||||
|
||||
if (backupFile) {
|
||||
reader.readAsBinaryString(backupFile);
|
||||
} else {
|
||||
$scope._doImport(backupText, $scope.password);
|
||||
copay.Compatibility.deleteOldWallet(backupOldWallet);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue