Fixed import profile and wallets

This commit is contained in:
Matias Pando 2014-11-07 17:35:32 -03:00
commit 73088bca2b
10 changed files with 37 additions and 104 deletions

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

@ -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,9 +225,6 @@ 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));
console.log('exportEncryptedWithWalletInfo');
return crypto.encrypt(this.password, this.exportWithWalletInfo(opts));
};
@ -290,10 +287,6 @@ Identity.prototype.close = function(cb) {
*/
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);
console.log('importEncryptedWallet');
var obj = crypto.decrypt(password, cypherText);
if (!obj) return cb(new Error('Could not decrypt'));
try {
@ -347,9 +340,7 @@ Identity.prototype.closeWallet = function(wallet, cb) {
Identity.importFromEncryptedFullJson = function(str, password, opts, cb) {
var crypto = opts.cryptoUtil || cryptoUtil;
//var key = crypto.kdf(password);
//var str = crypto.decrypt(key, str);
console.log('importFromEncryptedFullJson');
var str = crypto.decrypt(password, str);
if (!str) {
return cb('BADSTR');
@ -366,9 +357,6 @@ Identity.importFromFullJson = function(str, password, opts, cb) {
return cb('BADSTR: Unable to retrieve json from string', str);
}
// if (!_.isNumber(json.iterations))
// return cb('BADSTR: Missing iterations');
var email = json.email;
opts.email = email;

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

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