copay/js/models/Compatibility.js

257 lines
6.3 KiB
JavaScript
Raw Normal View History

2014-10-27 17:05:22 -07:00
'use strict';
2014-10-27 18:15:23 -07:00
var Identity = require('./Identity');
var Wallet = require('./Wallet');
var cryptoUtils = require('../util/crypto');
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
2014-10-28 11:15:42 -07:00
var sjcl = require('../../lib/sjcl');
2014-12-02 06:17:03 -08:00
var log = require('../util/log');
2014-10-28 11:15:42 -07:00
var preconditions = require('preconditions').instance();
var _ = require('lodash');
2014-10-27 18:15:23 -07:00
var Compatibility = {};
2014-10-28 11:15:42 -07:00
Compatibility.iterations = 100;
Compatibility.salt = 'mjuBtGybi/4=';
2014-10-27 18:15:23 -07:00
/**
* Reads from localstorage wallets saved previously to 0.8
*/
Compatibility._getWalletIds = function(cb) {
preconditions.checkArgument(cb);
var walletIds = [];
var uniq = {};
2014-10-30 12:13:56 -07:00
var key;
2014-10-27 18:15:23 -07:00
for (key in localStorage) {
var split = key.split('::');
if (split.length == 2) {
var walletId = split[0];
2014-10-29 04:34:07 -07:00
if (!walletId || walletId === 'nameFor' || walletId === 'lock' || walletId === 'wallet') {
2014-10-27 18:15:23 -07:00
continue;
}
2014-10-27 17:05:22 -07:00
2014-10-27 18:15:23 -07:00
if (typeof uniq[walletId] === 'undefined') {
walletIds.push(walletId);
uniq[walletId] = 1;
}
}
}
return cb(walletIds);
};
2014-10-27 17:05:22 -07:00
/**
* @param {string} encryptedWallet - base64-encoded encrypted wallet
2014-10-29 04:34:07 -07:00
* @param {string} password
* @returns {Object}
*/
2014-10-29 04:34:07 -07:00
Compatibility.importLegacy = function(encryptedWallet, password) {
var passphrase = this.kdf(password);
var ret = Compatibility._decrypt(encryptedWallet, passphrase);
2014-11-07 12:35:32 -08:00
if (!ret) return null;
return ret;
};
2014-10-27 18:15:23 -07:00
/**
* Decrypts using the CryptoJS library (unknown encryption schema)
*
* Don't use CryptoJS to encrypt. This still exists for compatibility reasons only.
*/
Compatibility._decrypt = function(base64, passphrase) {
var decryptedStr = null;
try {
var decrypted = CryptoJS.AES.decrypt(base64, passphrase);
if (decrypted)
decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
} catch (e) {
// Error while decrypting
return null;
}
return decryptedStr;
};
2014-10-27 17:05:22 -07:00
2014-10-27 18:15:23 -07:00
/**
* Reads an item from localstorage, decrypts it with passphrase
*/
Compatibility._read = function(k, passphrase, cb) {
preconditions.checkArgument(cb);
2014-10-27 17:05:22 -07:00
2014-10-27 18:15:23 -07:00
var ret = localStorage.getItem(k);
if (!ret) return cb(null);
var ret = self._decrypt(ret, passphrase);
if (!ret) return cb(null);
2014-10-27 17:05:22 -07:00
2014-10-27 18:15:23 -07:00
ret = ret.toString(CryptoJS.enc.Utf8);
ret = JSON.parse(ret);
return ret;
};
2014-10-27 17:05:22 -07:00
2014-10-27 18:15:23 -07:00
Compatibility.getWallets_Old = function(cb) {
preconditions.checkArgument(cb);
2014-10-27 17:05:22 -07:00
2014-10-27 18:15:23 -07:00
var wallets = [];
var self = this;
2014-10-27 17:05:22 -07:00
2014-10-27 18:15:23 -07:00
this._getWalletIds(function(ids) {
if (!ids.length) {
return cb([]);
2014-10-27 17:05:22 -07:00
}
2014-10-27 18:15:23 -07:00
_.each(ids, function(id) {
var name = localStorage.getItem('nameFor::' + id);
if (name) {
wallets.push({
id: id,
name: name,
});
2014-10-27 17:05:22 -07:00
}
});
2014-10-27 18:15:23 -07:00
return cb(wallets);
2014-10-27 17:05:22 -07:00
});
};
2014-10-27 18:15:23 -07:00
Compatibility.getWallets2 = function(cb) {
var self = this;
var re = /wallet::([^_]+)(_?(.*))/;
2014-10-30 12:13:56 -07:00
var va = /^{+/;
2014-10-27 18:15:23 -07:00
2014-10-30 12:13:56 -07:00
var key;
2014-10-27 18:15:23 -07:00
var keys = [];
for (key in localStorage) {
keys.push(key);
}
var wallets = _.compact(_.map(keys, function(key) {
if (key.indexOf('wallet::') !== 0)
return null;
var match = key.match(re);
2014-10-30 12:13:56 -07:00
var matchValue = localStorage[key].match(va);
2014-10-27 18:15:23 -07:00
if (match.length != 4)
return null;
2014-10-30 12:13:56 -07:00
if (matchValue)
return null;
2014-10-27 18:15:23 -07:00
return {
id: match[1],
name: match[3] ? match[3] : undefined,
2014-10-30 12:13:56 -07:00
value: localStorage[key]
2014-10-27 18:15:23 -07:00
};
}));
return cb(wallets);
2014-10-27 17:05:22 -07:00
};
2014-10-27 18:15:23 -07:00
/**
* Lists all wallets in localstorage
*/
2014-10-29 04:34:07 -07:00
Compatibility.listWalletsPre8 = function(cb) {
2014-10-27 18:15:23 -07:00
var self = this;
self.getWallets2(function(wallets) {
self.getWallets_Old(function(wallets2) {
var ids = _.pluck(wallets, 'id');
_.each(wallets2, function(w) {
if (!_.contains(ids, w.id))
wallets.push(w);
});
return cb(wallets);
});
})
};
2014-10-27 17:05:22 -07:00
2014-10-27 18:15:23 -07:00
/**
* Retrieves a wallet that predates the 0.8 release
*/
Compatibility.readWalletPre8 = function(walletId, password, cb) {
var self = this;
var passphrase = cryptoUtils.kdf(password);
var obj = {};
2014-10-31 11:27:03 -07:00
var key;
2014-10-27 18:15:23 -07:00
for (key in localStorage) {
if (key.indexOf('wallet::' + walletId) !== -1) {
var ret = self._read(localStorage.getItem(key), passphrase);
if (err) return cb(err);
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
obj[p] = ret[p];
});
if (!_.any(_.values(obj)))
return cb(new Error('Wallet not found'));
var w, err;
obj.id = walletId;
try {
w = self.fromObj(obj);
} catch (e) {
if (e && e.message && e.message.indexOf('MISSOPTS')) {
err = new Error('Could not read: ' + walletId);
} else {
err = e;
}
w = null;
}
return cb(err, w);
}
}
2014-10-27 17:05:22 -07:00
};
2014-10-28 11:15:42 -07:00
Compatibility.importEncryptedWallet = function(identity, cypherText, password, opts, cb) {
2014-10-30 12:13:56 -07:00
var crypto = (opts && opts.cryptoUtil) || cryptoUtils;
2014-11-07 12:35:32 -08:00
var obj = crypto.decrypt(password, cypherText);
if (!obj) {
// 0.7.3 broken KDF
log.debug('Trying legacy encryption 0.7.2...');
var passphrase = crypto.kdf(password, 'mjuBtGybi/4=', 100);
obj = crypto.decrypt(passphrase, cypherText);
}
2014-10-28 11:15:42 -07:00
if (!obj) {
2014-10-29 04:34:07 -07:00
log.info("Could not decrypt, trying legacy..");
2014-10-28 11:15:42 -07:00
obj = Compatibility.importLegacy(cypherText, password);
};
if (!obj) {
return cb('Could not decrypt', null);
}
2014-10-28 11:15:42 -07:00
try {
obj = JSON.parse(obj);
} catch (e) {
2014-11-07 12:35:32 -08:00
return cb('Could not read encrypted wallet', null);
2014-10-28 11:15:42 -07:00
}
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,
2014-10-29 04:34:07 -07:00
alg == 'sha1' ? function(key) {
2014-10-28 11:15:42 -07:00
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;
};
2014-10-30 12:13:56 -07:00
Compatibility.deleteOldWallet = function(walletObj) {
2014-12-15 05:48:41 -08:00
console.log('[Compatibility.js:249]',walletObj); //TODO
2014-11-07 12:35:32 -08:00
localStorage.removeItem('wallet::' + walletObj.id + '_' + walletObj.name);
2014-10-30 12:13:56 -07:00
log.info('Old wallet ' + walletObj.name + ' deleted: ' + walletObj.id);
};
2014-10-28 11:15:42 -07:00
2014-10-27 17:05:22 -07:00
module.exports = Compatibility;