Fixes import & export with new encryption

This commit is contained in:
Esteban Ordano 2014-10-28 00:31:30 -03:00
commit 35bab383b0
12 changed files with 86 additions and 67 deletions

View file

@ -83,6 +83,10 @@ module.exports = function(grunt) {
config: {
files: ['config.js'],
tasks: ['shell:dev', 'concat:main']
},
test: {
files: ['test/**/*.js'],
tasks: ['mochaTest']
}
},
mochaTest: {

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('HeadController', function($scope, $rootScope, notification, controllerUtils) {
angular.module('copayApp.controllers').controller('HeadController', function($scope, $rootScope, $filter, notification, controllerUtils) {
$scope.username = $rootScope.iden ? $rootScope.iden.fullName || $rootScope.iden.email : 'undefined';
$scope.hoverMenu = false;

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('ImportController',
function($scope, $rootScope, $location, controllerUtils, notification, isMobile, Compatibility) {
function($scope, $rootScope, $location, controllerUtils, notification, isMobile, Compatibility, cryptoUtils) {
$rootScope.title = 'Import a backup';
$scope.importStatus = 'Importing wallet - Reading backup...';
@ -25,7 +25,7 @@ angular.module('copayApp.controllers').controller('ImportController',
if ($scope.skipTxProposals)
skipFields.push('txProposals');
$rootScope.iden.importWallet(encryptedObj, password, skipFields, function(err, w) {
$rootScope.iden.importEncryptedWallet(encryptedObj, password, skipFields, function(err, w) {
if (!w) {
$scope.loading = false;
notification.error('Error', err || 'Wrong password');

View file

@ -35,6 +35,19 @@ Compatibility._getWalletIds = function(cb) {
return cb(walletIds);
};
/**
* @param {string} encryptedWallet - base64-encoded encrypted wallet
* @param {string} passphrase - base64-encoded passphrase
* @returns {Object}
*/
Compatibility.importLegacy = function(encryptedWallet, passphrase) {
var ret = Compatibility._decrypt(encryptedWallet, passphrase);
if (!ret) return null;
ret = ret.toString(CryptoJS.enc.Utf8);
ret = JSON.parse(ret);
return ret;
};
/**
* Decrypts using the CryptoJS library (unknown encryption schema)
*

View file

@ -207,13 +207,20 @@ Identity.prototype.toObj = function() {
_.pick(this, 'version', 'fullName', 'password', 'email'));
};
Identity.prototype.exportWithWalletInfo = function() {
Identity.prototype.exportEncryptedWithWalletInfo = function(opts) {
var crypto = opts.cryptoUtil || cryptoUtil;
var key = crypto.kdf(this.password);
return crypto.encrypt(key, this.exportWithWalletInfo);
};
Identity.prototype.exportWithWalletInfo = function(opts) {
return _.extend({
wallets: _.map(this.wallets, function(wallet) {
return wallet.toObj();
})
},
_.pick(this, 'version', 'fullName', 'password', 'email'));
_.pick(this, 'version', 'fullName', 'password', 'email')
);
};
/**
@ -248,8 +255,8 @@ Identity.prototype.close = function(cb) {
};
/**
* @desc Imports a wallet from an encrypted base64 object
* @param {string} base64 - the base64 encoded object
* @desc Imports a wallet from an encrypted string
* @param {string} cypherText - the encrypted object
* @param {string} passphrase - passphrase to decrypt it
* @param {string[]} opts.skipFields - fields to ignore when importing
* @param {string[]} opts.salt -
@ -257,12 +264,25 @@ Identity.prototype.close = function(cb) {
* @param {string[]} opts.importFunction - for stubbing
* @return {Wallet}
*/
Identity.prototype.importWallet = function(base64, password, opts, 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);
if (!obj) return cb(new Error('Could not decrypt'));
try {
obj = JSON.parse(obj);
} catch (e) {
return cb(new Error('Could not decrypt'));
}
return this.importWalletFromObj(obj, opts, cb)
};
Identity.prototype.importWalletFromObj = function(obj, opts, cb) {
var self = this;
preconditions.checkArgument(password);
preconditions.checkArgument(cb);
var importFunction = opts.importWallet || Wallet.fromUntrustedObj;
var crypto = opts.cryptoUtil || cryptoUtil;
var readOpts = {
networkOpts: this.networkOpts,
@ -270,12 +290,6 @@ Identity.prototype.importWallet = function(base64, password, opts, cb) {
skipFields: opts.skipFields,
};
// TODO set iter and salt using config.js
var key = crypto.kdf(password);
var obj = crypto.decrypt(key, base64);
if (!obj) return cb(new Error('Could not decrypt'));
var w = importFunction(obj, readOpts);
if (!w) return cb(new Error('Could not decrypt'));
@ -303,6 +317,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));
};
Identity.importFromFullJson = function(str, password, opts, cb) {
preconditions.checkArgument(str);
var json;
@ -320,7 +340,7 @@ Identity.importFromFullJson = function(str, password, opts, cb) {
json.wallets = json.wallets || {};
async.map(json.wallets, function(walletData, callback) {
iden.importWallet(wstr, password, opts, function(err, w) {
iden.importEncryptedWallet(wstr, password, opts, function(err, w) {
if (err) return callback(err);
log.debug('Wallet ' + w.getId() + ' imported');
callback();

View file

@ -6,6 +6,7 @@ var preconditions = require('preconditions').singleton();
var inherits = require('inherits');
var events = require('events');
var async = require('async');
var cryptoUtil = require('../util/crypto');
var bitcore = require('bitcore');
var bignum = bitcore.Bignum;
@ -683,7 +684,6 @@ Wallet.prototype.getNetworkName = function() {
};
/**
* @desc
* @return {bool}
*/
Wallet.prototype.isTestnet = function() {
@ -2864,4 +2864,11 @@ 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());
};
module.exports = Wallet;

View file

@ -1,8 +1,9 @@
'use strict';
var BackupService = function(notification) {
var BackupService = function($rootScope, notification, cryptoUtils) {
this.$rootScope = $rootScope;
this.notifications = notification;
this.cryptoUtils = cryptoUtils;
};
BackupService.prototype.getCopayer = function(wallet) {
@ -40,11 +41,11 @@ BackupService.prototype._download = function(ew, walletName, filename) {
};
BackupService.prototype.walletEncrypted = function(wallet) {
return wallet.toEncryptedObj();
return wallet.exportEncrypted(this.$rootScope.iden.password);
}
BackupService.prototype.walletDownload = function(wallet) {
var ew = wallet.toEncryptedObj();
var ew = this.walletEncrypted(wallet);
var walletName = wallet.getName();
var copayerName = this.getCopayer(wallet);
var filename = (copayerName ? copayerName + '-' : '') + walletName + '-keybackup.json.aes';
@ -52,17 +53,14 @@ BackupService.prototype.walletDownload = function(wallet) {
};
BackupService.prototype.profileEncrypted = function(iden) {
return iden.toEncryptedObj();
return iden.exportEncryptedWithWalletInfo(iden.password);
}
BackupService.prototype.profileDownload = function(iden) {
var ew = iden.toEncryptedObj();
var name = iden.profile.getName();
var ew = this.profileEncrypted(iden);
var name = iden.fullName;
var filename = name + '-profile.json';
this._download(ew, name, filename)
};
angular.module('copayApp.services').service('backupService', BackupService);

View file

@ -0,0 +1,5 @@
'use strict';
angular.module('copayApp.services').value('Compatibility', function() {
return require('copay').Compatibility;
});

View file

@ -0,0 +1,5 @@
'use strict';
angular.module('copayApp.services').factory('cryptoUtils', function() {
return require('../util/crypto');
});

View file

@ -1,8 +1,7 @@
var Compatibility = require('../js/models/Compatibility');
var compat = require('../js/models/Compatibility');
describe('Compatibility', function() {
var compat = new Compatibility();
describe('#import', function() {
it('should not be able to decrypt with wrong password', function() {
@ -13,6 +12,7 @@ describe('Compatibility', function() {
it('should be able to decrypt an old backup', function() {
var wo = compat.importLegacy(encryptedLegacy1, legacyPassword1);
should.exist(wo);
console.log(wo);
wo.opts.id.should.equal('48ba2f1ffdfe9708');
wo.opts.spendUnconfirmed.should.equal(true);
wo.opts.requiredCopayers.should.equal(1);

View file

@ -249,7 +249,7 @@ describe('Identity model', function() {
var fakeCrypto = {
kdf: sinon.stub().returns('passphrase'),
decrypt: sinon.stub().returns({walletId:123}),
decrypt: sinon.stub().returns('{"walletId":123}'),
};
var opts = {
@ -258,7 +258,7 @@ describe('Identity model', function() {
};
Identity.create(args.params, function(err, iden) {
iden.importWallet(123,'password', opts, function(err){
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');

View file

@ -1,6 +1,5 @@
'use strict';
var _ = require('lodash');
var chai = chai || require('chai');
var sinon = sinon || require('sinon');
@ -19,7 +18,7 @@ describe('cryptoUtil', function() {
};
var pass = '123456';
var phrase = crypto.kdf(pass, null, test.salt, test.iterations);
var phrase = crypto.kdf(pass, test.salt, test.iterations);
phrase.should.equal(test.phraseBase64);
});
@ -32,43 +31,11 @@ describe('cryptoUtil', function() {
phraseBase64: legacyPassphrase,
};
var phrase = crypto.kdf(legacyPassword, null, test.salt, test.iterations);
var phrase = crypto.kdf(legacyPassword, test.salt, test.iterations);
phrase.should.equal(test.phraseBase64);
});
it('should be able to decrypt an old backup',function(){
var wo = crypto.decrypt(legacyPassword, encryptedLegacy1);
console.log('[cryptoUtil.js.43:wo:]',wo); //TODO
should.exist(wo);
wo.opts.id.should.equal('48ba2f1ffdfe9708');
wo.opts.spendUnconfirmed.should.equal(true);
wo.opts.requiredCopayers.should.equal(1);
wo.opts.totalCopayers.should.equal(1);
wo.opts.name.should.equal('pepe wallet');
wo.opts.version.should.equal('0.4.7');
wo.publicKeyRing.walletId.should.equal('48ba2f1ffdfe9708');
wo.publicKeyRing.networkName.should.equal('testnet');
wo.publicKeyRing.requiredCopayers.should.equal(1);
wo.publicKeyRing.totalCopayers.should.equal(1);
wo.publicKeyRing.indexes.length.should.equal(2);
JSON.stringify(wo.publicKeyRing.indexes[0]).should.equal('{"copayerIndex":2147483647,"changeIndex":0,"receiveIndex":1}');
JSON.stringify(wo.publicKeyRing.indexes[1]).should.equal('{"copayerIndex":0,"changeIndex":0,"receiveIndex":1}');
wo.publicKeyRing.copayersBackup.length.should.equal(1);
wo.publicKeyRing.copayersBackup[0].should.equal('0298f65b2694c55f9048bc05f10368242727c7f9d2065cbd788c3ecde1ec57f33f');
wo.publicKeyRing.copayersExtPubKeys.length.should.equal(1);
wo.publicKeyRing.copayersExtPubKeys[0].should.equal('tpubD9SGoP7CXsqSKTiQxCZSCpicDcophqnE4yuqjfw5M9tAR3fSjT9GDGwPEUFCN7SSmRKGDLZgKQePYFaLWyK32akeSan45TNTd8sgef9Ymh6');
wo.privateKey.extendedPrivateKeyString.should.equal('tprv8ZgxMBicQKsPfQCscb7CtJKzixxcVSyrCVcfr3WCFbtT8kYTzNubhjQ5R7AuYJgPCcSH4R8T34YVxeohKGhAB9wbB4eFBbQFjUpjGCqptHm');
wo.privateKey.networkName.should.equal('testnet');
});
});
var legacyPassword = '1';
var legacyPassphrase = '1DUpLRbuVpgLkcEY8gY8iod/SmA7+OheGZJ9PtvmTlvNE0FkEWpCKW9STdzXYJqbn0wiAapE4ojHNYj2hjYYAQ==';
var encryptedLegacy1 = 'U2FsdGVkX19yGM1uBAIzQa8Po/dvUicmxt1YyRk/S97PcZ6I6rHMp9dMagIrehg4Qd6JHn/ustmFHS7vmBYj0EBpf6rdXiQezaWnVAJS9/xYjAO36EFUbl+NmUanuwujAxgYdSP/sNssRLeInvExmZYW993EEclxkwL6YUyX66kKsxGQo2oWng0NreBJNhFmrbOEWeFje2PiWP57oUjKsurFzwpluAAarUTYSLud+nXeabC7opzOP5yqniWBMJz0Ou8gpNCWCMhG/P9F9ccVPY7juyd0Hf41FVse8nd2++axKB57+paozLdO+HRfV6zkMqC3h8gWY7LkS75j3bvqcTw9LhXmzE0Sz21n9yDnRpA4chiAvtwQvvBGgj1pFMKhNQU6Obac9ZwKYzUTgdDn3Uzg1UlDzgyOh9S89rbRTV84WB+hXwhuVluWzbNNYV3vXe5PFrocVktIrtS3xQh+k/7my4A6/gRRrzNYpKrUASJqDS/9u9WBkG35xD63J/qXjtG2M0YPwbI57BK1IK4K510b8V72lz5U2XQrIC4ldBwni1rpSavwCJV9xF6hUdOmNV8fZsVHP0NeN1PYlLkSb2QgfuoWnkcsJerwuFR7GZC/i6efrswtpO0wMEQr/J0CLbeXlHAru6xxjCBhWoJvZpMGw72zgnDLoyMNsEVglNhx/VlV9ZMYkkdaEYAxPOEIyZdQ5MS+2jEAlXf818n/xzJSVrniCn9be8EPePvkw35pivprvy09vbW4cKsWBKvgIyoT6A3OhUOCCS8E9cg0WAjjav2EymrbKmGWRHaiD+EoJqaDg6s20zhHn1YEa/YwvGGSB5+Hg8baLHD8ZASvxz4cFFAAVZrBUedRFgHzqwaMUlFXLgueivWUj7RXlIw6GuNhLoo1QkhZMacf23hrFxxQYvGBRw1hekBuDmcsGWljA28udBxBd5f9i+3gErttMLJ6IPaud590uvrxRIclu0Sz9R2EQX64YJxqDtLpMY0PjddSMu8vaDRpK9/ZSrnz/xrXsyabaafz4rE/ItFXjwFUFkvtmuauHTz6nmuKjVfxvNLNAiKb/gI7vQyUhnTbKIApe7XyJsjedNDtZqsPoJRIzdDmrZYxGStbAZ7HThqFJlSJ9NPNhH+E2jm3TwL5mwt0fFZ5h+p497lHMtIcKffESo7KNa2juSVNMDREk0NcyxGXGiVB2FWl4sLdvyhcsVq0I7tmW6OGZKRf8W49GCJXq6Ie69DJ9LB1DO67NV1jsYbsLx9uhE2yEmpWZ3jkoCV/Eas4grxt0CGN6EavzQ==';