MCPcopy Index your code
hub / github.com/mailvelope/mailvelope / syncPublicKeys

Function syncPublicKeys

src/modules/keyring.js:591–632  ·  view source on GitHub ↗
({keyring, keyIds, allKeyrings = false, keyringId})

Source from the content-addressed store, hash-verified

589 * @param {Stringt} [keyringId] - the leading keyring of a scenario
590 */
591export async function syncPublicKeys({keyring, keyIds, allKeyrings = false, keyringId}) {
592 await keyringInitialized;
593 let srcKeyrings;
594 if (!keyring) {
595 keyring = await getById(keyringId);
596 }
597 keyIds = toArray(keyIds);
598 if (!keyIds.length) {
599 // nothing to sync
600 return;
601 }
602 // get all relevant source keyrings
603 if (allKeyrings || !keyringId) {
604 srcKeyrings = await getAll();
605 } else {
606 srcKeyrings = getPreferredKeyringQueue(keyringId);
607 }
608 for (let keyId of keyIds) {
609 keyId = typeof keyId === 'string' ? keyId : keyId.toHex();
610 // find newest key in all source keyrings
611 let lastModified = null;
612 for (const srcKeyring of srcKeyrings) {
613 let key = srcKeyring.keystore.getKeysForId(keyId, true);
614 if (!key) {
615 continue;
616 }
617 key = key[0];
618 const lastModifiedDate = getLastModifiedDate(key);
619 if (!lastModified || lastModifiedDate > lastModified.date || lastModifiedDate.valueOf() === lastModified.date.valueOf() && srcKeyring.id === keyring.id) {
620 lastModified = {date: lastModifiedDate, key, srcKeyring};
621 }
622 }
623 if (lastModified && lastModified.srcKeyring.id !== keyring.id) {
624 // there is a newer key available that needs to be imported into the destination keyring
625 try {
626 await keyring.importKeys([{armored: toPublic(lastModified.key).armor(), type: 'public'}]);
627 } catch (e) {
628 console.log('Key import error during sync process', e);
629 }
630 }
631 }
632}
633
634/**
635 * Get preferred keyring ID

Callers 8

keyring-test.jsFile · 0.90
decryptMessageFunction · 0.90
encryptMessageFunction · 0.90
verifyMessageFunction · 0.90
verifyDetachedSignatureFunction · 0.90
encryptFileFunction · 0.90
decryptFileFunction · 0.90

Calls 9

toArrayFunction · 0.90
getLastModifiedDateFunction · 0.90
toPublicFunction · 0.90
getByIdFunction · 0.85
getPreferredKeyringQueueFunction · 0.85
toHexMethod · 0.80
getKeysForIdMethod · 0.80
getAllFunction · 0.70
importKeysMethod · 0.45

Tested by

no test coverage detected