From 45226265440c6061c1eda698a5bbe7dd45e94552 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 29 Oct 2014 08:34:07 -0300 Subject: [PATCH] fix and add more tests to Compat --- js/models/Compatibility.js | 18 ++++++++---------- js/util/crypto.js | 2 +- test/Compatibility.js | 27 +++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/js/models/Compatibility.js b/js/models/Compatibility.js index 4a1d292a7..c455d4dd0 100644 --- a/js/models/Compatibility.js +++ b/js/models/Compatibility.js @@ -5,6 +5,7 @@ var Wallet = require('./Wallet'); var cryptoUtils = require('../util/crypto'); var CryptoJS = require('node-cryptojs-aes').CryptoJS; var sjcl = require('../../lib/sjcl'); +var log = require('../log'); var preconditions = require('preconditions').instance(); var _ = require('lodash'); @@ -24,10 +25,7 @@ Compatibility._getWalletIds = function(cb) { if (split.length == 2) { var walletId = split[0]; - if (!walletId - || walletId === 'nameFor' - || walletId === 'lock' - || walletId === 'wallet') { + if (!walletId || walletId === 'nameFor' || walletId === 'lock' || walletId === 'wallet') { continue; } @@ -42,11 +40,11 @@ Compatibility._getWalletIds = function(cb) { /** * @param {string} encryptedWallet - base64-encoded encrypted wallet - * @param {string} passphrase - base64-encoded passphrase + * @param {string} password * @returns {Object} */ -Compatibility.importLegacy = function(encryptedWallet, passphrase) { - passphrase = this.kdf(passphrase); +Compatibility.importLegacy = function(encryptedWallet, password) { + var passphrase = this.kdf(password); var ret = Compatibility._decrypt(encryptedWallet, passphrase); if (!ret) return null; return ret; @@ -136,7 +134,7 @@ Compatibility.getWallets2 = function(cb) { /** * Lists all wallets in localstorage */ -Compatibility.listWalletsPre8 = function (cb) { +Compatibility.listWalletsPre8 = function(cb) { var self = this; self.getWallets2(function(wallets) { self.getWallets_Old(function(wallets2) { @@ -192,7 +190,7 @@ Compatibility.importEncryptedWallet = function(identity, cypherText, password, o var key = crypto.kdf(password); var obj = crypto.decrypt(key, cypherText); if (!obj) { - console.warn("Could not decrypt, trying legacy.."); + log.info("Could not decrypt, trying legacy.."); obj = Compatibility.importLegacy(cypherText, password); if (!obj) { return cb(new Error('Could not decrypt')) @@ -218,7 +216,7 @@ Compatibility.kdf = function(password) { var crypto2 = function(key, salt, iterations, length, alg) { return sjcl.codec.hex.fromBits(sjcl.misc.pbkdf2(key, salt, iterations, length * 8, - alg == 'sha1' ? function(key) { + alg == 'sha1' ? function(key) { return new sjcl.misc.hmac(key, sjcl.hash.sha1) } : null )) diff --git a/js/util/crypto.js b/js/util/crypto.js index e5476082a..46da407df 100644 --- a/js/util/crypto.js +++ b/js/util/crypto.js @@ -61,7 +61,7 @@ module.exports = { try { return sjcl.decrypt(key, cyphertext); } catch (e) { - log.error('Decryption failed due to error: ' + e.message); + log.info('Decryption failed due to error: ' + e.message); return null; } } diff --git a/test/Compatibility.js b/test/Compatibility.js index 77c56b094..f90035a07 100644 --- a/test/Compatibility.js +++ b/test/Compatibility.js @@ -9,10 +9,17 @@ describe('Compatibility', function() { should.not.exist(wo); }); + it('should generate passphrases acording to old algorightm', function() { + var passphrase = compat.kdf(legacyPassword1); + passphrase.should.equal(legacyPassphrase1); + }); + + + it('should be able to decrypt an old backup', function() { - var wo = compat.importLegacy(encryptedLegacy1, legacyPassword1); - should.exist(wo); - console.log(wo); + var str = compat.importLegacy(encryptedLegacy1, legacyPassword1); + should.exist(str); + var wo = JSON.parse(str); wo.opts.id.should.equal('48ba2f1ffdfe9708'); wo.opts.spendUnconfirmed.should.equal(true); wo.opts.requiredCopayers.should.equal(1); @@ -33,8 +40,20 @@ describe('Compatibility', function() { wo.privateKey.extendedPrivateKeyString.should.equal('tprv8ZgxMBicQKsPfQCscb7CtJKzixxcVSyrCVcfr3WCFbtT8kYTzNubhjQ5R7AuYJgPCcSH4R8T34YVxeohKGhAB9wbB4eFBbQFjUpjGCqptHm'); wo.privateKey.networkName.should.equal('testnet'); }); + + it('should be able import an old backup', function(done) { + var iden = sinon.stub(); + iden.importWalletFromObj = sinon.stub().yields(null); + + compat.importEncryptedWallet(iden, encryptedLegacy1, legacyPassword1, {}, function(err){ + var s = iden.importWalletFromObj; + s.getCall(0).args[0].opts.id.should.equal('48ba2f1ffdfe9708'); + done(); + }); + }) }); }); -var legacyPassword1 = '1DUpLRbuVpgLkcEY8gY8iod/SmA7+OheGZJ9PtvmTlvNE0FkEWpCKW9STdzXYJqbn0wiAapE4ojHNYj2hjYYAQ=='; +var legacyPassword1 = '1'; +var legacyPassphrase1 = '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/xzJSVrniCn9be8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