// The provider's Swift WebAuthn encoder, held to the core's bytes. // // `crates/cotton-core/tests/passkey_vector.rs` makes a vector with // `cotton_core::keys::passkey` over a fixed synthetic key and commits it; this // reads the same file and must make the same COSE key, registration // authenticatorData and `none` attestation object, byte for byte. The // assertion's authenticatorData must match too, and its signature (Rust's, // deterministic) must verify under CryptoKit — while CryptoKit's own // (randomised) signature verifies under the same key. The key is synthetic // and opens nothing. import CottonMailbox import CryptoKit import XCTest final class PasskeyAuthenticatorTests: XCTestCase { private struct Vector: Decodable { let rp_id: String let user_handle: String let credential_id: String let private_scalar_hex: String let client_data_hash_hex: String let public_key: String let attestation: Attestation let assertion: Assertion struct Attestation: Decodable { let authenticator_data_hex: String let attestation_object_hex: String } struct Assertion: Decodable { let counter: Int let authenticator_data_hex: String let signature_der_hex: String } } private func vector() throws -> Vector { let root = URL(fileURLWithPath: #filePath) .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() .deletingLastPathComponent().deletingLastPathComponent() let file = root.appendingPathComponent("crates/cotton-core/tests/fixtures/passkey_vector.json") return try JSONDecoder().decode(Vector.self, from: Data(contentsOf: file)) } private func hex(_ text: String) -> Data { var out = Data() var index = text.startIndex while index < text.endIndex { let next = text.index(index, offsetBy: 2) out.append(UInt8(text[index.. String? { values[key] } func set(_ key: String, _ value: String?) { values[key] = value } } // RULE: a passkey entry is written under its own key in the share // channel's spelling, so a share drain never takes it and the passkey // drain never takes a share; waiting puts it back whole. func testPasskeyEntriesNeverMeetTheShareDrain() throws { let storage = Storage() let passkeys = PasskeyMailbox(storage: storage) let shares = ShareMailbox(storage: storage) shares.append(ShareEntry(at: "2026-11-08T09:00:00Z", kind: "url", url: "https://passkey.example")) let entry = PasskeyEntry( at: "2026-11-08T09:30:00Z", kind: PasskeyChannel.registered, credentialId: "wMHCw8TFxsfIycrLzM3Ozw", rpId: "passkey.example", userName: "ada@passkey.example", userHandle: "c3ludGhldGljLXVzZXItMQ", algorithm: "ES256", publicKey: "pQECAyYg", createdAt: "2026-11-08T09:30:00Z" ) passkeys.append(entry) XCTAssertNotNil(storage.values[PasskeyChannel.mailboxKey]) let raw = try XCTUnwrap(storage.values[PasskeyChannel.mailboxKey]) XCTAssertTrue(raw.contains("\"credential_id\""), "the core's snake keys: \(raw)") XCTAssertEqual(shares.drain().map(\.kind), ["url"]) XCTAssertEqual(passkeys.waiting(), [entry]) XCTAssertEqual(passkeys.drain(), [entry]) XCTAssertEqual(passkeys.drain(), []) passkeys.wait([entry]) XCTAssertEqual(passkeys.drain(), [entry]) XCTAssertEqual(entry.arguments["rp_id"], "passkey.example") XCTAssertNil(entry.arguments["used_at"]) } func testTheGroupOfTheProviderIsItsApps() { XCTAssertEqual(CottonDefaults.appGroup(bundleIdentifier: "com.cottondb.keys.dev.provider", family: .phone), "group.com.cottondb.keys.dev") XCTAssertEqual(PasskeyChannel.knockName(bundleIdentifier: "com.cottondb.keys.provider"), "group.com.cottondb.keys.passkey.knock") } }