import { PublicKey, type Subkey, UserIDPacket } from "openpgp"; import type { Signature } from "./sign.ts"; import { defined, get } from "../../utils/anonymous.ts"; export function getUserIDsFromKey( signature: Signature | undefined, key: PublicKey | Subkey, ): UserIDPacket[] { const packet = signature?.getPackets?.()?.[0]; const userID = packet?.signersUserID; if (userID) { return [UserIDPacket.fromObject(parseUserID(userID))]; } key = key instanceof PublicKey ? key : key.mainKey; return key.users.map(get("userID")).filter(defined); } function parseUserID(input: string) { const regex = /^(.*?)\s*(?:\((.*?)\))?\s*(?:<(.+?)>)?$/; const match = input.match(regex); if (!match) return {}; const [, name, comment, email] = match; return { name: name?.trim() || undefined, comment: comment?.trim() || undefined, email: email?.trim() || undefined, }; }