import { a as StandardSchemaProps, i as ParseResult, n as JsonSchema, o as ValidBrand, t as Id } from "./types-hGBnCpJj.mjs"; //#region src/codecs/opaque/key.d.ts /** Wire encoding for opaque AES key material (not Crockford base32). */ type OpaqueKeyFormat = "hex" | "base64url"; declare const opaqueKeyBrand: unique symbol; /** * Opaque imported handle for the Opaque Timestamp codec's AES-256 key. * * Holds the underlying `webcrypto.CryptoKey` internally; callers never access it directly. * Obtain handles via {@link importOpaqueKey} and pass them to * `createOpaqueTimestampId` as the `key` option. * * The same raw secret may safely back an `OpaqueKey` and any other codec's * handle (a **primary secret**): each codec derives its key under a distinct * HKDF label, so the derived keys are independent — but each codec needs its * own explicit import. See ADR-0027. */ type OpaqueKey = { readonly [opaqueKeyBrand]: "OpaqueKey"; }; /** * Imports operator key material into an {@link OpaqueKey} handle for the Opaque * Timestamp codec. * * The bytes are HKDF **input keying material**, not the AES key itself: the * codec derives an **AES-256** key from them via HKDF under the label * `@smonn/ids/opaque/aes` (ADR-0027). Accepts 16, 24, or 32 bytes; the input * size sets the entropy floor only — a 16-byte handle still yields AES-256 with * a 128-bit entropy floor. To store or transport key material, use * {@link encodeOpaqueKey} / {@link decodeOpaqueKey} (`"hex"` or `"base64url"` — * not Crockford base32). * * @param bytes - 16, 24, or 32 bytes of raw key material. * @throws {IdsError} `invalid_key_length` if `bytes.length` is not 16, 24, or 32. */ declare function importOpaqueKey(bytes: Uint8Array): Promise; /** * Encodes raw Opaque key material bytes for storage in env vars or secret managers. * * @param bytes - 16, 24, or 32 raw Opaque key material bytes. * @param format - `hex` (lowercase) or `base64url`. * @throws {IdsError} `invalid_key_format` if `format` is not `"hex"` or `"base64url"`. * @throws {IdsError} `invalid_key_length` if `bytes.length` is not 16, 24, or 32. */ declare function encodeOpaqueKey(bytes: Uint8Array, format: OpaqueKeyFormat): string; /** * Decodes key material emitted by `encodeOpaqueKey` (or `ids keygen`) back to raw bytes. * * @param encoded - Hex or base64url string. * @param format - Must match how the string was encoded. * @throws {IdsError} `invalid_key_format` if `format` is not `"hex"` or `"base64url"`. * @throws {IdsError} `invalid_key_encoding` if the string is malformed for its format. * @throws {IdsError} `invalid_key_length` if the decoded bytes are not 16, 24, or 32 bytes. */ declare function decodeOpaqueKey(encoded: string, format: OpaqueKeyFormat): Uint8Array; //#endregion //#region src/codecs/opaque/index.d.ts /** * Configuration options for an Opaque Timestamp codec instance. */ type OpaqueTimestampOptions = { /** * {@link OpaqueKey} handle for AES-CBC encryption and decryption. * Obtain via {@link importOpaqueKey}. * * A single key, not a ring: rotation is forward-only and caller-tracked — * hold one codec per key epoch and select it from your own records. The * library cannot trial keys (the payload is unauthenticated). See ADR-0013. */ key: OpaqueKey; /** Returns the current timestamp in milliseconds. Defaults to `Date.now`. */ now?: () => number; /** Writes random bytes into `target` for ID generation. Defaults to `crypto.getRandomValues`. */ rng?: (target: Uint8Array) => void; /** If true, silences the duplicate-brand warning in non-production environments. */ allowDuplicateBrand?: boolean; }; /** * A brand-scoped codec for generating and validating Opaque Timestamp IDs. * * Same wire shape as the Timestamp codec (`{brand}_` + 26 base32 chars) but the * payload is AES-CBC encrypted. `generate`, `generateAt`, and `extractTimestamp` * are async; parsing methods are sync. No `minIdForTime` / `maxIdForTime` — * encrypted payloads do not sort by creation time. * * @remarks * **Security properties (unauthenticated, deterministic, and malleable by design):** * * - The payload is AES-CBC encrypted but **unauthenticated** — there is no * integrity tag. A tampered or wrong-key payload decrypts to garbage bytes * without throwing. * - Opaque IDs must be treated as **opaque handles**, not as trusted or * authenticated tokens. * - `extractTimestamp` is best-effort on untrusted input: a wrong or tampered * key returns a plausible-looking `Date` without error, not a verification * failure. Do not treat the returned timestamp as proof of origin. */ type OpaqueTimestampCodec = { /** Produces a new canonical encrypted ID using the codec's `now` and `rng`. */generate(): Promise>; /** Produces a new canonical encrypted ID with timestamp bytes from `date`. Throws on invalid dates. */ generateAt(date: Date): Promise>; /** * Strict type guard: `true` only for already-canonical strings for this brand. * For untrusted input, use `safeParse()` or `parse()` instead. See ADR-0003. */ is(value: unknown): value is Id; /** * Lenient parse: normalises case and Crockford aliases, returns canonical `Id`, or throws. */ parse(value: unknown): Id; /** * Lenient parse without throwing: normalises to canonical form, or returns `{ ok: false, error }`. */ safeParse(value: unknown): ParseResult; /** * Decrypts and decodes the creation `Date` from an `Id`. Trusts the type — use `safeParse()` at boundaries first. See ADR-0002. * * Requires the same key used at generation; a wrong key returns a plausible * but wrong `Date`, never an error. With rotation, select the codec for the * ID's key epoch from your own records — the library cannot. See ADR-0013. */ extractTimestamp(id: Id): Promise; /** * JSON Schema for the canonical wire form. The `pattern` matches the canonical stored * form only and is deliberately stricter than `parse()`/`safeParse()`, which accept * uppercase letters and Crockford aliases (`o`/`i`/`l`) before normalising. See ADR-0003. * The `example` is a structural placeholder (generated at construction time). */ toJsonSchema(): JsonSchema; /** Standard Schema validate entry point. */ readonly "~standard": StandardSchemaProps; /** * Converts a trusted `Id` to an RFC 9562 canonical (lowercase, hyphenated) * UUID string by reinterpreting the 16-byte payload verbatim. The payload is the * encrypted ciphertext — `toUUID` does not decrypt it. Total — cannot fail. * Returns a plain `string` (brand is shed). See ADR-0024. */ toUUID(id: Id): string; /** * Parses a UUID string into an `Id`. Accepts case-insensitive `8-4-4-4-12` * hyphenated form only. Throws `IdsError` with `code: "invalid_id"` on bad input. * See ADR-0024. */ fromUUID(value: string): Id; /** * Non-throwing UUID parse. Returns `{ ok: true, id }` or * `{ ok: false, error: "not_string" | "invalid_uuid" }`. See ADR-0024. */ safeFromUUID(value: unknown): ParseResult; }; /** * Creates an Opaque Timestamp codec for `brand` (three lowercase a–z characters). * * @param brand - Entity type brand validated once at construction. * @param opts - Required `key` (an {@link OpaqueKey} from {@link importOpaqueKey}) plus * optional `now`, `rng`, and `allowDuplicateBrand` overrides. */ declare function createOpaqueTimestampId(brand: Brand & ValidBrand, opts: OpaqueTimestampOptions): OpaqueTimestampCodec; //#endregion export { type OpaqueKey, type OpaqueKeyFormat, OpaqueTimestampCodec, OpaqueTimestampOptions, createOpaqueTimestampId, decodeOpaqueKey, encodeOpaqueKey, importOpaqueKey }; //# sourceMappingURL=opaque.d.mts.map