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-11-29 13:35:48 -08:00
|
|
|
function($scope, $rootScope, $location, identityService, notification, isMobile, Compatibility) {
|
2014-08-04 07:27:46 -07:00
|
|
|
|
2014-11-14 10:05:00 -08:00
|
|
|
$rootScope.title = 'Import wallet';
|
2014-06-25 11:49:02 -07:00
|
|
|
$scope.importStatus = 'Importing wallet - Reading backup...';
|
2014-09-10 13:36:17 -07:00
|
|
|
$scope.hideAdv = true;
|
2014-10-07 12:37:22 -07:00
|
|
|
$scope.is_iOS = isMobile.iOS();
|
2014-11-14 10:05:00 -08:00
|
|
|
$scope.importOpts = {};
|
2014-06-25 11:49:02 -07:00
|
|
|
|
2014-10-30 12:13:56 -07:00
|
|
|
Compatibility.check($scope);
|
|
|
|
|
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-11-26 12:16:46 -08: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;
|
|
|
|
updateStatus('Importing wallet - Procesing backup...');
|
|
|
|
identityService.importWallet(encryptedObj, $scope.password, {}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
$scope.loading = false;
|
|
|
|
$scope.error = 'Could not read wallet. Please check your password';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-06-03 12:48:27 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
$scope.import = function(form) {
|
|
|
|
$scope.loading = true;
|
2014-04-25 15:12:13 -07:00
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
if (form.$invalid) {
|
|
|
|
$scope.loading = false;
|
|
|
|
$scope.error = 'There is an error in the form';
|
|
|
|
return;
|
|
|
|
}
|
2014-07-02 06:43:00 -07:00
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
var backupFile = $scope.file;
|
|
|
|
var backupText = form.backupText.$modelValue;
|
|
|
|
var backupOldWallet = form.backupOldWallet.$modelValue;
|
|
|
|
var password = form.password.$modelValue;
|
2014-05-05 09:16:56 -07:00
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
if (backupOldWallet) {
|
|
|
|
backupText = backupOldWallet.value;
|
|
|
|
}
|
2014-05-05 09:16:56 -07:00
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
if (!backupFile && !backupText) {
|
|
|
|
$scope.loading = false;
|
|
|
|
$scope.error = 'Please, select your backup file';
|
|
|
|
return;
|
|
|
|
}
|
2014-10-30 12:13:56 -07:00
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
$scope.importOpts = {};
|
2014-05-05 09:16:56 -07:00
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
var skipFields = [];
|
2014-11-14 10:05:00 -08:00
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
if ($scope.skipPublicKeyRing)
|
|
|
|
skipFields.push('publicKeyRing');
|
2014-11-14 10:05:00 -08:00
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
if ($scope.skipTxProposals)
|
|
|
|
skipFields.push('txProposals');
|
2014-11-14 10:05:00 -08:00
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
if (skipFields)
|
|
|
|
$scope.importOpts.skipFields = skipFields;
|
2014-11-14 10:05:00 -08:00
|
|
|
|
|
|
|
|
2014-11-26 12:16:46 -08:00
|
|
|
if (backupFile) {
|
|
|
|
reader.readAsBinaryString(backupFile);
|
|
|
|
} else {
|
|
|
|
updateStatus('Importing wallet - Procesing backup...');
|
|
|
|
identityService.importWallet(encryptedObj, $scope.password, $scope.importOpts, function(err) {
|
|
|
|
if (err) {
|
|
|
|
$scope.loading = false;
|
|
|
|
$scope.error = 'Could not read wallet. Please check your password';
|
2014-12-02 12:38:31 -08:00
|
|
|
$rootScope.$digest();
|
|
|
|
return;
|
2014-11-26 12:16:46 -08:00
|
|
|
}
|
|
|
|
copay.Compatibility.deleteOldWallet(backupOldWallet);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2014-12-02 06:23:49 -08:00
|
|
|
});
|