refactor on import wallet controllers

This commit is contained in:
Matias Alejo Garcia 2014-11-14 15:05:00 -03:00
parent 16e2194377
commit 45e44fba19
7 changed files with 107 additions and 146 deletions

View File

@ -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);
}
};
});

View File

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

View File

@ -289,8 +289,8 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) {
var w = importFunction(obj, readOpts);
if (!w) return cb(new Error('Could not decrypt'));
this._checkVersion(w.version);
this.addWallet(w);
self._checkVersion(w.version);
self.addWallet(w);
self.bindWallet(w);
self.storeWallet(w, function(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
* @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 data-alert class="loading-screen" ng-show="loading">
<i class="size-60 fi-bitcoin-circle icon-rotate spinner"></i>
{{ importStatus|translate }}
<i class="size-60 fi-bitcoin-circle icon-rotate spinner"></i> {{ importStatus|translate }}
</div>
<div ng-show="!loading">
@ -10,49 +9,42 @@
<div class="large-12 columns">
<div class="panel">
<form name="importForm" ng-submit="import(importForm)" novalidate>
<div class="text-warning size-12 m20b"
ng-show="error">
<i class="fi-x"></i>
{{error|translate}}
</div>
<div class="text-warning size-12 m20b" ng-show="error">
<i class="fi-x"></i> {{error|translate}}
</div>
<div ng-show="!is_iOS && !backupOldWallet">
<legend for="backupFile" class="m10b">
<span translate>Choose backup file from your computer</span> <i class="fi-laptop"></i>
<legend for="backupFile" class="m10b">
<span translate>Choose backup file from your computer</span> <i class="fi-laptop"></i>
</legend>
<input type="file" class="form-control"
placeholder="{{'Select a backup file'|translate}}" name="backupFile" ng-model="backupFile" ng-file-select>
<input type="file" class="form-control" placeholder="{{'Select a backup file'|translate}}" name="backupFile" ng-model="backupFile" ng-file-select>
</div>
<div ng-show="is_iOS && !backupOldWallet">
<label for="backupText" class="m10b">
<span translate>Paste backup plain text code</span> <i class="fi-clipboard"></i>
<label for="backupText" class="m10b">
<span translate>Paste backup plain text code</span> <i class="fi-clipboard"></i>
</label>
<textarea class="form-control"
name="backupText"
ng-model="backupText"
rows="5"></textarea>
<textarea class="form-control" name="backupText" ng-model="backupText" rows="5"></textarea>
</div>
<div ng-show="anyWallet && (!backupFile || !backupText)">
<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>
<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>
</label>
<select ng-model="backupOldWallet" name="backupOldWallet"
ng-options="wallet.name for wallet in oldWallets">
<select ng-model="backupOldWallet" name="backupOldWallet" ng-options="wallet.name for wallet in oldWallets">
<option value="">-- choose wallet --</option>
</select>
</div>
<label for="password" class="m10b"><span translate>Password</span> <small translate>Required</small></label>
<input type="password" class="form-control"
placeholder="{{'Your wallet password'|translate}}" name="password" ng-model="password" required>
<label for="password" class="m10b"><span translate>Password</span> <small translate>Required</small>
</label>
<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>
<a class="expand small" ng-click="hideAdv=!hideAdv">
<a class="expand small" ng-click="hideAdv=!hideAdv">
<i class="fi-widget m3r"></i>
<span translate ng-hide="!hideAdv">Show</span>
<span translate ng-hide="hideAdv">Hide</span>
@ -63,15 +55,13 @@
<div ng-hide="hideAdv" class="m10t">
<label>
<input type="checkbox" class="form-control"
name="skipPublicKeyRing" ng-model="skipPublicKeyRing">
<span translate>Skip public keys from peers</span>
<input type="checkbox" class="form-control" name="skipPublicKeyRing" ng-model="skipPublicKeyRing">
<span translate>Skip public keys</span>
</label>
<label>
<input type="checkbox" class="form-control"
name="skipTxProposals" ng-model="skipTxProposals">
<span translate>Skip transaction proposals from Backup</span>
<input type="checkbox" class="form-control" name="skipTxProposals" ng-model="skipTxProposals">
<span translate>Skip transaction proposals</span>
</label>
</div>

View File

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