refactor on import wallet controllers

This commit is contained in:
Matias Alejo Garcia 2014-11-14 15:05:00 -03:00
commit 45e44fba19
7 changed files with 105 additions and 144 deletions

View file

@ -3,10 +3,11 @@
angular.module('copayApp.controllers').controller('ImportController', angular.module('copayApp.controllers').controller('ImportController',
function($scope, $rootScope, $location, controllerUtils, notification, isMobile, Compatibility) { function($scope, $rootScope, $location, controllerUtils, notification, isMobile, Compatibility) {
$rootScope.title = 'Import a backup'; $rootScope.title = 'Import wallet';
$scope.importStatus = 'Importing wallet - Reading backup...'; $scope.importStatus = 'Importing wallet - Reading backup...';
$scope.hideAdv = true; $scope.hideAdv = true;
$scope.is_iOS = isMobile.iOS(); $scope.is_iOS = isMobile.iOS();
$scope.importOpts = {};
Compatibility.check($scope); Compatibility.check($scope);
@ -17,44 +18,6 @@ angular.module('copayApp.controllers').controller('ImportController',
$scope.$digest(); $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() { $scope.openFileDialog = function() {
if (window.cshell) { if (window.cshell) {
return cshell.send('backup:import'); return cshell.send('backup:import');
@ -62,72 +25,79 @@ angular.module('copayApp.controllers').controller('ImportController',
$scope.choosefile = !$scope.choosefile; $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, {}, $scope._doImport = function(encryptedObj, password) {
function(err, wallet) { updateStatus('Importing wallet - Procesing backup...');
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.import = function(form) { copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj,
$scope.loading = true; $scope.password, $scope.importOpts, function(err, wallet) {
if (err) {
if (form.$invalid) { $scope.loading = false;
$scope.loading = false; $scope.error = 'Could not read wallet. Please check your password';
$scope.error = 'There is an error in the form'; } else {
return; controllerUtils.installWalletHandlers($scope, wallet);
} updateStatus('Importing wallet - Scanning for transactions...');
wallet.updateIndexes(function(err) {
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);
controllerUtils.setFocusedWallet(wallet); 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);
}
};
});

View file

@ -5,7 +5,7 @@ angular.module('copayApp.controllers').controller('JoinController',
$rootScope.fromSetup = false; $rootScope.fromSetup = false;
$scope.loading = false; $scope.loading = false;
$scope.isMobile = !!window.cordova; $scope.isMobile = !!window.cordova;
$rootScope.title = 'Join an existent wallet'; $rootScope.title = 'Join shared wallet';
// QR code Scanner // QR code Scanner
var cameraInput; var cameraInput;

View file

@ -289,8 +289,8 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) {
var w = importFunction(obj, readOpts); var w = importFunction(obj, readOpts);
if (!w) return cb(new Error('Could not decrypt')); if (!w) return cb(new Error('Could not decrypt'));
this._checkVersion(w.version); self._checkVersion(w.version);
this.addWallet(w); self.addWallet(w);
self.bindWallet(w); self.bindWallet(w);
self.storeWallet(w, function(err) { self.storeWallet(w, function(err) {
if (err) return cb(err); if (err) return cb(err);

View file

@ -2487,7 +2487,8 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, comment, utxos
}; };
/** /**
* @desc Updates all the indexes for the current publicKeyRing * @desc Updates all the indexes for the current publicKeyRing. This scans
* the blockchain looking for transactions on derived addresses.
* *
* Triggers a wallet {@link Wallet#store} call * Triggers a wallet {@link Wallet#store} call
* @param {Function} callback - called when all indexes have been updated. Receives an error, if any, as first argument * @param {Function} callback - called when all indexes have been updated. Receives an error, if any, as first argument

View file

@ -1,7 +1,6 @@
<div class="import" ng-controller="ImportController"> <div class="import" ng-controller="ImportController">
<div data-alert class="loading-screen" ng-show="loading"> <div data-alert class="loading-screen" ng-show="loading">
<i class="size-60 fi-bitcoin-circle icon-rotate spinner"></i> <i class="size-60 fi-bitcoin-circle icon-rotate spinner"></i> {{ importStatus|translate }}
{{ importStatus|translate }}
</div> </div>
<div ng-show="!loading"> <div ng-show="!loading">
@ -10,49 +9,42 @@
<div class="large-12 columns"> <div class="large-12 columns">
<div class="panel"> <div class="panel">
<form name="importForm" ng-submit="import(importForm)" novalidate> <form name="importForm" ng-submit="import(importForm)" novalidate>
<div class="text-warning size-12 m20b" <div class="text-warning size-12 m20b" ng-show="error">
ng-show="error"> <i class="fi-x"></i> {{error|translate}}
<i class="fi-x"></i> </div>
{{error|translate}}
</div>
<div ng-show="!is_iOS && !backupOldWallet"> <div ng-show="!is_iOS && !backupOldWallet">
<legend for="backupFile" class="m10b"> <legend for="backupFile" class="m10b">
<span translate>Choose backup file from your computer</span> <i class="fi-laptop"></i> <span translate>Choose backup file from your computer</span> <i class="fi-laptop"></i>
</legend> </legend>
<input type="file" class="form-control" <input type="file" class="form-control" placeholder="{{'Select a backup file'|translate}}" name="backupFile" ng-model="backupFile" ng-file-select>
placeholder="{{'Select a backup file'|translate}}" name="backupFile" ng-model="backupFile" ng-file-select>
</div> </div>
<div ng-show="is_iOS && !backupOldWallet"> <div ng-show="is_iOS && !backupOldWallet">
<label for="backupText" class="m10b"> <label for="backupText" class="m10b">
<span translate>Paste backup plain text code</span> <i class="fi-clipboard"></i> <span translate>Paste backup plain text code</span> <i class="fi-clipboard"></i>
</label> </label>
<textarea class="form-control" <textarea class="form-control" name="backupText" ng-model="backupText" rows="5"></textarea>
name="backupText"
ng-model="backupText"
rows="5"></textarea>
</div> </div>
<div ng-show="anyWallet && (!backupFile || !backupText)"> <div ng-show="anyWallet && (!backupFile || !backupText)">
<label for="backupOldWAllet" class="m10b size-14"> <label for="backupOldWAllet" class="m10b size-14">
<span translate>You have old wallets in your localStorage. Choose one to import</span> <i class="fi-bitcoin"></i> <span translate>You have old wallets in your localStorage. Choose one to import</span> <i class="fi-bitcoin"></i>
</label> </label>
<select ng-model="backupOldWallet" name="backupOldWallet" <select ng-model="backupOldWallet" name="backupOldWallet" ng-options="wallet.name for wallet in oldWallets">
ng-options="wallet.name for wallet in oldWallets">
<option value="">-- choose wallet --</option> <option value="">-- choose wallet --</option>
</select> </select>
</div> </div>
<label for="password" class="m10b"><span translate>Password</span> <small translate>Required</small></label> <label for="password" class="m10b"><span translate>Password</span> <small translate>Required</small>
<input type="password" class="form-control" </label>
placeholder="{{'Your wallet password'|translate}}" name="password" ng-model="password" required> <input type="password" class="form-control" placeholder="{{'Your wallet password'|translate}}" name="password" ng-model="password" required>
<div class="line-dashed-h m10b m20t"></div> <div class="line-dashed-h m10b m20t"></div>
<a class="expand small" ng-click="hideAdv=!hideAdv"> <a class="expand small" ng-click="hideAdv=!hideAdv">
<i class="fi-widget m3r"></i> <i class="fi-widget m3r"></i>
<span translate ng-hide="!hideAdv">Show</span> <span translate ng-hide="!hideAdv">Show</span>
<span translate ng-hide="hideAdv">Hide</span> <span translate ng-hide="hideAdv">Hide</span>
@ -63,15 +55,13 @@
<div ng-hide="hideAdv" class="m10t"> <div ng-hide="hideAdv" class="m10t">
<label> <label>
<input type="checkbox" class="form-control" <input type="checkbox" class="form-control" name="skipPublicKeyRing" ng-model="skipPublicKeyRing">
name="skipPublicKeyRing" ng-model="skipPublicKeyRing"> <span translate>Skip public keys</span>
<span translate>Skip public keys from peers</span>
</label> </label>
<label> <label>
<input type="checkbox" class="form-control" <input type="checkbox" class="form-control" name="skipTxProposals" ng-model="skipTxProposals">
name="skipTxProposals" ng-model="skipTxProposals"> <span translate>Skip transaction proposals</span>
<span translate>Skip transaction proposals from Backup</span>
</label> </label>
</div> </div>

View file

@ -27,10 +27,10 @@
<li> <li>
<a class="text-gray size-16" href="#!/create" title="Create new wallet"> <a class="text-gray size-16" href="#!/create" title="Create new wallet">
<i class="fi-plus m10r"></i> {{'Create new wallet'|translate}}</a></li> <i class="fi-plus m10r"></i> {{'Create new wallet'|translate}}</a></li>
<li><a class="text-gray size-16" href="#!/join" title="Join an existent wallet"> <li><a class="text-gray size-16" href="#!/join" title="Join shared wallet">
<i class="fi-torsos-all m10r"></i> {{'Join an existent wallet'|translate}}</a></li> <i class="fi-torsos-all m10r"></i> {{'Join shared wallet'|translate}}</a></li>
<li><a class="text-gray size-16" href="#!/import" title="Import a backup"> <li><a class="text-gray size-16" href="#!/import" title="Import wallet">
<i class="fi-download m10r"></i> {{'Import a backup'|translate}}</a></li> <i class="fi-download m10r"></i> {{'Import wallet'|translate}}</a></li>
<li class="divider"></li> <li class="divider"></li>
<li><a class="text-gray size-16" href="#!/profile" title="Profile"> <li><a class="text-gray size-16" href="#!/profile" title="Profile">
<i class="fi-torso m10r"></i> {{'Profile'|translate}}</a></li> <i class="fi-torso m10r"></i> {{'Profile'|translate}}</a></li>

View file

@ -89,12 +89,12 @@
<i class="size-24 m20r fi-plus"></i> {{'Create new wallet' | translate }} </a> <i class="size-24 m20r fi-plus"></i> {{'Create new wallet' | translate }} </a>
</li> </li>
<li> <li>
<a href="#!/join" class="db p20h" title="Join an existent wallet"> <a href="#!/join" class="db p20h" title="Join shared wallet">
<i class="size-24 m20r fi-torsos-all"></i> {{'Join an existent wallet' | translate }} </a> <i class="size-24 m20r fi-torsos-all"></i> {{'Join shared wallet' | translate }} </a>
</li> </li>
<li> <li>
<a href="#!/import" class="db p20h" title="Import a backup"> <a href="#!/import" class="db p20h" title="Import wallet">
<i class="size-24 m20r fi-download"></i> {{'Import a backup' | translate }} </a> <i class="size-24 m20r fi-download"></i> {{'Import wallet' | translate }} </a>
</li> </li>
<li> <li>
<a href="#!/profile" class="db p20h" title="Profile"> <a href="#!/profile" class="db p20h" title="Profile">