refactor formatUserIds

This commit is contained in:
Sanjana Rajan 2017-07-24 18:33:46 +02:00
parent ea1b0a1088
commit fdcfcf3a54

View File

@ -98,8 +98,9 @@ export function destroyWorker() {
* @static * @static
*/ */
export function generateKey({ userIds=[], passphrase, numBits=2048, unlocked=false, curve=null, material=null } = {}) { export function generateKey({ userIds=[], passphrase, numBits=2048, unlocked=false, keyExpirationTime=0, curve=""} = {}) {
const options = formatUserIds({ userIds, passphrase, numBits, unlocked, curve, material }); userIds = formatUserIds(userIds);
const options = {userIds, passphrase, numBits, unlocked, keyExpirationTime, curve};
if (!util.getWebCryptoAll() && asyncProxy) { // use web worker if web crypto apis are not supported if (!util.getWebCryptoAll() && asyncProxy) { // use web worker if web crypto apis are not supported
return asyncProxy.delegate('generateKey', options); return asyncProxy.delegate('generateKey', options);
@ -125,7 +126,9 @@ export function generateKey({ userIds=[], passphrase, numBits=2048, unlocked=fal
* @static * @static
*/ */
export function reformatKey({ privateKey, userIds=[], passphrase="", unlocked=false, keyExpirationTime=0 } = {}) { export function reformatKey({ privateKey, userIds=[], passphrase="", unlocked=false, keyExpirationTime=0 } = {}) {
const options = formatUserIds({ privateKey, userIds, passphrase, unlocked, keyExpirationTime }); userIds = formatUserIds(userIds);
const options = {privateKey, userIds, passphrase, unlocked, keyExpirationTime};
if (asyncProxy) { if (asyncProxy) {
return asyncProxy.delegate('reformatKey', options); return asyncProxy.delegate('reformatKey', options);
@ -455,18 +458,12 @@ function checkCleartextOrMessage(message) {
/** /**
* Format user ids for internal use. * Format user ids for internal use.
*/ */
function formatUserIds(options) { function formatUserIds(userIds) {
if (!options.curve) { if (!userIds) {
delete options.curve; return userIds;
} }
if (!options.material) { userIds = toArray(userIds); // normalize to array
delete options.material; userIds = userIds.map(id => {
}
if (!options.userIds) {
return options;
}
options.userIds = toArray(options.userIds); // normalize to array
options.userIds = options.userIds.map(id => {
if (util.isString(id) && !util.isUserId(id)) { if (util.isString(id) && !util.isUserId(id)) {
throw new Error('Invalid user id format'); throw new Error('Invalid user id format');
} }
@ -485,7 +482,7 @@ function formatUserIds(options) {
} }
return id.name + '<' + id.email + '>'; return id.name + '<' + id.email + '>';
}); });
return options; return userIds;
} }
/** /**