#if COTTON_DEV // WalkVault.swift — DEVELOPER TOOL, compiled into Cotton Dev only. // // A standing simulator signs in to its synthetic object, so its copy is // `Remote` and nothing founds a vault for it: the walks stopped at // `vault_not_wrapped_yet`. Asked at launch by the Keys walks: // // WALK_FOUND_VAULT=1 publish this device's key and pull; the vault is // founded by the first need (the import), as on any // signed-in phone // WALK_KEYS_OPEN=1 read this device's vault key (the Face ID the walk's // host answers) and open every passkey row through // `passkey.mirror`; the toast says how many opened // // Counts and refusal codes only: no key, scalar or wrap is logged or said. import CottonKit import Foundation import os @MainActor enum WalkVault { private static let log = Logger(subsystem: "com.cottondb.keys", category: "walk-vault") static func run(core: any CottonCalling, say: @MainActor (String) -> Void) async { let environment = ProcessInfo.processInfo.environment if environment["WALK_FOUND_VAULT"] == "1", let refused = await found(core: core) { say(ShellLines.line("developer", "walk.vault_refused", ["code": refused])) } if environment["WALK_KEYS_OPEN"] == "1" { say(await opened(core: core)) } } /// Nil once this device's key is published and pulled; else the refusal. private static func found(core: any CottonCalling) async -> String? { let keyring = VaultKeyring() guard let published = await Task.detached(operation: { try? keyring.publicKey() }).value else { return "no_device_key" } do { _ = try await core.callJSON(CottonMethod.vaultPublishKey, ["publicKey": published]) // A wrap another device already wrote must be here before asking. _ = try await core.callJSON(CottonMethod.syncNow) log.notice("walk vault: key published") return nil } catch let refusal as CottonCallError { log.error("walk vault: refused \(refusal.code, privacy: .public)") return refusal.code } catch { return "unreadable" } } /// What the toast says: `Walk opened of passkeys`, or the refusal. private static func opened(core: any CottonCalling) async -> String { let keyring = VaultKeyring() let key = await Task.detached(priority: .userInitiated, operation: { try? keyring.privateKey() }).value guard let key else { return ShellLines.line("developer", "walk.no_vault_key") } do { let rows = try await core.callJSON(CottonMethod.passkeyIdentities)["identities"]?.array ?? [] let ids = rows.compactMap { $0.text("credential_id") } let request: [String: JSONValue] = ["credentialIds": .array(ids.map { .string($0) }), "privateKey": .string(key)] let answer = try await core.callJSON(CottonMethod.passkeyMirror, request) let opened = answer["keys"]?.array?.count ?? 0 log.notice("walk vault: opened \(opened, privacy: .public) of \(ids.count, privacy: .public)") return ShellLines.line("developer", "walk.opened", ["opened": String(opened), "total": String(ids.count)]) } catch let refusal as CottonCallError { return ShellLines.line("developer", "walk.open_refused", ["code": refusal.code]) } catch { return ShellLines.line("developer", "walk.open_refused", ["code": "unreadable"]) } } } #endif