({keyring, keyIds, allKeyrings = false, keyringId})
| 589 | * @param {Stringt} [keyringId] - the leading keyring of a scenario |
| 590 | */ |
| 591 | export 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 |
no test coverage detected