//! What is INSIDE a sealed secret (spec §3.1). //! //! The plain columns of `cotton_secrets` — `kind`, `name`, `site`, `username`, //! `service` — are what a list shows and what search indexes. Everything a //! person actually has to keep is in here, and it is sealed //! ([`super::seal`]) before it reaches the row. //! //! **One declaration.** [`SecretPayload`] is the serde shape, tagged on `kind`, //! so `{"kind":"login","password":"…"}` is the wire form in Rust, in the file //! and in Swift. [`SECRET_KINDS`] is the same declaration as DATA — which field //! belongs to which kind, and which one is the PRIMARY field — and //! `payload_tests.rs` holds the two to each other field for field, so neither //! can move without the other. `cotton-gen` renders the Swift twin from the //! data half; the apps never build this JSON by hand. //! //! **The plain columns are not repeated in here.** A name is on the row, where //! search can reach it; a second copy inside the payload would be a second //! answer to what a secret is called, and the sealed one is the one nothing can //! read to check. use serde::{Deserialize, Serialize}; /// How many characters of the primary field a hint carries. /// /// Four: enough to tell two keys apart on a screen, useless to anybody who has /// it. The same four `list_secrets` has always answered with. pub const HINT_CHARS: usize = 4; /// The largest file a `file` secret holds, in bytes before base64: signing keys /// and `.env` files are a few kilobytes; anything near this is not a secret. pub const FILE_MAX_BYTES: usize = 256 * 1024; /// The kinds a secret can be. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum SecretKind { Login, ApiKey, Note, Card, Identity, Document, Code, Wifi, Address, /// A file kept whole: an `.env`, a signing key (`.jks`, `.p12`, `.p8`), /// any file of up to [`FILE_MAX_BYTES`], sealed inside the secret itself /// with the passwords that open it (Cotton Secrets, 25 Nov 2026). File, } impl SecretKind { /// The wire spelling — and the `cotton_secrets.kind` CHECK's vocabulary. pub const fn as_str(self) -> &'static str { match self { SecretKind::Login => "login", SecretKind::ApiKey => "api_key", SecretKind::Note => "note", SecretKind::Card => "card", SecretKind::Identity => "identity", SecretKind::Document => "document", SecretKind::Code => "code", SecretKind::Wifi => "wifi", SecretKind::Address => "address", SecretKind::File => "file", } } /// Every kind, in declaration order — the order a form picker offers them. pub const ALL: [SecretKind; 10] = [ SecretKind::Login, SecretKind::ApiKey, SecretKind::Note, SecretKind::Card, SecretKind::Identity, SecretKind::Document, SecretKind::Code, SecretKind::Wifi, SecretKind::Address, SecretKind::File, ]; /// The kind's declaration: its fields, and which of them is primary. pub fn spec(self) -> &'static SecretKindSpec { SECRET_KINDS .iter() .find(|spec| spec.kind == self) .expect("SECRET_KINDS is total over SecretKind — the gate in payload_tests says so") } /// The field a hint is taken from, and the one a `raw` placement writes. pub fn primary_field(self) -> &'static str { self.spec().primary } pub fn parse(value: &str) -> Option { SecretKind::ALL.into_iter().find(|kind| kind.as_str() == value) } /// Whether the Passwords list holds this kind: a document's password /// stands on its document's page, never on the list (290 walk). pub const fn on_passwords(self) -> bool { match self { SecretKind::Login | SecretKind::ApiKey | SecretKind::Note | SecretKind::Card | SecretKind::Identity | SecretKind::Code | SecretKind::Wifi | SecretKind::Address | SecretKind::File => true, SecretKind::Document => false, } } /// `AND IN (…)` over exactly the kinds the Passwords list holds. pub fn on_passwords_clause(column: &str) -> String { let kinds: Vec = SecretKind::ALL .into_iter() .filter(|kind| kind.on_passwords()) .map(|kind| format!("'{}'", kind.as_str())) .collect(); format!(" AND {column} IN ({})", kinds.join(", ")) } } impl std::fmt::Display for SecretKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } } /// One field of one kind's payload. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct SecretField { /// The JSON key, which is also the Rust field name. pub name: &'static str, /// A required field is present in every payload of its kind; an optional /// one is absent rather than null when it is not set. pub required: bool, /// What the field IS, for a form label and for the Swift twin's doc line. pub meaning: &'static str, } /// One kind's payload, as data. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct SecretKindSpec { pub kind: SecretKind, /// The field a hint comes from — always the first, and always required. pub primary: &'static str, pub fields: &'static [SecretField], } /// The payload shapes, as data. The other half of [`SecretPayload`]. pub static SECRET_KINDS: &[SecretKindSpec] = &[ SecretKindSpec { kind: SecretKind::Login, primary: "password", fields: &[ SecretField { name: "password", required: true, meaning: "The password itself." }, SecretField { name: "notes", required: false, meaning: "Anything else worth keeping with it." }, SecretField { name: "totp", required: false, meaning: "The otpauth secret a one-time code is generated from." }, SecretField { name: "urls", required: false, meaning: "Other addresses it is used at, one a line." }, ], }, SecretKindSpec { kind: SecretKind::ApiKey, primary: "key", fields: &[ SecretField { name: "key", required: true, meaning: "The key itself." }, SecretField { name: "notes", required: false, meaning: "Anything else worth keeping with it." }, ], }, SecretKindSpec { kind: SecretKind::Note, primary: "body", fields: &[SecretField { name: "body", required: true, meaning: "The note, whole.", }], }, SecretKindSpec { kind: SecretKind::Card, primary: "number", fields: &[ SecretField { name: "number", required: true, meaning: "The card number." }, SecretField { name: "holder", required: false, meaning: "The name on the card." }, SecretField { name: "expiry", required: false, meaning: "MM/YY, as the card prints it." }, SecretField { name: "cvv", required: false, meaning: "The verification digits." }, SecretField { name: "notes", required: false, meaning: "Anything else worth keeping with it." }, ], }, SecretKindSpec { kind: SecretKind::Identity, primary: "number", fields: &[ SecretField { name: "number", required: true, meaning: "The document's number." }, SecretField { name: "id_type", required: false, meaning: "Passport, driving licence, tax id, and so on." }, SecretField { name: "holder", required: false, meaning: "Whose it is." }, SecretField { name: "issued", required: false, meaning: "The date it was issued." }, SecretField { name: "expires", required: false, meaning: "The date it runs out." }, SecretField { name: "country", required: false, meaning: "Who issued it." }, SecretField { name: "notes", required: false, meaning: "Anything else worth keeping with it." }, ], }, SecretKindSpec { kind: SecretKind::Document, primary: "password", fields: &[SecretField { name: "password", required: true, meaning: "The password that unlocks a filed document.", }], }, SecretKindSpec { kind: SecretKind::Code, primary: "secret", fields: &[ SecretField { name: "secret", required: true, meaning: "The otpauth address (or bare base32 secret) the one-time code is made from." }, SecretField { name: "notes", required: false, meaning: "Anything else worth keeping with it." }, ], }, SecretKindSpec { kind: SecretKind::Wifi, primary: "password", fields: &[ SecretField { name: "password", required: true, meaning: "The network's password." }, SecretField { name: "security", required: false, meaning: "WPA3, WPA2, WEP or none, as the router says." }, SecretField { name: "notes", required: false, meaning: "Anything else worth keeping with it." }, ], }, SecretKindSpec { kind: SecretKind::Address, primary: "address", fields: &[ SecretField { name: "address", required: true, meaning: "The postal address, as it is written on an envelope." }, SecretField { name: "full_name", required: false, meaning: "The name it is for." }, SecretField { name: "email", required: false, meaning: "An email address that goes with it." }, SecretField { name: "phone", required: false, meaning: "A phone number that goes with it." }, SecretField { name: "notes", required: false, meaning: "Anything else worth keeping with it." }, ], }, SecretKindSpec { kind: SecretKind::File, primary: "content", fields: &[ SecretField { name: "content", required: true, meaning: "The file's bytes, base64 (standard, padded)." }, SecretField { name: "file_name", required: false, meaning: "The file's name, as it was chosen or shared." }, SecretField { name: "store_password", required: false, meaning: "The password that opens a keystore (.jks, .p12)." }, SecretField { name: "key_alias", required: false, meaning: "The key's alias inside a keystore." }, SecretField { name: "key_password", required: false, meaning: "The password of that key, when it differs from the store's." }, SecretField { name: "notes", required: false, meaning: "Anything else worth keeping with it." }, ], }, ]; /// The payload of one secret — the serde half of [`SECRET_KINDS`]. /// /// `#[serde(tag = "kind")]`, so the JSON is FLAT: the tag and the kind's own /// fields at one level. That is what lets the Swift twin decode a payload /// straight into the matching struct, and what keeps a payload readable when /// somebody opens it in a debugger a decade from now. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(tag = "kind", rename_all = "snake_case")] pub enum SecretPayload { Login { password: String, #[serde(default, skip_serializing_if = "Option::is_none")] notes: Option, #[serde(default, skip_serializing_if = "Option::is_none")] totp: Option, #[serde(default, skip_serializing_if = "Option::is_none")] urls: Option, }, ApiKey { key: String, #[serde(default, skip_serializing_if = "Option::is_none")] notes: Option, }, Note { body: String, }, Card { number: String, #[serde(default, skip_serializing_if = "Option::is_none")] holder: Option, #[serde(default, skip_serializing_if = "Option::is_none")] expiry: Option, #[serde(default, skip_serializing_if = "Option::is_none")] cvv: Option, #[serde(default, skip_serializing_if = "Option::is_none")] notes: Option, }, Identity { number: String, #[serde(default, skip_serializing_if = "Option::is_none")] id_type: Option, #[serde(default, skip_serializing_if = "Option::is_none")] holder: Option, #[serde(default, skip_serializing_if = "Option::is_none")] issued: Option, #[serde(default, skip_serializing_if = "Option::is_none")] expires: Option, #[serde(default, skip_serializing_if = "Option::is_none")] country: Option, #[serde(default, skip_serializing_if = "Option::is_none")] notes: Option, }, Document { password: String, }, Code { secret: String, #[serde(default, skip_serializing_if = "Option::is_none")] notes: Option, }, Wifi { password: String, #[serde(default, skip_serializing_if = "Option::is_none")] security: Option, #[serde(default, skip_serializing_if = "Option::is_none")] notes: Option, }, Address { address: String, #[serde(default, skip_serializing_if = "Option::is_none")] full_name: Option, #[serde(default, skip_serializing_if = "Option::is_none")] email: Option, #[serde(default, skip_serializing_if = "Option::is_none")] phone: Option, #[serde(default, skip_serializing_if = "Option::is_none")] notes: Option, }, File { content: String, #[serde(default, skip_serializing_if = "Option::is_none")] file_name: Option, #[serde(default, skip_serializing_if = "Option::is_none")] store_password: Option, #[serde(default, skip_serializing_if = "Option::is_none")] key_alias: Option, #[serde(default, skip_serializing_if = "Option::is_none")] key_password: Option, #[serde(default, skip_serializing_if = "Option::is_none")] notes: Option, }, } impl SecretPayload { pub const fn kind(&self) -> SecretKind { match self { SecretPayload::Login { .. } => SecretKind::Login, SecretPayload::ApiKey { .. } => SecretKind::ApiKey, SecretPayload::Note { .. } => SecretKind::Note, SecretPayload::Card { .. } => SecretKind::Card, SecretPayload::Identity { .. } => SecretKind::Identity, SecretPayload::Document { .. } => SecretKind::Document, SecretPayload::Code { .. } => SecretKind::Code, SecretPayload::Wifi { .. } => SecretKind::Wifi, SecretPayload::Address { .. } => SecretKind::Address, SecretPayload::File { .. } => SecretKind::File, } } /// The VALUE of the kind's primary field — the hint's source, what a /// `raw` placement writes, and what Copy copies. pub fn primary_field(&self) -> &str { match self { SecretPayload::Login { password, .. } => password, SecretPayload::ApiKey { key, .. } => key, SecretPayload::Note { body } => body, SecretPayload::Card { number, .. } => number, SecretPayload::Identity { number, .. } => number, SecretPayload::Document { password } => password, SecretPayload::Code { secret, .. } => secret, SecretPayload::Wifi { password, .. } => password, SecretPayload::Address { address, .. } => address, SecretPayload::File { content, .. } => content, } } /// The four characters the row's `hint` column carries. pub fn hint(&self) -> String { hint_of(self.primary_field()) } } /// The last four characters of a value — what a list shows in place of it. /// /// CHARACTERS, not bytes: a value ending in a character outside ASCII must not /// be cut through the middle of it, and the answer a screen shows has to be /// text. A value of four characters or fewer answers with all of it, which is /// no worse than the hint of a longer one — a secret that short is already the /// person's problem, and truncating it further would only hide that. pub fn hint_of(value: &str) -> String { let characters: Vec = value.chars().collect(); let start = characters.len().saturating_sub(HINT_CHARS); characters[start..].iter().collect() } #[cfg(test)] #[path = "payload_tests.rs"] mod tests;