Merge pull request #1669 from matiaspando/feature/importProfile

Import profile
This commit is contained in:
Matias Alejo Garcia 2014-11-10 15:49:56 -03:00
commit 53843a578d
14 changed files with 209 additions and 51 deletions

1
.gitignore vendored
View file

@ -39,6 +39,7 @@ README.html
lib/*
!lib/socket.io.js
!lib/sjcl.js
js/copayBundle.js
js/copayMain.js

View file

@ -42,7 +42,7 @@ var defaultConfig = {
// local encryption/security config
passphraseConfig: {
iterations: 100,
iterations: 1000,
storageSalt: 'mjuBtGybi/4=',
},
@ -55,7 +55,7 @@ var defaultConfig = {
plugins: {
//LocalStorage: true,
// EncryptedLocalStorage: true,
// EncryptedLocalStorage: true,
//GoogleDrive: true,
//InsightStorage: true
EncryptedInsightStorage: true,

View file

@ -27,7 +27,7 @@ angular.module('copayApp.controllers').controller('ImportController',
if ($scope.skipTxProposals)
skipFields.push('txProposals');
$rootScope.iden.importEncryptedWallet(encryptedObj, password, skipFields, function(err, w) {
$rootScope.iden.importEncryptedWallet(encryptedObj, password, skipFields, opts, function(err, w) {
if (!w) {
$scope.loading = false;
notification.error('Error', err || 'Wrong password');
@ -67,9 +67,11 @@ angular.module('copayApp.controllers').controller('ImportController',
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){
function(err, wallet) {
if (err) {
$scope.loading = false;
notification.error('Error', 'Could not read wallet. Please check your password');
} else {
controllerUtils.installWalletHandlers($scope, wallet);
@ -109,23 +111,22 @@ angular.module('copayApp.controllers').controller('ImportController',
if (backupFile) {
reader.readAsBinaryString(backupFile);
}
else {
copay.Compatibility.importEncryptedWallet($rootScope.iden, backupText, $scope.password, {},
function(err, wallet){
if (err) {
notification.error('Error', 'Could not read wallet. Please check your password');
} else {
copay.Compatibility.deleteOldWallet(backupOldWallet);
controllerUtils.installWalletHandlers($scope, wallet);
controllerUtils.setFocusedWallet(wallet);
return;
}
} else {
copay.Compatibility.importEncryptedWallet($rootScope.iden, backupText, $scope.password, {},
function(err, wallet) {
if (err) {
notification.error('Error', 'Could not read wallet. Please check your password');
} else {
copay.Compatibility.deleteOldWallet(backupOldWallet);
controllerUtils.installWalletHandlers($scope, wallet);
controllerUtils.setFocusedWallet(wallet);
return;
}
);
}
);
try {
_importBackup(backupText);
} catch(e) {
} catch (e) {
copay.Compatibility.importEncryptedWallet(backupText, $scope.password, $scope.skipPublicKeyRing, $scope.skipTxProposals);
}
}

View file

@ -0,0 +1,83 @@
'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...');
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) {
controllerUtils.onErrorDigest(
$scope, (err.toString() || '').match('BADSTR') ? 'Bad password or corrupt profile file' : 'Unknown error');
} else {
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

@ -47,6 +47,7 @@ Compatibility._getWalletIds = function(cb) {
Compatibility.importLegacy = function(encryptedWallet, password) {
var passphrase = this.kdf(password);
var ret = Compatibility._decrypt(encryptedWallet, passphrase);
if (!ret) return null;
return ret;
};
@ -195,19 +196,19 @@ Compatibility.readWalletPre8 = function(walletId, password, cb) {
Compatibility.importEncryptedWallet = function(identity, cypherText, password, opts, cb) {
var crypto = (opts && opts.cryptoUtil) || cryptoUtils;
var key = crypto.kdf(password);
var obj = crypto.decrypt(key, cypherText);
var obj = crypto.decrypt(password, cypherText);
if (!obj) {
log.info("Could not decrypt, trying legacy..");
obj = Compatibility.importLegacy(cypherText, password);
if (!obj) {
return cb(new Error('Could not decrypt'))
return cb('Could not decrypt', null);
}
};
try {
obj = JSON.parse(obj);
} catch (e) {
return cb(new Error('Could not read encrypted wallet'));
return cb('Could not read encrypted wallet', null);
}
return identity.importWalletFromObj(obj, opts, cb);
};
@ -236,7 +237,7 @@ Compatibility.kdf = function(password) {
};
Compatibility.deleteOldWallet = function(walletObj) {
localStorage.removeItem('wallet::'+walletObj.id+'_'+walletObj.name);
localStorage.removeItem('wallet::' + walletObj.id + '_' + walletObj.name);
log.info('Old wallet ' + walletObj.name + ' deleted: ' + walletObj.id);
};

View file

@ -225,8 +225,7 @@ Identity.prototype.toObj = function() {
Identity.prototype.exportEncryptedWithWalletInfo = function(opts) {
var crypto = opts.cryptoUtil || cryptoUtil;
var key = crypto.kdf(this.password);
return crypto.encrypt(key, this.exportWithWalletInfo(opts));
return crypto.encrypt(this.password, this.exportWithWalletInfo(opts));
};
Identity.prototype.exportWithWalletInfo = function(opts) {
@ -287,11 +286,8 @@ Identity.prototype.close = function(cb) {
* @return {Wallet}
*/
Identity.prototype.importEncryptedWallet = function(cypherText, password, opts, cb) {
var crypto = opts.cryptoUtil || cryptoUtil;
// TODO set iter and salt using config.js
var key = crypto.kdf(password);
var obj = crypto.decrypt(key, cypherText);
var obj = crypto.decrypt(password, cypherText);
if (!obj) return cb(new Error('Could not decrypt'));
try {
obj = JSON.parse(obj);
@ -344,8 +340,12 @@ 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));
var str = crypto.decrypt(password, str);
if (!str) {
return cb('BADSTR');
}
return Identity.importFromFullJson(str, password, opts, cb);
};
Identity.importFromFullJson = function(str, password, opts, cb) {
@ -354,18 +354,21 @@ Identity.importFromFullJson = function(str, password, opts, cb) {
try {
json = JSON.parse(str);
} catch (e) {
return cb('Unable to retrieve json from string', str);
return cb('BADSTR: Unable to retrieve json from string', str);
}
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

@ -2967,8 +2967,7 @@ Wallet.prototype.getTransactionHistory = function(cb) {
Wallet.prototype.exportEncrypted = function(password, opts) {
opts = opts || {};
var crypto = opts.cryptoUtil || cryptoUtil;
var key = crypto.kdf(password);
return crypto.encrypt(key, this.toObj());
return crypto.encrypt(password, this.toObj());
};
module.exports = Wallet;

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
@ -109,7 +112,6 @@ angular
$location.path('unsupported');
} else {
if (!$rootScope.iden && next.logged) {
console.log('not logged... redirecting')
$idle.unwatch();
$location.path('/');
}

View file

@ -4,9 +4,10 @@
var sjcl = require('sjcl');
var log = require('../log.js');
var _ = require('lodash');
var config = require('../../config');
var defaultSalt = 'mjuBtGybi/4=';
var defaultIterations = 100;
var defaultSalt = (config && config.passphraseConfig && config.passphraseConfig.storageSalt) || 'mjuBtGybi/4=';
var defaultIterations = (config && config.passphraseConfig && config.passphraseConfig.iterations) || 1000;
module.exports = {
@ -50,6 +51,8 @@ module.exports = {
if (!_.isString(message)) {
message = JSON.stringify(message);
}
sjcl.json.defaults.salt = defaultSalt;
sjcl.json.defaults.iter = defaultIterations;
return sjcl.encrypt(key, message);
},

View file

@ -46,7 +46,6 @@ module.exports = function(config) {
'js/log.js',
'js/routes.js',
'js/services/*.js',
'js/util/*.js',
'js/directives.js',
'js/filters.js',
'js/controllers/*.js',

View file

@ -1,4 +1,3 @@
var compat = require('../js/models/Compatibility');
describe('Compatibility', function() {
@ -9,12 +8,12 @@ describe('Compatibility', function() {
should.not.exist(wo);
});
it('should generate passphrases acording to old algorightm', function() {
it('should generate passphrases acording to old algorithm', function() {
var passphrase = compat.kdf(legacyPassword1);
passphrase.should.equal(legacyPassphrase1);
});
it('should be able to decrypt an old backup', function() {
var str = compat.importLegacy(encryptedLegacy1, legacyPassword1);
@ -45,8 +44,8 @@ describe('Compatibility', function() {
var iden = sinon.stub();
iden.importWalletFromObj = sinon.stub().yields(null);
compat.importEncryptedWallet(iden, encryptedLegacy1, legacyPassword1, {}, function(err){
var s = iden.importWalletFromObj;
compat.importEncryptedWallet(iden, encryptedLegacy1, legacyPassword1, {}, function(err) {
var s = iden.importWalletFromObj;
s.getCall(0).args[0].opts.id.should.equal('48ba2f1ffdfe9708');
done();
});

View file

@ -260,8 +260,7 @@ describe('Identity model', function() {
sinon.stub(iden, 'importWalletFromObj').yields(null);
iden.importEncryptedWallet(123, 'password', opts, function(err) {
should.not.exist(err);
fakeCrypto.kdf.getCall(0).args[0].should.equal('password');
fakeCrypto.decrypt.getCall(0).args[0].should.equal('passphrase');
fakeCrypto.decrypt.getCall(0).args[0].should.equal('password');
fakeCrypto.decrypt.getCall(0).args[1].should.equal(123);
done();
});

View file

@ -58,12 +58,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>