// The shared clipboard seam for every secret column and transient reveal.// Copying a secret WITHOUT showing it.//// A seam rather than two `#if os` lines at the call site, for two reasons that// are both about this being a credential://// * **the expiry is a platform capability, not a promise.** iOS can be told// to drop a pasteboard entry after its seconds whether Cotton runs or not;// macOS forgets it only while Cotton runs, and the screen says which it// got rather than claiming the safer one everywhere.// * **a secret is marked concealed.** Clipboard managers, Universal// Clipboard and Cotton's own Clips honour the nspasteboard.org marks, so a// password copied here never becomes a clip anywhere.// * a test must be able to assert what was copied without a pasteboard. A// simulator's pasteboard is shared between runs, so a test that wrote to// the real one is a test that passes once and then lies.//// The value never reaches the SCREEN on this path. That is what makes Copy the// gesture to use in a room with other people in it, and why it is offered// beside every sealed field rather than only after a reveal.//// Every copy an act asks for goes through here too (`RecordActionOutcomes`),// a plain one (an address) with no expiry and a sealed one with its// `SecretClipboard.seconds` — so no surface writes to a pasteboard of its own.importFoundation#if canImport(UIKit)import UIKitimportUniformTypeIdentifiers#elseif canImport(AppKit)importAppKit#endif/// How long Cotton's own copy of a secret stays on the clipboard, on every/// platform: two minutes (owner, 14 Oct 2026; was sixty seconds). A column's/// reveal policy may ask for less, never more.enumSecretClipboard {staticlet seconds: TimeInterval = 120/// The expiry a copy with this policy gets: the policy's own, capped.staticfunc seconds(policy: TimeInterval?) -> TimeInterval {guardlet policy, policy > 0else { return seconds }return min(policy, seconds) }}/// The pasteboard marks of nspasteboard.org that every well-behaved clipboard/// reader honours: a concealed item is a secret, a transient one is a/// program's scratch. Cotton writes the first on every secret it copies and/// its Clips watcher never keeps an item carrying either.publicenumPasteboardMark {publicstaticlet concealed = "org.nspasteboard.ConcealedType"publicstaticlet transient = "org.nspasteboard.TransientType"/// Both, as the type identifiers a reader checks an item's types against.publicstaticlet skipped: Set<String> = [concealed, transient]}/// Where a copied value goes.protocolClipboard: Sendable {/// Put a value on the clipboard, asking the platform to forget it after/// `seconds`. Answers whether the EXPIRY was honoured — the copy itself/// always happens. @discardableResult func put(_ value: String, expiringAfter seconds: TimeInterval) -> Bool/// Put a value that is not a secret on the clipboard, kept until the/// next copy — an address, a clone command.func put(_ value: String)}/// The real one.structSystemClipboard: Clipboard, @unchecked Sendable { #if canImport(UIKit)privatelet pasteboard: UIPasteboardinit(pasteboard: UIPasteboard = .general) { self.pasteboard = pasteboard } #elseif canImport(AppKit)privatelet pasteboard: NSPasteboardinit(pasteboard: NSPasteboard = .general) { self.pasteboard = pasteboard } #endif @discardableResultfunc put(_ value: String, expiringAfter seconds: TimeInterval) -> Bool { #if canImport(UIKit)// `expirationDate` is the system's own promise: the entry is// removed at that instant whether the app is running or not, which// a timer in this process could not manage.// `localOnly` keeps a secret off Universal Clipboard; the// concealed mark keeps it out of every clip reader on this one. pasteboard.setItems( [[UTType.plainText.identifier: value, PasteboardMark.concealed: Data()]], options: [.expirationDate: Date().addingTimeInterval(seconds), .localOnly: true] )returntrue #elseif canImport(AppKit)let concealed = NSPasteboard.PasteboardType(PasteboardMark.concealed) pasteboard.declareTypes([.string, concealed], owner: nil)guard pasteboard.setString(value, forType: .string), pasteboard.setData(Data(), forType: concealed) else { returnfalse }let change = pasteboard.changeCountlet board = pasteboard// A newer copy belongs to someone else, including another app.DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {if board.changeCount == change { board.clearContents() } }returntrue #elsereturnfalse #endif }func put(_ value: String) { #if canImport(UIKit) UIPasteboard.general.string = value #elseif canImport(AppKit) NSPasteboard.general.clearContents() NSPasteboard.general.setString(value, forType: .string) #endif }}/// What the tests drive, and the only way to assert a copy without a/// pasteboard.finalclassRecordingClipboard: Clipboard, @unchecked Sendable {privatelet lock = NSLock()privatevar copied: [(value: String, seconds: TimeInterval)] = []privatevar plainCopies: [String] = []privatelet expires: Boolinit(expires: Bool = true) {self.expires = expires } @discardableResultfunc put(_ value: String, expiringAfter seconds: TimeInterval) -> Bool { lock.lock()defer { lock.unlock() } copied.append((value, seconds))return expires }func put(_ value: String) { lock.lock()defer { lock.unlock() } plainCopies.append(value) }/// The values copied with no expiry, in order.var plain: [String] { lock.lock()defer { lock.unlock() }return plainCopies }var entries: [(value: String, seconds: TimeInterval)] { lock.lock()defer { lock.unlock() }return copied }}