Fixes import for 0.6.4

This commit is contained in:
ssotomayor 2014-10-28 15:15:42 -03:00 committed by Esteban Ordano
commit 0fb6e3ea15
4 changed files with 76 additions and 7 deletions

View file

@ -4,8 +4,13 @@ var Identity = require('./Identity');
var Wallet = require('./Wallet');
var cryptoUtils = require('../util/crypto');
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
var sjcl = require('../../lib/sjcl');
var preconditions = require('preconditions').instance();
var _ = require('lodash');
var Compatibility = {};
Compatibility.iterations = 100;
Compatibility.salt = 'mjuBtGybi/4=';
/**
* Reads from localstorage wallets saved previously to 0.8
@ -41,10 +46,9 @@ Compatibility._getWalletIds = function(cb) {
* @returns {Object}
*/
Compatibility.importLegacy = function(encryptedWallet, passphrase) {
passphrase = this.kdf(passphrase);
var ret = Compatibility._decrypt(encryptedWallet, passphrase);
if (!ret) return null;
ret = ret.toString(CryptoJS.enc.Utf8);
ret = JSON.parse(ret);
return ret;
};
@ -183,4 +187,47 @@ Compatibility.readWalletPre8 = function(walletId, password, cb) {
}
};
Compatibility.importEncryptedWallet = function(identity, cypherText, password, opts, cb) {
var crypto = opts.cryptoUtil || cryptoUtils;
var key = crypto.kdf(password);
var obj = crypto.decrypt(key, cypherText);
if (!obj) {
console.warn("Could not decrypt, trying legacy..");
obj = Compatibility.importLegacy(cypherText, password);
if (!obj) {
return cb(new Error('Could not decrypt'))
}
};
try {
obj = JSON.parse(obj);
} catch (e) {
return cb(new Error('Could not read encrypted wallet'));
}
return identity.importWalletFromObj(obj, opts, cb);
};
/**
* @desc Generate a WordArray expanding a password
*
* @param {string} password - the password to expand
* @returns WordArray 512 bits with the expanded key generated from password
*/
Compatibility.kdf = function(password) {
var hash = sjcl.hash.sha256.hash(sjcl.hash.sha256.hash(password));
var salt = sjcl.codec.base64.toBits(this.salt);
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) {
return new sjcl.misc.hmac(key, sjcl.hash.sha1)
} : null
))
};
var key512 = crypto2(hash, salt, this.iterations, 64, 'sha1');
var sbase64 = sjcl.codec.base64.fromBits(sjcl.codec.hex.toBits(key512));
return sbase64;
};
module.exports = Compatibility;