Import profile

This commit is contained in:
Matias Pando 2014-10-31 19:39:01 -03:00
parent d475ea4f16
commit 16c3d66f33
5 changed files with 184 additions and 6 deletions

View File

@ -0,0 +1,91 @@
'use strict';
angular.module('copayApp.controllers').controller('ImportProfileController',
function($scope, $rootScope, $location, controllerUtils, notification, isMobile, pluginManager) {
controllerUtils.redirIfLogged();
$scope.title = 'Import a backup';
$scope.importStatus = 'Importing wallet - Reading backup...';
$scope.hideAdv = true;
$scope.is_iOS = isMobile.iOS();
var reader = new FileReader();
var updateStatus = function(status) {
$scope.importStatus = status;
$scope.$digest();
}
var _importBackup = function(str) {
var password = $scope.password;
updateStatus('Importing profile - Setting things up...');
// var skipFields = [];
// if ($scope.skipPublicKeyRing)
// skipFields.push('publicKeyRing');
//
// if ($scope.skipTxProposals)
// skipFields.push('txProposals');
copay.Identity.importFromEncryptedFullJson(str, password, {
pluginManager: pluginManager,
network: config.network,
networkName: config.networkName,
walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig,
}, function(err, iden) {
if (err && !iden) {
console.log('Error:' + err)
controllerUtils.onErrorDigest(
$scope, (err.toString() || '').match('BADSTR') ? 'Bad password or corrupt profile file' : 'Unknown error');
} else {
console.log('Success.....Profile imported successfully');
notification.info('Success', 'Profile imported successfully');
$location.path('/');
}
});
};
$scope.openFileDialog = function() {
if (window.cshell) {
return cshell.send('backup:import');
}
$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;
_importBackup(encryptedObj);
}
};
};
$scope.import = function(form) {
$scope.loading = true;
if (form.$invalid) {
$scope.loading = false;
notification.error('Error', 'There is an error in the form.');
return;
}
var backupFile = $scope.file;
var backupText = form.backupText.$modelValue;
var password = form.password.$modelValue;
if (!backupFile && !backupText) {
$scope.loading = false;
notification.error('Error', 'Please, select your backup file');
$scope.loading = false;
return;
}
if (backupFile) {
reader.readAsBinaryString(backupFile);
} else {
_importBackup(backupText);
}
};
});

View File

@ -287,6 +287,16 @@ Identity.prototype.close = function(cb) {
* @return {Wallet}
*/
Identity.prototype.importEncryptedWallet = function(cypherText, password, opts, cb) {
console.log('importEncryptedWallet------');
console.log('opts');
console.log(opts);
console.log('password');
console.log(password);
console.log('cypherText');
console.log(cypherText);
var crypto = opts.cryptoUtil || cryptoUtil;
// TODO set iter and salt using config.js
@ -345,7 +355,7 @@ Identity.prototype.closeWallet = function(wallet, cb) {
Identity.importFromEncryptedFullJson = function(str, password, opts, cb) {
var crypto = opts.cryptoUtil || cryptoUtil;
var key = crypto.kdf(password);
return Identity.importFromFullJson(crypto.decript(key, str));
return Identity.importFromFullJson(crypto.decrypt(key, str), password, opts, cb);
};
Identity.importFromFullJson = function(str, password, opts, cb) {
@ -357,15 +367,21 @@ Identity.importFromFullJson = function(str, password, opts, cb) {
return cb('Unable to retrieve json from string', str);
}
if (!_.isNumber(json.iterations))
return cb('BADSTR: Missing iterations');
// if (!_.isNumber(json.iterations))
// return cb('BADSTR: Missing iterations');
var email = json.email;
var iden = new Identity(email, password, opts);
opts.email = email;
opts.password = password;
var iden = new Identity(opts);
json.wallets = json.wallets || {};
async.map(json.wallets, function(walletData, callback) {
iden.importEncryptedWallet(wstr, password, opts, function(err, w) {
iden.importWalletFromObj(walletData, opts, function(err, w) {
if (err) return callback(err);
log.debug('Wallet ' + w.getId() + ' imported');
callback();

View File

@ -34,6 +34,9 @@ angular
templateUrl: 'views/import.html',
logged: true
})
.when('/importProfile', {
templateUrl: 'views/importProfile.html',
})
.when('/create', {
templateUrl: 'views/create.html',
logged: true

View File

@ -63,12 +63,18 @@
</button>
</form>
<div class="box-setup-footer">
<div class="left">
<div class="left m10r">
<a class="button-setup text-gray" href="#!/createProfile">
<i class="fi-torso"></i>
<span translate>Create a profile</span>
</a>
</div>
<div class="left">
<a class="button-setup text-gray" href="#!/importProfile">
<i class="fi-upload"></i>
<span translate>Import a profile</span>
</a>
</div>
<div class="right m10t">
<a class="text-gray" href="#!/settings">
<i class="fi-wrench"></i>

62
views/importProfile.html Normal file
View File

@ -0,0 +1,62 @@
<div class="home" ng-controller="ImportProfileController">
<div class="large-4 large-centered medium-6 medium-centered columns">
<div class="logo-setup">
<img src="img/logo-negative-beta.svg" alt="Copay" width="146" height="59">
<div ng-include="'views/includes/version.html'"></div>
</div>
<div class="box-setup">
<h1><span translate>Import Profile<span></h1>
<form name="importProfileForm" ng-submit="import(importProfileForm)" novalidate>
<div ng-show="!is_iOS">
<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>
</div>
<div ng-show="is_iOS">
<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>
</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>
<div class="text-right m20t">
<a class="back-button text-white m20r" href="#!/">&laquo; <span translate>Back</span></a>
<button translate type="submit" class="button primary m0" ng-disabled="importForm.$invalid">
Import backup
</button>
</div>
</form>
<div class="box-setup-footer">
<div class="right m10t">
<a class="text-gray" href="#!/">
<i class="fi-arrow"></i>
<span translate>Back</span>
</a>
</div>
</div>
</div>
</div>
</div>