1 import type { KeyReference, PrivateKeyReference } from '../worker/api.models';
2 import { CryptoProxy } from './proxy';
5 * Find the key that generated the given signature.
6 * If the signature is signed by multiple keys, only one matching key is returned.
7 * Either `binarySignature` or `armoredSignature` must be provided.
8 * @param keys - keys to search
9 * @return signing key, if found among `keys`
11 export async function getMatchingSigningKey(options: {
12 armoredSignature: string;
14 }): Promise<KeyReference | undefined>;
15 export async function getMatchingSigningKey(options: {
16 binarySignature: Uint8Array;
18 }): Promise<KeyReference | undefined>;
19 export async function getMatchingSigningKey(options: {
20 binarySignature?: Uint8Array;
21 armoredSignature?: string;
23 }): Promise<KeyReference | undefined> {
24 const { binarySignature, armoredSignature, keys } = options;
26 const { signingKeyIDs } = binarySignature
27 ? await CryptoProxy.getSignatureInfo({ binarySignature })
28 : await CryptoProxy.getSignatureInfo({ armoredSignature: armoredSignature! });
30 for (const signingKeyID of signingKeyIDs) {
31 // If the signing key is a subkey, we still return the full key entity
32 const signingKey = keys.find((key) => {
33 const keyIDs = key.getKeyIDs();
34 return keyIDs.indexOf(signingKeyID) >= 0;
43 * Create public key reference given a private key one.
44 * The returned key reference is independent of the input one (i.e. clearing either key reference does not affect the other).
45 * NOTE: this function is is considerably more expensive than the former `key.toPublic()`. It is only intended for long-term storage of the public key, as a new key entry will be added to the internal key store.
46 * When using `CryptoProxy`, it is safe to pass a `PrivateKeyReference` where a `PublicKeyReference` is expected.
48 export async function toPublicKeyReference(privateKey: PrivateKeyReference) {
49 return CryptoProxy.importPublicKey({
50 binaryKey: await CryptoProxy.exportPublicKey({ key: privateKey, format: 'binary' }),