Cotton code
84 lines · 4.9 KB · SwiftRaw
// SIGNING IN WITHOUT A TEXT MESSAGE (19 Nov 2026; the owner's// order, 17 Nov: approve from a phone already signed in, then a Cotton// passkey, then a code by text for the first device and recovery).//// The core decides every rule and every word (`crates/cotton-ffi/src/phone/// signin_approval.rs`, `account_passkey.rs`); this file only names the calls// and carries them, off the main thread, the way `BusinessClient` carries// the code sign-in. What a granted credential becomes is `BusinessClient.keep`.//// THE ASKING DEVICE (signed out, at the open host)://   personal.ask        → the card: heading, line, left, pollSeconds, sendCode, cancel//   personal.askPoll    → waiting (a `code` once the other phone opened it) or done;//                         `approved` carries the credential, already claimed//   personal.passkeyBegin / personal.passkeySignIn → the system passkey sheet between them//// THE APPROVING DEVICE (signed in, through its object door)://   signin.asks / signin.show / signin.approve / signin.refuse//   (never signin.approveSibling or signin.siblingTicket: no code, no presence;//   only Android's signature-checked provider may carry them, and an iPhone//   app shares the keychain group instead)//   signin.passkeys / signin.passkeyOptions / signin.passkeyAdd / signin.passkeyRemoveimport Foundationextension CottonMethod {    /// Ask a signed-in device of this number to let this one in.    public static let personalAsk = "personal.ask"    /// Where that ask stands; approved, the credential is claimed with this device's key.    public static let personalAskPoll = "personal.askPoll"    /// The passkey sheet's request, for a sign-in with no number typed.    public static let personalPasskeyBegin = "personal.passkeyBegin"    /// The sheet's answer, proved at the open host: a credential.    public static let personalPasskeySignIn = "personal.passkeySignIn"    /// The asks waiting on this signed-in device; a read only looks.    public static let signinAsks = "signin.asks"    /// *This is me, show the code*: the tap that opens one ask, so its device shows the code.    public static let signinShow = "signin.show"    /// Approve one ask with the code typed from the other screen.    public static let signinApprove = "signin.approve"    /// Turn one ask away.    public static let signinRefuse = "signin.refuse"    /// The new device's page while another device of the person's must say yes (GETTING-IN.md): it asks by itself.    public static let gettingInScreen = "gettingIn.screen"    /// Ask again: a fresh request that replaces the one before.    public static let gettingInAgain = "gettingIn.again"    /// Account's passkeys, with their words.    public static let signinPasskeys = "signin.passkeys"    /// The creation request a new passkey is made from.    public static let signinPasskeyOptions = "signin.passkeyOptions"    /// The new passkey, as the sheet made it.    public static let signinPasskeyAdd = "signin.passkeyAdd"    /// One passkey removed.    public static let signinPasskeyRemove = "signin.passkeyRemove"}extension BusinessClient {    /// `personal.ask`: this device asks, naming its vault key's public half so    /// one approval also lets it into the vault. The public half is read with    /// no prompt (`VaultKeyring.publicKey`); without one the ask signs in only.    public func ask(phone: String, deviceName: String, vaultKey: String?) async throws -> JSONValue {        struct Ask: Encodable { let server: String; let phone: String; let deviceName: String; let vaultKey: String? }        return try await calling.callJSON(CottonMethod.personalAsk, Ask(server: address.server, phone: phone, deviceName: deviceName, vaultKey: vaultKey))    }    /// `personal.askPoll`: the ask's state, in the core's words.    public func askPoll(askId: String) async throws -> JSONValue {        struct Poll: Encodable { let server: String; let askId: String }        return try await calling.callJSON(CottonMethod.personalAskPoll, Poll(server: address.server, askId: askId))    }    /// `personal.passkeyBegin`: `{challengeId, requestJson}` for the sheet.    public func passkeyBegin() async throws -> JSONValue {        struct Begin: Encodable { let server: String }        return try await calling.callJSON(CottonMethod.personalPasskeyBegin, Begin(server: address.server))    }    /// `personal.passkeySignIn`, then the credential kept as a code's is.    public func passkeySignIn(challengeId: String, responseJSON: String) async throws -> BusinessSession {        struct SignIn: Encodable { let server: String; let challengeId: String; let responseJson: String }        let granted = try await calling.callJSON(CottonMethod.personalPasskeySignIn,                                                 SignIn(server: address.server, challengeId: challengeId, responseJson: responseJSON))        return try keep(granted: granted, phone: granted.text("phone"))    }}