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/wrapped/key.d.ts /** Wire encoding for wrapping operator secret bytes (not Crockford base32). */ type WrappingKeyFormat = "hex" | "base64url"; declare const wrappingKeyBrand: unique symbol; /** * Opaque imported handle for one operator wrapping secret. * * Holds derived AES and HMAC subkeys internally; callers never access subkeys * or raw `webcrypto.CryptoKey` values directly. Obtain handles via {@link importWrappingKey} * and pass them to `createWrappedKeyId` as the `keys` wrapping keyring. * * Distinct from the **Opaque key** used by `@smonn/ids/opaque` — one raw * secret must not silently serve both codecs without an explicit import. */ type WrappingKey = { readonly [wrappingKeyBrand]: "WrappingKey"; }; /** * Import raw operator secret bytes into a {@link WrappingKey} handle. * * The bytes are HKDF **input keying material**, not raw AES or HMAC keys: the * codec derives an **AES-256** subkey and an **HMAC-SHA-256** subkey via HKDF * under the labels `@smonn/ids/wrapped/aes` and `@smonn/ids/wrapped/hmac`. * Accepts 16, 24, or 32 bytes; the input size sets the entropy floor only — a * 16-byte handle still yields AES-256 and HMAC-SHA-256 subkeys with a 128-bit * entropy floor. To store or transport key material, use {@link encodeWrappingKey} / * {@link decodeWrappingKey} (`"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 importWrappingKey(bytes: Uint8Array): Promise; /** * Encode raw wrapping operator secret bytes for storage in env vars or secret managers. * * Supports `"hex"` (lowercase) and `"base64url"`. Output round-trips through * {@link decodeWrappingKey} 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 encodeWrappingKey(bytes: Uint8Array, format: WrappingKeyFormat): string; /** * Decode key material emitted by {@link encodeWrappingKey} back to raw bytes. * * The result can be passed directly to {@link importWrappingKey}. * * @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 decodeWrappingKey(encoded: string, format: WrappingKeyFormat): Uint8Array; //#endregion //#region src/codecs/wrapped/index.d.ts /** * Integer kind for a {@link WrappedKeyCodec}, fixed at construction time. * * - `"u32"` — unsigned 32-bit integer; JS type `number`, range `[0, 4 294 967 295]`. * - `"i32"` — signed 32-bit integer; JS type `number`, range `[-2 147 483 648, 2 147 483 647]`. * - `"u64"` — unsigned 64-bit integer; JS type `bigint`, range `[0n, 18 446 744 073 709 551 615n]`. * - `"i64"` — signed 64-bit integer; JS type `bigint`, range `[-9 223 372 036 854 775 808n, 9 223 372 036 854 775 807n]`. * * 32-bit kinds use safe JavaScript `number` values; 64-bit kinds always use `bigint` * even when the magnitude would fit in a `number`, preventing silent truncation or sign erasure. */ type WrappedKind = "u32" | "i32" | "u64" | "i64"; type LookupKeyForKind = K extends "u32" | "i32" ? number : bigint; /** Error vocabulary shared by {@link UnwrapResult} and {@link SafeVerifyResult}'s failure branches. */ type UnwrapError = ParseError | "verification_failed"; /** * Result returned by {@link WrappedKeyCodec.safeUnwrap}. * * On success, `id` is the canonical {@link Id} and `lookupKey` is the recovered * integer (`number` for 32-bit kinds, `bigint` for 64-bit kinds). * On failure, `error` is a {@link ParseError} for structural problems or * `"verification_failed"` when the payload is structurally valid but the * verification tag does not match any entry in the wrapping keyring. */ type UnwrapResult = { ok: true; id: Id; lookupKey: LookupKeyForKind; } | { ok: false; error: UnwrapError; }; /** * Result returned by {@link WrappedKeyCodec.safeVerify}. * * A verify-only view of {@link UnwrapResult}: the success branch carries only the * canonical `id`, dropping the recovered `lookupKey`. The failure branch is * identical to {@link UnwrapResult}'s. This shape lets the Wrapped key codec * satisfy the HTTP adapters' `IdVerifiableCodec` structural interface. See ADR-0036. */ type SafeVerifyResult = { ok: true; id: Id; } | { ok: false; error: UnwrapError; }; /** * Codec returned by {@link createWrappedKeyId}. * * Wraps a caller-owned integer **lookup key** into a public {@link Id} and * recovers it on unwrap. The codec is deterministic under fixed key material: * the same lookup key always yields the same public ID (**equality leakage**). * * - `wrap` / `unwrap` / `safeUnwrap` / `safeVerify` are async (WebCrypto). * - `is`, `parse`, `safeParse`, and `toJsonSchema` are synchronous and require * no key material — they validate prefix and base32 shape only. * - The `Kind` type parameter drives value types at the TypeScript boundary: * `u32` / `i32` → `number`; `u64` / `i64` → `bigint`. * * @remarks * **Security properties (correctness-grade verification, not AEAD):** * * - The construction is deterministic — the same lookup key always yields the * same public ID (**equality leakage**). * - The verification tag is a fixed **64-bit (8-byte) truncation** of a * domain-separated HMAC over the brand, kind, and lookup key lane. * - False-accept rate is approximately `keyring_size / 2^64` per `unwrap` * trial — correctness-grade verification, not AEAD-strength origin * authentication. * - Consumers requiring full AEAD guarantees must use a different construction. */ type WrappedKeyCodec = { /** * Wrap `lookupKey` into a public ID using the current (first) wrapping key. * * Throws if `lookupKey` is out of range or the wrong JS type for `Kind`. */ wrap(lookupKey: LookupKeyForKind): Promise>; /** * Verify the payload of a trusted `Id` and return the lookup key. * * Throws `IdsError` with `code: "verification_failed"` if no entry in the * wrapping keyring matches the payload tag. Use {@link safeUnwrap} for * untrusted input. */ unwrap(id: Id): Promise>; /** * Non-throwing path for untrusted input. * * Structurally parses `input` first (same rules as {@link safeParse}), then * verifies the payload. Returns `{ ok: false, error }` on any failure — * `ParseError` for structural problems or `"verification_failed"` for tag * mismatch — without throwing. Tamper, wrong keyring, and revoked-key cases * all surface as `"verification_failed"`. */ safeUnwrap(input: unknown): Promise>; /** * Verify-only alias of {@link safeUnwrap} for untrusted input. * * Structurally parses `input`, then verifies the payload — returning * `{ ok: true, id }` on success (dropping the recovered `lookupKey`) or * `{ ok: false, error }` on any failure, without throwing. Deliberately * redundant with {@link safeUnwrap} (a wrapped ID cannot be verified without * unwrapping it): use {@link safeUnwrap} when the lookup key is needed, and * `safeVerify` to gate on authenticity alone. This is the method that lets the * codec satisfy the HTTP adapters' `IdVerifiableCodec` interface under * `verify: true`. See ADR-0036. */ safeVerify(input: unknown): Promise>; /** Strict type guard: `true` only for already-canonical `Id` strings. */ 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; toJsonSchema(): JsonSchema; 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; }; /** * Construction options for {@link createWrappedKeyId}. */ type WrappedKeyOptions = { /** Integer kind for the codec — fixed for the lifetime of the codec. Drives the JS value type (`number` for 32-bit, `bigint` for 64-bit). */kind: K; /** Non-empty ordered wrapping keyring. The first entry is current (used by `wrap`); all entries are tried on `unwrap`. Duplicate operator secrets are rejected at construction. */ keys: [WrappingKey, ...WrappingKey[]]; /** If true, silences the duplicate-brand warning in non-production environments. */ allowDuplicateBrand?: boolean; }; /** * Construct a {@link WrappedKeyCodec} for `brand` and the given `kind`. * * `opts.kind` fixes the integer type at construction time — one brand, one * kind. `opts.keys` is a non-empty ordered wrapping keyring: the first entry * is current (used by `wrap`); all entries are tried on `unwrap`; duplicate * operator secrets are rejected at construction. * * @example * ```ts * const key = await importWrappingKey(new Uint8Array(32)); * const invoices = createWrappedKeyId("inv", { kind: "u32", keys: [key] }); * * const id = await invoices.wrap(42); // Id<"inv"> * await invoices.unwrap(id); // 42 * ``` */ declare function createWrappedKeyId(brand: Brand & ValidBrand, opts: WrappedKeyOptions): WrappedKeyCodec; //#endregion export { SafeVerifyResult, UnwrapError, UnwrapResult, WrappedKeyCodec, WrappedKeyOptions, WrappedKind, type WrappingKey, type WrappingKeyFormat, createWrappedKeyId, decodeWrappingKey, encodeWrappingKey, importWrappingKey }; //# sourceMappingURL=wrapped.d.mts.map