import { a as StandardSchemaProps, i as ParseResult, n as JsonSchema, o as ValidBrand, r as ParseError, t as Id } from "./types-hGBnCpJj.mjs"; //#region src/codecs/signed/key.d.ts /** Wire encoding for signing key raw key bytes (not Crockford base32). */ type SigningKeyFormat = "hex" | "base64url"; declare const signingKeyBrand: unique symbol; /** * Opaque imported handle for one operator signing key. * * Holds a single HMAC-SHA-256 key derived via HKDF under the domain-separation * label `@smonn/ids/signed/hmac`. The underlying `webcrypto.CryptoKey` is held * internally and never exposed to callers. Obtain handles via * {@link importSigningKey} and pass them to `createSignedTimestampId` as the * `keys` signing keyring. * * Distinct from both the **Opaque key** and the **Wrapping key** — the same * raw key material must not silently serve multiple codecs without an explicit import. */ type SigningKey = { readonly [signingKeyBrand]: "SigningKey"; }; /** * Import raw operator key material into a {@link SigningKey} handle. * * Derives a single HMAC-SHA-256 key via HKDF under the domain-separation label * `@smonn/ids/signed/hmac`. Accepts 16, 24, or 32 bytes. To store or * transport key material, use {@link encodeSigningKey} / {@link decodeSigningKey} * (`"hex"` or `"base64url"` — not Crockford base32). * * @param bytes - 16, 24, or 32 raw key bytes. * @throws {IdsError} `invalid_key_length` if `bytes.length` is not 16, 24, or 32. */ declare function importSigningKey(bytes: Uint8Array): Promise; /** * Encode raw signing operator key material for storage in env vars or secret managers. * * Supports `"hex"` (lowercase) and `"base64url"`. Output round-trips through * {@link decodeSigningKey} back to the original bytes. * * @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 encodeSigningKey(bytes: Uint8Array, format: SigningKeyFormat): string; /** * Decode key material emitted by {@link encodeSigningKey} back to raw bytes. * * The result can be passed directly to {@link importSigningKey}. * * @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 decodeSigningKey(encoded: string, format: SigningKeyFormat): Uint8Array; //#endregion //#region src/codecs/signed/index.d.ts /** * Configuration options for a Signed Timestamp codec instance. */ type SignedTimestampOptions = { /** * Non-empty ordered signing keyring. The first entry is current — the only one * `generate` / `generateAt` sign with. `verify` / `safeVerify` trial every entry * until the tag matches. Duplicate raw secrets are rejected at construction. */ keys: [SigningKey, ...SigningKey[]]; /** Returns the current timestamp in milliseconds. Defaults to `Date.now`. */ now?: () => number; /** Writes 5 random bytes into `target` for the random tail. Defaults to `crypto.getRandomValues`. */ rng?: (target: Uint8Array) => void; /** If true, silences the duplicate-brand warning in non-production environments. */ allowDuplicateBrand?: boolean; }; /** * Result returned by {@link SignedTimestampCodec.safeVerify}. * * On success, `id` is the canonical {@link Id}. * On failure, `error` is a {@link ParseError} for structural problems or * `"verification_failed"` when the HMAC tag does not match any entry in the * signing keyring. */ type SafeVerifyResult = { ok: true; id: Id; } | { ok: false; error: ParseError | "verification_failed"; }; /** * Codec returned by {@link createSignedTimestampId}. * * Keeps the 6-byte millisecond timestamp **readable and sortable** like the * Timestamp codec, but replaces half of the 10-byte random tail with a truncated * HMAC tag, making IDs **tamper-evident and verifiable without a database lookup**. * * Byte layout: `ts6 ‖ rand5 ‖ tag5` where the 40-bit tag = * `trunc(HMAC-SHA256(hmacKey, brand ‖ ts6 ‖ rand5), 40)`. * * - Async (HMAC): `generate`, `generateAt`, `verify`, `safeVerify`. * - Sync (no key / plaintext timestamp): all other methods. */ type SignedTimestampCodec = { /** Produces a canonical ID signed with the current (first) key. */generate(): Promise>; /** * Produces a canonical ID with timestamp from `date`, signed with the current key. * Throws on invalid dates. */ generateAt(date: Date): Promise>; /** * Recomputes the HMAC tag across every keyring entry. * * Throws `IdsError` with `code: "verification_failed"` if no entry matches. * Tamper of the brand, timestamp bytes, or random bytes all fail here. */ verify(id: Id): Promise; /** * Non-throwing path for untrusted input. * * Structurally parses `input` first (same rules as {@link safeParse}), then * verifies the HMAC tag. Returns `{ ok: false, error }` on any failure — * {@link ParseError} for structural problems or `"verification_failed"` for tag * mismatch — without throwing. */ safeVerify(input: unknown): Promise>; /** * Decodes the creation `Date` from an `Id`. * Sync — the 6-byte timestamp is plaintext. Trusts the type; use `safeParse()` at boundaries first. * * Best-effort: the timestamp is returned **without checking the HMAC tag** — a tampered * or unsigned ID yields the attacker-controlled timestamp without error. Call * `verify()` / `safeVerify()` first if you need an authenticated timestamp. */ extractTimestamp(id: Id): Date; /** * Tight lower bound sentinel for range scans (`ts(t) ‖ 0x00×10`). * **Not verifiable** — carries no valid tag. */ minIdForTime(date: Date): Id; /** * Tight upper bound sentinel for range scans (`ts(t) ‖ 0xff×10`). * **Not verifiable** — carries no valid tag. */ maxIdForTime(date: Date): Id; /** * Strict type guard: `true` only for already-canonical `Id` strings. * For untrusted input, use `safeParse()` or `safeVerify()` instead. */ is(value: unknown): value is Id; /** Normalise to canonical form, or throw on parse failure. */ parse(value: unknown): Id; /** Normalise to canonical form, or return `{ ok: false, error }`. */ safeParse(value: unknown): ParseResult; /** * 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. */ 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. 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; }; /** * Construct a {@link SignedTimestampCodec} for `brand`. * * `opts.keys` is a non-empty ordered signing keyring — the first entry is current * (used by `generate` / `generateAt`); all entries are tried on `verify` / * `safeVerify`; duplicate operator secrets are rejected at construction. * * @example * ```ts * const key = await importSigningKey(new Uint8Array(32)); * const usr = createSignedTimestampId("usr", { keys: [key] }); * * const id = await usr.generate(); // Id<"usr"> * await usr.verify(id); // passes * usr.extractTimestamp(id); // Date — sync, timestamp is plaintext * ``` */ declare function createSignedTimestampId(brand: Brand & ValidBrand, opts: SignedTimestampOptions): SignedTimestampCodec; //#endregion export { SafeVerifyResult, SignedTimestampCodec, SignedTimestampOptions, type SigningKey, type SigningKeyFormat, createSignedTimestampId, decodeSigningKey, encodeSigningKey, importSigningKey }; //# sourceMappingURL=signed.d.mts.map