Creation of new profile on startup works.

This commit is contained in:
Brendon Duncan 2018-06-28 20:31:22 +12:00
commit 52b9a206c3
3 changed files with 67 additions and 62 deletions

View file

@ -53,17 +53,54 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
});
};
/**
*
* @param {*} str
* @param {CryptoJS.WordArray} key
* @param {string, hex} iv
*/
function _decryptUsingCryptoJS(str, key, iv) {
var plaintext = CryptoJS.AES.decrypt(str, key, { iv: iv});
return plaintext;
$log.debug('decrypt() str: ' + str);
$log.debug('decrypt() using iv:' + iv + ', key: ' + JSON.stringify(key));
var ivWords = CryptoJS.enc.Hex.parse(iv);
var plaintext = CryptoJS.AES.decrypt(str, key, { iv: ivWords });
$log.debug('plaintext', JSON.stringify(plaintext));
var plaintextWords = CryptoJS.lib.WordArray.create();
plaintextWords.init(plaintext.words, plaintext.sigBytes);
$log.debug('plaintextWords', JSON.stringify(plaintextWords));
var plaintextString = plaintextWords.toString(CryptoJS.enc.Utf8);
$log.debug('plaintextString: ', JSON.stringify(plaintextString));
return plaintextString;
}
function _encryptUsingCryptoJS(str, key) {
$log.debug('encrypt() str: ' + str);
var iv = CryptoJS.lib.WordArray.random(16);
$log.debug('Encrypting profile: ', JSON.stringify(str));
var cipherParams = CryptoJS.AES.encrypt(str, key, { iv: iv });
$log.debug('cipherText: ' + cipherParams.ciphertext);
var ciphertext = cipherParams.ciphertext.toString(CryptoJS.enc.Base64);
var iv = iv.toString(CryptoJS.enc.Hex);
$log.debug('ciphertext: ' + ciphertext);
$log.debug('iv: ' + iv);
root.decrypt(ciphertext, {iv: iv}, function onDecryptionTest(err, decrypted){
if (err) {
$log.error('Failed to decrypt encrypted.', err);
} else {
$log.debug('Freshly decrypted:', JSON.stringify(decrypted));
}
});
return {
ciphertext: cipherParams.ciphertext.toString(CryptoJS.enc.Base64),
@ -87,6 +124,7 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
};
root.encrypt = function(str, cb) {
$log.debug('encrypt()', JSON.stringify('str'));
$log.debug('*** crypto exists: ' + !!crypto);
$log.debug('*** CryptoJS exists: ' + !!CryptoJS);

View file

@ -35,11 +35,11 @@
var encryptedData = jsonObj.encryptedData;
// extract ciphertext from json object, and create cipher params object
var ciphertext = CryptoJS.enc.Base64.parse(encryptedData.ciphertext)
var iv = CryptoJS.enc.Hex.parse(encryptedData.iv);
// TODO: Need to convert iv into WordArray?
//var ciphertext = CryptoJS.enc.Base64.parse(encryptedData.ciphertext)
//var iv = CryptoJS.enc.Hex.parse(encryptedData.iv);
var ciphertext = encryptedData.ciphertext;
var iv = encryptedData.iv;
return {
ciphertext: ciphertext,
opts: {

View file

@ -121,11 +121,22 @@ angular.module('copayApp.services')
root.storeProfile = function(profile, cb) {
var profileString = profile.toObj();
//if (platformInfo.isNW) {
storage.set('profile', profileString, cb);
//} else {
// secureStorageService.set('profile', profileString, cb);
//}
encryptionService.encrypt(profileString, function onProfileEncrypted(encryptionErr, encryptedProfile){
if (encryptionErr) {
$log.error('Failed to encrypt profile.', enctryptionErr);
cb(encryptionErr, null);
return;
}
$log.debug('storing profile ciphertext:', JSON.stringify(encryptedProfile.ciphertext));
var persistentProfileStr = jsonEncryptionService.stringify(
encryptedProfile.ciphertext,
encryptedProfile.opts
);
storage.set('profile', persistentProfileStr, cb);
});
};
/**
@ -264,6 +275,7 @@ angular.module('copayApp.services')
var isEncrypted = jsonEncryptionService.isEncrypted(profileStr);
if (isEncrypted) {
$log.debug('profile was encrypted.');
$log.debug('profileStr: ', profileStr);
var encryptedProfileObject;
try {
@ -274,6 +286,8 @@ angular.module('copayApp.services')
return;
}
$log.debug('profileStr after JSON: ', JSON.stringify(encryptedProfileObject));
encryptionService.decrypt(
encryptedProfileObject.ciphertext,
encryptedProfileObject.opts,
@ -284,6 +298,8 @@ angular.module('copayApp.services')
return
}
$log.debug('Decrypted profile:', JSON.stringify(decryptedProfile));
var profileObj = Profile.fromString(decryptedProfile);
cb(null, profileObj);
});
@ -300,56 +316,7 @@ angular.module('copayApp.services')
});
}
});
/*
if (platformInfo.isNW) {
storage.get('profile', function(getErr, getStr) {
_onOldProfileRetrieved(getErr, getStr, cb);
});
return
}
secureStorageService.get('profile', function(secureErr, secureStr) {
var secureProfile;
var oldProfile;
if (secureErr) {
return cb(secureErr, null);
}
if (secureStr) {
try {
secureProfile = Profile.fromString(secureStr);
$log.debug('profile: ' + JSON.stringify(secureProfile));
} catch (e) {
$log.error(e);
return cb(e, null);
}
}
storage.get('profile', function(getErr, getStr) {
_onOldProfileRetrieved(getErr, getStr, function(oldErr, oldProfile){
if (oldErr) {
return cb(oldErr, null);
}
if (!oldProfile) {
if (secureProfile) {
return cb(null, secureProfile);
} else {
// No profiles found. No errors either.
return cb(null, null);
}
}
_migrateProfiles(oldProfile, secureProfile, cb);
});
});
});
*/
};
root.setFeedbackInfo = function(feedbackValues, cb) {