import Foundation /// A just-paired device may have published its key before the serving Mac /// has wrapped the vault key for it. Complete that handoff before a signed /// create or claim. The presence-protected private key is read only once. enum PersonalSigner { static func unlock( core: any CottonCalling, privateKey: String, pause: @Sendable () async throws -> Void ) async throws { struct Unlock: Encodable { let privateKey: String } for attempt in 0...10 { try Task.checkCancellation() do { _ = try await core.callJSON(CottonMethod.vaultUnlock, Unlock(privateKey: privateKey)) return } catch let error as CottonCallError where error.code == "vault_not_wrapped_yet" && attempt < 10 { // Push the publication and pull the keeper's wrap. Each later // pass can collect the wrap made after the previous exchange. _ = try await core.callJSON(CottonMethod.syncNow) try Task.checkCancellation() try await pause() } } } }