#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.importCottonKitimportFoundationimport os@MainActorenumWalkVault {privatestaticlet log = Logger(subsystem: "com.cottondb.keys", category: "walk-vault")staticfunc run(core: any CottonCalling, say: @MainActor (String) -> Void) async {let environment = ProcessInfo.processInfo.environmentif 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.privatestaticfunc found(core: any CottonCalling) async -> String? {let keyring = VaultKeyring()guardlet published = awaitTask.detached(operation: { try? keyring.publicKey() }).value else {return"no_device_key" }do { _ = tryawait core.callJSON(CottonMethod.vaultPublishKey, ["publicKey": published])// A wrap another device already wrote must be here before asking. _ = tryawait core.callJSON(CottonMethod.syncNow) log.notice("walk vault: key published")returnnil } catchlet refusal asCottonCallError { log.error("walk vault: refused \(refusal.code, privacy: .public)")return refusal.code } catch {return"unreadable" } }/// What the toast says: `Walk opened <n> of <m> passkeys`, or the refusal.privatestaticfunc opened(core: any CottonCalling) async -> String {let keyring = VaultKeyring()let key = awaitTask.detached(priority: .userInitiated, operation: { try? keyring.privateKey() }).valueguardlet key else { returnShellLines.line("developer", "walk.no_vault_key") }do {let rows = tryawait 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 = tryawait 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)")returnShellLines.line("developer", "walk.opened", ["opened": String(opened), "total": String(ids.count)]) } catchlet refusal asCottonCallError {returnShellLines.line("developer", "walk.open_refused", ["code": refusal.code]) } catch {returnShellLines.line("developer", "walk.open_refused", ["code": "unreadable"]) } }}#endif