Creation of new profile on startup works.
This commit is contained in:
parent
ecaa13f6d4
commit
52b9a206c3
3 changed files with 67 additions and 62 deletions
|
|
@ -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) {
|
function _decryptUsingCryptoJS(str, key, iv) {
|
||||||
var plaintext = CryptoJS.AES.decrypt(str, key, { iv: iv});
|
$log.debug('decrypt() str: ' + str);
|
||||||
return plaintext;
|
$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) {
|
function _encryptUsingCryptoJS(str, key) {
|
||||||
|
$log.debug('encrypt() str: ' + str);
|
||||||
var iv = CryptoJS.lib.WordArray.random(16);
|
var iv = CryptoJS.lib.WordArray.random(16);
|
||||||
|
|
||||||
|
$log.debug('Encrypting profile: ', JSON.stringify(str));
|
||||||
|
|
||||||
var cipherParams = CryptoJS.AES.encrypt(str, key, { iv: iv });
|
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 {
|
return {
|
||||||
ciphertext: cipherParams.ciphertext.toString(CryptoJS.enc.Base64),
|
ciphertext: cipherParams.ciphertext.toString(CryptoJS.enc.Base64),
|
||||||
|
|
@ -87,6 +124,7 @@ angular.module('copayApp.services').factory('encryptionService', function($log,
|
||||||
};
|
};
|
||||||
|
|
||||||
root.encrypt = function(str, cb) {
|
root.encrypt = function(str, cb) {
|
||||||
|
$log.debug('encrypt()', JSON.stringify('str'));
|
||||||
$log.debug('*** crypto exists: ' + !!crypto);
|
$log.debug('*** crypto exists: ' + !!crypto);
|
||||||
$log.debug('*** CryptoJS exists: ' + !!CryptoJS);
|
$log.debug('*** CryptoJS exists: ' + !!CryptoJS);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,11 +35,11 @@
|
||||||
|
|
||||||
var encryptedData = jsonObj.encryptedData;
|
var encryptedData = jsonObj.encryptedData;
|
||||||
// extract ciphertext from json object, and create cipher params object
|
// extract ciphertext from json object, and create cipher params object
|
||||||
var ciphertext = CryptoJS.enc.Base64.parse(encryptedData.ciphertext)
|
//var ciphertext = CryptoJS.enc.Base64.parse(encryptedData.ciphertext)
|
||||||
var iv = CryptoJS.enc.Hex.parse(encryptedData.iv);
|
//var iv = CryptoJS.enc.Hex.parse(encryptedData.iv);
|
||||||
|
var ciphertext = encryptedData.ciphertext;
|
||||||
// TODO: Need to convert iv into WordArray?
|
var iv = encryptedData.iv;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ciphertext: ciphertext,
|
ciphertext: ciphertext,
|
||||||
opts: {
|
opts: {
|
||||||
|
|
|
||||||
|
|
@ -121,11 +121,22 @@ angular.module('copayApp.services')
|
||||||
|
|
||||||
root.storeProfile = function(profile, cb) {
|
root.storeProfile = function(profile, cb) {
|
||||||
var profileString = profile.toObj();
|
var profileString = profile.toObj();
|
||||||
//if (platformInfo.isNW) {
|
encryptionService.encrypt(profileString, function onProfileEncrypted(encryptionErr, encryptedProfile){
|
||||||
storage.set('profile', profileString, cb);
|
if (encryptionErr) {
|
||||||
//} else {
|
$log.error('Failed to encrypt profile.', enctryptionErr);
|
||||||
// secureStorageService.set('profile', profileString, cb);
|
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);
|
var isEncrypted = jsonEncryptionService.isEncrypted(profileStr);
|
||||||
if (isEncrypted) {
|
if (isEncrypted) {
|
||||||
$log.debug('profile was encrypted.');
|
$log.debug('profile was encrypted.');
|
||||||
|
$log.debug('profileStr: ', profileStr);
|
||||||
|
|
||||||
var encryptedProfileObject;
|
var encryptedProfileObject;
|
||||||
try {
|
try {
|
||||||
|
|
@ -274,6 +286,8 @@ angular.module('copayApp.services')
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$log.debug('profileStr after JSON: ', JSON.stringify(encryptedProfileObject));
|
||||||
|
|
||||||
encryptionService.decrypt(
|
encryptionService.decrypt(
|
||||||
encryptedProfileObject.ciphertext,
|
encryptedProfileObject.ciphertext,
|
||||||
encryptedProfileObject.opts,
|
encryptedProfileObject.opts,
|
||||||
|
|
@ -284,6 +298,8 @@ angular.module('copayApp.services')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$log.debug('Decrypted profile:', JSON.stringify(decryptedProfile));
|
||||||
|
|
||||||
var profileObj = Profile.fromString(decryptedProfile);
|
var profileObj = Profile.fromString(decryptedProfile);
|
||||||
cb(null, profileObj);
|
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) {
|
root.setFeedbackInfo = function(feedbackValues, cb) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue