// A refusal the gate stands on, as the shell draws it.//// The core decides every refusal it can see (`shell.home`'s gate: its code,// its title, its body, its reason, its ways out and where it is drawn) and// this type carries its words to the banner and the slip// (`init(gate:)`). The three it cannot see are the Keychain's, at launch,// before any core is open (`forKeyRead`): those are the only words written// here.importCottonKitimportFoundationimportSecurity/// A refusal's one way out beyond Try again.publicenumRefusalWayOut: String, Equatable, Sendable {case signOut = "sign_out"case resetKey = "reset_key"case none}publicstructServerRefusal: Equatable, Sendable {publiclet code: Stringpubliclet title: Stringpubliclet body: Stringpubliclet reason: String?publiclet retryable: Boolpubliclet wayOut: RefusalWayOut/// The core's ways out, in order, when the core answered this refusal.publiclet waysOut: [HomeAnswer.WayOut]publiclet erase: Boolpublicinit( code: String, title: String, body: String, reason: String? = nil, retryable: Bool, wayOut: RefusalWayOut, waysOut: [HomeAnswer.WayOut] = [], erase: Bool = false ) {self.code = codeself.title = titleself.body = bodyself.reason = reasonself.retryable = retryableself.wayOut = wayOutself.waysOut = waysOutself.erase = erase }/// The refusal the core's gate stands on, in its words; `nil` when none.publicinit?(gate: HomeAnswer.Gate) {guardlet code = gate.code else { returnnil }let ids = gate.ways_out.map(\.id)self.init( code: code, title: gate.words.title ?? "", body: gate.words.body ?? "", reason: gate.words.reason, retryable: ids.contains("retry"), wayOut: ids.contains("reset_key") ? .resetKey : ids.contains("sign_out") ? .signOut : .none, waysOut: gate.ways_out, erase: gate.erase ) }/// The Keychain's three, which no core can answer: it is not open yet.publicstaticfunc of(code: String) -> ServerRefusal? {switch code {case"device_key_unreadable", "no_device_key":returnServerRefusal( code: code, title: CottonStrings.refusalKeyUnreadableTitle, body: CottonStrings.refusalKeyUnreadableBody, reason: CottonStrings.refusalReasonKeyUnreadable, retryable: false, wayOut: .resetKey )case"keychain_unavailable":returnServerRefusal( code: code, title: CottonStrings.refusalKeychainUnavailableTitle, body: CottonStrings.refusalKeychainUnavailableBody, reason: CottonStrings.refusalReasonKeychain, retryable: true, wayOut: .none )case"keychain_locked":returnServerRefusal( code: code, title: CottonStrings.refusalKeychainLockedTitle, body: CottonStrings.refusalKeychainLockedBody, reason: CottonStrings.refusalReasonKeychainLocked, retryable: true, wayOut: .none )default:returnnil } }/// The refusal a failed Keychain read at launch paints, before any core/// is open.////// **Three states, and "Start over" only fixes one of them.** A slot that is/// present and unreadable is `device_key_unreadable`, and forgetting it/// lets the next open mint a key. But a build the Keychain refuses/// outright — `errSecMissingEntitlement`, which every simulator build made/// with `CODE_SIGNING_ALLOWED=NO` answers to every read, write and delete —/// has no slot to forget: the delete fails, the re-open fails the same way,/// and the screen came back exactly as it was. That was the reported "Start/// over does nothing". It is `keychain_unavailable`, with Retry and no/// button that promises what it cannot do.////// And a Keychain that answers `errSecInteractionNotAllowed` is not/// refusing anything for good: the key is kept "after first unlock", and/// the app was opened before the phone had been unlocked since it started/// (a notification, a background wake). Starting over there/// would throw away a perfectly good identity. It is `keychain_locked`,/// with Retry — and the app retries by itself when the phone unlocks.publicstaticfunc forKeyRead(_ error: Error) -> ServerRefusal {let status: OSStatus?switch error as? DeviceKeyError {case .storeUnreadable(let answered), .notWritten(let answered): status = answereddefault: status = nil }// One sentence per code, so the table in `of(code:)` is the only copy.// Each unwrap names a code that table declares; `GateSlipTests` runs// every branch, so a code dropped from the table fails there first.switch status {case errSecMissingEntitlement:return of(code: "keychain_unavailable")!case errSecInteractionNotAllowed:return of(code: "keychain_locked")!default:return of(code: "device_key_unreadable")! } }}