// The passkey channel: what Cotton Keys' credential provider tells the app.//// The SECOND KIND of mailbox, in the same spelling as the share channel// (`ShareMailbox`, one App Group `UserDefaults` key, raw-JSON append, drained// whole), under its own key so neither drain ever takes the other's entries:// Cotton Keys takes no shares, and a share drain must never swallow a// passkey. The provider has no core and no database (it is the share// extension's shape), so it writes one entry and knocks; the app drains://// passkey.registered {rp_id, rp_name, user_name, user_handle, credential_id,// algorithm, public_key (COSE, base64url), created_at}// passkey.used {credential_id, used_at}//// No key material ever rides here: the private half is the shared keychain// group's (`PasskeyKeychain`), read by the app for the one call that seals it.//// THE KNOCK IS A DARWIN NOTIFICATION, not a URL: a credential provider cannot// open its app. A running Keys drains at once; otherwise the next foreground// drains. An entry the app cannot apply yet (the vault is locked, the row it// counts a use for is not written yet) is put back to wait.importFoundation/// The channel's names.publicenumPasskeyChannel {/// The App Group `UserDefaults` key. Wire protocol, like the share key.publicstaticlet mailboxKey = "cotton.passkey.mailbox.v1"/// The two kinds an entry is.publicstaticlet registered = "passkey.registered"publicstaticlet used = "passkey.used"/// The Darwin notification the provider posts after it writes an entry,/// named by the app group so Keys and Keys Dev never hear each other.publicstaticfunc knockName(bundleIdentifier: String? = Bundle.main.bundleIdentifier) -> String {CottonDefaults.appGroup(bundleIdentifier: bundleIdentifier) + ".passkey.knock" }/// Knock: tell a running app an entry is waiting.publicstaticfunc knock(bundleIdentifier: String? = Bundle.main.bundleIdentifier) {let name = CFNotificationName(knockName(bundleIdentifier: bundleIdentifier) as CFString) CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), name, nil, nil, true) }}/// One thing the provider did, as it wrote it down.publicstructPasskeyEntry: Codable, Equatable, Sendable {publicvar id: String/// When the provider wrote it, ISO-8601.publicvar at: String/// ``PasskeyChannel/registered`` or ``PasskeyChannel/used``.publicvar kind: Stringpublicvar credentialId: Stringpublicvar rpId: String?publicvar rpName: String?publicvar userName: String?publicvar userHandle: String?publicvar algorithm: String?publicvar publicKey: String?publicvar createdAt: String?publicvar usedAt: String?publicinit( id: String = UUID().uuidString, at: String, kind: String, credentialId: String, rpId: String? = nil, rpName: String? = nil, userName: String? = nil, userHandle: String? = nil, algorithm: String? = nil, publicKey: String? = nil, createdAt: String? = nil, usedAt: String? = nil ) {self.id = idself.at = atself.kind = kindself.credentialId = credentialIdself.rpId = rpIdself.rpName = rpNameself.userName = userNameself.userHandle = userHandleself.algorithm = algorithmself.publicKey = publicKeyself.createdAt = createdAtself.usedAt = usedAt }privateenumCodingKeys: String, CodingKey {case id, at, kindcase credentialId = "credential_id"case rpId = "rp_id"case rpName = "rp_name"case userName = "user_name"case userHandle = "user_handle"case algorithmcase publicKey = "public_key"case createdAt = "created_at"case usedAt = "used_at" }/// The columns this entry carries, as `passkey.register` / `passkey.used`/// take them (the core's snake keys).publicvar arguments: [String: String] {var out: [String: String] = ["credential_id": credentialId]for (key, value) in [ ("rp_id", rpId), ("rp_name", rpName), ("user_name", userName), ("user_handle", userHandle), ("algorithm", algorithm), ("public_key", publicKey), ("created_at", createdAt), ("used_at", usedAt), ] {iflet value { out[key] = value } }return out }}/// The passkey mailbox: ``ShareMailbox`` under ``PasskeyChannel/mailboxKey``.publicstructPasskeyMailbox: Sendable {privatelet mailbox: ShareMailboxpublicinit(storage: any ShareMailboxStorage = DefaultsShareMailboxStorage()) { mailbox = ShareMailbox(storage: storage, key: PasskeyChannel.mailboxKey) }publicfunc append(_ entry: PasskeyEntry) { mailbox.append(entry) }/// Take everything waiting, removing the list (``ShareMailbox/drain``).publicfunc drain() -> [PasskeyEntry] { mailbox.drain(PasskeyEntry.self) }/// Put entries back to wait for a later drain (the vault was locked, the/// row they count for is not written yet). Unbounded by design: a passkey/// waits for its vault however long that takes, and the deck says so.publicfunc wait(_ entries: [PasskeyEntry]) {for entry in entries { mailbox.append(entry) } }/// What is waiting, without taking it (the deck's line counts it).publicfunc waiting() -> [PasskeyEntry] { mailbox.peek(PasskeyEntry.self) }}