@cardanowall/sdk-ts 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +207 -0
  3. package/dist/client/index.cjs +2695 -0
  4. package/dist/client/index.cjs.map +1 -0
  5. package/dist/client/index.d.cts +397 -0
  6. package/dist/client/index.d.ts +397 -0
  7. package/dist/client/index.js +2641 -0
  8. package/dist/client/index.js.map +1 -0
  9. package/dist/conformance/cli.cjs +4901 -0
  10. package/dist/conformance/cli.cjs.map +1 -0
  11. package/dist/conformance/cli.d.cts +18 -0
  12. package/dist/conformance/cli.d.ts +18 -0
  13. package/dist/conformance/cli.js +4878 -0
  14. package/dist/conformance/cli.js.map +1 -0
  15. package/dist/fetch/index.cjs +335 -0
  16. package/dist/fetch/index.cjs.map +1 -0
  17. package/dist/fetch/index.d.cts +13 -0
  18. package/dist/fetch/index.d.ts +13 -0
  19. package/dist/fetch/index.js +323 -0
  20. package/dist/fetch/index.js.map +1 -0
  21. package/dist/fetch-outbound-BT5-NiYN.d.cts +76 -0
  22. package/dist/fetch-outbound-BT5-NiYN.d.ts +76 -0
  23. package/dist/hash/index.cjs +25 -0
  24. package/dist/hash/index.cjs.map +1 -0
  25. package/dist/hash/index.d.cts +1 -0
  26. package/dist/hash/index.d.ts +1 -0
  27. package/dist/hash/index.js +21 -0
  28. package/dist/hash/index.js.map +1 -0
  29. package/dist/identity/index.cjs +1388 -0
  30. package/dist/identity/index.cjs.map +1 -0
  31. package/dist/identity/index.d.cts +27 -0
  32. package/dist/identity/index.d.ts +27 -0
  33. package/dist/identity/index.js +1362 -0
  34. package/dist/identity/index.js.map +1 -0
  35. package/dist/ids/index.cjs +146 -0
  36. package/dist/ids/index.cjs.map +1 -0
  37. package/dist/ids/index.d.cts +55 -0
  38. package/dist/ids/index.d.ts +55 -0
  39. package/dist/ids/index.js +135 -0
  40. package/dist/ids/index.js.map +1 -0
  41. package/dist/index-BhnlWJAY.d.cts +10 -0
  42. package/dist/index-BhnlWJAY.d.ts +10 -0
  43. package/dist/index-Cg1QqVmA.d.cts +19 -0
  44. package/dist/index-Cg1QqVmA.d.ts +19 -0
  45. package/dist/index.cjs +7127 -0
  46. package/dist/index.cjs.map +1 -0
  47. package/dist/index.d.cts +21 -0
  48. package/dist/index.d.ts +21 -0
  49. package/dist/index.js +7004 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/merkle/index.cjs +396 -0
  52. package/dist/merkle/index.cjs.map +1 -0
  53. package/dist/merkle/index.d.cts +2 -0
  54. package/dist/merkle/index.d.ts +2 -0
  55. package/dist/merkle/index.js +387 -0
  56. package/dist/merkle/index.js.map +1 -0
  57. package/dist/types-B8Q3gW54.d.ts +123 -0
  58. package/dist/types-BQMtbRCb.d.cts +321 -0
  59. package/dist/types-BQMtbRCb.d.ts +321 -0
  60. package/dist/types-CLXdbjqr.d.cts +123 -0
  61. package/dist/verifier/index.cjs +4901 -0
  62. package/dist/verifier/index.cjs.map +1 -0
  63. package/dist/verifier/index.d.cts +176 -0
  64. package/dist/verifier/index.d.ts +176 -0
  65. package/dist/verifier/index.js +4848 -0
  66. package/dist/verifier/index.js.map +1 -0
  67. package/package.json +108 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../crypto-core/src/cbor/canonical.ts","../../../crypto-core/src/sig/ed25519.ts","../../../crypto-core/src/cose/sign1.ts","../../../crypto-core/src/hash/sha-256.ts","../../../crypto-core/src/hash/blake2b-256.ts","../../../crypto-core/src/hash/merkle-sha2-256.ts","../../../poe-standard/src/schema.ts","../../../poe-standard/src/encoder.ts","../../../poe-standard/src/chunked.ts","../../src/client/off-host-sign.ts","../../src/client/http-error.ts","../../src/client/batch-empty-error.ts","../../src/client/batch-too-large-error.ts","../../src/client/forbidden-error.ts","../../src/client/idempotency-conflict-error.ts","../../src/client/insufficient-funds-error.ts","../../src/client/insufficient-scope-error.ts","../../src/client/internal-server-error.ts","../../src/client/invalid-body-error.ts","../../src/client/malformed-cbor-error.ts","../../src/client/not-found-error.ts","../../src/client/quote-already-consumed-error.ts","../../src/client/quote-expired-error.ts","../../src/client/quote-not-found-error.ts","../../src/client/rate-limited-error.ts","../../src/client/record-not-found-error.ts","../../src/client/service-unavailable-error.ts","../../src/client/unauthorized-error.ts","../../src/client/validation-failed-error.ts","../../src/client/parse-http-error.ts","../../src/client/http-helpers.ts","../../src/client/account.ts","../../src/client/invalid-client-config-error.ts","../../src/hex.ts","../../../crypto-core/src/merkle/leaves-list.ts","../../../../node_modules/.pnpm/@noble+post-quantum@0.6.1/node_modules/@noble/post-quantum/src/utils.ts","../../../../node_modules/.pnpm/@noble+post-quantum@0.6.1/node_modules/@noble/post-quantum/src/_crystals.ts","../../../../node_modules/.pnpm/@noble+post-quantum@0.6.1/node_modules/@noble/post-quantum/src/ml-kem.ts","../../../../node_modules/.pnpm/@noble+post-quantum@0.6.1/node_modules/@noble/post-quantum/src/hybrid.ts","../../../crypto-core/src/aead/chacha20-poly1305.ts","../../../crypto-core/src/aead/xchacha20-poly1305.ts","../../../crypto-core/src/kdf/hkdf.ts","../../../crypto-core/src/kem/mlkem768x25519.ts","../../../crypto-core/src/kem/x25519.ts","../../../crypto-core/src/sealed-poe/errors.ts","../../../crypto-core/src/sealed-poe/slots-codec.ts","../../../crypto-core/src/sealed-poe/wrap.ts","../../src/client/partial-upload-error.ts","../../src/client/publish.ts","../../src/client/poe.ts","../../src/client/records.ts","../../src/client/cip309-client.ts"],"names":["encode","sortCoreDeterministic","encodeCanonicalCbor","ed","sha512","nobleSha256","blake2b","sha256","z","hashes","UTF8_ENCODER","EMPTY_BYTES","readString","DIGEST_LENGTH","abytes","randb","abytes_","opts","N","Q","F","ROOT_OF_UNITY","reverseBits","FFTCore","shake128","u32","swap32IfBE","shake256","sha3_256","sha3_512","bytesToNumberLE","bytesToNumberBE","anumber","x25519","concatBytes","asciiToBytes","chacha20poly1305","xchacha20poly1305","hkdf","CHUNK_MAX_BYTES","randomBytes","hmac","ED25519_PUBLIC_KEY_LENGTH","ED25519_SIGNATURE_LENGTH","X25519_PUBLIC_KEY_LENGTH","MLKEM768X25519_PUBLIC_KEY_LENGTH","bytesToHex","readJson","parseRetryAfter","throwIfNotOk","buildJsonHeaders","buildMultipartHeaders","buildHeaders"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBO,SAAS,oBAAoB,KAAA,EAAuC;AACzE,EAAA,OAAOA,aAAO,KAAA,EAAO;IACnB,GAAA,EAAK,IAAA;IACL,eAAA,EAAiB,IAAA;IACjB,mBAAA,EAAqB,IAAA;IACrB,QAAA,EAAUC;GACX,CAAA;AACH;AAPO,SAASC,qBAAoB,KAAA,EAAuC;AACzE,EAAA,OAAOF,aAAO,KAAA,EAAO;IACnB,GAAA,EAAK,IAAA;IACL,eAAA,EAAiB,IAAA;IACjB,mBAAA,EAAqB,IAAA;IACrB,QAAA,EAAUC;GACX,CAAA;AACH;ACpBGE,aAAA,CAAA,MAAA,CAAO,MAAA,GAASC,cAAA;AAGND,aAAA,CAAA,KAAA,CAAM,KAAA,EAAA,CAAQ;ACepB,IAAM,6BAAA,GAAgC,2BAAA;AAOtC,IAAM,mCAAA,GAAsC,IAAI,WAAA,EAAA,CAAc,MAAA;AACnE,EAAA;AACF,CAAA;AAKA,IAAI,mCAAA,CAAoC,WAAW,EAAA,EAAI;AACrD,EAAA,MAAM,IAAI,KAAA;AACR,IAAA,CAAA,4EAAA,EAA+E,oCAAoC,MAAM,CAAA;AAAA,GAAA;AAE7H;AAEA,IAAM,WAAA,GAAc,IAAI,UAAA,CAAW,CAAC,CAAA;AAqB7B,SAAS,kBAAkB,IAAA,EAAyC;AACzE,EAAA,OAAOD,oBAAAA,CAAoB;IACzB,IAAA,CAAK,OAAA;IACL,IAAA,CAAK,kBAAA;IACL,IAAA,CAAK,WAAA;IACL,IAAA,CAAK;GAC2B,CAAA;AACpC;AAcO,SAAS,wBAAwB,IA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A,GAAU,MAAA,CAAO,OAAA,EAAS,IAAA,EAAK;AACrC,EAAA,IAAI,OAAA,KAAY,MAAA,IAAa,OAAA,KAAY,EAAA,EAAI;AAC3C,IAAA,MAAM,IAAI,wBAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF;AACA,EAAA,OAAO,OAAA,CAAQ,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AAClC;AAEO,IAAM,eAAN,MAAmB;AAAA,EACR,GAAA;AAAA,EACA,OAAA;AAAA,EACA,OAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAahB,YAAY,MAAA,EAA4B;AACtC,IAAA,MAAM,SAAA,GAAY,YAAA,CAAa,MAAA,CAAO,KAAK,CAAA;AAC3C,IAAA,MAAM,OAAA,GAAU,eAAe,MAAM,CAAA;AACrC,IAAA,MAAM,WAAW,EAAE,MAAA,EAAQ,OAAO,MAAA,EAAQ,OAAA,EAAS,OAAO,SAAA,EAAU;AACpE,IAAA,IAAA,CAAK,GAAA,GAAM,IAAI,YAAA,CAAa,QAAQ,CAAA;AACpC,IAAA,IAAA,CAAK,OAAA,GAAU,IAAI,gBAAA,CAAiB,QAAQ,CAAA;AAC5C,IAAA,IAAA,CAAK,OAAA,GAAU,IAAI,gBAAA,CAAiB,QAAQ,CAAA;AAAA,EAC9C;AACF","file":"index.cjs","sourcesContent":["import { cdeDecodeOptions, decode, encode } from 'cbor2';\nimport { sortCoreDeterministic } from 'cbor2/sorts';\n\nimport { CanonicalCborError } from './errors';\n\nexport type CanonicalCborValue =\n | null\n | boolean\n | number\n | bigint\n | string\n | Uint8Array\n | readonly CanonicalCborValue[]\n | { readonly [key: string]: CanonicalCborValue }\n | ReadonlyMap<string | number, CanonicalCborValue>;\n\nexport function encodeCanonicalCbor(value: CanonicalCborValue): Uint8Array {\n return encode(value, {\n cde: true,\n collapseBigInts: true,\n rejectDuplicateKeys: true,\n sortKeys: sortCoreDeterministic,\n });\n}\n\nexport function decodeCanonicalCbor(bytes: Uint8Array): unknown {\n try {\n return decode(bytes, {\n ...cdeDecodeOptions,\n rejectStreaming: true,\n rejectDuplicateKeys: true,\n // A CIP-309 record carries integers, byte/text strings, arrays, maps and\n // `null` — and nothing else. Without these rejections the major-type-7\n // surface leaks into the decoder: a float16/32/64 that happens to hold an\n // integral value (e.g. 1.0) silently decodes to the integer 1 and passes\n // a `z.literal(1)` / Number.isInteger schema check, so two byte strings\n // that are NOT byte-identical canonicalise to the same record. That\n // breaks the cross-implementation parity invariant (the Python twin\n // already rejects non-integer `v` / `enc.scheme` outright). Reject the\n // whole non-record surface — floats, negative zero, undefined, and\n // non-{true,false,null} simple values — so any such input surfaces as\n // MALFORMED_CBOR via mapDecodeError rather than decoding to a look-alike.\n rejectFloats: true,\n rejectNegativeZero: true,\n rejectUndefined: true,\n rejectSimple: true,\n });\n } catch (cause) {\n throw mapDecodeError(cause);\n }\n}\n\nfunction mapDecodeError(cause: unknown): CanonicalCborError {\n const message = cause instanceof Error ? cause.message : String(cause);\n const lower = message.toLowerCase();\n // Every canonical-decode violation collapses to the single public taxonomy\n // code MALFORMED_CBOR: indefinite-length (streaming) items, duplicate keys,\n // non-canonical (unsorted) key ordering, non-minimal integer encodings, and\n // invalid UTF-8 in text strings. cbor2 raises the SAME \"Duplicate or out of\n // order key\" message for both true duplicates AND distinct-but-unsorted keys,\n // so the two are indistinguishable by message — and per the CIP-309 taxonomy\n // both belong under MALFORMED_CBOR anyway. The specific cause survives in the\n // human-readable message below; for indefinite-length we state it explicitly\n // so the diagnostic is not lost when the code is collapsed.\n const isIndefinite = lower.includes('streaming') || lower.includes('indefinite');\n const detail = isIndefinite\n ? `indefinite-length items are not permitted in canonical CBOR: ${message}`\n : message;\n return new CanonicalCborError('MALFORMED_CBOR', `cbor decode failed: ${detail}`, { cause });\n}\n","import * as ed from '@noble/ed25519';\nimport { sha512 } from '@noble/hashes/sha2.js';\n\ned.hashes.sha512 = sha512;\n\n// Ed25519 group order L (= 2^252 + 27742317777372353535851937790883648493).\nconst L = ed.Point.CURVE().n;\n\nexport interface SignEd25519Opts {\n readonly seed: Uint8Array;\n readonly message: Uint8Array;\n}\n\nexport interface VerifyEd25519Opts {\n readonly publicKey: Uint8Array;\n readonly message: Uint8Array;\n readonly signature: Uint8Array;\n}\n\nexport interface GetPublicKeyEd25519Opts {\n readonly seed: Uint8Array;\n}\n\nexport function signEd25519(opts: SignEd25519Opts): Uint8Array {\n return ed.sign(opts.message, opts.seed);\n}\n\n// Little-endian 32-byte scalar → bigint.\nfunction leBytesToBigInt(bytes: Uint8Array): bigint {\n let value = 0n;\n for (let i = bytes.length - 1; i >= 0; i--) {\n value = (value << 8n) | BigInt(bytes[i]!);\n }\n return value;\n}\n\n// Strict (non-cofactored) Ed25519 verification per RFC 8032 §5.1.7, matching\n// libsodium/PyNaCl `crypto_sign_verify_detached` and ed25519-dalek\n// `verify_strict`. The cofactor-less check rejects every small-order /\n// torsion-component edge case in the C2SP/CCTV corpus, which noble's\n// `{ zip215: false }` mode does NOT (it remains cofactored: it checks\n// `[8]([S]B - [k]A - R) == 0`, accepting torsion components).\n//\n// The verification equation is the unscaled `[S]B == R + [k]A`, rewritten as\n// `[S]B - [k]A - R == identity`. We reject S >= L (non-canonical scalar) and\n// any small-order A or R up front, so a torsion component can never be smuggled\n// through the cofactor multiplication the cofactored variant performs.\nexport function verifyEd25519(opts: VerifyEd25519Opts): boolean {\n const { signature, message, publicKey } = opts;\n if (signature.length !== 64 || publicKey.length !== 32) return false;\n\n // S = LE(sig[32..64]); reject if not a canonical scalar (S >= L).\n const S = leBytesToBigInt(signature.subarray(32, 64));\n if (S >= L) return false;\n\n // Decode A (public key) and R (sig[0..32]) with the canonical (non-zip215)\n // point encoding; a non-canonical encoding throws and rejects.\n let A: ed.Point;\n let R: ed.Point;\n try {\n A = ed.Point.fromBytes(publicKey);\n R = ed.Point.fromBytes(signature.subarray(0, 32));\n } catch {\n return false;\n }\n\n // Reject small-order (cofactor-torsion) A or R: this is exactly the strictness\n // that distinguishes verify_strict from the cofactored check.\n if (A.isSmallOrder() || R.isSmallOrder()) return false;\n\n // k = SHA-512(R || A || M) reduced mod L.\n const k =\n leBytesToBigInt(ed.hash(concatBytes(signature.subarray(0, 32), publicKey, message))) % L;\n\n // Accept iff [S]B - [k]A - R == identity. `multiplyUnsafe` returns the\n // identity for a 0 scalar, but guard explicitly to avoid relying on that.\n const sB = S === 0n ? ed.Point.ZERO : ed.Point.BASE.multiplyUnsafe(S);\n const kA = k === 0n ? ed.Point.ZERO : A.multiplyUnsafe(k);\n return sB.subtract(kA).subtract(R).is0();\n}\n\nfunction concatBytes(...parts: Uint8Array[]): Uint8Array {\n let total = 0;\n for (const p of parts) total += p.length;\n const out = new Uint8Array(total);\n let offset = 0;\n for (const p of parts) {\n out.set(p, offset);\n offset += p.length;\n }\n return out;\n}\n\nexport function getPublicKeyEd25519(opts: GetPublicKeyEd25519Opts): Uint8Array {\n return ed.getPublicKey(opts.seed);\n}\n","import {\n decodeCanonicalCbor,\n encodeCanonicalCbor,\n type CanonicalCborValue,\n} from '../cbor/canonical';\nimport { CanonicalCborError } from '../cbor/errors';\nimport { blake2b224 } from '../hash/blake2b-256';\nimport { signEd25519, verifyEd25519 } from '../sig/ed25519';\nimport { compareCt } from '../util/compare-ct';\n\nimport { CoseVerifyError, type CoseVerifyResult } from './errors';\n\nexport type CoseHeader = Map<number | string, unknown>;\n\n// CIP-309 v1 domain separator embedded as a prefix on `Sig_structure[3]`\n// (`to_sign`). The separator is\n// NOT placed in `Sig_structure[2]` (`external_aad`) because CIP-30 `signData`\n// — the only realistic wallet-signing path on Cardano — explicitly forbids a\n// non-empty `external_aad`. Pinning the prefix into the payload preserves the\n// anti-replay property while keeping wallet-produced signatures byte-identical\n// to verifier-side recomputation.\nexport const CARDANO_POE_SIG_DOMAIN_PREFIX = 'cardano-poe-record-sig-v1' as const;\n// Composer path-2 wallet flow consumes the prefix bytes directly\n// to assemble `toSign = prefix || canonical_cbor(record_body)` BEFORE calling\n// `walletSignData` (the wallet's `signData()` receives this concatenation as\n// its `payload` argument verbatim per CIP-30). The bytes constant is exported\n// so a composer can build the input without re-encoding the prefix at every\n// call site.\nexport const CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES = new TextEncoder().encode(\n CARDANO_POE_SIG_DOMAIN_PREFIX,\n);\n\n// Fail-fast: the prefix length is byte-pinned at 25 UTF-8 bytes. A different\n// runtime encoding would silently break round-tripping\n// against the reference vectors.\nif (CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES.length !== 25) {\n throw new Error(\n `cardano-poe-record-sig-v1 prefix must encode to exactly 25 UTF-8 bytes, got ${CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES.length}`,\n );\n}\n\nconst EMPTY_BYTES = new Uint8Array(0);\n\nexport interface CoseSign1Decoded {\n readonly protectedHeader: CoseHeader;\n // preserved for Sig_structure reconstruction — never re-encode the decoded header map (RFC 9052 §4.4)\n readonly protectedBytes: Uint8Array;\n readonly unprotectedHeader: CoseHeader;\n readonly payload: Uint8Array | null;\n readonly signature: Uint8Array;\n}\n\nexport interface BuildSigStructureArgs {\n readonly context: 'Signature1';\n readonly bodyProtectedBytes: Uint8Array;\n readonly externalAad: Uint8Array;\n readonly payload: Uint8Array;\n}\n\n// Raw RFC 9052 §4.4 Sig_structure builder. General-purpose: callers control\n// `external_aad` and `payload` exactly. For CIP-309 record signing use\n// `buildCip309SigStructure` instead — it enforces the CIP-309 record-signature invariants.\nexport function buildSigStructure(args: BuildSigStructureArgs): Uint8Array {\n return encodeCanonicalCbor([\n args.context,\n args.bodyProtectedBytes,\n args.externalAad,\n args.payload,\n ] as readonly CanonicalCborValue[]);\n}\n\nexport interface BuildCip309SigStructureArgs {\n readonly bodyProtectedBytes: Uint8Array;\n // Canonical CBOR of the record body with `sigs` removed.\n readonly recordBodyCbor: Uint8Array;\n}\n\n// CIP-309 v1 specialisation of `Sig_structure` (RFC 9052 §4.4 base structure):\n// to_sign = utf8(\"cardano-poe-record-sig-v1\") || canonical_cbor(record_body_minus_sigs)\n// Sig_structure = [ \"Signature1\", body_protected, h'' (empty), to_sign ]\n// Always forces `external_aad = h''` (empty bstr) — the CIP-30 wallet path\n// cannot carry a non-empty `external_aad`, so the domain separator lives in\n// `Sig_structure[3]` rather than `Sig_structure[2]`.\nexport function buildCip309SigStructure(args: BuildCip309SigStructureArgs): Uint8Array {\n const toSign = new Uint8Array(\n CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES.length + args.recordBodyCbor.length,\n );\n toSign.set(CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES, 0);\n toSign.set(args.recordBodyCbor, CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES.length);\n return buildSigStructure({\n context: 'Signature1',\n bodyProtectedBytes: args.bodyProtectedBytes,\n externalAad: EMPTY_BYTES,\n payload: toSign,\n });\n}\n\nexport interface EncodeCoseSign1Args {\n readonly protectedHeader: CoseHeader;\n readonly unprotectedHeader: CoseHeader;\n readonly payload: Uint8Array | null;\n readonly signature: Uint8Array;\n}\n\nexport function encodeCoseSign1(args: EncodeCoseSign1Args): Uint8Array {\n const protectedBytes =\n args.protectedHeader.size === 0\n ? EMPTY_BYTES\n : encodeCanonicalCbor(args.protectedHeader as CanonicalCborValue);\n return encodeCanonicalCbor([\n protectedBytes,\n args.unprotectedHeader as CanonicalCborValue,\n args.payload,\n args.signature,\n ] as readonly CanonicalCborValue[]);\n}\n\n// cbor2's decoder returns Map for integer-keyed maps but plain Object for empty\n// or string-keyed maps; normalise both representations to Map.\nfunction asCoseHeader(value: unknown): CoseHeader | null {\n if (value instanceof Map) return value as CoseHeader;\n if (value !== null && typeof value === 'object' && (value as object).constructor === Object) {\n return new Map(Object.entries(value as Record<string, unknown>));\n }\n return null;\n}\n\nexport function decodeCoseSign1(bytes: Uint8Array): CoseSign1Decoded {\n let arr: unknown;\n try {\n arr = decodeCanonicalCbor(bytes);\n } catch (cause) {\n throw new CoseVerifyError('MALFORMED_SIG_COSE', 'cose decode failed', { cause });\n }\n if (!Array.isArray(arr) || arr.length !== 4) {\n throw new CoseVerifyError('MALFORMED_SIG_COSE', 'expected 4-element array');\n }\n const [protectedBytesRaw, unprotectedRaw, payloadRaw, signatureRaw] = arr;\n if (!(protectedBytesRaw instanceof Uint8Array)) {\n throw new CoseVerifyError('MALFORMED_SIG_COSE', 'protected_bytes must be bytes');\n }\n const unprotectedHeader = asCoseHeader(unprotectedRaw);\n if (unprotectedHeader === null) {\n throw new CoseVerifyError('MALFORMED_SIG_COSE', 'unprotected header must be map');\n }\n if (payloadRaw !== null && !(payloadRaw instanceof Uint8Array)) {\n throw new CoseVerifyError('MALFORMED_SIG_COSE', 'payload must be bytes or null');\n }\n if (!(signatureRaw instanceof Uint8Array) || signatureRaw.length !== 64) {\n throw new CoseVerifyError('MALFORMED_SIG_COSE', 'signature must be 64 bytes');\n }\n let protectedHeader: CoseHeader;\n if (protectedBytesRaw.length === 0) {\n protectedHeader = new Map();\n } else {\n let decodedProtected: unknown;\n try {\n decodedProtected = decodeCanonicalCbor(protectedBytesRaw);\n } catch (cause) {\n throw new CoseVerifyError('MALFORMED_SIG_COSE', 'protected header decode failed', { cause });\n }\n const ph = asCoseHeader(decodedProtected);\n if (ph === null) {\n throw new CoseVerifyError('MALFORMED_SIG_COSE', 'protected header must decode to map');\n }\n // Empty protected header MUST encode as the single byte 0x40 (zero-length bstr),\n // not 0x41 0xA0 (a 1-byte bstr containing an empty CBOR map). RFC 9052 §3 +\n // CIP-309 canonical-CBOR mandate.\n if (ph.size === 0) {\n throw new CoseVerifyError(\n 'MALFORMED_SIG_COSE',\n 'empty protected header must encode as 0x40 (zero-length bstr), not as an empty map',\n );\n }\n protectedHeader = ph;\n }\n return {\n protectedHeader,\n protectedBytes: protectedBytesRaw,\n unprotectedHeader,\n payload: payloadRaw,\n signature: signatureRaw,\n };\n}\n\nexport type CoseSign1BuildErrorCode = 'SIGNER_NOT_PROVIDED' | 'SIGNER_AND_SEED_BOTH_PROVIDED';\n\nexport class CoseSign1BuildError extends Error {\n readonly code: CoseSign1BuildErrorCode;\n\n constructor(code: CoseSign1BuildErrorCode, message: string) {\n super(message);\n this.name = 'CoseSign1BuildError';\n this.code = code;\n }\n}\n\nexport interface CoseSign1Cip309BuildArgs {\n readonly protectedHeader: CoseHeader;\n readonly unprotectedHeader: CoseHeader;\n // Canonical CBOR of the record body with `sigs` removed. The\n // builder prepends the 25-byte UTF-8 domain prefix `cardano-poe-record-sig-v1`\n // internally — callers MUST NOT pre-concatenate it.\n readonly recordBodyCbor: Uint8Array;\n // EITHER the raw 32-byte Ed25519 seed (used by KAT tests, Python parity, and\n // the off-host signing helper) OR an injected signer closure that signs the\n // assembled Sig_structure bytes (composer-side use — keeps the private key\n // inside the unlock-store closure so it never escapes scope).\n // Exactly one of the two MUST be provided; mutual exclusion enforced at\n // runtime via CoseSign1BuildError.\n readonly signerSecretKey?: Uint8Array;\n readonly signer?: (sigStructureBytes: Uint8Array) => Uint8Array;\n}\n\n// CIP-309 v1 record-signature builder:\n// 1. compute `to_sign = utf8(\"cardano-poe-record-sig-v1\") || recordBodyCbor`\n// 2. Sig_structure = [ \"Signature1\", bodyProtected, h'', to_sign ]\n// 3. Ed25519-sign Sig_structure (via seed OR injected closure)\n// 4. emit COSE_Sign1 with payload = CBOR null (detached signature, mandatory)\nexport function coseSign1Cip309Build(args: CoseSign1Cip309BuildArgs): Uint8Array {\n if (args.signerSecretKey === undefined && args.signer === undefined) {\n throw new CoseSign1BuildError(\n 'SIGNER_NOT_PROVIDED',\n 'coseSign1Cip309Build requires either signerSecretKey or signer',\n );\n }\n if (args.signerSecretKey !== undefined && args.signer !== undefined) {\n throw new CoseSign1BuildError(\n 'SIGNER_AND_SEED_BOTH_PROVIDED',\n 'coseSign1Cip309Build accepts signerSecretKey XOR signer (not both)',\n );\n }\n const protectedBytes =\n args.protectedHeader.size === 0\n ? EMPTY_BYTES\n : encodeCanonicalCbor(args.protectedHeader as CanonicalCborValue);\n const sigStructureBytes = buildCip309SigStructure({\n bodyProtectedBytes: protectedBytes,\n recordBodyCbor: args.recordBodyCbor,\n });\n let signature: Uint8Array;\n if (args.signer !== undefined) {\n signature = args.signer(sigStructureBytes);\n if (!(signature instanceof Uint8Array) || signature.length !== 64) {\n throw new CoseSign1BuildError(\n 'SIGNER_NOT_PROVIDED',\n `injected signer must return a 64-byte Uint8Array; got ${signature instanceof Uint8Array ? `${signature.length}-byte Uint8Array` : typeof signature}`,\n );\n }\n } else {\n signature = signEd25519({ seed: args.signerSecretKey!, message: sigStructureBytes });\n }\n return encodeCoseSign1({\n protectedHeader: args.protectedHeader,\n unprotectedHeader: args.unprotectedHeader,\n payload: null,\n signature,\n });\n}\n\nexport interface CoseSign1Cip309VerifyArgs {\n readonly message: Uint8Array;\n // Canonical CBOR of the record body with `sigs` removed (verifier-recomputed;\n // the 25-byte UTF-8 prefix is prepended internally — callers\n // MUST NOT pre-concatenate it).\n readonly detachedRecordBodyCbor: Uint8Array;\n // Optional out-of-band signer key (path-2 wallet path resolves the key from\n // `sigs[i].cose_key`). Path-1 records carry the 32-byte raw Ed25519 pubkey\n // in the protected header at label 4 (`kid`) and need no out-of-band hint.\n readonly expectedSignerKey?: Uint8Array;\n}\n\n// CIP-309 v1 record-signature verifier:\n// - Decode COSE_Sign1\n// - Reject COSE_Sign1[2] != CBOR null (attached payload — including h'') as\n// MALFORMED_SIG_COSE_SIGN1\n// - Recompute to_sign = utf8(\"cardano-poe-record-sig-v1\") || detachedRecordBodyCbor\n// - Sig_structure = [ \"Signature1\", protectedBytes, h'', to_sign ]\n// - Strict Ed25519 verify (RFC 8032 §5.1.7 — `zip215: false` per ed25519.ts)\n//\n// The verifier does NOT accept an `externalAad` argument: CIP-309 v1 pins\n// `external_aad = h''` and any deviation would either silently weaken the\n// domain separator or quietly accept malformed records. If a future CIP\n// revision re-enables external_aad, this helper takes a v-bump.\nexport function coseSign1Cip309Verify(args: CoseSign1Cip309VerifyArgs): CoseVerifyResult {\n let decoded: CoseSign1Decoded;\n try {\n decoded = decodeCoseSign1(args.message);\n } catch (e) {\n if (e instanceof CoseVerifyError) {\n return { ok: false, error: { code: e.code, message: 'errors.cose.malformed' } };\n }\n if (e instanceof CanonicalCborError) {\n return {\n ok: false,\n error: { code: 'MALFORMED_SIG_COSE', message: 'errors.cose.malformed_cbor' },\n };\n }\n throw e;\n }\n // CIP-309 v1 mandate: COSE_Sign1[2] (payload field) MUST be CBOR `null` (0xF6).\n // Any non-null payload — including a zero-length byte string `h''` — MUST\n // be rejected as MALFORMED_SIG_COSE_SIGN1.\n if (decoded.payload !== null) {\n return {\n ok: false,\n error: {\n code: 'MALFORMED_SIG_COSE_SIGN1',\n message: 'errors.cose.attached_payload_forbidden',\n },\n };\n }\n const alg = decoded.protectedHeader.get(1);\n if (typeof alg !== 'number' || alg !== -8) {\n return {\n ok: false,\n error: { code: 'UNSUPPORTED_SIG_ALG', message: 'errors.cose.unsupported_alg' },\n };\n }\n const kidRaw = decoded.protectedHeader.get(4);\n let signerKey: Uint8Array | undefined;\n if (kidRaw instanceof Uint8Array && kidRaw.length === 32) {\n signerKey = kidRaw;\n } else if (args.expectedSignerKey instanceof Uint8Array && args.expectedSignerKey.length === 32) {\n signerKey = args.expectedSignerKey;\n }\n if (signerKey === undefined) {\n return {\n ok: false,\n error: { code: 'KID_UNRESOLVED', message: 'errors.cose.kid_unresolved' },\n };\n }\n // When both a protected-header kid AND an expectedSignerKey are provided,\n // require they agree (constant-time). A protected kid that disagrees with\n // the caller's out-of-band binding is a misuse, not a transient mismatch.\n if (\n kidRaw instanceof Uint8Array &&\n kidRaw.length === 32 &&\n args.expectedSignerKey instanceof Uint8Array &&\n args.expectedSignerKey.length === 32 &&\n !compareCt(kidRaw, args.expectedSignerKey)\n ) {\n return {\n ok: false,\n error: { code: 'KID_UNRESOLVED', message: 'errors.cose.kid_mismatch' },\n };\n }\n // CIP-8 `hashed = true` mode (the wallet-signed path-2 variant). The unprotected\n // header carries the literal text key `\"hashed\"` with boolean value `true`\n // (text-keyed CBOR maps decode to `Map<string, unknown>` via cbor2). When\n // set, both producer and verifier build `Sig_structure[3] = Blake2b-224(to_sign)`\n // (28-byte digest of the FULL `to_sign` payload including the 25-byte\n // domain prefix). When absent or false, the standard non-hashed path\n // applies unchanged.\n const hashedFlag = decoded.unprotectedHeader.get('hashed');\n let sigStructureBytes: Uint8Array;\n if (hashedFlag === true) {\n const toSign = new Uint8Array(\n CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES.length + args.detachedRecordBodyCbor.length,\n );\n toSign.set(CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES, 0);\n toSign.set(args.detachedRecordBodyCbor, CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES.length);\n const hashedPayload = blake2b224(toSign);\n sigStructureBytes = buildSigStructure({\n context: 'Signature1',\n bodyProtectedBytes: decoded.protectedBytes,\n externalAad: EMPTY_BYTES,\n payload: hashedPayload,\n });\n } else {\n sigStructureBytes = buildCip309SigStructure({\n bodyProtectedBytes: decoded.protectedBytes,\n recordBodyCbor: args.detachedRecordBodyCbor,\n });\n }\n const valid = verifyEd25519({\n publicKey: signerKey,\n message: sigStructureBytes,\n signature: decoded.signature,\n });\n if (!valid) {\n return {\n ok: false,\n error: { code: 'SIGNATURE_INVALID', message: 'errors.cose.signature_invalid' },\n };\n }\n return { ok: true, signerKey, alg };\n}\n","import { sha256 as nobleSha256 } from '@noble/hashes/sha2.js';\n\nexport function sha256(input: Uint8Array): Uint8Array {\n return nobleSha256(input);\n}\n","import { blake2b } from '@noble/hashes/blake2.js';\n\nexport function blake2b256(input: Uint8Array): Uint8Array {\n return blake2b(input, { dkLen: 32 });\n}\n\n// CIP-19 stake-address derivation, used for the wallet path-2 signer binding,\n// requires the 28-byte BLAKE2b digest of the signer's Ed25519 public key.\n// The Cardano ledger encodes stake addresses as\n// `network_header_byte || Blake2b-224(stake_vk)`\n// per CIP-19, so this output length is fixed by spec.\nexport function blake2b224(input: Uint8Array): Uint8Array {\n return blake2b(input, { dkLen: 28 });\n}\n","// RFC 9162 §2.1.1 binary Merkle tree under SHA-256.\n// This implements the algorithm tier identified on the wire as the\n// `rfc9162-sha256` OPT-INFO; the record's `merkle[]` field carries the proof.\n//\n// Construction (RFC 9162 §2.1.1):\n// - Single leaf: MTH({d_0}) = SHA-256(0x00 || d_0)\n// - Internal node: MTH(L) = SHA-256(0x01 || MTH(L[0:k]) || MTH(L[k:n]))\n// where k = largest power of 2 strictly less than n.\n// - Empty trees (n == 0) are FORBIDDEN.\n// - The 0x00 leaf / 0x01 internal prefixes prevent the CVE-2012-2459\n// leaf-vs-internal collision family.\n\nimport { sha256 } from '@noble/hashes/sha2.js';\n\nimport { compareCt } from '../util/compare-ct';\n\nexport const MERKLE_ALG_ID = 'rfc9162-sha256' as const;\n\nconst LEAF_PREFIX = 0x00;\nconst NODE_PREFIX = 0x01;\nconst DIGEST_LENGTH = 32;\n\nfunction validateLeaves(leaves: ReadonlyArray<Uint8Array>, fnName: string): void {\n if (leaves.length === 0) {\n throw new Error(`${fnName}: empty leaf list (n == 0 is forbidden by RFC 9162 §2.1.1)`);\n }\n for (let i = 0; i < leaves.length; i++) {\n const leaf = leaves[i];\n if (!(leaf instanceof Uint8Array) || leaf.length !== DIGEST_LENGTH) {\n throw new Error(\n `${fnName}: leaf[${i}] must be a Uint8Array(${DIGEST_LENGTH}); got length ${\n leaf instanceof Uint8Array ? leaf.length : 'non-Uint8Array'\n }`,\n );\n }\n }\n}\n\nexport function merkleSha2256Root(leaves: ReadonlyArray<Uint8Array>): Uint8Array {\n validateLeaves(leaves, 'merkleSha2256Root');\n return mthRecursive(leaves, 0, leaves.length);\n}\n\nexport function merkleSha2256InclusionProof(\n leaves: ReadonlyArray<Uint8Array>,\n index: number,\n): Uint8Array[] {\n validateLeaves(leaves, 'merkleSha2256InclusionProof');\n if (!Number.isInteger(index) || index < 0 || index >= leaves.length) {\n throw new Error(\n `merkleSha2256InclusionProof: index ${index} out of range [0, ${leaves.length})`,\n );\n }\n return auditPath(leaves, index, 0, leaves.length);\n}\n\n/**\n * Verify an inclusion proof per RFC 9162 §2.1.3.2 (iterative form).\n *\n * `proof` is ordered leaf-to-root: `proof[0]` is the sibling at the leaf\n * level, `proof[m-1]` is the top-level sibling. The fold uses the\n * `sn`/`fn` tracking from RFC 9162: `sn` is the leaf index within the\n * current subtree, `fn` is (subtree_size - 1). At each step, `sn` odd\n * OR `sn == fn` means the current node is a right child (sibling on\n * the left); otherwise it is a left child (sibling on the right).\n * Both shift right by one each iteration. This handles non-power-of-2\n * sizes including the \"promote a lone right subtree\" cases.\n */\nexport function merkleSha2256VerifyInclusion(\n leaf: Uint8Array,\n index: number,\n treeSize: number,\n proof: ReadonlyArray<Uint8Array>,\n root: Uint8Array,\n): boolean {\n if (!(leaf instanceof Uint8Array) || leaf.length !== DIGEST_LENGTH) return false;\n if (!(root instanceof Uint8Array) || root.length !== DIGEST_LENGTH) return false;\n if (\n !Number.isInteger(index) ||\n !Number.isInteger(treeSize) ||\n treeSize < 1 ||\n index < 0 ||\n index >= treeSize\n ) {\n return false;\n }\n for (let i = 0; i < proof.length; i++) {\n const sibling = proof[i];\n if (!(sibling instanceof Uint8Array) || sibling.length !== DIGEST_LENGTH) {\n return false;\n }\n }\n\n if (treeSize === 1) {\n if (proof.length !== 0 || index !== 0) return false;\n return compareCt(hashLeaf(leaf), root);\n }\n\n let h = hashLeaf(leaf);\n let sn = index;\n let fn = treeSize - 1;\n for (let i = 0; i < proof.length; i++) {\n if (fn === 0) return false;\n const sibling = proof[i] as Uint8Array;\n if ((sn & 1) === 1 || sn === fn) {\n h = hashNode(sibling, h);\n while ((sn & 1) === 0 && sn !== 0) {\n sn >>>= 1;\n fn >>>= 1;\n }\n } else {\n h = hashNode(h, sibling);\n }\n sn >>>= 1;\n fn >>>= 1;\n }\n if (fn !== 0) return false;\n return compareCt(h, root);\n}\n\nfunction largestPow2Lt(n: number): number {\n let k = 1;\n while (k * 2 < n) k *= 2;\n return k;\n}\n\nfunction hashLeaf(d: Uint8Array): Uint8Array {\n const buf = new Uint8Array(1 + d.length);\n buf[0] = LEAF_PREFIX;\n buf.set(d, 1);\n return sha256(buf);\n}\n\nfunction hashNode(left: Uint8Array, right: Uint8Array): Uint8Array {\n const buf = new Uint8Array(1 + left.length + right.length);\n buf[0] = NODE_PREFIX;\n buf.set(left, 1);\n buf.set(right, 1 + left.length);\n return sha256(buf);\n}\n\nfunction mthRecursive(leaves: ReadonlyArray<Uint8Array>, start: number, end: number): Uint8Array {\n const n = end - start;\n if (n === 1) {\n return hashLeaf(leaves[start] as Uint8Array);\n }\n const k = largestPow2Lt(n);\n const left = mthRecursive(leaves, start, start + k);\n const right = mthRecursive(leaves, start + k, end);\n return hashNode(left, right);\n}\n\nfunction auditPath(\n leaves: ReadonlyArray<Uint8Array>,\n i: number,\n start: number,\n end: number,\n): Uint8Array[] {\n const n = end - start;\n if (n === 1) return [];\n const k = largestPow2Lt(n);\n if (i < k) {\n const subPath = auditPath(leaves, i, start, start + k);\n subPath.push(mthRecursive(leaves, start + k, end));\n return subPath;\n }\n const subPath = auditPath(leaves, i - k, start + k, end);\n subPath.push(mthRecursive(leaves, start, start + k));\n return subPath;\n}\n","// CIP-309 v1 PoE record Zod schemas.\n//\n// Scope: structural shape gate. The schema enforces per-field types, length\n// bounds (chunk size, digest length, supersedes length, nonce length,\n// passphrase salt length), closed-map invariants (`sigs[i]`, `slot`,\n// `passphrase`, `merkle[i]`), and the `v == 1` literal. Cross-field rules\n// (item.hashes content-hash binding when `enc` present, slots/passphrase\n// exclusivity, `crit[]` shape, registry membership of algorithm\n// identifiers, COSE_Sign1 structural decode, URI per-scheme shape rules)\n// fire in `validator.ts` so the validator can emit the precise structural\n// codes (`UNSUPPORTED_*_ALG`, `ENC_*`, `SIG_*`, `INVALID_URI`,\n// `CRIT_SHAPE_INVALID`, …) rather than a generic schema-mismatch.\n//\n// Refinements that DO live in the schema (because the validator's domain\n// pass lifts these as `SCHEMA_*` / `*_LENGTH_MISMATCH` codes directly):\n// - chunk size `[1, 64]` → `CHUNK_TOO_LARGE`\n// - 32-byte digest / 32-byte root / 32-byte supersedes → `HASH_DIGEST_LENGTH_MISMATCH`\n// / `SUPERSEDES_TX_INVALID_LENGTH`\n// - 24-byte nonce / 32-byte slots_mac →\n// `NONCE_LENGTH_MISMATCH` / `ENC_SLOTS_MAC_INVALID_LENGTH`\n// - passphrase salt 16..64 bytes → `ENC_PASSPHRASE_SALT_TOO_SHORT` /\n// `ENC_PASSPHRASE_SALT_TOO_LONG`\n//\n// Per-slot recipient lengths (`epk`, `kem_ct`, `wrap`) are NOT enforced here:\n// the required slot shape depends on the envelope-level `kem`, which a slot\n// cannot see in isolation. The KEM-driven slot descriptor in `validator.ts`\n// emits the precise `KEM_EPK_LENGTH_MISMATCH` / `KEM_CT_LENGTH_MISMATCH` /\n// `WRAP_LENGTH_MISMATCH` / `ENC_SLOT_INVALID_SHAPE` codes instead.\n\nimport { z } from 'zod';\n\n// =============================================================================\n// Chunked-bytes / chunked-text arrays\n// =============================================================================\n\n// `[1* bstr .size (1..64)]`. A zero-length chunk (0 < 1) is rejected with the\n// SAME `CHUNK_TOO_LARGE` code as oversized chunks (any length outside\n// `[1, 64]`).\nexport const ChunkedBytesArraySchema = z\n .array(\n z.instanceof(Uint8Array).refine((b) => b.length >= 1 && b.length <= 64, {\n params: { code: 'CHUNK_TOO_LARGE' },\n }),\n )\n .min(1);\nexport type ChunkedBytesArray = z.infer<typeof ChunkedBytesArraySchema>;\n\n// `[1* tstr .size (1..64)]` — chunk byte length is the UTF-8-encoded length\n// (each `tstr` is wire-encoded as UTF-8). The `tstr .size (1..64)` pin is a\n// byte-count constraint, not a code-unit constraint.\nconst UTF8_ENCODER = new TextEncoder();\nexport const UriChunkArraySchema = z\n .array(\n z.string().refine(\n (s) => {\n const n = UTF8_ENCODER.encode(s).length;\n return n >= 1 && n <= 64;\n },\n { params: { code: 'CHUNK_TOO_LARGE' } },\n ),\n )\n .min(1);\nexport type UriChunkArray = z.infer<typeof UriChunkArraySchema>;\n\n// =============================================================================\n// Hashes map\n// =============================================================================\n//\n// `hashes` is a non-empty CBOR map keyed by content-hash algorithm identifier\n// (a CBOR text string from the content-hash registry) with the 32-byte digest\n// as value. cbor2 surfaces a text-keyed CBOR map as a plain JS object — z.record\n// admits any string key here. Both the registry-membership check\n// (`UNSUPPORTED_HASH_ALG`) and the per-algorithm digest-length check\n// (`HASH_DIGEST_LENGTH_MISMATCH`) live in the validator's domain pass so\n// each violation emits its precise code; the schema only enforces the\n// value is a CBOR byte string.\n\nexport const HashDigestSchema = z.instanceof(Uint8Array);\n\nexport const HashesMapSchema = z.record(z.string(), HashDigestSchema);\nexport type HashesMap = z.infer<typeof HashesMapSchema>;\n\n// =============================================================================\n// Top-level `merkle[]`\n// =============================================================================\n//\n// Each commit is a closed map `{alg, root, leaf_count, ? uris}`. `alg` is open\n// (registry membership is enforced in the validator's domain pass — unknown\n// identifiers emit `UNSUPPORTED_MERKLE_COMMIT_ALG`).\n\nexport const MerkleCommitSchema = z\n .object({\n alg: z.string(),\n root: z.instanceof(Uint8Array),\n leaf_count: z.number().int().min(1),\n uris: z.array(UriChunkArraySchema).min(1).optional(),\n })\n .strict();\nexport type MerkleCommit = z.infer<typeof MerkleCommitSchema>;\n\n// =============================================================================\n// Encryption envelope\n// =============================================================================\n\n// Per-slot recipient entry. The slot shape is KEM-driven:\n//\n// - x25519: `{ epk: bstr(32), wrap: bstr(48) }` — `epk` is the\n// ephemeral X25519 public key, `wrap` is the 32-byte CEK + 16-byte\n// ChaCha20-Poly1305 tag.\n// - mlkem768x25519: `{ kem_ct: [ bstr .size (1..64) ], wrap: bstr(48) }` —\n// `kem_ct` is the 1120-byte X-Wing `enc` carried as a chunked byte-string\n// array (the same `bytes-chunk-array` shape `sigs[i].cose_sign1` uses);\n// there is NO per-slot `epk` on the hybrid path.\n//\n// The `kem` identifier is hoisted to envelope scope (a per-slot `kem` would\n// be wire-bloat). The schema is deliberately PERMISSIVE:\n// `epk`, `kem_ct`, and `wrap` are all optional and `.strict()` is NOT applied.\n// Both the per-field length checks (`KEM_EPK_LENGTH_MISMATCH`,\n// `KEM_CT_LENGTH_MISMATCH`, `WRAP_LENGTH_MISMATCH`) and the KEM-driven\n// shape gate (which field MUST/MUST NOT be present for the declared `kem`,\n// emitting `ENC_SLOT_INVALID_SHAPE`) live in the validator's domain pass —\n// the structural schema cannot know the envelope `kem` from a slot in\n// isolation, and we want the precise KEM-aware code rather than a generic\n// schema mismatch. Because `.strict()` is dropped, the domain pass MUST\n// explicitly reject cross-KEM contamination (an x25519 slot carrying\n// `kem_ct`, or a hybrid slot carrying `epk`).\nexport const SlotSchema = z.object({\n epk: z.instanceof(Uint8Array).optional(),\n kem_ct: ChunkedBytesArraySchema.optional(),\n wrap: z.instanceof(Uint8Array).optional(),\n});\nexport type Slot = z.infer<typeof SlotSchema>;\n\n// Argon2id params `{m, t, p}` are a closed map. Each value MUST be a CBOR\n// unsigned integer; the FLOOR check (`m ≥ 65536`,\n// `t ≥ 3`, `p ≥ 1`) emits `ENC_PASSPHRASE_ARGON2_PARAMS_TOO_LOW` in the\n// validator's domain pass — keeping it out of the schema lets us emit the\n// distinct salt-length code when salt itself is malformed too.\nexport const Argon2idParamsSchema = z\n .object({\n m: z.number().int(),\n t: z.number().int(),\n p: z.number().int(),\n })\n .strict();\nexport type Argon2idParams = z.infer<typeof Argon2idParamsSchema>;\n\n// Passphrase block. `alg` is open (registry membership checked in the\n// validator's domain pass → `ENC_PASSPHRASE_ALG_UNSUPPORTED`);\n// `params` is open here (validator narrows on the registered `alg` value and\n// emits `SCHEMA_UNKNOWN_FIELD` for extra keys, `ENC_PASSPHRASE_ARGON2_PARAMS_TOO_LOW`\n// for sub-floor values). `salt` length floor/ceiling are schema-layer\n// refinements with the dedicated `ENC_PASSPHRASE_SALT_TOO_SHORT/TOO_LONG`\n// codes — they belong at the schema layer because a slot cannot otherwise\n// see the salt length.\nexport const PassphraseBlockSchema = z\n .object({\n alg: z.string(),\n salt: z.instanceof(Uint8Array).superRefine((bytes, ctx) => {\n if (bytes.length < 16) {\n ctx.addIssue({\n code: 'custom',\n path: [],\n message: `passphrase.salt length ${bytes.length} < 16`,\n params: { code: 'ENC_PASSPHRASE_SALT_TOO_SHORT' },\n });\n } else if (bytes.length > 64) {\n ctx.addIssue({\n code: 'custom',\n path: [],\n message: `passphrase.salt length ${bytes.length} > 64`,\n params: { code: 'ENC_PASSPHRASE_SALT_TOO_LONG' },\n });\n }\n }),\n params: z.record(z.string(), z.unknown()),\n })\n .strict();\nexport type PassphraseBlock = z.infer<typeof PassphraseBlockSchema>;\n\n// Sealed-PoE envelope. The wire format admits any combination of\n// `kem` / `slots` / `slots_mac` / `passphrase` keys (permissive superset);\n// cross-field invariants (slots ⊕ passphrase, slots ↔ slots_mac, slots\n// requires kem, content-hash binding, slots non-empty) are enforced in the\n// validator's domain pass so each violation emits its typed code rather\n// than a generic shape mismatch.\nexport const EncryptionEnvelopeSchema = z\n .object({\n scheme: z.unknown(),\n aead: z.string(),\n kem: z.string().optional(),\n nonce: z.instanceof(Uint8Array),\n slots: z.array(SlotSchema).optional(),\n slots_mac: z\n .instanceof(Uint8Array)\n .refine((b) => b.length === 32, {\n params: { code: 'ENC_SLOTS_MAC_INVALID_LENGTH' },\n })\n .optional(),\n passphrase: PassphraseBlockSchema.optional(),\n })\n .strict();\nexport type EncryptionEnvelope = z.infer<typeof EncryptionEnvelopeSchema>;\n\n// =============================================================================\n// Item entry\n// =============================================================================\n\nexport const ItemEntrySchema = z\n .object({\n hashes: HashesMapSchema,\n uris: z.array(UriChunkArraySchema).min(1).optional(),\n // Captured as `unknown` so the validator can run the\n // `ENC_REQUIRES_CONTENT_HASH` pre-check ahead of any inner-shape errors\n // and surface the most informative code first.\n enc: z.unknown().optional(),\n })\n .strict();\nexport type ItemEntry = z.infer<typeof ItemEntrySchema>;\n\n// =============================================================================\n// Sig entry\n// =============================================================================\n//\n// Closed CBOR map `{cose_sign1, ? cose_key}`. Canonical CBOR map-key sort\n// (RFC 8949 §4.2.1, bytewise lex on encoded keys) places `cose_key`\n// (length-8 tstr, `0x68`) BEFORE `cose_sign1` (length-10 tstr, `0x6a`); the\n// schema property-order is irrelevant — the canonical encoder handles it.\nexport const SigEntrySchema = z\n .object({\n cose_key: ChunkedBytesArraySchema.optional(),\n cose_sign1: ChunkedBytesArraySchema,\n })\n .strict();\nexport type SigEntry = z.infer<typeof SigEntrySchema>;\n\n// =============================================================================\n// Supersedence\n// =============================================================================\n\nexport const SupersedesSchema = z.instanceof(Uint8Array).refine((b) => b.length === 32, {\n params: { code: 'SUPERSEDES_TX_INVALID_LENGTH' },\n});\nexport type Supersedes = z.infer<typeof SupersedesSchema>;\n\n// =============================================================================\n// Top-level record\n// =============================================================================\n//\n// `v == 1` is a literal — a future major (`v: 2`) MUST be rejected with\n// `SCHEMA_INVALID_LITERAL`. `z.literal(1)` preserves the narrow `1` type for\n// the inferred `PoeRecord[\"v\"]` (so consumers can dispatch on it) and emits\n// Zod's `invalid_value` code which the validator's mapper lifts to\n// `SCHEMA_INVALID_LITERAL`.\n//\n// `looseObject` admits extension keys (matching `^x-.+` or `^[a-z]+-.+`); the\n// validator's domain pass rejects unknown keys that match neither pattern with\n// `SCHEMA_UNKNOWN_FIELD`.\nexport const VersionLiteralSchema = z.literal(1);\n\nexport const PoeRecordSchema = z.looseObject({\n v: VersionLiteralSchema,\n items: z.array(ItemEntrySchema).optional(),\n merkle: z.array(MerkleCommitSchema).optional(),\n supersedes: SupersedesSchema.optional(),\n sigs: z.array(SigEntrySchema).optional(),\n crit: z.array(z.string()).optional(),\n});\nexport type PoeRecord = z.infer<typeof PoeRecordSchema>;\n\n// =============================================================================\n// Closed top-level base-key registry\n// =============================================================================\n//\n// Used by the validator's domain pass to distinguish unknown-typo keys from\n// well-formed extension keys (`^x-.+` / `^[a-z]+-.+`).\nexport const TOP_LEVEL_BASE_KEYS: ReadonlySet<string> = new Set([\n 'v',\n 'items',\n 'merkle',\n 'supersedes',\n 'sigs',\n 'crit',\n]);\n\n// Extension-key namespaces. Anchored at both ends so an\n// embedded newline cannot smuggle a multi-segment key past the check: `.`\n// excludes `\\n` in JS, and the `\\n?$` tail tolerates exactly ONE trailing\n// newline (matching the Python validator's `re.fullmatch(r'^(x-.+|[a-z]+-.+)$')`\n// semantics, where `$` likewise admits a single trailing `\\n`). So `x-note\\n`\n// is an extension key, but `x-a\\nb`, `x-note\\n\\n`, and `x-\\n` are not.\nexport const EXTENSION_KEY_VENDOR_RE = /^x-.+\\n?$/;\nexport const EXTENSION_KEY_COMPANION_RE = /^[a-z]+-.+\\n?$/;\n\nexport function isExtensionKey(k: string): boolean {\n return EXTENSION_KEY_VENDOR_RE.test(k) || EXTENSION_KEY_COMPANION_RE.test(k);\n}\n","// CIP-309 v1 record encoder.\n//\n// Produces canonical CBOR bytes per RFC 8949 §4.2.1 deterministic encoding —\n// definite-length, sorted bytewise lex map keys, no duplicates, preferred\n// integer/float form. The canonical layer (`@cardanowall/crypto-core/cbor`)\n// configures `cbor2` with `cde: true, rejectDuplicateKeys: true`, so the\n// encoder's only job is to translate the validator-typed record shape into\n// the `CanonicalCborValue` algebra.\n//\n// Wire-shape contract:\n// - `items[i].hashes` is a CBOR MAP (text-keyed) — not an array of `{alg,h}`.\n// - `merkle[]` is a top-level array, peer to `items` and `sigs`.\n// - Each `sigs[i]` is a CBOR MAP `{cose_sign1, ? cose_key}` (canonical\n// sort places the optional `cose_key` BEFORE `cose_sign1`).\n// - The encryption envelope uses `scheme` (NOT `v`), `aead` (NOT `alg`),\n// `nonce` (NOT `iv`), `slots` (NOT `recipients`), `slots_mac` (NOT\n// `hdr_mac`); the KEM identifier is hoisted to envelope scope as `kem`.\n// - The passphrase block uses key name `passphrase` and `alg = \"argon2id\"`.\n//\n// Round-trip property: for every record `R` that the validator accepts,\n// validate(encode(R)).ok === true\n// && validate(encode(R)).record ≡ R (modulo CBOR-canonical key sort)\n\nimport { encodeCanonicalCbor, type CanonicalCborValue } from '@cardanowall/crypto-core/cbor';\n\nimport type {\n EncryptionEnvelope,\n ItemEntry,\n MerkleCommit,\n PassphraseBlock,\n PoeRecord,\n SigEntry,\n Slot,\n} from './schema';\n\ntype CborMap = { [key: string]: CanonicalCborValue };\n\nexport function encodePoeRecord(record: PoeRecord): Uint8Array {\n return encodeCanonicalCbor(recordToCbor(record));\n}\n\n// Helper: build the canonical-CBOR `record_body` (the bytes that record-level\n// `sigs[i]` signs over). The body is the full record map MINUS the `sigs`\n// field; producers prepend the 25-byte UTF-8 domain prefix\n// `cardano-poe-record-sig-v1` before invoking Ed25519 (the crypto-core\n// helper `buildCip309SigStructure` handles the prefix and `Sig_structure`\n// wrapping).\nexport function encodeRecordBodyForSigning(record: PoeRecord): Uint8Array {\n const body: CborMap = recordToCborInternal(record, /* includeSigs */ false);\n return encodeCanonicalCbor(body);\n}\n\nfunction recordToCbor(record: PoeRecord): CanonicalCborValue {\n return recordToCborInternal(record, /* includeSigs */ true);\n}\n\nfunction recordToCborInternal(record: PoeRecord, includeSigs: boolean): CborMap {\n const out: CborMap = { v: record.v };\n if (record.items !== undefined) out['items'] = record.items.map(itemToCbor);\n if (record.merkle !== undefined) out['merkle'] = record.merkle.map(merkleToCbor);\n if (record.supersedes !== undefined) out['supersedes'] = record.supersedes;\n if (includeSigs && record.sigs !== undefined) out['sigs'] = record.sigs.map(sigEntryToCbor);\n if (record.crit !== undefined) out['crit'] = record.crit.slice();\n // Preserve extension keys verbatim — they are part of the signed\n // `record_body` and MUST round-trip byte-identical.\n for (const [k, v] of Object.entries(record)) {\n if (\n k === 'v' ||\n k === 'items' ||\n k === 'merkle' ||\n k === 'supersedes' ||\n k === 'sigs' ||\n k === 'crit'\n ) {\n continue;\n }\n out[k] = v as CanonicalCborValue;\n }\n return out;\n}\n\nfunction itemToCbor(item: ItemEntry): CanonicalCborValue {\n const out: CborMap = { hashes: hashesToCbor(item.hashes) };\n if (item.uris !== undefined) {\n out['uris'] = item.uris.map((chunks) => chunks.slice());\n }\n if (item.enc !== undefined) {\n out['enc'] = envelopeToCbor(item.enc as EncryptionEnvelope);\n }\n return out;\n}\n\nfunction hashesToCbor(hashes: Readonly<Record<string, Uint8Array>>): CanonicalCborValue {\n // text-keyed CBOR map — canonical sort orders by encoded-key bytewise lex\n // automatically (`sha2-256` `0x68` precedes `blake2b-256` `0x6b`).\n const out: CborMap = {};\n for (const [alg, digest] of Object.entries(hashes)) {\n out[alg] = digest;\n }\n return out;\n}\n\nfunction merkleToCbor(commit: MerkleCommit): CanonicalCborValue {\n const out: CborMap = {\n alg: commit.alg,\n root: commit.root,\n leaf_count: commit.leaf_count,\n };\n if (commit.uris !== undefined) {\n out['uris'] = commit.uris.map((chunks) => chunks.slice());\n }\n return out;\n}\n\nfunction envelopeToCbor(enc: EncryptionEnvelope): CanonicalCborValue {\n const out: CborMap = {\n scheme: enc.scheme as CanonicalCborValue,\n aead: enc.aead,\n nonce: enc.nonce,\n };\n if (enc.kem !== undefined) out['kem'] = enc.kem;\n if (enc.slots !== undefined) out['slots'] = enc.slots.map(slotToCbor);\n if (enc.slots_mac !== undefined) out['slots_mac'] = enc.slots_mac;\n if (enc.passphrase !== undefined) out['passphrase'] = passphraseToCbor(enc.passphrase);\n return out;\n}\n\nfunction slotToCbor(slot: Slot): CanonicalCborValue {\n // KEM-driven slot serialization. The canonical encoder sorts map keys by\n // length-then-bytewise (RFC 8949 §4.2.1), so it emits `wrap` (4-byte key)\n // before `kem_ct` (6-byte key) and `epk` (3-byte key) before `wrap`\n // automatically — insertion order here is irrelevant to the wire bytes.\n //\n // - x25519: `{ epk: bstr(32), wrap: bstr(48) }`\n // - mlkem768x25519: `{ kem_ct: [ bstr, ... ], wrap: bstr(48) }` — `kem_ct`\n // is the already-chunked array (NOT re-chunked here), so the bytes match\n // what crypto-core committed to `slots_mac` byte-for-byte.\n if (slot.kem_ct !== undefined) {\n return { kem_ct: slot.kem_ct.map((c) => c), wrap: slot.wrap! };\n }\n return { epk: slot.epk!, wrap: slot.wrap! };\n}\n\nfunction passphraseToCbor(pp: PassphraseBlock): CanonicalCborValue {\n return {\n alg: pp.alg,\n salt: pp.salt,\n params: pp.params as { readonly [key: string]: CanonicalCborValue },\n };\n}\n\nfunction sigEntryToCbor(entry: SigEntry): CanonicalCborValue {\n const out: CborMap = { cose_sign1: entry.cose_sign1.map((b) => b) };\n if (entry.cose_key !== undefined) {\n out['cose_key'] = entry.cose_key.map((b) => b);\n }\n return out;\n}\n","// CIP-309 v1 chunked-bytes and chunked-text helpers.\n//\n// The Cardano ledger CDDL constrains every `transaction_metadatum` byte string\n// (`bstr`) and text string (`tstr`) to ≤ 64 bytes. CIP-309 therefore carries\n// any logical value larger than 64 bytes as an ARRAY of ≤ 64-byte chunks. Two\n// chunked shapes exist:\n//\n// * `bytes-chunk-array` — `[ 1* bstr .size (1..64) ]` — used for chunked\n// `COSE_Sign1` bytes (`sigs[i].cose_sign1`) and the chunked\n// `cbor<COSE_Key>` blob (`sigs[i].cose_key`).\n// * `uri-chunk-array` — `[ 1* tstr .size (1..64) ]` — used as the inner\n// element of `items[i].uris` and `merkle[i].uris`.\n//\n// Two reconstruction invariants are normative:\n//\n// 1. **Per-chunk size.** `[1, 64]` bytes (zero-length chunks rejected\n// identically to oversized chunks). The validator's schema layer enforces\n// this; the helpers here assume the schema gate has fired.\n// 2. **UTF-8 codepoint integrity (text only).** The reconstructed\n// concatenation MUST be valid UTF-8. The canonical-CBOR decoder already\n// rejects any `tstr` that is not valid UTF-8 (→ `MALFORMED_CBOR`) before\n// these helpers run, so each chunk arrives as a well-formed string; the\n// `TextDecoder({ fatal: true })` pass below is the residual structural\n// guard.\n\nconst CHUNK_MAX_BYTES = 64;\n\nconst UTF8_ENCODER = new TextEncoder();\n\n/**\n * Split a logical byte string into ≤ 64-byte CBOR-bytes chunks\n * (`bytes-chunk-array`). Always returns a non-empty array.\n *\n * For empty inputs, returns `[<empty>]` so the caller's schema gate fails\n * later via `CHUNK_TOO_LARGE` (zero-length chunks are rejected). Real callers\n * feed COSE_Sign1 / cbor<COSE_Key> byte strings, which are never empty.\n */\nexport function chunkBytes(value: Uint8Array): Uint8Array[] {\n if (value.length === 0) return [new Uint8Array(0)];\n const chunks: Uint8Array[] = [];\n for (let i = 0; i < value.length; i += CHUNK_MAX_BYTES) {\n chunks.push(value.subarray(i, Math.min(i + CHUNK_MAX_BYTES, value.length)));\n }\n return chunks;\n}\n\n/**\n * Reverse of {@link chunkBytes}: concatenate chunked bytes (`sigs[i].cose_sign1`,\n * `sigs[i].cose_key`) into a single buffer for downstream CBOR/COSE decode.\n * The validator-layer schema enforces the per-chunk size + non-empty-array\n * invariants before this helper runs, so it makes no length checks.\n */\nexport function bytesChunkArrayConcat(chunks: ReadonlyArray<Uint8Array>): Uint8Array {\n let total = 0;\n for (const c of chunks) total += c.length;\n const out = new Uint8Array(total);\n let offset = 0;\n for (const c of chunks) {\n out.set(c, offset);\n offset += c.length;\n }\n return out;\n}\n\nexport type ReconstructUriResult =\n | { ok: true; uri: string }\n | { ok: false; code: 'INVALID_URI'; reason: string };\n\n/**\n * Reconstruct a chunked URI (`uri-chunk-array`) into its logical string.\n *\n * The chunks arrive as JS strings produced by the canonical-CBOR decoder,\n * which already rejects any non-UTF-8 `tstr` (surfacing it upstream as\n * `MALFORMED_CBOR`) — so by the time this helper runs the only structural\n * task left is to byte-concatenate and decode. We re-encode each chunk to its\n * UTF-8 bytes, concatenate, and decode the whole with `{ fatal: true }`. A\n * conformant producer never splits a multi-byte codepoint across chunks (the\n * Cardano 64-byte cap is applied on codepoint boundaries), so this decode\n * succeeds for every well-formed record; the `INVALID_URI` branch is the\n * residual guard for a byte sequence that does not reconstruct to valid UTF-8.\n *\n * Per-scheme shape validation (the IPFS CID profile) and absolute-URI /\n * fragment-identifier / scheme-set checks fire in `validator.ts`, NOT here —\n * this helper is structural-only.\n */\nexport function reconstructChunkedUri(chunks: ReadonlyArray<string>): ReconstructUriResult {\n const merged = bytesChunkArrayConcat(chunks.map((c) => UTF8_ENCODER.encode(c)));\n try {\n const uri = new TextDecoder('utf-8', { fatal: true }).decode(merged);\n return { ok: true, uri };\n } catch (cause) {\n return {\n ok: false,\n code: 'INVALID_URI',\n reason: cause instanceof Error ? cause.message : String(cause),\n };\n }\n}\n\n/**\n * Chunk a URI string into `[ tstr .size (1..64) ]`, splitting on UTF-8 byte\n * boundaries so no multi-byte codepoint straddles a chunk.\n *\n * For pure-ASCII URIs (the common `ar://`, `ipfs://` cases) this collapses\n * to plain 64-byte byte-slice chunks. For URIs with non-ASCII path components\n * (rare but possible — RFC 3986 §2.5 IRIs / percent-encoded UTF-8) the\n * algorithm rewinds to the nearest codepoint boundary at each chunk break.\n */\nexport function chunkUri(uri: string): string[] {\n const bytes = UTF8_ENCODER.encode(uri);\n if (bytes.length === 0) return [''];\n if (bytes.length <= CHUNK_MAX_BYTES) return [uri];\n const decoder = new TextDecoder('utf-8', { fatal: true });\n const chunks: string[] = [];\n let cursor = 0;\n while (cursor < bytes.length) {\n let end = Math.min(cursor + CHUNK_MAX_BYTES, bytes.length);\n // Rewind to the start of the previous UTF-8 codepoint if we landed in\n // the middle of a multibyte sequence. UTF-8 continuation bytes match\n // 0b10xx_xxxx; rewind while the byte at `end` is a continuation.\n while (end < bytes.length && (bytes[end]! & 0xc0) === 0x80) end--;\n chunks.push(decoder.decode(bytes.subarray(cursor, end)));\n cursor = end;\n }\n return chunks;\n}\n","// CIP-309 v1 off-host signing helper. The signing key never leaves the user\n// device — this module touches only the public-data inputs (record bytes +\n// pubkey) and the public-data output (the 64-byte Ed25519 signature). The\n// integrator's signer callback owns the private key material.\n//\n// Wire-format invariants enforced by this module:\n// - Sig_structure carries the 25-byte UTF-8 domain prefix\n// `cardano-poe-record-sig-v1` with `external_aad = h''` (RFC 9052 §4.4\n// under the CIP-30 compatibility constraint).\n// - COSE_Sign1 (RFC 9052 §4.2) has a detached payload: COSE_Sign1[2] = CBOR\n// null. CIP-8 `hashed = true` mode places the literal text key\n// `\"hashed\"` in the unprotected header.\n// - Path-1 `kid-as-public-key` convention: 32-byte raw Ed25519 pubkey in\n// protected header label 4; path-1 / path-2 are mutually exclusive on\n// the wire.\n// - chunked-bytes-array: per-chunk size in [1, 64].\n//\n// Use cases (the four integration shapes this surface is intended for):\n// 1. AWS KMS `Sign` over the returned Sig_structure bytes — wrap KMS as\n// `(bytes) => Promise<signature>`.\n// 2. Google Cloud HSM — same `(bytes) => Promise<signature>` shape.\n// 3. YubiHSM — local hardware-backed signer; same shape via the YubiHSM SDK.\n// 4. Air-gapped offline signer — transport the Sig_structure bytes via QR /\n// USB / sneakernet to the offline workstation; transport the 64-byte\n// Ed25519 signature back.\n//\n// This module is PATH-1 ONLY. The CIP-30 wallet path (path-2) is handled\n// separately — adding a `cose_key` sidecar here would violate the path-1 /\n// path-2 mutual-exclusion rule (SIG_ENTRY_KID_COSE_KEY_CONFLICT).\n//\n// Hashed-mode (`prepareSigStructureHashed` / `assembleCoseSign1Hashed`) is\n// DISCOURAGED for software off-host signers (AWS KMS, GCP HSM, YubiHSM —\n// each accepts arbitrary-length input) — use the non-hashed mode instead.\n// Hashed mode exists only for hardware co-signers with limited screen /\n// buffer. The verifier MUST recognise the `\"hashed\": true` unprotected-header\n// flag and substitute `Sig_structure[3]` with `Blake2b-224(to_sign)` before\n// strict Ed25519 verification.\n//\n// Privacy contract: the SDK never sees, stores, logs, or transmits any byte\n// string that contains the integrator's Ed25519 private signing key. The\n// integrator's signer handles the seed; this module touches only the 32-byte\n// public key and the 64-byte signature (both public data).\n//\n// The Python SDK carries the same builder (snake_case function names);\n// byte-identical outputs across the two are enforced by a shared\n// known-answer-test corpus.\n\nimport { encodeCanonicalCbor, type CanonicalCborValue } from '@cardanowall/crypto-core/cbor';\nimport {\n CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES,\n buildCip309SigStructure,\n buildSigStructure,\n encodeCoseSign1,\n type CoseHeader,\n} from '@cardanowall/crypto-core/cose';\nimport { blake2b224 } from '@cardanowall/crypto-core/hash';\nimport {\n chunkBytes,\n encodeRecordBodyForSigning,\n type ChunkedBytesArray,\n type PoeRecord,\n type SigEntry,\n} from '@cardanowall/poe-standard';\n\nconst EMPTY_BYTES = new Uint8Array(0);\nconst ED25519_PUBLIC_KEY_LENGTH = 32;\nconst ED25519_SIGNATURE_LENGTH = 64;\n\nexport type OffHostSignErrorCode = 'INVALID_PUBKEY_LENGTH' | 'INVALID_SIGNATURE_LENGTH';\n\nexport class OffHostSignError extends Error {\n readonly code: OffHostSignErrorCode;\n\n constructor(code: OffHostSignErrorCode, message: string) {\n super(message);\n this.name = 'OffHostSignError';\n this.code = code;\n }\n}\n\nexport interface PrepareSigStructureArgs {\n readonly record: PoeRecord;\n readonly signerPubkey: Uint8Array;\n}\n\nexport interface PrepareSigStructureResult {\n readonly sigStructureBytes: Uint8Array;\n readonly protectedHeaderBytes: Uint8Array;\n}\n\nexport interface PrepareSigStructureHashedResult extends PrepareSigStructureResult {\n readonly toSignHashBytes: Uint8Array;\n}\n\nexport interface AssembleCoseSign1Args {\n readonly record: PoeRecord;\n readonly signerPubkey: Uint8Array;\n readonly signature: Uint8Array;\n}\n\nexport interface AssembleCoseSign1Result {\n readonly coseSign1Bytes: Uint8Array;\n readonly sigEntry: SigEntry;\n}\n\n// `to_sign = utf8(\"cardano-poe-record-sig-v1\") || canonical_cbor(record_body_minus_sigs)`.\n// The first 25 bytes are byte-pinned to the prefix constant; bytes 25..end\n// are the canonical CBOR of the record body with `sigs` removed.\nexport function buildToSign(record: PoeRecord): Uint8Array {\n const body = encodeRecordBodyForSigning(record);\n const out = new Uint8Array(CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES.length + body.length);\n out.set(CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES, 0);\n out.set(body, CARDANO_POE_SIG_DOMAIN_PREFIX_BYTES.length);\n return out;\n}\n\n// Path-1 protected header canonical CBOR for `{1: -8, 4: <signerPubkey>}`.\n// Always 38 bytes: `a2 01 27 04 58 20 || <32-byte pubkey>`.\nfunction encodePath1ProtectedHeader(signerPubkey: Uint8Array): {\n protectedHeader: CoseHeader;\n protectedHeaderBytes: Uint8Array;\n} {\n const protectedHeader: CoseHeader = new Map<number | string, unknown>([\n [1, -8],\n [4, signerPubkey],\n ]);\n const protectedHeaderBytes = encodeCanonicalCbor(protectedHeader as CanonicalCborValue);\n return { protectedHeader, protectedHeaderBytes };\n}\n\n// Returns the full `Sig_structure = [ \"Signature1\", protected_bytes, h'', to_sign ]`\n// canonical-CBOR bytes that the off-host signer feeds verbatim to Ed25519.\n// `protectedHeaderBytes` is exposed so integrators can byte-compare against\n// fixtures (always 38 bytes for the path-1 `{1:-8, 4:<pub>}` map).\nexport function prepareSigStructure(args: PrepareSigStructureArgs): PrepareSigStructureResult {\n if (args.signerPubkey.length !== ED25519_PUBLIC_KEY_LENGTH) {\n throw new OffHostSignError(\n 'INVALID_PUBKEY_LENGTH',\n `signerPubkey must be 32 bytes (Ed25519 raw public key), got ${args.signerPubkey.length}`,\n );\n }\n const { protectedHeaderBytes } = encodePath1ProtectedHeader(args.signerPubkey);\n const recordBodyCbor = encodeRecordBodyForSigning(args.record);\n const sigStructureBytes = buildCip309SigStructure({\n bodyProtectedBytes: protectedHeaderBytes,\n recordBodyCbor,\n });\n return { sigStructureBytes, protectedHeaderBytes };\n}\n\n// Assembles `COSE_Sign1 = [ protected_bytes, unprotected_map, null, signature ]`\n// (detached payload, `alg = -8`, protected `kid = signerPubkey`), chunks the\n// result into the CIP-309 chunked-bytes-array shape, and emits a path-1-only\n// `{cose_sign1}` `sigs[i]` entry.\nexport function assembleCoseSign1(args: AssembleCoseSign1Args): AssembleCoseSign1Result {\n if (args.signerPubkey.length !== ED25519_PUBLIC_KEY_LENGTH) {\n throw new OffHostSignError(\n 'INVALID_PUBKEY_LENGTH',\n `signerPubkey must be 32 bytes (Ed25519 raw public key), got ${args.signerPubkey.length}`,\n );\n }\n if (args.signature.length !== ED25519_SIGNATURE_LENGTH) {\n throw new OffHostSignError(\n 'INVALID_SIGNATURE_LENGTH',\n `signature must be 64 bytes (Ed25519 raw signature), got ${args.signature.length}`,\n );\n }\n const { protectedHeader } = encodePath1ProtectedHeader(args.signerPubkey);\n const coseSign1Bytes = encodeCoseSign1({\n protectedHeader,\n unprotectedHeader: new Map(),\n payload: null,\n signature: args.signature,\n });\n const chunks = chunkBytes(coseSign1Bytes) as ChunkedBytesArray;\n const sigEntry: SigEntry = { cose_sign1: chunks };\n return { coseSign1Bytes, sigEntry };\n}\n\n// CIP-8 `hashed = true` companion. Substitutes `Sig_structure[3]` with\n// `Blake2b-224(to_sign)`. The hash covers the ENTIRE `to_sign` payload\n// (i.e. `utf8(prefix) || record_body_cbor`) — keeping the 25-byte domain\n// separator inside the hash boundary preserves cross-protocol replay\n// protection even in hashed mode.\n//\n// DISCOURAGED for software off-host signers; use only for hardware co-signers\n// with screen / buffer constraints.\nexport function prepareSigStructureHashed(\n args: PrepareSigStructureArgs,\n): PrepareSigStructureHashedResult {\n if (args.signerPubkey.length !== ED25519_PUBLIC_KEY_LENGTH) {\n throw new OffHostSignError(\n 'INVALID_PUBKEY_LENGTH',\n `signerPubkey must be 32 bytes (Ed25519 raw public key), got ${args.signerPubkey.length}`,\n );\n }\n const { protectedHeaderBytes } = encodePath1ProtectedHeader(args.signerPubkey);\n const toSign = buildToSign(args.record);\n const toSignHashBytes = blake2b224(toSign);\n const sigStructureBytes = buildSigStructure({\n context: 'Signature1',\n bodyProtectedBytes: protectedHeaderBytes,\n externalAad: EMPTY_BYTES,\n payload: toSignHashBytes,\n });\n return { sigStructureBytes, protectedHeaderBytes, toSignHashBytes };\n}\n\n// Assemble hashed-mode COSE_Sign1. The unprotected header carries\n// `\"hashed\": true` (text key), signalling to the verifier that the\n// Blake2b-224 substitution applies.\nexport function assembleCoseSign1Hashed(args: AssembleCoseSign1Args): AssembleCoseSign1Result {\n if (args.signerPubkey.length !== ED25519_PUBLIC_KEY_LENGTH) {\n throw new OffHostSignError(\n 'INVALID_PUBKEY_LENGTH',\n `signerPubkey must be 32 bytes (Ed25519 raw public key), got ${args.signerPubkey.length}`,\n );\n }\n if (args.signature.length !== ED25519_SIGNATURE_LENGTH) {\n throw new OffHostSignError(\n 'INVALID_SIGNATURE_LENGTH',\n `signature must be 64 bytes (Ed25519 raw signature), got ${args.signature.length}`,\n );\n }\n const { protectedHeader } = encodePath1ProtectedHeader(args.signerPubkey);\n const unprotectedHeader: CoseHeader = new Map<number | string, unknown>([['hashed', true]]);\n const coseSign1Bytes = encodeCoseSign1({\n protectedHeader,\n unprotectedHeader,\n payload: null,\n signature: args.signature,\n });\n const chunks = chunkBytes(coseSign1Bytes) as ChunkedBytesArray;\n const sigEntry: SigEntry = { cose_sign1: chunks };\n return { coseSign1Bytes, sigEntry };\n}\n","// RFC 7807 `application/problem+json` envelope and the typed error class\n// hierarchy the SDK throws on every non-2xx response.\n//\n// Every CIP-309 gateway `/api/v1/*` route emits the canonical shape:\n//\n// Content-Type: application/problem+json\n// {\n// \"type\": \"about:blank\",\n// \"title\": \"Payment Required\",\n// \"status\": 402,\n// \"detail\": \"Required $0.18 for this publish; balance is $0.05.\",\n// \"code\": \"insufficient-funds\",\n// \"trace_id\": \"01977c...\",\n// \"errors\": [{\"field\": \"items.0.hashes\", \"code\": \"invalid_type\", \"detail\": \"...\"}],\n// <extension members per RFC 7807 §3.2, e.g. balance_usd_micros / required_usd_micros>\n// }\n//\n// Field semantics:\n// - `code` is lowercase-kebab. Consumers dispatch on `code`; the SDK already\n// dispatches on `code` to pick the most-specific subclass.\n// - `status` matches the HTTP status. `httpStatus` is a convenience alias.\n// - `errors[]` carries per-field validation errors (Zod-derived on the\n// server). `field` is the dotted JSON path; empty string denotes a\n// body-level issue.\n// - `trace_id` is echoed on the `X-Request-Id` response header for log\n// correlation. Use `err.traceId` when filing bug reports (the SDK\n// surfaces it as the camelCase `traceId` property — only the wire field\n// is snake_case, to match the rest of the API surface).\n// - Extension members (anything outside the canonical seven fields) are\n// surfaced on `err.extensions`. Typed subclasses project the relevant\n// extension fields onto camelCase getters (e.g. `top_up_url` → `topUpUrl`).\n\n/** RFC 7807 per-field error entry. */\nexport interface ProblemErrorEntry {\n /** Dotted JSON path of the offending field; empty for body-level errors. */\n readonly field: string;\n /** Stable lowercase-kebab (or Zod issue) code for the specific failure. */\n readonly code: string;\n /** Human-readable explanation of this individual field error. */\n readonly detail: string;\n}\n\n/**\n * RFC 7807 `application/problem+json` document as emitted by every CIP-309\n * gateway `/api/v1/*` route.\n *\n * Canonical fields (`type`, `title`, `status`, `detail`, `code`, `trace_id`)\n * are always present. `errors` is present on validation responses.\n * `instance` is optional per RFC 7807 §3.1.\n *\n * Additional top-level fields are RFC 7807 §3.2 extension members and are\n * preserved verbatim on `Cip309HttpError.extensions`.\n */\nexport interface ProblemDetails {\n readonly type: string;\n readonly title: string;\n readonly status: number;\n readonly detail: string;\n readonly code: string;\n readonly trace_id: string;\n readonly errors?: ReadonlyArray<ProblemErrorEntry>;\n readonly instance?: string;\n /** RFC 7807 §3.2 extension members. */\n readonly [extension: string]: unknown;\n}\n\n/** The set of canonical RFC 7807 fields, used to split extensions cleanly. */\nconst CANONICAL_PROBLEM_KEYS: ReadonlySet<string> = new Set([\n 'type',\n 'title',\n 'status',\n 'detail',\n 'code',\n 'trace_id',\n 'errors',\n 'instance',\n]);\n\n/**\n * Pull RFC 7807 §3.2 extension members out of a problem document. The result\n * is a fresh object containing every top-level key that is NOT one of the\n * canonical fields above.\n */\nexport function extractProblemExtensions(problem: ProblemDetails): Record<string, unknown> {\n const out: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(problem)) {\n if (!CANONICAL_PROBLEM_KEYS.has(key)) {\n out[key] = value;\n }\n }\n return out;\n}\n\nexport interface Cip309HttpErrorInit {\n /** The verbatim problem document. */\n readonly problem: ProblemDetails;\n /** Pre-split extension members. Computed from `problem` when omitted. */\n readonly extensions?: Record<string, unknown>;\n /** Value of the `X-Request-Id` response header. Falls back to `problem.trace_id`. */\n readonly requestId?: string | undefined;\n /** Value of the `Retry-After` response header (seconds), if present. */\n readonly retryAfterSeconds?: number | undefined;\n}\n\n/**\n * Parent class for every typed SDK HTTP error. Carries the full RFC 7807\n * problem document plus headers (`X-Request-Id`, `Retry-After`) relevant for\n * retry logic and log correlation.\n *\n * Consumers can dispatch on:\n * - `err.code` — lowercase-kebab problem code\n * - `err.httpStatus` — HTTP status (= `err.problem.status`)\n * - `instanceof <SpecificError>` — see the subclasses re-exported from\n * `@cardanowall/sdk-ts`\n */\nexport class Cip309HttpError extends Error {\n public readonly problem: ProblemDetails;\n public readonly code: string;\n public readonly httpStatus: number;\n public readonly title: string;\n public readonly detail: string;\n public readonly type: string;\n public readonly traceId: string;\n public readonly instance: string | undefined;\n public readonly errors: ReadonlyArray<ProblemErrorEntry> | undefined;\n public readonly extensions: Record<string, unknown>;\n public readonly requestId: string;\n public readonly retryAfterSeconds: number | undefined;\n\n constructor(init: Cip309HttpErrorInit) {\n super(init.problem.detail || `${init.problem.title} (HTTP ${init.problem.status})`);\n this.name = 'Cip309HttpError';\n this.problem = init.problem;\n this.code = init.problem.code;\n this.httpStatus = init.problem.status;\n this.title = init.problem.title;\n this.detail = init.problem.detail;\n this.type = init.problem.type;\n this.traceId = init.problem.trace_id;\n this.instance = init.problem.instance;\n this.errors = init.problem.errors;\n this.extensions = init.extensions ?? extractProblemExtensions(init.problem);\n // X-Request-Id falls back to the in-body trace_id so callers always have a\n // correlation handle even when the header is stripped by a proxy.\n this.requestId = init.requestId ?? init.problem.trace_id;\n this.retryAfterSeconds = init.retryAfterSeconds;\n }\n}\n","// 400 batch-empty — the `records[]` array on `/api/v1/poe/publish-batch` was\n// empty. The batch endpoint requires at least one record.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class BatchEmptyError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'BatchEmptyError';\n }\n}\n","// 400 batch-too-large — the `records[]` array on `/api/v1/poe/publish-batch`\n// carries more entries than the per-call ceiling (max 50).\n//\n// Wire-format extension members:\n// { \"max\": <int>, \"got\": <int> }\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nfunction readInt(value: unknown): number | undefined {\n return typeof value === 'number' && Number.isFinite(value) ? value : undefined;\n}\n\nexport class BatchTooLargeError extends Cip309HttpError {\n public readonly max: number | undefined;\n public readonly got: number | undefined;\n\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'BatchTooLargeError';\n this.max = readInt(this.extensions['max']);\n this.got = readInt(this.extensions['got']);\n }\n}\n","// 403 Forbidden — the caller is authenticated but lacks permission. Covers\n// the generic `forbidden` code plus the edge-proxy `csrf-invalid` flavour;\n// scope-specific failures surface as `InsufficientScopeError` instead.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class ForbiddenError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'ForbiddenError';\n }\n}\n","// 409 idempotency-key-conflict — the supplied `Idempotency-Key` has been\n// seen before with a different request body within its 24h TTL window.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class IdempotencyConflictError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'IdempotencyConflictError';\n }\n}\n","// 402 insufficient-funds — the caller's balance with the gateway is below the\n// cost of the requested operation. This is the generic paid-gateway \"balance\n// too low\" signal; how a particular gateway prices operations and tops up\n// balances is its own concern.\n//\n// Wire-format extension members (RFC 7807 §3.2). Money fields land as decimal\n// strings so JSON parsing preserves bigint precision in callers that parse to\n// Number by default:\n//\n// {\n// \"balance_usd_micros\": \"<decimal string>\",\n// \"required_usd_micros\": \"<decimal string>\",\n// \"top_up_url\": \"<gateway-provided URL, optional>\"\n// }\n//\n// Field-name mapping (wire → SDK):\n// balance_usd_micros → balanceUsdMicros (string → bigint)\n// required_usd_micros → requiredUsdMicros (string → bigint)\n// top_up_url → topUpUrl (snake_case → camelCase)\n//\n// Idempotency contract: a 402 is \"non-committing\" — the gateway does NOT cache\n// the response under the request's Idempotency-Key. After raising the balance,\n// the SDK consumer MAY retry with the SAME Idempotency-Key within the gateway's\n// TTL window; the handler runs fresh and (assuming the balance now suffices)\n// the retry returns 202 with a freshly assigned `id`.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nfunction readBigIntString(value: unknown): bigint | undefined {\n if (typeof value !== 'string') return undefined;\n if (!/^-?[0-9]+$/.test(value)) return undefined;\n try {\n return BigInt(value);\n } catch {\n return undefined;\n }\n}\n\nfunction readString(value: unknown): string | undefined {\n return typeof value === 'string' ? value : undefined;\n}\n\nexport class InsufficientFundsError extends Cip309HttpError {\n public readonly balanceUsdMicros: bigint | undefined;\n public readonly requiredUsdMicros: bigint | undefined;\n public readonly topUpUrl: string | undefined;\n\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'InsufficientFundsError';\n this.balanceUsdMicros = readBigIntString(this.extensions['balance_usd_micros']);\n this.requiredUsdMicros = readBigIntString(this.extensions['required_usd_micros']);\n this.topUpUrl = readString(this.extensions['top_up_url']);\n }\n}\n","// 403 insufficient-scope — the API key authenticated but does not grant the\n// scope required for the endpoint.\n//\n// Wire-format extension members:\n// { \"required\": [\"poe:create\"], \"granted\": [\"poe:read\", \"account:read\"] }\n//\n// Both arrays are surfaced verbatim on the typed error. `requiredScope` is a\n// convenience for the common single-scope case (the server emits a one-element\n// `required` array today).\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nfunction readScopeArray(value: unknown): ReadonlyArray<string> {\n if (!Array.isArray(value)) return [];\n return value.filter((entry): entry is string => typeof entry === 'string');\n}\n\nexport class InsufficientScopeError extends Cip309HttpError {\n public readonly requiredScopes: ReadonlyArray<string>;\n public readonly grantedScopes: ReadonlyArray<string>;\n\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'InsufficientScopeError';\n this.requiredScopes = readScopeArray(this.extensions['required']);\n this.grantedScopes = readScopeArray(this.extensions['granted']);\n }\n\n /** Convenience for the single-scope case; first entry of `requiredScopes`. */\n get requiredScope(): string | undefined {\n return this.requiredScopes[0];\n }\n}\n","// 500 internal-error — unexpected server-side failure. The detail message\n// is deliberately generic; correlate the failure via `err.traceId` in\n// server logs.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class InternalServerError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'InternalServerError';\n }\n}\n","// 400 invalid-body — the request body was structurally malformed (e.g. not\n// valid JSON, or a higher-level shape check failed before Zod ran). Schema\n// validation failures emit `validation-failed` (→ `ValidationFailedError`)\n// instead.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class InvalidBodyError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'InvalidBodyError';\n }\n}\n","// 400 malformed-cbor — the `record_bytes` payload could not be parsed as\n// canonical CBOR per the CIP-309 deterministic encoding rules.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class MalformedCborError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'MalformedCborError';\n }\n}\n","// 404 not-found — generic missing-resource response. Domain-specific 404s\n// (notably `record-not-found`) deserialise to their own subclass.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class NotFoundError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'NotFoundError';\n }\n}\n","// 409 quote-already-consumed — the publish quote referenced by `quote_id`\n// has already been used by a prior /publish call. Quotes are single-use; a\n// freshly-issued quote covers exactly one PoE submission. The caller should\n// request a fresh quote via POST /api/v1/poe/quote and retry.\n//\n// Wire-format extension members (RFC 7807 §3.2):\n// { \"quote_id\": \"<uuid>\" }\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nfunction readString(value: unknown): string | undefined {\n return typeof value === 'string' ? value : undefined;\n}\n\nexport class QuoteAlreadyConsumedError extends Cip309HttpError {\n public readonly quoteId: string | undefined;\n\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'QuoteAlreadyConsumedError';\n this.quoteId = readString(this.extensions['quote_id']);\n }\n}\n","// 410 quote-expired — the publish quote referenced by `quote_id` exceeded\n// its TTL (15 minutes from issuance) before /publish consumed it. The\n// caller should request a fresh quote via POST /api/v1/poe/quote and retry.\n//\n// Wire-format extension members (RFC 7807 §3.2):\n// { \"quote_id\": \"<uuid>\" }\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nfunction readString(value: unknown): string | undefined {\n return typeof value === 'string' ? value : undefined;\n}\n\nexport class QuoteExpiredError extends Cip309HttpError {\n public readonly quoteId: string | undefined;\n\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'QuoteExpiredError';\n this.quoteId = readString(this.extensions['quote_id']);\n }\n}\n","// 404 quote-not-found — the supplied `quote_id` does not exist for the\n// authenticated account. Either the UUID is wrong, or the quote belongs to\n// a different account (the server enforces account scoping on quote rows).\n//\n// Wire-format extension members (RFC 7807 §3.2):\n// { \"quote_id\": \"<uuid>\" }\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nfunction readString(value: unknown): string | undefined {\n return typeof value === 'string' ? value : undefined;\n}\n\nexport class QuoteNotFoundError extends Cip309HttpError {\n public readonly quoteId: string | undefined;\n\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'QuoteNotFoundError';\n this.quoteId = readString(this.extensions['quote_id']);\n }\n}\n","// 429 rate-limited — the caller exceeded the per-key request quota.\n//\n// The retry hint lives on the standard `Retry-After` HTTP response header\n// (RFC 9110 §10.2.3). The SDK parses it into `retryAfterSeconds` on the\n// thrown error; the value is undefined if the header is absent or\n// non-numeric. Per RFC 7807, no retry hint appears in the problem body.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class RateLimitedError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'RateLimitedError';\n }\n}\n","// 404 record-not-found — no CIP-309 record is registered for the requested\n// tx_hash.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class RecordNotFoundError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'RecordNotFoundError';\n }\n}\n","// 503 service-unavailable — temporary inability to serve the request. The\n// retry hint, if any, is on the standard `Retry-After` header — surfaced on\n// `err.retryAfterSeconds`.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class ServiceUnavailableError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'ServiceUnavailableError';\n }\n}\n","// 401 Unauthorized — caller is not authenticated. The server emits this when\n// the `Authorization: Bearer` header is missing, malformed, or names a\n// revoked / unknown API key.\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class UnauthorizedError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'UnauthorizedError';\n }\n}\n","// 400 validation-failed — the request body parsed as JSON but failed the\n// route's schema check. The per-field issues live on `err.errors[]` (Zod\n// issue codes; e.g. `invalid_type`, `too_small`, `custom`).\n\nimport { Cip309HttpError, type Cip309HttpErrorInit } from './http-error';\n\nexport class ValidationFailedError extends Cip309HttpError {\n constructor(init: Cip309HttpErrorInit) {\n super(init);\n this.name = 'ValidationFailedError';\n }\n}\n","// Decodes an RFC 7807 `application/problem+json` body into the most-specific\n// `Cip309HttpError` subclass.\n//\n// Dispatch order:\n// 1. By `code` (lowercase-kebab) — preferred. Each registered code maps to a\n// named subclass with typed projections of its extension members.\n// 2. By HTTP status, when the body is missing/non-conforming. In that case\n// we synthesise a minimal `ProblemDetails` so consumers always see a\n// well-formed `err.problem`.\n//\n// The dispatcher is intentionally exhaustive over the codes the API emits;\n// codes the SDK doesn't recognise fall through to the parent\n// `Cip309HttpError` with the verbatim problem document.\n// Forward-compatibility: a server can introduce new codes without breaking\n// older SDKs — consumers either catch the parent class or dispatch on\n// `err.code` directly.\n\nimport { BatchEmptyError } from './batch-empty-error';\nimport { BatchTooLargeError } from './batch-too-large-error';\nimport { ForbiddenError } from './forbidden-error';\nimport {\n Cip309HttpError,\n extractProblemExtensions,\n type ProblemDetails,\n type ProblemErrorEntry,\n} from './http-error';\nimport { IdempotencyConflictError } from './idempotency-conflict-error';\nimport { InsufficientFundsError } from './insufficient-funds-error';\nimport { InsufficientScopeError } from './insufficient-scope-error';\nimport { InternalServerError } from './internal-server-error';\nimport { InvalidBodyError } from './invalid-body-error';\nimport { MalformedCborError } from './malformed-cbor-error';\nimport { NotFoundError } from './not-found-error';\nimport { QuoteAlreadyConsumedError } from './quote-already-consumed-error';\nimport { QuoteExpiredError } from './quote-expired-error';\nimport { QuoteNotFoundError } from './quote-not-found-error';\nimport { RateLimitedError } from './rate-limited-error';\nimport { RecordNotFoundError } from './record-not-found-error';\nimport { ServiceUnavailableError } from './service-unavailable-error';\nimport { UnauthorizedError } from './unauthorized-error';\nimport { ValidationFailedError } from './validation-failed-error';\n\nfunction asString(value: unknown): string | undefined {\n return typeof value === 'string' ? value : undefined;\n}\n\nfunction asNumber(value: unknown): number | undefined {\n return typeof value === 'number' && Number.isFinite(value) ? value : undefined;\n}\n\nfunction asProblemErrorEntries(value: unknown): readonly ProblemErrorEntry[] | undefined {\n if (!Array.isArray(value)) return undefined;\n const out: ProblemErrorEntry[] = [];\n for (const entry of value) {\n if (entry === null || typeof entry !== 'object') continue;\n const e = entry as Record<string, unknown>;\n out.push({\n field: typeof e['field'] === 'string' ? e['field'] : '',\n code: typeof e['code'] === 'string' ? e['code'] : '',\n detail: typeof e['detail'] === 'string' ? e['detail'] : '',\n });\n }\n return out;\n}\n\n/** Synthesise a minimal `ProblemDetails` for non-conforming bodies. */\nfunction synthesiseProblem(httpStatus: number, requestId: string | undefined): ProblemDetails {\n const code = `http-${httpStatus}`;\n return {\n type: `about:blank`,\n title: `HTTP ${httpStatus}`,\n status: httpStatus,\n detail: `Server returned HTTP ${httpStatus} without a problem+json body.`,\n code,\n trace_id: requestId ?? '',\n };\n}\n\nfunction toProblemDetails(\n httpStatus: number,\n body: unknown,\n requestId: string | undefined,\n): ProblemDetails {\n if (body === null || typeof body !== 'object') {\n return synthesiseProblem(httpStatus, requestId);\n }\n const b = body as Record<string, unknown>;\n // Heuristic: a real RFC 7807 body has at minimum `code` and `status` or\n // `title`. If neither is present we treat the body as non-conforming.\n const code = asString(b['code']);\n const status = asNumber(b['status']) ?? httpStatus;\n const title = asString(b['title']);\n if (code === undefined && title === undefined) {\n return synthesiseProblem(httpStatus, requestId);\n }\n const errors = asProblemErrorEntries(b['errors']);\n // Preserve every top-level field. `code`/`title`/`status` fall back when\n // the server omitted them; everything else flows through verbatim as\n // RFC 7807 §3.2 extension members.\n const base: Record<string, unknown> = {\n ...b,\n // RFC 7807 §4.2: `about:blank` is the default when no type URI is supplied.\n // The client is gateway-agnostic, so it must not invent a vendor-specific\n // problem-type namespace; the machine-readable discriminator is `code`.\n type: asString(b['type']) ?? 'about:blank',\n title: title ?? `HTTP ${status}`,\n status,\n detail: asString(b['detail']) ?? '',\n code: code ?? `http-${status}`,\n trace_id: asString(b['trace_id']) ?? requestId ?? '',\n };\n if (errors !== undefined) base['errors'] = errors;\n return base as ProblemDetails;\n}\n\nexport interface ParseHttpErrorArgs {\n readonly httpStatus: number;\n readonly body: unknown;\n /** `X-Request-Id` header from the response, when available. */\n readonly requestId?: string | undefined;\n /** `Retry-After` header from the response, parsed as integer seconds. */\n readonly retryAfterSeconds?: number | undefined;\n}\n\nexport function parseHttpError(args: ParseHttpErrorArgs): Cip309HttpError {\n const problem = toProblemDetails(args.httpStatus, args.body, args.requestId);\n const extensions = extractProblemExtensions(problem);\n const init = {\n problem,\n extensions,\n requestId: args.requestId,\n retryAfterSeconds: args.retryAfterSeconds,\n } as const;\n\n switch (problem.code) {\n case 'unauthorized':\n return new UnauthorizedError(init);\n case 'forbidden':\n case 'csrf-invalid':\n return new ForbiddenError(init);\n case 'insufficient-scope':\n return new InsufficientScopeError(init);\n case 'insufficient-funds':\n return new InsufficientFundsError(init);\n case 'quote-expired':\n return new QuoteExpiredError(init);\n case 'quote-not-found':\n return new QuoteNotFoundError(init);\n case 'quote-already-consumed':\n return new QuoteAlreadyConsumedError(init);\n case 'not-found':\n return new NotFoundError(init);\n case 'record-not-found':\n return new RecordNotFoundError(init);\n case 'idempotency-key-conflict':\n return new IdempotencyConflictError(init);\n case 'rate-limited':\n return new RateLimitedError(init);\n case 'validation-failed':\n return new ValidationFailedError(init);\n case 'invalid-body':\n return new InvalidBodyError(init);\n case 'malformed-cbor':\n return new MalformedCborError(init);\n case 'batch-too-large':\n return new BatchTooLargeError(init);\n case 'batch-empty':\n return new BatchEmptyError(init);\n case 'internal-error':\n return new InternalServerError(init);\n // A gateway that prices on a live FX oracle may surface a transient\n // `fx-stale` pricing outage; to a vendor-neutral client that is just a\n // temporary inability to serve, i.e. a service-unavailable condition.\n case 'service-unavailable':\n case 'fx-stale':\n return new ServiceUnavailableError(init);\n default:\n return new Cip309HttpError(init);\n }\n}\n","// Shared response-handling helpers for the HTTP client namespaces (poe,\n// records, account). They parse the body, lift `X-Request-Id` /\n// `Retry-After`, and throw a typed `Cip309HttpError` on non-2xx the same\n// way, so the logic lives here once.\n\nimport { parseHttpError } from './parse-http-error';\n\n/** Parse a JSON response body; returns `null` for empty or non-JSON bodies. */\nexport async function readJson(response: Response): Promise<unknown> {\n const text = await response.text();\n if (text.length === 0) return null;\n try {\n return JSON.parse(text);\n } catch {\n return null;\n }\n}\n\n/** Parse the `Retry-After` header as integer seconds; `undefined` when absent or non-numeric. */\nexport function parseRetryAfter(header: string | null): number | undefined {\n if (header === null) return undefined;\n const parsed = Number(header);\n return Number.isFinite(parsed) ? parsed : undefined;\n}\n\n/**\n * Throw the most-specific `Cip309HttpError` subclass on a non-2xx\n * response (decoding the RFC 7807 body, request id, and retry-after); no-op on\n * 2xx so callers can `await throwIfNotOk(res)` before reading the success body.\n */\nexport async function throwIfNotOk(response: Response): Promise<void> {\n if (response.ok) return;\n const body = await readJson(response);\n const requestId = response.headers.get('x-request-id') ?? undefined;\n const retryAfterSeconds = parseRetryAfter(response.headers.get('retry-after'));\n throw parseHttpError({ httpStatus: response.status, body, requestId, retryAfterSeconds });\n}\n","// `client.account.*` wraps the account read surface:\n//\n// GET /api/v1/account/balance → account.balance()\n//\n// Auth is required (Bearer with `account:read` scope, or a session cookie when\n// the gateway is browser-fronted). The configured API key is forwarded as\n// `Authorization: Bearer …`.\n//\n// The balance is USD micro-cents carried as a decimal string on the wire\n// (`balance_usd_micros`). The SDK preserves it verbatim as a string —\n// `AccountBalance.balanceUsdMicros` — and never coerces it to a JS number, so\n// the bigint value survives without precision loss.\n\nimport { readJson, throwIfNotOk } from './http-helpers';\nimport type { AccountBalance, FetchImpl } from './types';\n\ninterface ResolvedConfig {\n readonly apiKey: string | undefined;\n readonly baseUrl: string;\n readonly fetch: FetchImpl;\n}\n\nfunction buildHeaders(apiKey: string | undefined): Headers {\n const headers = new Headers({\n 'content-type': 'application/json',\n accept: 'application/json',\n });\n if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`);\n return headers;\n}\n\ninterface AccountBalanceWire {\n readonly balance_usd_micros: string;\n}\n\nexport class AccountNamespace {\n private readonly config: ResolvedConfig;\n\n constructor(config: ResolvedConfig) {\n this.config = config;\n }\n\n /**\n * Fetch the caller's current prepaid USD balance.\n *\n * Returns `{ balanceUsdMicros }`, the gateway's `balance_usd_micros` field\n * (USD micro-cents as a decimal string). The string is preserved verbatim —\n * never parsed into a number — so no precision is lost. An account with no\n * ledger activity yet reads `\"0\"`.\n *\n * Requires authentication: 401 (UnauthorizedError) when anonymous, 403\n * (InsufficientScopeError) when the Bearer key lacks the `account:read`\n * scope.\n */\n async balance(): Promise<AccountBalance> {\n const response = await this.config.fetch(`${this.config.baseUrl}/api/v1/account/balance`, {\n method: 'GET',\n headers: buildHeaders(this.config.apiKey),\n });\n await throwIfNotOk(response);\n const body = (await readJson(response)) as AccountBalanceWire;\n return { balanceUsdMicros: body.balance_usd_micros };\n }\n}\n","// Raised synchronously from the Cip309Client constructor when the config\n// cannot be resolved into a usable gateway target. The single trigger: a\n// missing or empty `baseUrl`. The client is gateway-agnostic and has no default\n// deployment, so a base URL must always be supplied. The `apiKey` is an opaque\n// bearer token and is never the cause of this error.\n\nexport class InvalidClientConfigError extends Error {\n public readonly code = 'INVALID_CLIENT_CONFIG' as const;\n constructor(message: string) {\n super(message);\n this.name = 'InvalidClientConfigError';\n }\n}\n","// Lowercase, no-`0x`-prefix hex encoder shared across the SDK. Single\n// implementation so the verifier, the wire serialiser, and the publish client\n// all emit byte-identical hex (the Python parity twin and the cross-language\n// fixtures depend on this exact form).\n\nexport function bytesToHex(bytes: Uint8Array): string {\n return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');\n}\n","// Canonical-CBOR codec for the off-chain Merkle leaves-list artefact.\n// The on-chain `merkle[]` field binds to this file via `uris[]` / `leaf_count`;\n// the file itself carries the full leaf set. Canonical CBOR is RFC 8949 §4.2.1.\n//\n// CDDL:\n//\n// leaves-list = {\n// \"format\": \"cardano-poe-merkle-leaves-v1\",\n// \"tree_alg\": \"rfc9162-sha256\",\n// \"root\": bytes .size 32,\n// \"leaves\": [ + bytes .size 32 ],\n// \"leaf_count\": uint,\n// ? \"leaf_alg\": tstr,\n// }\n//\n// Canonical ordering is bytewise-lexicographic on encoded map keys (RFC 8949\n// §4.2.1) so the wire-key order is fixed by `cde:true` regardless of insertion\n// order: root (4B) < format (6B) < leaves (6B) < leaf_alg (8B) < tree_alg (8B)\n// < leaf_count (10B).\n\nimport { decodeCanonicalCbor, encodeCanonicalCbor } from '../cbor/canonical';\nimport { compareCt } from '../util/compare-ct';\nimport { merkleSha2256Root } from '../hash/merkle-sha2-256';\n\nexport const LEAVES_LIST_FORMAT_V1 = 'cardano-poe-merkle-leaves-v1' as const;\nconst TREE_ALG_RFC9162 = 'rfc9162-sha256' as const;\nconst DIGEST_LENGTH = 32;\nconst REGISTERED_FORMATS = new Set<string>([LEAVES_LIST_FORMAT_V1]);\n\nexport type MerkleLeavesListErrorCode =\n | 'SCHEMA_MERKLE_LEAVES_FORMAT_UNSUPPORTED'\n | 'SCHEMA_MERKLE_LEAVES_MALFORMED'\n | 'SCHEMA_MERKLE_LEAF_COUNT_MISMATCH'\n | 'MERKLE_ROOT_MISMATCH';\n\nexport class MerkleLeavesListError extends Error {\n readonly code: MerkleLeavesListErrorCode;\n constructor(code: MerkleLeavesListErrorCode, message?: string) {\n super(message ? `${code}: ${message}` : code);\n this.code = code;\n this.name = 'MerkleLeavesListError';\n }\n}\n\nexport interface EncodeLeavesListArgs {\n readonly leaves: ReadonlyArray<Uint8Array>;\n readonly root: Uint8Array;\n readonly leafAlg?: string;\n}\n\nexport interface DecodedLeavesList {\n readonly format: typeof LEAVES_LIST_FORMAT_V1;\n readonly treeAlg: typeof TREE_ALG_RFC9162;\n readonly root: Uint8Array;\n readonly leaves: Uint8Array[];\n readonly leafCount: number;\n readonly leafAlg?: string;\n}\n\nexport function encodeLeavesList(args: EncodeLeavesListArgs): Uint8Array {\n if (!(args.root instanceof Uint8Array) || args.root.length !== DIGEST_LENGTH) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n `root must be a Uint8Array(${DIGEST_LENGTH})`,\n );\n }\n if (args.leaves.length < 1) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n 'leaves array must be non-empty',\n );\n }\n const leavesCopy: Uint8Array[] = [];\n for (let i = 0; i < args.leaves.length; i++) {\n const leaf = args.leaves[i];\n if (!(leaf instanceof Uint8Array) || leaf.length !== DIGEST_LENGTH) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n `leaves[${i}] must be a Uint8Array(${DIGEST_LENGTH})`,\n );\n }\n leavesCopy.push(leaf);\n }\n if (args.leafAlg !== undefined && typeof args.leafAlg !== 'string') {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n 'leaf_alg must be a string when present',\n );\n }\n const map: Record<string, unknown> = {\n format: LEAVES_LIST_FORMAT_V1,\n tree_alg: TREE_ALG_RFC9162,\n root: args.root,\n leaves: leavesCopy,\n leaf_count: leavesCopy.length,\n };\n if (args.leafAlg !== undefined) {\n map['leaf_alg'] = args.leafAlg;\n }\n return encodeCanonicalCbor(map as never);\n}\n\nexport function decodeLeavesList(bytes: Uint8Array): DecodedLeavesList {\n const decoded = decodeCanonicalCbor(bytes);\n if (typeof decoded !== 'object' || decoded === null || Array.isArray(decoded)) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n 'leaves-list MUST be a CBOR map',\n );\n }\n const m = decoded as Record<string, unknown>;\n\n const format = m['format'];\n if (typeof format !== 'string') {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n 'format must be a text string',\n );\n }\n if (!REGISTERED_FORMATS.has(format)) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_FORMAT_UNSUPPORTED',\n `format '${format}' is not in the registered set`,\n );\n }\n\n const treeAlg = m['tree_alg'];\n if (treeAlg !== TREE_ALG_RFC9162) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n `tree_alg '${String(treeAlg)}' is not '${TREE_ALG_RFC9162}'`,\n );\n }\n\n const root = m['root'];\n if (!(root instanceof Uint8Array) || root.length !== DIGEST_LENGTH) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n `root must be a ${DIGEST_LENGTH}-byte byte string`,\n );\n }\n\n const leavesRaw = m['leaves'];\n if (!Array.isArray(leavesRaw) || leavesRaw.length < 1) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n 'leaves must be a non-empty array',\n );\n }\n const leaves: Uint8Array[] = [];\n for (let i = 0; i < leavesRaw.length; i++) {\n const leaf = leavesRaw[i];\n if (!(leaf instanceof Uint8Array) || leaf.length !== DIGEST_LENGTH) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n `leaves[${i}] must be a ${DIGEST_LENGTH}-byte byte string`,\n );\n }\n leaves.push(leaf);\n }\n\n const leafCountRaw = m['leaf_count'];\n let leafCount: number;\n if (typeof leafCountRaw === 'number' && Number.isInteger(leafCountRaw) && leafCountRaw >= 0) {\n leafCount = leafCountRaw;\n } else if (typeof leafCountRaw === 'bigint' && leafCountRaw >= 0n) {\n if (leafCountRaw > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n 'leaf_count exceeds Number.MAX_SAFE_INTEGER',\n );\n }\n leafCount = Number(leafCountRaw);\n } else {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n 'leaf_count must be a non-negative CBOR uint',\n );\n }\n if (leaves.length !== leafCount) {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAF_COUNT_MISMATCH',\n `leaves.length (${leaves.length}) != leaf_count (${leafCount})`,\n );\n }\n\n let leafAlg: string | undefined;\n if (m['leaf_alg'] !== undefined) {\n if (typeof m['leaf_alg'] !== 'string') {\n throw new MerkleLeavesListError(\n 'SCHEMA_MERKLE_LEAVES_MALFORMED',\n 'leaf_alg must be a text string when present',\n );\n }\n leafAlg = m['leaf_alg'];\n }\n\n const recomputed = merkleSha2256Root(leaves);\n if (!compareCt(recomputed, root)) {\n throw new MerkleLeavesListError(\n 'MERKLE_ROOT_MISMATCH',\n 'leaves recompute does not match declared root',\n );\n }\n\n const out: DecodedLeavesList = {\n format: LEAVES_LIST_FORMAT_V1,\n treeAlg: TREE_ALG_RFC9162,\n root,\n leaves,\n leafCount,\n ...(leafAlg !== undefined ? { leafAlg } : {}),\n };\n return out;\n}\n","/**\n * Utilities for hex, bytearray and number handling.\n * @module\n */\n/*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */\nimport {\n type CHash,\n type TypedArray,\n abytes,\n abytes as abytes_,\n concatBytes,\n isLE,\n randomBytes as randb,\n} from '@noble/hashes/utils.js';\n/**\n * Bytes API type helpers for old + new TypeScript.\n *\n * TS 5.6 has `Uint8Array`, while TS 5.9+ made it generic `Uint8Array<ArrayBuffer>`.\n * We can't use specific return type, because TS 5.6 will error.\n * We can't use generic return type, because most TS 5.9 software will expect specific type.\n *\n * Maps typed-array input leaves to broad forms.\n * These are compatibility adapters, not ownership guarantees.\n *\n * - `TArg` keeps byte inputs broad.\n * - `TRet` marks byte outputs for TS 5.6 and TS 5.9+ compatibility.\n */\nexport type TypedArg<T> = T extends BigInt64Array\n ? BigInt64Array\n : T extends BigUint64Array\n ? BigUint64Array\n : T extends Float32Array\n ? Float32Array\n : T extends Float64Array\n ? Float64Array\n : T extends Int16Array\n ? Int16Array\n : T extends Int32Array\n ? Int32Array\n : T extends Int8Array\n ? Int8Array\n : T extends Uint16Array\n ? Uint16Array\n : T extends Uint32Array\n ? Uint32Array\n : T extends Uint8ClampedArray\n ? Uint8ClampedArray\n : T extends Uint8Array\n ? Uint8Array\n : never;\n/** Maps typed-array output leaves to narrow TS-compatible forms. */\nexport type TypedRet<T> = T extends BigInt64Array\n ? ReturnType<typeof BigInt64Array.of>\n : T extends BigUint64Array\n ? ReturnType<typeof BigUint64Array.of>\n : T extends Float32Array\n ? ReturnType<typeof Float32Array.of>\n : T extends Float64Array\n ? ReturnType<typeof Float64Array.of>\n : T extends Int16Array\n ? ReturnType<typeof Int16Array.of>\n : T extends Int32Array\n ? ReturnType<typeof Int32Array.of>\n : T extends Int8Array\n ? ReturnType<typeof Int8Array.of>\n : T extends Uint16Array\n ? ReturnType<typeof Uint16Array.of>\n : T extends Uint32Array\n ? ReturnType<typeof Uint32Array.of>\n : T extends Uint8ClampedArray\n ? ReturnType<typeof Uint8ClampedArray.of>\n : T extends Uint8Array\n ? ReturnType<typeof Uint8Array.of>\n : never;\n/** Recursively adapts byte-carrying API input types. See {@link TypedArg}. */\nexport type TArg<T> =\n | T\n | ([TypedArg<T>] extends [never]\n ? T extends (...args: infer A) => infer R\n ? ((...args: { [K in keyof A]: TRet<A[K]> }) => TArg<R>) & {\n [K in keyof T]: T[K] extends (...args: any) => any ? T[K] : TArg<T[K]>;\n }\n : T extends [infer A, ...infer R]\n ? [TArg<A>, ...{ [K in keyof R]: TArg<R[K]> }]\n : T extends readonly [infer A, ...infer R]\n ? readonly [TArg<A>, ...{ [K in keyof R]: TArg<R[K]> }]\n : T extends (infer A)[]\n ? TArg<A>[]\n : T extends readonly (infer A)[]\n ? readonly TArg<A>[]\n : T extends Promise<infer A>\n ? Promise<TArg<A>>\n : T extends object\n ? { [K in keyof T]: TArg<T[K]> }\n : T\n : TypedArg<T>);\n/** Recursively adapts byte-carrying API output types. See {@link TypedArg}. */\nexport type TRet<T> = T extends unknown\n ? T &\n ([TypedRet<T>] extends [never]\n ? T extends (...args: infer A) => infer R\n ? ((...args: { [K in keyof A]: TArg<A[K]> }) => TRet<R>) & {\n [K in keyof T]: T[K] extends (...args: any) => any ? T[K] : TRet<T[K]>;\n }\n : T extends [infer A, ...infer R]\n ? [TRet<A>, ...{ [K in keyof R]: TRet<R[K]> }]\n : T extends readonly [infer A, ...infer R]\n ? readonly [TRet<A>, ...{ [K in keyof R]: TRet<R[K]> }]\n : T extends (infer A)[]\n ? TRet<A>[]\n : T extends readonly (infer A)[]\n ? readonly TRet<A>[]\n : T extends Promise<infer A>\n ? Promise<TRet<A>>\n : T extends object\n ? { [K in keyof T]: TRet<T[K]> }\n : T\n : TypedRet<T>)\n : never;\n/**\n * Asserts that a value is a byte array and optionally checks its length.\n * Returns the original reference unchanged on success, and currently also accepts Node `Buffer`\n * values through the upstream validator.\n * This helper throws on malformed input, so APIs that must return `false` need to guard lengths\n * before decoding or before calling it.\n * @example\n * Validate that a value is a byte array with the expected length.\n * ```ts\n * abytes(new Uint8Array([1]), 1);\n * ```\n */\nconst abytesDoc: typeof abytes = abytes;\nexport { abytesDoc as abytes };\n/**\n * Concatenates byte arrays into a new `Uint8Array`.\n * Zero arguments return an empty `Uint8Array`.\n * Invalid segments throw before allocation because each argument is validated first.\n * @example\n * Concatenate two byte arrays into one result.\n * ```ts\n * concatBytes(new Uint8Array([1]), new Uint8Array([2]));\n * ```\n */\nconst concatBytesDoc: typeof concatBytes = concatBytes;\nexport { concatBytesDoc as concatBytes };\n/**\n * Returns cryptographically secure random bytes.\n * Requires `globalThis.crypto.getRandomValues` and throws if that API is unavailable.\n * `bytesLength` is validated by the upstream helper as a non-negative integer before allocation,\n * so negative and fractional values both throw instead of truncating through JS `ToIndex`.\n * @param bytesLength - Number of random bytes to generate.\n * @returns Fresh random bytes.\n * @example\n * Generate a fresh random seed.\n * ```ts\n * const seed = randomBytes(4);\n * ```\n */\nexport const randomBytes: typeof randb = randb;\n\n/**\n * Compares two byte arrays in a length-constant way for equal lengths.\n * Unequal lengths return `false` immediately, and there is no runtime type validation.\n * @param a - First byte array.\n * @param b - Second byte array.\n * @returns Whether both arrays contain the same bytes.\n * @example\n * Compare two byte arrays for equality.\n * ```ts\n * equalBytes(new Uint8Array([1]), new Uint8Array([1]));\n * ```\n */\nexport function equalBytes(a: TArg<Uint8Array>, b: TArg<Uint8Array>): boolean {\n if (a.length !== b.length) return false;\n let diff = 0;\n for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];\n return diff === 0;\n}\n\n/**\n * Copies bytes into a fresh `Uint8Array`.\n * Returns a detached plain `Uint8Array` after validating that the input is real bytes.\n * @param bytes - Source bytes.\n * @returns Copy of the input bytes.\n * @example\n * Copy bytes into a fresh array.\n * ```ts\n * copyBytes(new Uint8Array([1, 2]));\n * ```\n */\nexport function copyBytes(bytes: TArg<Uint8Array>): TRet<Uint8Array> {\n // `Uint8Array.from(...)` would also accept arrays / other typed arrays. Keep this helper strict\n // because callers use it at byte-validation boundaries before mutating the detached copy.\n return Uint8Array.from(abytes(bytes)) as TRet<Uint8Array>;\n}\n\n/**\n * Byte-swaps each 64-bit lane in place.\n * Falcon's exact binary64 tables are stored as little-endian byte payloads, so BE runtimes need\n * this boundary helper before aliasing them as host `Float64Array` lanes.\n * @param arr - Byte buffer whose length is a multiple of 8.\n * @returns The same buffer after in-place 64-bit lane byte swaps.\n * @example\n * Byte-swap one 64-bit lane in place.\n * ```ts\n * byteSwap64(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]));\n * ```\n */\nexport function byteSwap64<T extends ArrayBufferView>(arr: T): T {\n const bytes = new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);\n for (let i = 0; i < bytes.length; i += 8) {\n const a0 = bytes[i + 0];\n const a1 = bytes[i + 1];\n const a2 = bytes[i + 2];\n const a3 = bytes[i + 3];\n bytes[i + 0] = bytes[i + 7];\n bytes[i + 1] = bytes[i + 6];\n bytes[i + 2] = bytes[i + 5];\n bytes[i + 3] = bytes[i + 4];\n bytes[i + 4] = a3;\n bytes[i + 5] = a2;\n bytes[i + 6] = a1;\n bytes[i + 7] = a0;\n }\n return arr;\n}\n/**\n * Byte-swaps 64-bit lanes on big-endian runtimes and returns the input unchanged on little-endian.\n * This keeps Falcon's binary64 tables in canonical little-endian order before aliasing them as\n * `Float64Array` lanes on the current host.\n * @param arr - Buffer to pass through or swap in place.\n * @returns The same buffer, normalized for Falcon's little-endian table layout.\n * @example\n * Normalize one host-endian buffer for Falcon's float tables.\n * ```ts\n * baswap64If(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]));\n * ```\n */\nexport const baswap64If: <T extends ArrayBufferView>(arr: T) => T = isLE\n ? (arr) => arr\n : byteSwap64;\n\n/** Shared key-generation surface for signers and KEMs. */\nexport type CryptoKeys = {\n /** Optional metadata about the algorithm family or variant. */\n info?: { type?: string };\n /** Public byte lengths for the exported key material. */\n lengths: { seed?: number; publicKey?: number; secretKey?: number };\n /**\n * Generate one secret/public keypair.\n * @param seed - Optional seed bytes for deterministic key generation.\n * @returns Fresh secret/public keypair.\n */\n keygen: (seed?: TArg<Uint8Array>) => {\n secretKey: TRet<Uint8Array>;\n publicKey: TRet<Uint8Array>;\n };\n /**\n * Derive one public key from a secret key.\n * @param secretKey - Secret key bytes.\n * @returns Public key bytes.\n */\n getPublicKey: (secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;\n};\n\n/** Verification options shared by the signature APIs. */\nexport type VerOpts = {\n /** Optional application-defined context string. */\n context?: Uint8Array;\n};\n/** Signing options shared by the signature APIs. */\nexport type SigOpts = VerOpts & {\n // Compatibility with @noble/curves: false to disable, enabled by default, user can pass U8A\n /** Optional extra entropy or `false` to disable randomized signing. */\n extraEntropy?: Uint8Array | false;\n};\n\n/**\n * Validates that an options bag is a plain object.\n * @param opts - Options object to validate.\n * @throws On wrong argument types. {@link TypeError}\n * @example\n * Validate that an options bag is a plain object.\n * ```ts\n * validateOpts({});\n * ```\n */\nexport function validateOpts(opts: object): void {\n // Arrays silently passed here before, but these call sites expect named option-bag fields.\n if (Object.prototype.toString.call(opts) !== '[object Object]')\n throw new TypeError('expected valid options object');\n}\n\n/**\n * Validates common verification options.\n * `context` itself is validated with `abytes(...)`, and individual algorithms may narrow support\n * further after this shared plain-object gate.\n * @param opts - Verification options. See {@link VerOpts}.\n * @throws On wrong argument types. {@link TypeError}\n * @example\n * Validate common verification options.\n * ```ts\n * validateVerOpts({ context: new Uint8Array([1]) });\n * ```\n */\nexport function validateVerOpts(opts: TArg<VerOpts>): void {\n validateOpts(opts);\n if (opts.context !== undefined) abytes(opts.context, undefined, 'opts.context');\n}\n\n/**\n * Validates common signing options.\n * `extraEntropy` is validated with `abytes(...)`; exact lengths and extra algorithm-specific\n * restrictions are enforced later by callers.\n * @param opts - Signing options. See {@link SigOpts}.\n * @throws On wrong argument types. {@link TypeError}\n * @example\n * Validate common signing options.\n * ```ts\n * validateSigOpts({ extraEntropy: new Uint8Array([1]) });\n * ```\n */\nexport function validateSigOpts(opts: TArg<SigOpts>): void {\n validateVerOpts(opts);\n if (opts.extraEntropy !== false && opts.extraEntropy !== undefined)\n abytes(opts.extraEntropy, undefined, 'opts.extraEntropy');\n}\n\n/** Generic signature interface with key generation, signing, and verification. */\nexport type Signer = CryptoKeys & {\n /** Public byte lengths for signatures and signing randomness. */\n lengths: { signRand?: number; signature?: number };\n /**\n * Sign one message.\n * @param msg - Message bytes to sign.\n * @param secretKey - Secret key bytes.\n * @param opts - Optional signing options.\n * @returns Signature bytes.\n */\n sign: (\n msg: TArg<Uint8Array>,\n secretKey: TArg<Uint8Array>,\n opts?: TArg<SigOpts>\n ) => TRet<Uint8Array>;\n /**\n * Verify one signature.\n * @param sig - Signature bytes.\n * @param msg - Signed message bytes.\n * @param publicKey - Public key bytes.\n * @param opts - Optional verification options.\n * @returns `true` when the signature is valid, `false` when all inputs are well-formed but the\n * signature check does not pass. Some implementations also treat malformed signature encodings as\n * a verification failure and return `false`.\n * @throws On malformed API arguments or unsupported verification options.\n */\n verify: (\n sig: TArg<Uint8Array>,\n msg: TArg<Uint8Array>,\n publicKey: TArg<Uint8Array>,\n opts?: TArg<VerOpts>\n ) => boolean;\n};\n\n/** Generic key encapsulation mechanism interface. */\nexport type KEM = CryptoKeys & {\n /** Public byte lengths for ciphertexts and optional message randomness. */\n lengths: { cipherText?: number; msg?: number; msgRand?: number };\n /**\n * Encapsulate one shared secret to a recipient public key.\n * @param publicKey - Recipient public key bytes.\n * @param msg - Optional caller-provided randomness/message seed.\n * @returns Ciphertext plus shared secret.\n */\n encapsulate: (\n publicKey: TArg<Uint8Array>,\n msg?: TArg<Uint8Array>\n ) => {\n cipherText: TRet<Uint8Array>;\n sharedSecret: TRet<Uint8Array>;\n };\n /**\n * Recover the shared secret from a ciphertext and recipient secret key.\n * @param cipherText - Ciphertext bytes.\n * @param secretKey - Recipient secret key bytes.\n * @returns Decapsulated shared secret.\n */\n decapsulate: (cipherText: TArg<Uint8Array>, secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;\n};\n\n/** Bidirectional encoder/decoder interface. */\nexport interface Coder<F, T> {\n /**\n * Serialize one value.\n * @param from - Value to encode.\n * @returns Encoded representation.\n */\n encode(from: F): T;\n /**\n * Parse one serialized value.\n * @param to - Encoded representation.\n * @returns Decoded value.\n */\n decode(to: T): F;\n}\n\n/** Encoder/decoder interface specialized for byte arrays. */\nexport interface BytesCoder<T> extends Coder<T, Uint8Array> {\n /**\n * Serialize one value into bytes.\n * @param data - Value to encode.\n * @returns Encoded bytes.\n */\n encode: (data: T) => Uint8Array;\n /**\n * Parse one byte array into a value.\n * @param bytes - Encoded bytes.\n * @returns Decoded value.\n */\n decode: (bytes: Uint8Array) => T;\n}\n\n/** Fixed-length byte encoder/decoder. */\nexport type BytesCoderLen<T> = BytesCoder<T> & { bytesLen: number };\n\n// nano-packed, because struct encoding is hard.\ntype UnCoder<T> = T extends BytesCoder<infer U> ? U : never;\ntype SplitOut<T extends (number | BytesCoderLen<any>)[]> = {\n [K in keyof T]: T[K] extends number ? Uint8Array : UnCoder<T[K]>;\n};\n/**\n * Builds a fixed-layout coder from byte lengths and nested coders.\n * Raw-length fields decode as zero-copy `subarray(...)` views, and nested coders may preserve that\n * aliasing too. Nested coder `encode(...)` results are treated as owned scratch: `splitCoder`\n * copies them into the output and then zeroizes them with `fill(0)`. If a nested encoder forwards\n * caller-owned bytes, it must do so only after detaching them into a disposable copy.\n * @param label - Label used in validation errors.\n * @param lengths - Field lengths or nested coders.\n * @returns Composite fixed-length coder.\n * @example\n * Build a fixed-layout coder from byte lengths and nested coders.\n * ```ts\n * splitCoder('demo', 1, 2).encode([new Uint8Array([1]), new Uint8Array([2, 3])]);\n * ```\n */\nexport function splitCoder<T extends (number | BytesCoderLen<any>)[]>(\n label: string,\n ...lengths: T\n): TRet<BytesCoder<SplitOut<T>> & { bytesLen: number }> {\n const getLength = (c: TArg<number | BytesCoderLen<any>>) =>\n typeof c === 'number' ? c : (c as BytesCoderLen<any>).bytesLen;\n const bytesLen: number = lengths.reduce((sum: number, a) => sum + getLength(a), 0);\n return {\n bytesLen,\n encode: (bufs: T) => {\n const res = new Uint8Array(bytesLen);\n for (let i = 0, pos = 0; i < lengths.length; i++) {\n const c = lengths[i];\n const l = getLength(c);\n const b: Uint8Array = typeof c === 'number' ? (bufs[i] as any) : c.encode(bufs[i]);\n abytes_(b, l, label);\n res.set(b, pos);\n if (typeof c !== 'number') b.fill(0); // clean\n pos += l;\n }\n return res;\n },\n decode: (buf: TArg<Uint8Array>) => {\n abytes_(buf, bytesLen, label);\n const res = [];\n for (const c of lengths) {\n const l = getLength(c);\n const b = buf.subarray(0, l);\n res.push(typeof c === 'number' ? b : c.decode(b));\n buf = buf.subarray(l);\n }\n return res as SplitOut<T>;\n },\n } as any;\n}\n// nano-packed.array (fixed size)\n/**\n * Builds a fixed-length vector coder from another fixed-length coder.\n * Element decoding receives `subarray(...)` views, so aliasing depends on the element coder.\n * Element coder `encode(...)` results are treated as owned scratch: `vecCoder` copies them into\n * the output and then zeroizes them with `fill(0)`. If an element encoder forwards caller-owned\n * bytes, it must do so only after detaching them into a disposable copy. `vecCoder` also trusts\n * the `BytesCoderLen` contract: each encoded element must already be exactly `c.bytesLen` bytes.\n * @param c - Element coder.\n * @param vecLen - Number of elements in the vector.\n * @returns Fixed-length vector coder.\n * @example\n * Build a fixed-length vector coder from another fixed-length coder.\n * ```ts\n * vecCoder(\n * { bytesLen: 1, encode: (n: number) => Uint8Array.of(n), decode: (b: Uint8Array) => b[0] || 0 },\n * 2\n * ).encode([1, 2]);\n * ```\n */\nexport function vecCoder<T>(c: TArg<BytesCoderLen<T>>, vecLen: number): TRet<BytesCoderLen<T[]>> {\n const coder = c as BytesCoderLen<T>;\n const bytesLen = vecLen * coder.bytesLen;\n return {\n bytesLen,\n encode: (u: TArg<T[]>): TRet<Uint8Array> => {\n if (u.length !== vecLen)\n throw new RangeError(`vecCoder.encode: wrong length=${u.length}. Expected: ${vecLen}`);\n const res = new Uint8Array(bytesLen);\n for (let i = 0, pos = 0; i < u.length; i++) {\n const b = coder.encode(u[i] as T);\n res.set(b, pos);\n b.fill(0); // clean\n pos += b.length;\n }\n return res as TRet<Uint8Array>;\n },\n decode: (a: TArg<Uint8Array>): TRet<T[]> => {\n abytes_(a, bytesLen);\n const r: T[] = [];\n for (let i = 0; i < a.length; i += coder.bytesLen)\n r.push(coder.decode(a.subarray(i, i + coder.bytesLen)));\n return r as TRet<T[]>;\n },\n } as any;\n}\n\n/**\n * Overwrites supported typed-array inputs with zeroes in place.\n * Accepts direct typed arrays and one-level arrays of them.\n * @param list - Typed arrays or one-level lists of typed arrays to clear.\n * @example\n * Overwrite typed arrays with zeroes.\n * ```ts\n * const buf = Uint8Array.of(1, 2, 3);\n * cleanBytes(buf);\n * ```\n */\nexport function cleanBytes(...list: (TypedArray | TypedArray[])[]): void {\n for (const t of list) {\n if (Array.isArray(t)) for (const b of t) b.fill(0);\n else t.fill(0);\n }\n}\n\n/**\n * Creates a 32-bit mask with the lowest `bits` bits set.\n * @param bits - Number of low bits to keep.\n * @returns Bit mask with `bits` ones.\n * @throws On wrong argument ranges or values. {@link RangeError}\n * @example\n * Create a low-bit mask for packed-field operations.\n * ```ts\n * const mask = getMask(4);\n * ```\n */\nexport function getMask(bits: number): number {\n if (!Number.isSafeInteger(bits) || bits < 0 || bits > 32)\n throw new RangeError(`expected bits in [0..32], got ${bits}`);\n // JS shifts are modulo 32, so bit 32 needs an explicit full-width mask.\n return bits === 32 ? 0xffffffff : ~(-1 << bits) >>> 0;\n}\n\n/** Shared empty byte array used as the default context. */\nexport const EMPTY: TRet<Uint8Array> = /* @__PURE__ */ Uint8Array.of();\n\n/**\n * Builds the domain-separated message payload for the pure sign/verify paths.\n * Context length `255` is valid; only `ctx.length > 255` is rejected.\n * @param msg - Message bytes.\n * @param ctx - Optional context bytes.\n * @returns Domain-separated message payload.\n * @throws On wrong argument ranges or values. {@link RangeError}\n * @example\n * Build the domain-separated payload before direct signing.\n * ```ts\n * const payload = getMessage(new Uint8Array([1, 2]));\n * ```\n */\nexport function getMessage(msg: TArg<Uint8Array>, ctx: TArg<Uint8Array> = EMPTY): TRet<Uint8Array> {\n abytes_(msg);\n abytes_(ctx);\n if (ctx.length > 255) throw new RangeError('context should be 255 bytes or less');\n return concatBytes(new Uint8Array([0, ctx.length]), ctx, msg);\n}\n\n// DER tag+length plus the shared NIST hash OID arc 2.16.840.1.101.3.4.2.* used by the\n// FIPS 204 / FIPS 205 pre-hash wrappers; the final byte selects SHA-256, SHA-512, SHAKE128,\n// SHAKE256, or another approved hash/XOF under that subtree.\n// 06 09 60 86 48 01 65 03 04 02\nconst oidNistP = /* @__PURE__ */ Uint8Array.from([6, 9, 0x60, 0x86, 0x48, 1, 0x65, 3, 4, 2]);\n\n/**\n * Validates that a hash exposes a NIST hash OID and enough collision resistance.\n * Current accepted surface is broader than the FIPS algorithm tables: any hash/XOF under the NIST\n * `2.16.840.1.101.3.4.2.*` subtree is accepted if its effective `outputLen` is strong enough.\n * XOF callers must pass a callable whose `outputLen` matches the digest length they actually intend\n * to sign; bare `shake128` / `shake256` defaults are too short for the stronger prehash modes.\n * @param hash - Hash function to validate.\n * @param requiredStrength - Minimum required collision-resistance strength in bits.\n * @throws If the hash metadata or collision resistance is insufficient. {@link Error}\n * @example\n * Validate that a hash exposes a NIST hash OID and enough collision resistance.\n * ```ts\n * import { sha256 } from '@noble/hashes/sha2.js';\n * import { checkHash } from '@noble/post-quantum/utils.js';\n * checkHash(sha256, 128);\n * ```\n */\nexport function checkHash(hash: CHash, requiredStrength: number = 0): void {\n if (!hash.oid || !equalBytes(hash.oid.subarray(0, 10), oidNistP))\n throw new Error('hash.oid is invalid: expected NIST hash');\n // FIPS 204 / FIPS 205 require both collision and second-preimage strength; for approved NIST\n // hashes/XOFs under this OID subtree, the collision bound from the configured digest length is\n // the tighter runtime check, so enforce that lower bound here.\n const collisionResistance = (hash.outputLen * 8) / 2;\n if (requiredStrength > collisionResistance) {\n throw new Error(\n 'Pre-hash security strength too low: ' +\n collisionResistance +\n ', required: ' +\n requiredStrength\n );\n }\n}\n\n/**\n * Builds the domain-separated prehash payload for the prehash sign/verify paths.\n * Callers are expected to vet `hash.oid` first, e.g. via `checkHash(...)`; calling this helper\n * directly with a hash object that lacks `oid` currently throws later inside `concatBytes(...)`.\n * Context length `255` is valid; only `ctx.length > 255` is rejected.\n * @param hash - Prehash function.\n * @param msg - Message bytes.\n * @param ctx - Optional context bytes.\n * @returns Domain-separated prehash payload.\n * @throws On wrong argument ranges or values. {@link RangeError}\n * @example\n * Build the domain-separated prehash payload for external hashing.\n * ```ts\n * import { sha256 } from '@noble/hashes/sha2.js';\n * import { getMessagePrehash } from '@noble/post-quantum/utils.js';\n * getMessagePrehash(sha256, new Uint8Array([1, 2]));\n * ```\n */\nexport function getMessagePrehash(\n hash: CHash,\n msg: TArg<Uint8Array>,\n ctx: TArg<Uint8Array> = EMPTY\n): TRet<Uint8Array> {\n abytes_(msg);\n abytes_(ctx);\n if (ctx.length > 255) throw new RangeError('context should be 255 bytes or less');\n const hashed = hash(msg);\n return concatBytes(new Uint8Array([1, ctx.length]), ctx, hash.oid!, hashed);\n}\n","/**\n * Internal methods for lattice-based ML-KEM and ML-DSA.\n * @module\n */\n/*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */\nimport { FFTCore, reverseBits } from '@noble/curves/abstract/fft.js';\nimport { shake128, shake256 } from '@noble/hashes/sha3.js';\nimport type { TypedArray } from '@noble/hashes/utils.js';\nimport {\n type BytesCoderLen,\n cleanBytes,\n type Coder,\n getMask,\n type TArg,\n type TRet,\n} from './utils.ts';\n\n/** Extendable-output reader used by the CRYSTALS implementations. */\nexport type XOF = (\n seed: Uint8Array,\n blockLen?: number\n) => {\n /**\n * Read diagnostic counters for the current XOF session.\n * @returns Current call and XOF block counters.\n */\n stats: () => { calls: number; xofs: number };\n /**\n * Select one `(x, y)` coordinate pair and get a block reader for it.\n * Only one coordinate stream is live at a time: a later `get(...)` call rebinds the shared\n * SHAKE state and invalidates older readers.\n * Each squeeze aliases one mutable internal output buffer, so callers must copy blocks they\n * want to retain before the next read.\n * @param x - First matrix coordinate.\n * @param y - Second matrix coordinate.\n * @returns Lazy block reader for that coordinate pair.\n */\n get: (x: number, y: number) => () => Uint8Array; // return block aligned to blockLen and 3\n /** Wipe any buffered state once the reader is no longer needed. */\n clean: () => void;\n};\n\n/** CRYSTALS (ml-kem, ml-dsa) options */\n/** Shared polynomial and NTT parameters for CRYSTALS algorithms. */\nexport type CrystalOpts<T extends TypedArray> = {\n /**\n * Allocate one zeroed polynomial/vector container.\n * @param n - Number of coefficients to allocate.\n * @returns Fresh typed container.\n */\n newPoly: TypedCons<T>;\n /** Polynomial size, typically `256`. */\n N: number;\n /** Prime modulus used for all coefficient arithmetic. */\n Q: number;\n /** Inverse transform normalization factor:\n * `256**-1 mod q` for Dilithium, `128**-1 mod q` for Kyber.\n */\n F: number;\n /** Principal root of unity for the transform domain. */\n ROOT_OF_UNITY: number;\n /** Number of bits used for bit-reversal ordering. */\n brvBits: number;\n /** `true` for Kyber/ML-KEM mode, `false` for Dilithium/ML-DSA mode. */\n isKyber: boolean;\n};\n\n/** Constructor function for typed polynomial containers. */\nexport type TypedCons<T extends TypedArray> = (n: number) => T;\n\ntype Crystals<T extends TypedArray> = {\n mod: (a: number, modulo?: number) => number;\n smod: (a: number, modulo?: number) => number;\n nttZetas: T;\n NTT: {\n /** Forward transform in place. Mutates and returns `r`. */\n encode: (r: T) => T;\n /** Inverse transform in place. Mutates and returns `r`. */\n decode: (r: T) => T;\n };\n bitsCoder: (d: number, c: Coder<number, number>) => BytesCoderLen<T>;\n};\n\n/**\n * Creates shared modular arithmetic, NTT, and packing helpers for CRYSTALS schemes.\n * @param opts - Polynomial and transform parameters. See {@link CrystalOpts}.\n * @returns CRYSTALS arithmetic and encoding helpers.\n * @example\n * Create shared modular arithmetic and NTT helpers for a CRYSTALS parameter set.\n * ```ts\n * const crystals = genCrystals({\n * newPoly: (n) => new Uint16Array(n),\n * N: 256,\n * Q: 3329,\n * F: 3303,\n * ROOT_OF_UNITY: 17,\n * brvBits: 7,\n * isKyber: true,\n * });\n * const reduced = crystals.mod(-1);\n * ```\n */\nexport const genCrystals = <T extends TypedArray>(opts: CrystalOpts<T>): TRet<Crystals<T>> => {\n // isKyber: true means Kyber, false means Dilithium\n const { newPoly, N, Q, F, ROOT_OF_UNITY, brvBits, isKyber } = opts;\n // Normalize JS `%` into the canonical Z_m representative `[0, modulo-1]` expected by\n // FIPS 203 §2.3 / FIPS 204 §2.3 before downstream mod-q arithmetic.\n const mod = (a: number, modulo = Q): number => {\n const result = a % modulo | 0;\n return (result >= 0 ? result | 0 : (modulo + result) | 0) | 0;\n };\n // FIPS 204 §7.4 uses the centered `mod ±` representative for low bits, keeping the\n // positive midpoint when `modulo` is even.\n // Center to `[-floor((modulo-1)/2), floor(modulo/2)]`.\n const smod = (a: number, modulo = Q): number => {\n const r = mod(a, modulo) | 0;\n return (r > modulo >> 1 ? (r - modulo) | 0 : r) | 0;\n };\n // Kyber uses the FIPS 203 Appendix A `BitRev_7` table here via the first 128 entries, while\n // Dilithium uses the FIPS 204 §7.5 / Appendix B `BitRev_8` zetas table over all 256 entries.\n function getZettas() {\n const out = newPoly(N);\n for (let i = 0; i < N; i++) {\n const b = reverseBits(i, brvBits);\n const p = BigInt(ROOT_OF_UNITY) ** BigInt(b) % BigInt(Q);\n out[i] = Number(p) | 0;\n }\n return out;\n }\n const nttZetas = getZettas();\n\n // Number-Theoretic Transform\n // Explained: https://electricdusk.com/ntt.html\n\n // Kyber has slightly different params, since there is no 512th primitive root of unity mod q,\n // only 256th primitive root of unity mod. Which also complicates MultiplyNTT.\n\n const field = {\n add: (a: number, b: number) => mod((a | 0) + (b | 0)) | 0,\n sub: (a: number, b: number) => mod((a | 0) - (b | 0)) | 0,\n mul: (a: number, b: number) => mod((a | 0) * (b | 0)) | 0,\n inv: (_a: number) => {\n throw new Error('not implemented');\n },\n };\n const nttOpts = {\n N,\n roots: nttZetas as any,\n invertButterflies: true,\n skipStages: isKyber ? 1 : 0,\n brp: false,\n };\n const dif = FFTCore(field, { dit: false, ...nttOpts });\n const dit = FFTCore(field, { dit: true, ...nttOpts });\n const NTT = {\n encode: (r: T): T => {\n return dif(r) as any;\n },\n decode: (r: T): T => {\n dit(r as any);\n // The inverse-NTT normalization factor is family-specific: FIPS 203 Algorithm 10 line 14\n // uses `128^-1 mod q` for Kyber, while FIPS 204 Algorithm 42 lines 21-23 use `256^-1 mod q`.\n // kyber uses 128 here, because brv && stuff\n for (let i = 0; i < r.length; i++) r[i] = mod(F * r[i]);\n return r;\n },\n };\n // Pack one little-endian `d`-bit word per coefficient, matching FIPS 203 ByteEncode /\n // ByteDecode and the FIPS 204 BitsToBytes-based polynomial packing helpers.\n const bitsCoder = (d: number, c: Coder<number, number>): TRet<BytesCoderLen<T>> => {\n const mask = getMask(d);\n const bytesLen = d * (N / 8);\n return {\n bytesLen,\n encode: (poly_: TArg<T>): TRet<Uint8Array> => {\n const poly = poly_ as T;\n const r = new Uint8Array(bytesLen);\n for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < poly.length; i++) {\n buf |= (c.encode(poly[i]) & mask) << bufLen;\n bufLen += d;\n for (; bufLen >= 8; bufLen -= 8, buf >>= 8) r[pos++] = buf & getMask(bufLen);\n }\n return r as TRet<Uint8Array>;\n },\n decode: (bytes: TArg<Uint8Array>): TRet<T> => {\n const r = newPoly(N);\n for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < bytes.length; i++) {\n buf |= bytes[i] << bufLen;\n bufLen += 8;\n for (; bufLen >= d; bufLen -= d, buf >>= d) r[pos++] = c.decode(buf & mask);\n }\n return r as TRet<T>;\n },\n } as TRet<BytesCoderLen<T>>;\n };\n\n return {\n mod,\n smod,\n nttZetas: nttZetas as TRet<T>,\n NTT: {\n encode: (r: TArg<T>): TRet<T> => NTT.encode(r as T) as TRet<T>,\n decode: (r: TArg<T>): TRet<T> => NTT.decode(r as T) as TRet<T>,\n },\n bitsCoder: bitsCoder as TRet<Crystals<T>>['bitsCoder'],\n };\n};\n\nconst createXofShake =\n (shake: typeof shake128): TRet<XOF> =>\n (seed: TArg<Uint8Array>, blockLen?: number) => {\n if (!blockLen) blockLen = shake.blockLen;\n // Optimizations that won't mater:\n // - cached seed update (two .update(), on start and on the end)\n // - another cache which cloned into working copy\n\n // Faster than multiple updates, since seed less than blockLen\n const _seed = new Uint8Array(seed.length + 2);\n _seed.set(seed);\n const seedLen = seed.length;\n const buf = new Uint8Array(blockLen); // == shake128.blockLen\n let h = shake.create({});\n let calls = 0;\n let xofs = 0;\n return {\n stats: () => ({ calls, xofs }),\n get: (x: number, y: number) => {\n // Rebind to `seed || x || y` so callers can implement the spec's per-coordinate\n // SHAKE inputs like `rho || j || i` and `rho || IntegerToBytes(counter, 2)`.\n _seed[seedLen + 0] = x;\n _seed[seedLen + 1] = y;\n h.destroy();\n h = shake.create({}).update(_seed);\n calls++;\n return () => {\n xofs++;\n return h.xofInto(buf) as TRet<Uint8Array>;\n };\n },\n clean: () => {\n h.destroy();\n cleanBytes(buf, _seed);\n },\n };\n };\n\n/**\n * SHAKE128-based extendable-output reader factory used by ML-KEM.\n * `get(x, y)` selects one coordinate pair at a time; calling it again invalidates previously\n * returned readers, and each squeeze reuses one mutable internal output buffer.\n * @param seed - Seed bytes for the reader.\n * @param blockLen - Optional output block length.\n * @returns Stateful XOF reader.\n * @example\n * Build the ML-KEM SHAKE128 matrix expander and read one block.\n * ```ts\n * import { randomBytes } from '@noble/post-quantum/utils.js';\n * import { XOF128 } from '@noble/post-quantum/_crystals.js';\n * const reader = XOF128(randomBytes(32));\n * const block = reader.get(0, 0)();\n * ```\n */\nexport const XOF128: TRet<XOF> = /* @__PURE__ */ createXofShake(shake128);\n/**\n * SHAKE256-based extendable-output reader factory used by ML-DSA.\n * `get(x, y)` appends raw one-byte coordinates to the seed, invalidates previously returned\n * readers, and reuses one mutable internal output buffer for each squeeze.\n * @param seed - Seed bytes for the reader.\n * @param blockLen - Optional output block length.\n * @returns Stateful XOF reader.\n * @example\n * Build the ML-DSA SHAKE256 coefficient expander and read one block.\n * ```ts\n * import { randomBytes } from '@noble/post-quantum/utils.js';\n * import { XOF256 } from '@noble/post-quantum/_crystals.js';\n * const reader = XOF256(randomBytes(32));\n * const block = reader.get(0, 0)();\n * ```\n */\nexport const XOF256: TRet<XOF> = /* @__PURE__ */ createXofShake(shake256);\n","/**\n * ML-KEM: Module Lattice-based Key Encapsulation Mechanism from\n * [FIPS-203](https://csrc.nist.gov/pubs/fips/203/ipd). A.k.a. CRYSTALS-Kyber.\n *\n * Key encapsulation is similar to DH / ECDH (think X25519), with important differences:\n * * Unlike in ECDH, we can't verify if it was \"Bob\" who've sent the shared secret\n * * Unlike ECDH, it is probabalistic and relies on quality of randomness (CSPRNG).\n * * Decapsulation never throws an error, even when shared secret was\n * encrypted by a different public key. It will just return a different shared secret.\n *\n * There are some concerns with regards to security: see\n * [djb blog](https://blog.cr.yp.to/20231003-countcorrectly.html) and\n * [mailing list](https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/W2VOzy0wz_E).\n *\n * Has similar internals to ML-DSA, but their keys and params are different.\n *\n * Check out [official site](https://www.pq-crystals.org/kyber/resources.shtml),\n * [repo](https://github.com/pq-crystals/kyber),\n * [spec](https://datatracker.ietf.org/doc/draft-cfrg-schwabe-kyber/).\n * @module\n */\n/*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */\nimport { sha3_256, sha3_512, shake256 } from '@noble/hashes/sha3.js';\nimport { type CHash, swap32IfBE, u32 } from '@noble/hashes/utils.js';\nimport { genCrystals, type XOF, XOF128 } from './_crystals.ts';\nimport {\n abytes,\n cleanBytes,\n type Coder,\n copyBytes,\n equalBytes,\n getMask,\n type KEM,\n randomBytes,\n splitCoder,\n type TArg,\n type TRet,\n vecCoder,\n} from './utils.ts';\n\n/** Key encapsulation mechanism interface */\n\nconst N = 256; // Kyber (not FIPS-203) supports different lengths, but all std modes were using 256\nconst Q = 3329; // 13*(2**8)+1, modulo prime\nconst F = 3303; // 3303 ≡ 128**(−1) mod q (FIPS-203)\nconst ROOT_OF_UNITY = 17; // ζ = 17 ∈ Zq is a primitive 256-th root of unity modulo Q. ζ**128 ≡−1\n// treeshake: keep genCrystals behind the object so PARAMS-only bundles can drop it entirely.\n// Shared CRYSTALS helper in the ML-KEM branch: Kyber mode, 7-bit bit-reversal,\n// and Uint16Array polys because current coefficients stay reduced modulo q.\nconst crystals = /* @__PURE__ */ genCrystals({\n N,\n Q,\n F,\n ROOT_OF_UNITY,\n newPoly: (n: number): TRet<Uint16Array> => new Uint16Array(n) as TRet<Uint16Array>,\n brvBits: 7,\n isKyber: true,\n});\n\n/** FIPS 203: 7. Parameter Sets */\n/** Public ML-KEM parameter-set description. */\nexport type KEMParam = {\n /** Polynomial size. */\n N: number;\n /** Module rank. */\n K: number;\n /** Prime modulus. */\n Q: number;\n /** CBD parameter used for secret-key noise. */\n ETA1: number;\n /** CBD parameter used for error noise. */\n ETA2: number;\n /** Compression width for the `u` vector. */\n du: number;\n /** Compression width for the `v` polynomial. */\n dv: number;\n /** Required strength of the randomness source in bits. */\n RBGstrength: number;\n};\n/** Internal params of ML-KEM versions */\n// prettier-ignore\n/** Built-in ML-KEM parameter presets keyed by the public export names\n * `ml_kem512` / `ml_kem768` / `ml_kem1024`.\n * `RBGstrength` is Table 2's required randomness-source strength in bits,\n * not a generic security label.\n */\nexport const PARAMS: Record<string, KEMParam> = /* @__PURE__ */ (() =>\n Object.freeze({\n 512: Object.freeze({ N, Q, K: 2, ETA1: 3, ETA2: 2, du: 10, dv: 4, RBGstrength: 128 }),\n 768: Object.freeze({ N, Q, K: 3, ETA1: 2, ETA2: 2, du: 10, dv: 4, RBGstrength: 192 }),\n 1024: Object.freeze({ N, Q, K: 4, ETA1: 2, ETA2: 2, du: 11, dv: 5, RBGstrength: 256 }),\n } as const))();\n\n// FIPS-203: compress/decompress\nconst compress = (d: number): Coder<number, number> => {\n // d=12 is the ByteEncode12/ByteDecode12 path, not lossy compression.\n // ByteDecode12 interprets each 12-bit word modulo q; without that reduction the public-key\n // modulus check in encapsulate() becomes a no-op for malformed coefficients like 4095.\n if (d >= 12) return { encode: (i: number) => i, decode: (i: number) => (i >= Q ? i - Q : i) };\n // Comments map to python implementation in RFC (draft-cfrg-schwabe-kyber)\n // const round = (i: number) => Math.floor(i + 0.5) | 0;\n const a = 2 ** (d - 1);\n return {\n // This only matches standalone Compress_d after bitsCoder masks the result into Z_(2^d).\n encode: (i: number) => ((i << d) + Q / 2) / Q,\n // const decompress = (i: number) => round((Q / 2 ** d) * i);\n decode: (i: number) => (i * Q + a) >>> d,\n };\n};\n\n// Raw ByteEncode_d / ByteDecode_d from FIPS 203 operate on d-bit words directly.\n// That differs from `polyCoder(d)` for d<12, where noble folds packing together with the lossy\n// ciphertext compression step used by u/v. Tests that exercise the spec's raw packing surface need\n// this exact non-lossy variant instead.\nconst byteCoder = (d: number) =>\n crystals.bitsCoder(\n d,\n d === 12\n ? { encode: (i: number) => i, decode: (i: number) => (i >= Q ? i - Q : i) }\n : { encode: (i: number) => i, decode: (i: number) => i }\n );\n\n// NOTE: we merge encoding and compress because it is faster, also both require same d param\n// d=12 is the ByteEncode12/ByteDecode12 path rather than compression, and caller-side\n// public-key modulus checks route through this helper's decode/encode roundtrip.\n// Converts between bytes and d-bits compressed representation.\n// Kinda like convertRadix2 from @scure/base.\n// decode(encode(t)) == t, but there is loss of information on encode(decode(t))\nconst polyCoder = (d: number) => (d === 12 ? byteCoder(12) : crystals.bitsCoder(d, compress(d)));\n\n// Poly is mod Q, so 12 bits\ntype Poly = Uint16Array;\n\nfunction polyAdd(a_: TArg<Poly>, b_: TArg<Poly>) {\n const a = a_ as Poly;\n const b = b_ as Poly;\n // Mutates `a` in place; callers must pass two N=256 polynomials.\n for (let i = 0; i < N; i++) a[i] = crystals.mod(a[i] + b[i]); // a += b\n}\nfunction polySub(a_: TArg<Poly>, b_: TArg<Poly>) {\n const a = a_ as Poly;\n const b = b_ as Poly;\n // Mutates `a` in place; callers must pass two N=256 polynomials.\n for (let i = 0; i < N; i++) a[i] = crystals.mod(a[i] - b[i]); // a -= b\n}\n\n// FIPS-203: Computes the product of two degree-one polynomials with respect to a quadratic modulus\nfunction BaseCaseMultiply(a0: number, a1: number, b0: number, b1: number, zeta: number) {\n // `zeta` here is Algorithm 11's γ = ζ^(2BitRev_7(i)+1).\n const c0 = crystals.mod(a1 * b1 * zeta + a0 * b0);\n const c1 = crystals.mod(a0 * b1 + a1 * b0);\n return { c0, c1 };\n}\n\n// FIPS-203: Computes the product (in the ring Tq) of two NTT representations.\n// Works in place on `f`; `g` is read-only and both inputs must already be in NTT form.\nfunction MultiplyNTTs(f_: TArg<Poly>, g_: TArg<Poly>): TRet<Poly> {\n const f = f_ as Poly;\n const g = g_ as Poly;\n for (let i = 0; i < N / 2; i++) {\n let z = crystals.nttZetas[64 + (i >> 1)];\n if (i & 1) z = -z;\n const { c0, c1 } = BaseCaseMultiply(f[2 * i + 0], f[2 * i + 1], g[2 * i + 0], g[2 * i + 1], z);\n f[2 * i + 0] = c0;\n f[2 * i + 1] = c1;\n }\n return f as TRet<Poly>;\n}\n\ntype PRF = (l: number, key: Uint8Array, nonce: number) => Uint8Array;\n\ntype XofGet = ReturnType<ReturnType<XOF>['get']>;\n\ntype KyberOpts = KEMParam & {\n HASH256: CHash;\n HASH512: CHash;\n KDF: CHash<any, { dkLen?: number }>;\n XOF: XOF; // (seed: Uint8Array, len: number, x: number, y: number) => Uint8Array;\n PRF: PRF;\n};\n\n// Return poly in NTT representation\nfunction SampleNTT(xof_: TArg<XofGet>): TRet<Poly> {\n const xof = xof_ as XofGet;\n // The reader must already bind the Algorithm 7 seed||j||i bytes\n // and return block lengths divisible by 3.\n const r: Poly = new Uint16Array(N);\n for (let j = 0; j < N; ) {\n const b = xof();\n if (b.length % 3) throw new Error('SampleNTT: unaligned block');\n for (let i = 0; j < N && i + 3 <= b.length; i += 3) {\n const d1 = ((b[i + 0] >> 0) | (b[i + 1] << 8)) & 0xfff;\n const d2 = ((b[i + 1] >> 4) | (b[i + 2] << 4)) & 0xfff;\n if (d1 < Q) r[j++] = d1;\n if (j < N && d2 < Q) r[j++] = d2;\n }\n }\n return r as TRet<Poly>;\n}\n\n// Sampling from the centered binomial distribution\n// Returns poly with small coefficients (noise/errors) stored modulo q in ordinary coefficient form.\n// Current callers only use Table 2 eta values {2,3} and PRF outputs of exactly 64*eta bytes.\nconst sampleCBDBytes = (buf: TArg<Uint8Array>, eta: number): TRet<Poly> => {\n const r: Poly = new Uint16Array(N);\n // CBD consumes the PRF bitstream in little-endian byte order; normalize the word view on BE,\n // then swap it back so callers still observe `buf` as read-only.\n const b32 = u32(buf);\n swap32IfBE(b32);\n let len = 0;\n for (let i = 0, p = 0, bb = 0, t0 = 0; i < b32.length; i++) {\n let b = b32[i];\n for (let j = 0; j < 32; j++) {\n bb += b & 1;\n b >>= 1;\n len += 1;\n if (len === eta) {\n t0 = bb;\n bb = 0;\n } else if (len === 2 * eta) {\n r[p++] = crystals.mod(t0 - bb);\n bb = 0;\n len = 0;\n }\n }\n }\n swap32IfBE(b32);\n if (len) throw new Error(`sampleCBD: leftover bits: ${len}`);\n return r as TRet<Poly>;\n};\n\nfunction sampleCBD(\n PRF_: TArg<PRF>,\n seed: TArg<Uint8Array>,\n nonce: number,\n eta: number\n): TRet<Poly> {\n const PRF = PRF_ as PRF;\n return sampleCBDBytes(PRF((eta * N) / 4, seed, nonce), eta);\n}\n\n// K-PKE\n// Internal ML-KEM subroutine only: exact 32-byte `seed` / `msg` inputs\n// come from Algorithms 13-15, and the helper mutates decoded temporary\n// polynomials in place while leaving caller byte arrays unchanged.\nconst genKPKE = (opts_: TArg<KyberOpts>) => {\n const opts = opts_ as KyberOpts;\n const { K, PRF, XOF, HASH512, ETA1, ETA2, du, dv } = opts;\n const poly1 = polyCoder(1);\n const polyV = polyCoder(dv);\n const polyU = polyCoder(du);\n const publicCoder = splitCoder('publicKey', vecCoder(polyCoder(12), K), 32);\n const secretCoder = vecCoder(polyCoder(12), K);\n const cipherCoder = splitCoder('ciphertext', vecCoder(polyU, K), polyV);\n const seedCoder = splitCoder('seed', 32, 32);\n return {\n secretCoder,\n lengths: {\n secretKey: secretCoder.bytesLen,\n publicKey: publicCoder.bytesLen,\n cipherText: cipherCoder.bytesLen,\n },\n keygen: (seed: TArg<Uint8Array>) => {\n abytes(seed, 32, 'seed');\n const seedDst = new Uint8Array(33);\n seedDst.set(seed);\n // FIPS 203 Algorithm 13 appends the parameter-set byte `k`\n // before `G(d || k)`, so expanding the same 32-byte seed\n // under a different ML-KEM parameter set yields unrelated keys.\n seedDst[32] = K;\n const seedHash = HASH512(seedDst);\n\n const [rho, sigma] = seedCoder.decode(seedHash);\n const sHat: Poly[] = [];\n const tHat: Poly[] = [];\n for (let i = 0; i < K; i++) sHat.push(crystals.NTT.encode(sampleCBD(PRF, sigma, i, ETA1)));\n const x = XOF(rho);\n for (let i = 0; i < K; i++) {\n const e = crystals.NTT.encode(sampleCBD(PRF, sigma, K + i, ETA1));\n for (let j = 0; j < K; j++) {\n const aji = SampleNTT(x.get(j, i)); // A[i][j], inplace\n polyAdd(e, MultiplyNTTs(aji, sHat[j]));\n }\n tHat.push(e); // t ← A ◦ s + e\n }\n x.clean();\n const res = {\n publicKey: publicCoder.encode([tHat, rho]),\n secretKey: secretCoder.encode(sHat),\n };\n cleanBytes(rho, sigma, sHat, tHat, seedDst, seedHash);\n return res;\n },\n encrypt: (\n publicKey: TArg<Uint8Array>,\n msg: TArg<Uint8Array>,\n seed: TArg<Uint8Array>\n ): TRet<Uint8Array> => {\n const [tHat, rho] = publicCoder.decode(publicKey);\n const rHat = [];\n for (let i = 0; i < K; i++) rHat.push(crystals.NTT.encode(sampleCBD(PRF, seed, i, ETA1)));\n const x = XOF(rho);\n const tmp2 = new Uint16Array(N);\n const u = [];\n for (let i = 0; i < K; i++) {\n const e1 = sampleCBD(PRF, seed, K + i, ETA2);\n const tmp = new Uint16Array(N);\n for (let j = 0; j < K; j++) {\n const aij = SampleNTT(x.get(i, j)); // A[j][i], inplace transpose access\n polyAdd(tmp, MultiplyNTTs(aij, rHat[j])); // t += aij * rHat[j]\n }\n polyAdd(e1, crystals.NTT.decode(tmp)); // e1 += tmp\n u.push(e1);\n polyAdd(tmp2, MultiplyNTTs(tHat[i], rHat[i])); // t2 += tHat[i] * rHat[i]\n cleanBytes(tmp);\n }\n x.clean();\n const e2 = sampleCBD(PRF, seed, 2 * K, ETA2);\n polyAdd(e2, crystals.NTT.decode(tmp2)); // e2 += tmp2\n const v = poly1.decode(msg); // encode plaintext m into polynomial v\n polyAdd(v, e2); // v += e2\n cleanBytes(tHat, rHat, tmp2, e2);\n return cipherCoder.encode([u, v]) as TRet<Uint8Array>;\n },\n decrypt: (cipherText: TArg<Uint8Array>, privateKey: TArg<Uint8Array>): TRet<Uint8Array> => {\n const [u, v] = cipherCoder.decode(cipherText);\n const sk = secretCoder.decode(privateKey); // s ← ByteDecode_12(dkPKE)\n const tmp = new Uint16Array(N);\n // tmp += sk[i] * u[i]\n for (let i = 0; i < K; i++) polyAdd(tmp, MultiplyNTTs(sk[i], crystals.NTT.encode(u[i])));\n polySub(v, crystals.NTT.decode(tmp)); // w = v' - tmp\n cleanBytes(tmp, sk, u);\n return poly1.encode(v) as TRet<Uint8Array>;\n },\n };\n};\n\n/**\n * Public ML-KEM wrapper over the internal K-PKE subroutine.\n * `keygen(seed)` and `encapsulate(publicKey, msg)` are deterministic/test-oriented hooks that map\n * more directly to Algorithms 16-17 than to the pure no-input / random-internal Algorithms 19-20.\n * decapsulate() tries to follow the Algorithms 18/21 implicit-reject structure as closely as\n * practical here by re-encrypting, comparing ciphertexts, returning `Khat` on match or `Kbar` on\n * mismatch, and zeroizing the non-returned shared-secret candidate; JS/JIT still provides no\n * constant-time guarantees for that path.\n */\nfunction createKyber(opts: TArg<KyberOpts>): TRet<KEM> {\n const rawOpts = opts as KyberOpts;\n const KPKE = genKPKE(rawOpts);\n const { HASH256, HASH512, KDF } = rawOpts;\n const { secretCoder: KPKESecretCoder, lengths } = KPKE;\n const secretCoder = splitCoder('secretKey', lengths.secretKey, lengths.publicKey, 32, 32);\n const msgLen = 32;\n const seedLen = 64;\n const kemLengths = Object.freeze({\n ...lengths,\n seed: 64,\n msg: msgLen,\n msgRand: msgLen,\n secretKey: secretCoder.bytesLen,\n });\n return Object.freeze({\n info: Object.freeze({ type: 'ml-kem' }),\n lengths: kemLengths,\n keygen: (seed: TArg<Uint8Array> = randomBytes(seedLen)) => {\n abytes(seed, seedLen, 'seed');\n const { publicKey, secretKey: sk } = KPKE.keygen(seed.subarray(0, 32));\n const publicKeyHash = HASH256(publicKey);\n // (dkPKE||ek||H(ek)||z)\n const secretKey = secretCoder.encode([sk, publicKey, publicKeyHash, seed.subarray(32)]);\n cleanBytes(sk, publicKeyHash);\n return {\n publicKey: publicKey as TRet<Uint8Array>,\n secretKey: secretKey as TRet<Uint8Array>,\n };\n },\n getPublicKey: (secretKey: TArg<Uint8Array>): TRet<Uint8Array> => {\n const [_sk, publicKey, _publicKeyHash, _z] = secretCoder.decode(secretKey);\n return Uint8Array.from(publicKey) as TRet<Uint8Array>;\n },\n encapsulate: (publicKey: TArg<Uint8Array>, msg: TArg<Uint8Array> = randomBytes(msgLen)) => {\n abytes(publicKey, lengths.publicKey, 'publicKey');\n abytes(msg, msgLen, 'message');\n\n // FIPS-203 includes additional verification check for modulus\n const eke = publicKey.subarray(0, 384 * opts.K);\n // Copy because of inplace encoding\n const ek = KPKESecretCoder.encode(KPKESecretCoder.decode(copyBytes(eke)));\n // (Modulus check.) Perform the computation ek ← ByteEncode12(ByteDecode12(eke)).\n // If ek = ̸ eke, the input is invalid. (See Section 4.2.1.)\n if (!equalBytes(ek, eke)) {\n cleanBytes(ek);\n throw new Error('ML-KEM.encapsulate: wrong publicKey modulus');\n }\n cleanBytes(ek);\n // derive randomness\n const kr = HASH512.create().update(msg).update(HASH256(publicKey)).digest();\n const cipherText = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));\n cleanBytes(kr.subarray(32));\n return {\n cipherText: cipherText as TRet<Uint8Array>,\n sharedSecret: kr.subarray(0, 32) as TRet<Uint8Array>,\n };\n },\n decapsulate: (cipherText: TArg<Uint8Array>, secretKey: TArg<Uint8Array>): TRet<Uint8Array> => {\n abytes(secretKey, secretCoder.bytesLen, 'secretKey'); // 768*k + 96\n abytes(cipherText, lengths.cipherText, 'cipherText'); // 32(du*k + dv)\n // test ← H(dk[384𝑘 ∶ 768𝑘 + 32])) .\n const k768 = secretCoder.bytesLen - 96;\n const start = k768 + 32;\n const test = HASH256(secretKey.subarray(k768 / 2, start));\n // If test ≠ dk[768𝑘 + 32 ∶ 768𝑘 + 64], then input checking has failed.\n if (!equalBytes(test, secretKey.subarray(start, start + 32)))\n throw new Error('invalid secretKey: hash check failed');\n const [sk, publicKey, publicKeyHash, z] = secretCoder.decode(secretKey);\n const msg = KPKE.decrypt(cipherText, sk);\n // derive randomness, Khat, rHat = G(mHat || h)\n const kr = HASH512.create().update(msg).update(publicKeyHash).digest();\n const Khat = kr.subarray(0, 32);\n // re-encrypt using the derived randomness\n const cipherText2 = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));\n // if ciphertexts do not match, “implicitly reject”\n const isValid = equalBytes(cipherText, cipherText2);\n const Kbar = KDF.create({ dkLen: 32 }).update(z).update(cipherText).digest();\n cleanBytes(msg, cipherText2, !isValid ? Khat : Kbar);\n return (isValid ? Khat : Kbar) as TRet<Uint8Array>;\n },\n });\n}\n\n// FIPS 203's PRF_eta binding: current callers use only 32-byte keys, one-byte nonces,\n// and dkLen values {128, 192}; out-of-range nonce numbers still wrap modulo 256 here.\nfunction shakePRF(dkLen: number, key: TArg<Uint8Array>, nonce: number): TRet<Uint8Array> {\n return shake256\n .create({ dkLen })\n .update(key)\n .update(new Uint8Array([nonce]))\n .digest() as TRet<Uint8Array>;\n}\n\n// Fixed ML-KEM hash/XOF bindings. `KDF` here is the spec's fixed 32-byte `J` call,\n// and swapping any field changes the scheme rather than tuning an internal dependency.\nconst opts = /* @__PURE__ */ (() => ({\n HASH256: sha3_256,\n HASH512: sha3_512,\n KDF: shake256,\n XOF: XOF128,\n PRF: shakePRF,\n}))();\n// Parameter-set instantiation step for the spec's \"ML-KEM-x\" names; current correctness relies\n// on the internal PARAMS rows rather than local validation of arbitrary KEMParam objects.\nconst mk = (params: KEMParam) =>\n createKyber({\n ...opts,\n ...params,\n });\n\n/**\n * ML-KEM-512: Table 2 row `k=2, η1=3, η2=2, du=10, dv=4`; Table 3 sizes `800/1632/768/32`.\n * The ASD lifecycle note here is external policy guidance, not a FIPS 203 requirement.\n */\nexport const ml_kem512: TRet<KEM> = /* @__PURE__ */ (() => mk(PARAMS[512]))();\n/**\n * ML-KEM-768: Table 2 row `k=3, η1=2, η2=2, du=10, dv=4`; Table 3 sizes `1184/2400/1088/32`.\n * The ASD lifecycle note here is external policy guidance, not a FIPS 203 requirement.\n */\nexport const ml_kem768: TRet<KEM> = /* @__PURE__ */ (() => mk(PARAMS[768]))();\n/**\n * ML-KEM-1024: Table 2 row `k=4, η1=2, η2=2, du=11, dv=5`; Table 3 sizes `1568/3168/1568/32`.\n * The ASD lifecycle note here is external policy guidance, not a FIPS 203 requirement.\n */\nexport const ml_kem1024: TRet<KEM> = /* @__PURE__ */ (() => mk(PARAMS[1024]))();\n\n// NOTE: for tests only, don't use. This keeps the exact internal ML-KEM math surfaces available\n// without re-implementing them in separate test code.\nexport const __tests: any = /* @__PURE__ */ (() =>\n Object.freeze({\n Compress_d: (x: number, d: number) => {\n if (d < 1 || d > 11) throw new Error(`Compress_d: expected d in [1..11], got ${d}`);\n return compress(d).encode(x) & getMask(d);\n },\n Decompress_d: (y: number, d: number) => {\n if (d < 1 || d > 11) throw new Error(`Decompress_d: expected d in [1..11], got ${d}`);\n return compress(d).decode(y);\n },\n ByteEncode_d: (F: TArg<Uint16Array>, d: number) => {\n if (d < 1 || d > 12) throw new Error(`ByteEncode_d: expected d in [1..12], got ${d}`);\n return byteCoder(d).encode(F as TRet<Uint16Array>);\n },\n ByteDecode_d: (B: TArg<Uint8Array>, d: number) => {\n if (d < 1 || d > 12) throw new Error(`ByteDecode_d: expected d in [1..12], got ${d}`);\n return byteCoder(d).decode(B);\n },\n NTT: (f: TArg<Uint16Array>) => crystals.NTT.encode(Uint16Array.from(f)),\n NTT_inv: (fHat: TArg<Uint16Array>) => crystals.NTT.decode(Uint16Array.from(fHat)),\n MultiplyNTTs: (fHat: TArg<Uint16Array>, gHat: TArg<Uint16Array>) =>\n MultiplyNTTs(Uint16Array.from(fHat), Uint16Array.from(gHat)),\n SamplePolyCBD: (B: TArg<Uint8Array>, eta: number) => {\n abytes(B, 64 * eta, 'B');\n return sampleCBDBytes(B, eta);\n },\n SampleNTT: (B: TArg<Uint8Array>) => {\n abytes(B, 34, 'B');\n const xof = XOF128(B.subarray(0, 32));\n try {\n return SampleNTT(xof.get(B[32], B[33]));\n } finally {\n xof.clean();\n }\n },\n }))();\n","/**\n * Post-Quantum Hybrid Cryptography\n *\n * The current implementation is flawed and likely redundant. We should offer\n * a small, generic API to compose hybrid schemes instead of reimplementing\n * protocol-specific logic (SSH, GPG, etc.) with ad hoc encodings.\n *\n * 1. Core Issues\n * - sign/verify: implemented as two separate operations with different keys.\n * - EC getSharedSecret: could be refactored into a proper KEM.\n * - Multiple calls: keys, signatures, and shared secrets could be\n * concatenated to reduce the number of API invocations.\n * - Reinvention: most libraries add strange domain separations and\n * encodings instead of simple byte concatenation.\n *\n * 2. API Goals\n * - Provide primitives to build hybrids generically.\n * - Avoid embedding SSH- or GPG-specific formats in the core API.\n *\n * 3. Edge Cases\n * • Variable-length signatures:\n * - DER-encoded (Weierstrass curves).\n * - Falcon (unpadded).\n * - Concatenation works only if length is fixed; otherwise a length\n * prefix is required (but that breaks compatibility).\n *\n * • getSharedSecret:\n * - Default: non-KEM (authenticated ECDH).\n * - KEM conversion: generate a random SK to remove implicit auth.\n *\n * 4. Common Pitfalls\n * - Seed expansion:\n * • Expanding a small seed into multiple keys reduces entropy.\n * • API should allow identity mapping (no expansion).\n *\n * - Skipping full point encoding:\n * • Some omit the compression byte (parity) for WebCrypto compatibility.\n * • Better: hash the raw secret; coordinate output is already non-uniform.\n * • Some curves (e.g., X448) produce secrets that must be re-hashed to match\n * symmetric-key lengths.\n *\n * - Combiner inconsistencies:\n * • Different domain separations and encodings across libraries.\n * • Should live at the application layer, since key lengths vary.\n *\n * 5. Protocol Examples\n * - SSH:\n * • Concatenate keys.\n * • Combiner: SHA-512.\n *\n * - GPG:\n * • Concatenate keys.\n * • Combiner:\n * SHA3-256(kemShare || ecdhShare || ciphertext || pubKey || algId || domSep || len(domSep))\n *\n * - TLS:\n * • Transcript-based derivation (HKDF).\n *\n * 6. Relevant Specs & Implementations\n * - IETF Hybrid KEM drafts:\n * • draft-irtf-cfrg-hybrid-kems\n * • draft-connolly-cfrg-xwing-kem\n * • draft-westerbaan-tls-xyber768d00\n *\n * - PQC Libraries:\n * • superdilithium (cyph/pqcrypto.js) – low adoption.\n * • hybrid-pqc (DogeProtocol, quantumcoinproject) – complex encodings.\n *\n * 7. Signatures\n * - Ed25519: fixed-size, easy to support.\n * - Variable-size: introduces custom format requirements; best left to\n * higher-level code.\n *\n * @module\n */\n/*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */\nimport { type EdDSA } from '@noble/curves/abstract/edwards.js';\nimport { type MontgomeryECDH } from '@noble/curves/abstract/montgomery.js';\nimport { type ECDSA } from '@noble/curves/abstract/weierstrass.js';\nimport { x25519 } from '@noble/curves/ed25519.js';\nimport { p256, p384 } from '@noble/curves/nist.js';\nimport {\n asciiToBytes,\n bytesToNumberBE,\n bytesToNumberLE,\n concatBytes,\n numberToBytesBE,\n} from '@noble/curves/utils.js';\nimport { expand, extract } from '@noble/hashes/hkdf.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\nimport { sha3_256, shake256 } from '@noble/hashes/sha3.js';\nimport { abytes, ahash, anumber, type CHash, type CHashXOF } from '@noble/hashes/utils.js';\nimport { ml_kem1024, ml_kem768 } from './ml-kem.ts';\nimport {\n cleanBytes,\n copyBytes,\n randomBytes,\n splitCoder,\n validateSigOpts,\n validateVerOpts,\n type CryptoKeys,\n type KEM,\n type Signer,\n type TArg,\n type TRet,\n} from './utils.ts';\n\ntype CurveAll = ECDSA | EdDSA | MontgomeryECDH;\ntype CurveECDH = ECDSA | MontgomeryECDH;\ntype CurveSign = ECDSA | EdDSA;\n\n// Can re-use if decide to signatures support, on other hand getSecretKey is specific and ugly\nfunction ecKeygen(curve: CurveAll, allowZeroKey: boolean = false) {\n const lengths = curve.lengths;\n let keygen = curve.keygen;\n if (allowZeroKey) {\n // Only the ECDSA/Weierstrass branch uses raw scalar-byte secret keys here. Edwards seeds are\n // hashed/pruned and Montgomery keys are clamped byte strings, so forcing Point.Fn semantics on\n // those curves would change key construction instead of just relaxing scalar range handling.\n if (!('getSharedSecret' in curve && 'sign' in curve && 'verify' in curve))\n throw new Error('allowZeroKey requires a Weierstrass curve');\n // This legacy flag is really \"skip the +1 shift\" for vector matching, not \"accept scalar 0\".\n // It swaps seeded Weierstrass keygen from reduction into [1, ORDER) to direct reduction into\n // [0, ORDER), which preserves exact reduced bytes but still leaves scalar 0 invalid.\n // This is ugly, but we need to return exact results here.\n const wCurve = curve as ECDSA;\n const Fn = wCurve.Point.Fn;\n // Unlike noble-curves' seeded Weierstrass keygen, this path removes the post-reduction +1.\n // That is enough to match exact reduced-vector bytes, but an all-zero seed still reduces to\n // scalar 0 here and getPublicKey(secretKey) throws instead of \"allowing zero\".\n keygen = (seed: TArg<Uint8Array> = randomBytes(lengths.seed)) => {\n abytes(seed, lengths.seed!, 'seed');\n const seedScalar = Fn.isLE ? bytesToNumberLE(seed) : bytesToNumberBE(seed);\n // Reduce directly into [0, ORDER); scalar 0 still stays invalid.\n const secretKey = Fn.toBytes(Fn.create(seedScalar));\n return {\n secretKey: secretKey as TRet<Uint8Array>,\n publicKey: curve.getPublicKey(secretKey) as TRet<Uint8Array>,\n };\n };\n }\n return {\n lengths: { secretKey: lengths.secretKey, publicKey: lengths.publicKey, seed: lengths.seed },\n keygen: (seed?: TArg<Uint8Array>) =>\n keygen(seed) as TRet<{\n secretKey: Uint8Array;\n publicKey: Uint8Array;\n }>,\n getPublicKey: (secretKey: TArg<Uint8Array>) =>\n curve.getPublicKey(secretKey) as TRet<Uint8Array>,\n };\n}\n\n/**\n * Wraps an ECDH-capable curve as a KEM.\n * Shared secrets stay in the wrapped curve's raw ECDH byte format with no built-in KDF.\n * On SEC 1 / Weierstrass curves, that means the compressed shared-point body without the\n * 1-byte `0x02` / `0x03` prefix.\n * The X25519 path also leaves RFC 7748's optional all-zero shared-secret check to callers.\n * @param curve - Curve with `getSharedSecret`.\n * @param allowZeroKey - Legacy vector-matching toggle for Weierstrass keygen.\n * On Weierstrass curves this removes the usual post-reduction `+1` shift, changing seeded scalar\n * reduction from `[1, ORDER)` to direct reduction into `[0, ORDER)`. It does not make scalar zero\n * valid: an all-zero seed still derives scalar `0` and throws in `curve.getPublicKey(...)`.\n * Only supported on Weierstrass/ECDSA curves.\n * @returns KEM wrapper over the curve.\n * @throws If the curve does not expose `getSharedSecret`. {@link Error}\n * @example\n * Wrap an ECDH-capable curve as a generic KEM.\n * ```ts\n * import { x25519 } from '@noble/curves/ed25519.js';\n * import { ecdhKem } from '@noble/post-quantum/hybrid.js';\n * const kem = ecdhKem(x25519);\n * const publicKeyLen = kem.lengths.publicKey;\n * ```\n */\nexport function ecdhKem(curve: CurveECDH, allowZeroKey: boolean = false): TRet<KEM> {\n const kg = ecKeygen(curve, allowZeroKey);\n if (!curve.getSharedSecret) throw new Error('wrong curve'); // ed25519 doesn't have one!\n return {\n lengths: { ...kg.lengths, msg: kg.lengths.seed, cipherText: kg.lengths.publicKey },\n keygen: kg.keygen,\n getPublicKey: kg.getPublicKey,\n encapsulate(\n publicKey: TArg<Uint8Array>,\n rand: TArg<Uint8Array> = randomBytes(curve.lengths.seed)\n ) {\n // Some curve.keygen(seed) paths reuse the provided seed buffer as secretKey; detach caller\n // randomness first so cleanBytes() only wipes wrapper-owned material.\n const seed = copyBytes(rand);\n let ek: Uint8Array | undefined = undefined;\n try {\n ek = this.keygen(seed).secretKey;\n const sharedSecret = this.decapsulate(publicKey, ek);\n const cipherText = curve.getPublicKey(ek) as TRet<Uint8Array>;\n return { sharedSecret, cipherText };\n } finally {\n // Invalid peer public keys can make decapsulation throw; wipe both the detached seed and\n // derived ephemeral secret key even when encapsulation aborts before returning.\n cleanBytes(seed);\n if (ek) cleanBytes(ek);\n }\n },\n decapsulate(cipherText: TArg<Uint8Array>, secretKey: TArg<Uint8Array>) {\n const res = curve.getSharedSecret(secretKey, cipherText);\n return (curve.lengths.publicKeyHasPrefix ? res.subarray(1) : res) as TRet<Uint8Array>;\n },\n };\n}\n\n/**\n * Wraps a curve signer as a generic `Signer`.\n * Signatures stay in the wrapped curve's native byte encoding.\n * This wrapper does not normalize or document which per-curve signing options are meaningful.\n * @param curve - Curve with `sign` and `verify`.\n * @param allowZeroKey - Legacy vector-matching toggle for Weierstrass keygen.\n * On Weierstrass curves this removes the usual post-reduction `+1` shift, changing seeded scalar\n * reduction from `[1, ORDER)` to direct reduction into `[0, ORDER)`. It does not make scalar zero\n * valid: an all-zero seed still derives scalar `0` and throws in `curve.getPublicKey(...)`.\n * Only supported on Weierstrass/ECDSA curves.\n * @returns Signer wrapper over the curve.\n * @throws If the curve does not expose `sign` and `verify`. {@link Error}\n * @example\n * Wrap a curve signer as a generic signer.\n * ```ts\n * import { ed25519 } from '@noble/curves/ed25519.js';\n * import { ecSigner } from '@noble/post-quantum/hybrid.js';\n * const signer = ecSigner(ed25519);\n * const sigLen = signer.lengths.signature;\n * ```\n */\nexport function ecSigner(curve: CurveSign, allowZeroKey: boolean = false): TRet<Signer> {\n const kg = ecKeygen(curve, allowZeroKey);\n if (!curve.sign || !curve.verify) throw new Error('wrong curve'); // ed25519 doesn't have one!\n return {\n lengths: { ...kg.lengths, signature: curve.lengths.signature, signRand: 0 },\n keygen: kg.keygen,\n getPublicKey: kg.getPublicKey,\n sign: (message, secretKey, opts = {}) => {\n validateSigOpts(opts);\n // This generic wrapper intentionally keeps the Signer contract to message + key only.\n // Backend-specific knobs like ECDSA extraEntropy or Ed25519ctx context cannot be forwarded\n // uniformly through combineSigners(), so callers that need them must use the curve directly.\n if (opts.extraEntropy !== undefined)\n throw new Error(\n 'ecSigner does not support extraEntropy; use the underlying curve directly'\n );\n if (opts.context !== undefined)\n throw new Error('ecSigner does not support context; use the underlying curve directly');\n return curve.sign(message, secretKey) as TRet<Uint8Array>;\n },\n /** Verify one wrapped curve signature.\n * Returns the wrapped curve's `verify()` result for well-formed inputs. Throws on unsupported\n * generic opts and lets wrapped-curve malformed-input errors escape unchanged.\n */\n verify: (signature, message, publicKey, opts = {}) => {\n validateVerOpts(opts);\n if (opts.context !== undefined)\n throw new Error('ecSigner does not support context; use the underlying curve directly');\n return curve.verify(signature, message, publicKey);\n },\n };\n}\n\nfunction splitLengths<K extends string, T extends { lengths: Partial<Record<K, number>> }>(\n lst: T[],\n name: K\n) {\n // Preserve caller order exactly; raw numeric fields still decode as splitCoder() subarray views.\n return splitCoder(\n name,\n ...lst.map((i) => {\n if (typeof i.lengths[name] !== 'number') throw new Error('wrong length: ' + name);\n return i.lengths[name];\n })\n );\n}\n\n/** Seed-expansion callback used by the hybrid combiners. */\nexport type ExpandSeed = (seed: TArg<Uint8Array>, len: number) => TRet<Uint8Array>;\ntype XOF = CHashXOF<any, { dkLen: number }>;\n\n// It is XOF for most cases, but can be more complex!\n/**\n * Adapts an XOF into an `ExpandSeed` callback.\n * The returned callback interprets its second argument as an output byte length passed as `dkLen`.\n * @param xof - Extendable-output hash function.\n * @returns Seed expander using `dkLen`.\n * @example\n * Adapt an XOF into a seed expander.\n * ```ts\n * import { shake256 } from '@noble/hashes/sha3.js';\n * import { expandSeedXof } from '@noble/post-quantum/hybrid.js';\n * const expandSeed = expandSeedXof(shake256);\n * const seed = expandSeed(new Uint8Array([1]), 4);\n * ```\n */\nexport function expandSeedXof(xof: TArg<XOF>): TRet<ExpandSeed> {\n // Forward the caller seed directly: XOFs are expected to treat inputs as read-only, and this\n // adapter only translates the requested byte length into the hash API's `dkLen` option.\n return ((seed: TArg<Uint8Array>, seedLen: number): TRet<Uint8Array> =>\n (xof as XOF)(seed, { dkLen: seedLen }) as TRet<Uint8Array>) as TRet<ExpandSeed>;\n}\n\n/** Combines public keys, ciphertexts, and shared secrets into one shared secret. */\nexport type Combiner = (\n publicKeys: TArg<Uint8Array[]>,\n cipherTexts: TArg<Uint8Array[]>,\n sharedSecrets: TArg<Uint8Array[]>\n) => TRet<Uint8Array>;\n\nfunction combineKeys(\n realSeedLen: number | undefined, // how much bytes expandSeed expects\n expandSeed_: TArg<ExpandSeed>,\n ...ck_: TArg<CryptoKeys[]>\n) {\n const expandSeed = expandSeed_ as ExpandSeed;\n const ck = ck_ as CryptoKeys[];\n const seedCoder = splitLengths(ck, 'seed');\n const pkCoder = splitLengths(ck, 'publicKey');\n // Allows to use identity functions for combiner/expandSeed\n if (realSeedLen === undefined) realSeedLen = seedCoder.bytesLen;\n anumber(realSeedLen);\n function expandDecapsulationKey(seed: TArg<Uint8Array>): TRet<{\n secretKey: Uint8Array[];\n publicKey: Uint8Array[];\n }> {\n abytes(seed, realSeedLen!);\n const expandedRaw = expandSeed(seed, seedCoder.bytesLen);\n // Identity/subarray expanders can hand back caller-owned seed storage. Detach those outputs so\n // later cleanup can wipe the expanded schedule without mutating the caller's root seed bytes.\n const expandedSeed = expandedRaw.buffer === seed.buffer ? copyBytes(expandedRaw) : expandedRaw;\n const expanded: Uint8Array[] = [];\n const keySecret: Uint8Array[] = [];\n const secretKey: Uint8Array[] = [];\n const publicKey: Uint8Array[] = [];\n let ok = false;\n try {\n // seedCoder.decode() returns zero-copy slices into expandedSeed and can throw before child\n // keygen() runs, so keep the raw expanded buffer separate and copy each child seed before any\n // later cleanup wipes the shared backing bytes.\n for (const part of seedCoder.decode(expandedSeed)) expanded.push(copyBytes(part));\n for (let i = 0; i < ck.length; i++) {\n const keys = ck[i].keygen(expanded[i]);\n keySecret.push(keys.secretKey);\n secretKey.push(copyBytes(keys.secretKey));\n publicKey.push(keys.publicKey);\n }\n ok = true;\n return { secretKey, publicKey } as TRet<{\n secretKey: Uint8Array[];\n publicKey: Uint8Array[];\n }>;\n } finally {\n // Child keygen() can throw after deriving only a prefix of the composite key schedule. Keep\n // the exported copies on success, but wipe all temporary and partially built secret material\n // on either path so failures do not strand derived child seeds in memory.\n cleanBytes(expandedSeed, expanded, keySecret);\n if (!ok) cleanBytes(secretKey);\n }\n }\n return {\n info: { lengths: { seed: realSeedLen, publicKey: pkCoder.bytesLen, secretKey: realSeedLen } },\n getPublicKey(secretKey: TArg<Uint8Array>) {\n // Composite secret keys are root seeds, so public-key derivation reruns key expansion from\n // that seed instead of decoding a packed child-secret-key structure.\n return this.keygen(secretKey).publicKey as TRet<Uint8Array>;\n },\n keygen(seed: TArg<Uint8Array> = randomBytes(realSeedLen)) {\n const { publicKey: pk, secretKey } = expandDecapsulationKey(seed);\n try {\n const publicKey = pkCoder.encode(pk) as TRet<Uint8Array>;\n return { secretKey: seed as TRet<Uint8Array>, publicKey };\n } finally {\n cleanBytes(pk);\n // The exported secretKey is the caller/root seed itself; child secret keys are internal\n // expansion outputs that are cleaned whether encoding succeeds or throws.\n cleanBytes(secretKey);\n }\n },\n expandDecapsulationKey,\n realSeedLen,\n };\n}\n\n// This generic function that combines multiple KEMs into single one\n/**\n * Combines multiple KEMs into one composite KEM.\n * @param realSeedLen - Input seed length expected by `expandSeed`.\n * @param realMsgLen - Shared-secret length returned by `combiner`.\n * @param expandSeed - Seed expander used to derive per-KEM seeds.\n * @param combiner - Combines the per-KEM outputs into one shared secret.\n * @param kems - KEM implementations to combine.\n * @returns Composite KEM.\n * @example\n * Combine multiple KEMs into one composite KEM.\n * ```ts\n * import { shake256 } from '@noble/hashes/sha3.js';\n * import { combineKEMS, expandSeedXof } from '@noble/post-quantum/hybrid.js';\n * import { ml_kem768 } from '@noble/post-quantum/ml-kem.js';\n * const hybrid = combineKEMS(\n * 32,\n * 32,\n * expandSeedXof(shake256),\n * (_pk, _ct, sharedSecrets) => sharedSecrets[0],\n * ml_kem768,\n * ml_kem768\n * );\n * const { publicKey } = hybrid.keygen();\n * ```\n */\nexport function combineKEMS(\n realSeedLen: number | undefined, // how much bytes expandSeed expects\n realMsgLen: number | undefined, // how much bytes combiner returns\n expandSeed: TArg<ExpandSeed>,\n combiner: TArg<Combiner>,\n ...kems: TArg<KEM[]>\n): TRet<KEM> {\n const rawCombiner = combiner as Combiner;\n const rawKems = kems as KEM[];\n const keys = combineKeys(realSeedLen, expandSeed, ...rawKems);\n const ctCoder = splitLengths(rawKems, 'cipherText');\n const pkCoder = splitLengths(rawKems, 'publicKey');\n const msgCoder = splitLengths(rawKems, 'msg');\n if (realMsgLen === undefined) realMsgLen = msgCoder.bytesLen;\n anumber(realMsgLen);\n const lengths = Object.freeze({\n ...keys.info.lengths,\n msg: realMsgLen,\n msgRand: msgCoder.bytesLen,\n cipherText: ctCoder.bytesLen,\n });\n return Object.freeze({\n lengths,\n getPublicKey: keys.getPublicKey,\n keygen: keys.keygen,\n encapsulate(\n pk: TArg<Uint8Array>,\n randomness: TArg<Uint8Array> = randomBytes(msgCoder.bytesLen)\n ) {\n const pks = pkCoder.decode(pk);\n const rand = msgCoder.decode(randomness);\n const sharedSecret: Uint8Array[] = [];\n const cipherText: Uint8Array[] = [];\n try {\n for (let i = 0; i < rawKems.length; i++) {\n const enc = rawKems[i].encapsulate(pks[i], rand[i]);\n sharedSecret.push(enc.sharedSecret);\n cipherText.push(enc.cipherText);\n }\n return {\n // Detach the combiner result before cleanup: a caller-provided combiner may alias one of\n // the child sharedSecret buffers, and those child buffers are zeroized immediately below.\n sharedSecret: copyBytes(rawCombiner(pks, cipherText, sharedSecret)),\n cipherText: ctCoder.encode(cipherText) as TRet<Uint8Array>,\n };\n } finally {\n // Child encapsulation or combiner failures can happen after some components already\n // returned secret material; zeroize whatever was produced before propagating the error.\n cleanBytes(sharedSecret, cipherText);\n }\n },\n decapsulate(ct: TArg<Uint8Array>, seed: TArg<Uint8Array>) {\n const cts = ctCoder.decode(ct);\n const { publicKey, secretKey } = keys.expandDecapsulationKey(seed);\n const sharedSecret = rawKems.map((i, j) => i.decapsulate(cts[j], secretKey[j]));\n try {\n // Detach the decapsulation result before cleanup: the combiner may hand back one of the\n // child shared-secret buffers, and those temporary buffers are zeroized below.\n return copyBytes(rawCombiner(publicKey, cts, sharedSecret));\n } finally {\n // Decapsulation only needs the expanded child secret keys and child shared secrets for this\n // call; keep the caller/root seed intact, but wipe all derived material even on errors.\n cleanBytes(secretKey, sharedSecret);\n }\n },\n });\n}\n// There is no specs for this, but can be useful\n// realSeedLen: how much bytes expandSeed expects.\n/**\n * Combines multiple signers into one composite signer.\n * @param realSeedLen - Input seed length expected by `expandSeed`.\n * @param expandSeed - Seed expander used to derive per-signer seeds.\n * @param signers - Signers to combine.\n * @returns Composite signer.\n * @example\n * Combine multiple signers into one composite signer.\n * ```ts\n * import { shake256 } from '@noble/hashes/sha3.js';\n * import { combineSigners, expandSeedXof } from '@noble/post-quantum/hybrid.js';\n * import { ml_dsa44 } from '@noble/post-quantum/ml-dsa.js';\n * const hybrid = combineSigners(32, expandSeedXof(shake256), ml_dsa44, ml_dsa44);\n * const { publicKey } = hybrid.keygen();\n * ```\n */\nexport function combineSigners(\n realSeedLen: number | undefined,\n expandSeed: TArg<ExpandSeed>,\n ...signers: TArg<Signer[]>\n): TRet<Signer> {\n const rawSigners = signers as Signer[];\n const keys = combineKeys(realSeedLen, expandSeed, ...rawSigners);\n const sigCoder = splitLengths(rawSigners, 'signature');\n const pkCoder = splitLengths(rawSigners, 'publicKey');\n return {\n lengths: { ...keys.info.lengths, signature: sigCoder.bytesLen, signRand: 0 },\n getPublicKey: keys.getPublicKey,\n keygen: keys.keygen,\n sign(message, seed, opts = {}) {\n validateSigOpts(opts);\n // This generic wrapper intentionally keeps the composite signer contract to message + root\n // seed only. Per-signer opts like context or extraEntropy cannot be preserved uniformly\n // across mixed backends, so callers that need them must use the underlying signer directly.\n if (opts.extraEntropy !== undefined)\n throw new Error(\n 'combineSigners does not support extraEntropy; use the underlying signer directly'\n );\n if (opts.context !== undefined)\n throw new Error(\n 'combineSigners does not support context; use the underlying signer directly'\n );\n const { secretKey } = keys.expandDecapsulationKey(seed);\n try {\n const sigs = rawSigners.map((i, j) => i.sign(message, secretKey[j]));\n return sigCoder.encode(sigs) as TRet<Uint8Array>;\n } finally {\n // Composite secret keys are root seeds; the per-signer child secret keys are temporary\n // expansion outputs and must not stay live after the combined signature is produced.\n cleanBytes(secretKey);\n }\n },\n /** Verify one combined signature.\n * Returns `false` when the aggregate signature/publicKey decode succeeds but any child verify\n * check fails. Throws on unsupported generic opts or malformed aggregate encodings.\n */\n verify: (signature, message, publicKey, opts = {}) => {\n validateVerOpts(opts);\n if (opts.context !== undefined)\n throw new Error(\n 'combineSigners does not support context; use the underlying signer directly'\n );\n const pks = pkCoder.decode(publicKey);\n const sigs = sigCoder.decode(signature);\n for (let i = 0; i < rawSigners.length; i++) {\n if (!rawSigners[i].verify(sigs[i], message, pks[i])) return false;\n }\n return true;\n },\n };\n}\n\n/**\n * Builds a QSF hybrid KEM preset from a PQ KEM and an elliptic-curve KEM.\n * The combined shared-secret length follows `kdf.outputLen`; the built-in presets use 32-byte\n * SHA3-256 output, while custom `kdf` choices inherit their own digest size.\n * Its combiner hashes `ss0 || ss1 || ct1 || pk1 || label`, not the full\n * `(c1, c2, ek1, ek2)` example input shape from SP 800-227 equation (15).\n * Labels are encoded with `asciiToBytes()`, so non-ASCII labels are rejected.\n * @param label - Domain-separation label.\n * @param pqc - Post-quantum KEM.\n * @param curveKEM - Classical curve KEM.\n * @param xof - XOF used for seed expansion.\n * @param kdf - Hash used for the final combiner.\n * @returns Hybrid KEM.\n * @example\n * Build a QSF hybrid KEM preset from a PQ KEM and an elliptic-curve KEM.\n * ```ts\n * import { p256 } from '@noble/curves/nist.js';\n * import { sha3_256, shake256 } from '@noble/hashes/sha3.js';\n * import { QSF, ecdhKem } from '@noble/post-quantum/hybrid.js';\n * import { ml_kem768 } from '@noble/post-quantum/ml-kem.js';\n * const kem = QSF('example', ml_kem768, ecdhKem(p256, true), shake256, sha3_256);\n * const publicKeyLen = kem.lengths.publicKey;\n * ```\n */\nexport function QSF(\n label: string,\n pqc: TArg<KEM>,\n curveKEM: TArg<KEM>,\n xof: TArg<XOF>,\n kdf: CHash\n): TRet<KEM> {\n ahash(xof);\n ahash(kdf);\n return combineKEMS(\n 32,\n kdf.outputLen,\n expandSeedXof(xof),\n (pk: TArg<Uint8Array[]>, ct: TArg<Uint8Array[]>, ss: TArg<Uint8Array[]>) =>\n kdf(concatBytes(ss[0], ss[1], ct[1], pk[1], asciiToBytes(label))),\n pqc,\n curveKEM\n );\n}\n\n/** QSF preset combining ML-KEM-768 with P-256. */\nexport const QSF_ml_kem768_p256: TRet<KEM> = /* @__PURE__ */ (() =>\n QSF(\n 'QSF-KEM(ML-KEM-768,P-256)-XOF(SHAKE256)-KDF(SHA3-256)',\n ml_kem768,\n ecdhKem(p256, true),\n shake256,\n sha3_256\n ))();\n/** QSF preset combining ML-KEM-1024 with P-384. */\nexport const QSF_ml_kem1024_p384: TRet<KEM> = /* @__PURE__ */ (() =>\n QSF(\n 'QSF-KEM(ML-KEM-1024,P-384)-XOF(SHAKE256)-KDF(SHA3-256)',\n ml_kem1024,\n ecdhKem(p384, true),\n shake256,\n sha3_256\n ))();\n\n/**\n * Builds the \"KitchenSink\" hybrid KEM combiner.\n * The current builder always derives a fixed 32-byte output,\n * regardless of the hash's native output size.\n * Its HKDF extract step uses implicit zero salt with IKM\n * `hybrid_prk || ss0 || ss1 || ct0 || pk0 || ct1 || pk1 || label`.\n * Its HKDF expand step fixes `info` to `len || 'shared_secret' || ''`.\n * Labels are encoded with `asciiToBytes()`, so non-ASCII labels are rejected.\n * @param label - Domain-separation label.\n * @param pqc - Post-quantum KEM.\n * @param curveKEM - Classical curve KEM.\n * @param xof - XOF used for seed expansion.\n * @param hash - Hash used for HKDF extraction and expansion.\n * @returns Hybrid KEM.\n * @example\n * Build the \"KitchenSink\" hybrid KEM combiner.\n * ```ts\n * import { sha256 } from '@noble/hashes/sha2.js';\n * import { shake256 } from '@noble/hashes/sha3.js';\n * import { createKitchenSink, ecdhKem } from '@noble/post-quantum/hybrid.js';\n * import { ml_kem768 } from '@noble/post-quantum/ml-kem.js';\n * import { x25519 } from '@noble/curves/ed25519.js';\n * const kem = createKitchenSink('example', ml_kem768, ecdhKem(x25519), shake256, sha256);\n * const publicKeyLen = kem.lengths.publicKey;\n * ```\n */\nexport function createKitchenSink(\n label: string,\n pqc: TArg<KEM>,\n curveKEM: TArg<KEM>,\n xof: TArg<XOF>,\n hash: CHash\n): TRet<KEM> {\n ahash(xof);\n ahash(hash);\n return combineKEMS(\n 32,\n 32,\n expandSeedXof(xof),\n (pk: TArg<Uint8Array[]>, ct: TArg<Uint8Array[]>, ss: TArg<Uint8Array[]>) => {\n const preimage = concatBytes(ss[0], ss[1], ct[0], pk[0], ct[1], pk[1], asciiToBytes(label));\n const len = 32;\n const ikm = concatBytes(asciiToBytes('hybrid_prk'), preimage);\n const prk = extract(hash, ikm);\n const info = concatBytes(\n numberToBytesBE(len, 2),\n asciiToBytes('shared_secret'),\n asciiToBytes('')\n );\n const res = expand(hash, prk, info, len);\n cleanBytes(prk, info, ikm, preimage);\n return res;\n },\n pqc,\n curveKEM\n );\n}\n\n// Internal alias only: this stays exactly `ecdhKem(x25519)`\n// and inherits that wrapper's mutation/oracle behavior.\nconst x25519kem = /* @__PURE__ */ ecdhKem(x25519);\n/** KitchenSink preset combining ML-KEM-768 with X25519.\n * Caller randomness splits into 32 ML-KEM coins plus a 32-byte X25519 ephemeral-secret seed.\n */\nexport const KitchenSink_ml_kem768_x25519: TRet<KEM> = /* @__PURE__ */ (() =>\n createKitchenSink(\n 'KitchenSink-KEM(ML-KEM-768,X25519)-XOF(SHAKE256)-KDF(HKDF-SHA-256)',\n ml_kem768,\n x25519kem,\n shake256,\n sha256\n ))();\n\n// Always X25519 and ML-KEM - 768, no point to export\n/** X25519 + ML-KEM-768 hybrid preset.\n * Uses the hard-coded domain-separation label `\\\\.//^\\\\` and hashes only `ct1 || pk1`\n * from the X25519 side in addition to the two component shared secrets.\n */\nexport const ml_kem768_x25519: TRet<KEM> = /* @__PURE__ */ (() =>\n combineKEMS(\n 32,\n 32,\n expandSeedXof(shake256),\n // Awesome label, so much escaping hell in a single line.\n (pk: TArg<Uint8Array[]>, ct: TArg<Uint8Array[]>, ss: TArg<Uint8Array[]>) =>\n sha3_256(concatBytes(ss[0], ss[1], ct[1], pk[1], asciiToBytes('\\\\.//^\\\\'))),\n ml_kem768,\n x25519kem\n ))();\n\n/**\n * Internal SEC 1-style KEM wrapper for NIST curves.\n * `nseed` is only the rejection-sampling byte budget for deriving one nonzero scalar:\n * current presets use `128` bytes for P-256 and `48` bytes for P-384.\n * `decapsulate()` returns the uncompressed shared point body `x || y` without the `0x04`\n * prefix, not the SEC 1 `x_P`-only primitive output, because current hybrid combiners hash\n * both coordinates.\n */\nfunction nistCurveKem(curve: ECDSA, scalarLen: number, elemLen: number, nseed: number): TRet<KEM> {\n const Fn = curve.Point.Fn;\n if (!Fn) throw new Error('no Point.Fn');\n // Scan scalar-sized windows until one decodes to a nonzero scalar in `[1, n-1]`; if every\n // window is zero or out of range, fail instead of silently reducing modulo `n`.\n function rejectionSampling(seed: TArg<Uint8Array>): TRet<{\n secretKey: Uint8Array;\n publicKey: Uint8Array;\n }> {\n let sk: bigint;\n for (let start = 0, end = scalarLen; ; start = end, end += scalarLen) {\n if (end > seed.length) throw new Error('rejection sampling failed');\n sk = Fn.fromBytes(seed.subarray(start, end), true);\n if (Fn.isValidNot0(sk)) break;\n }\n const secretKey = Fn.toBytes(Fn.create(sk));\n const publicKey = curve.getPublicKey(secretKey, false);\n return { secretKey, publicKey } as TRet<{\n secretKey: Uint8Array;\n publicKey: Uint8Array;\n }>;\n }\n\n return {\n lengths: {\n secretKey: scalarLen,\n publicKey: elemLen,\n seed: nseed,\n msg: nseed,\n cipherText: elemLen,\n },\n keygen(seed: TArg<Uint8Array> = randomBytes(nseed)) {\n abytes(seed, nseed, 'seed');\n return rejectionSampling(seed);\n },\n getPublicKey(secretKey: TArg<Uint8Array>) {\n return curve.getPublicKey(secretKey, false) as TRet<Uint8Array>;\n },\n encapsulate(publicKey: TArg<Uint8Array>, rand: TArg<Uint8Array> = randomBytes(nseed)) {\n abytes(rand, nseed, 'rand');\n let ek: Uint8Array | undefined = undefined;\n try {\n ek = rejectionSampling(rand).secretKey;\n const sharedSecret = this.decapsulate(publicKey, ek);\n const cipherText = curve.getPublicKey(ek, false) as TRet<Uint8Array>;\n return { sharedSecret, cipherText };\n } finally {\n // Rejection-sampled NIST-curve ephemeral secret keys are temporary encapsulation state and\n // must be wiped even if peer-key validation or shared-secret derivation throws.\n if (ek) cleanBytes(ek);\n }\n },\n decapsulate(cipherText: TArg<Uint8Array>, secretKey: TArg<Uint8Array>) {\n const full = curve.getSharedSecret(secretKey, cipherText);\n return full.subarray(1) as TRet<Uint8Array>;\n },\n };\n}\n\n/**\n * Internal ML-KEM + NIST-curve combiner.\n * `nseed` controls only the curve-side rejection-sampling budget; it is expanded from the\n * 32-byte root seed and is not itself part of the exported secret-key length.\n * The domain-separation `label` is used only in the final `sha3_256` combiner, not in\n * `shake256(seed, { dkLen: 64 + nseed })`,\n * and the combiner hashes `ss0 || ss1 || ct1 || pk1 || label`.\n */\nfunction concreteHybridKem(\n label: string,\n mlkem: TArg<KEM>,\n curve: ECDSA,\n nseed: number\n): TRet<KEM> {\n const { secretKey: scalarLen, publicKeyUncompressed: elemLen } = curve.lengths;\n if (!scalarLen || !elemLen) throw new Error('wrong curve');\n const curveKem = nistCurveKem(curve, scalarLen, elemLen, nseed);\n const mlkemSeedLen = 64;\n const totalSeedLen = mlkemSeedLen + nseed;\n\n return combineKEMS(\n 32,\n 32,\n (seed: TArg<Uint8Array>): TRet<Uint8Array> => {\n abytes(seed, 32);\n const expanded = shake256(seed, { dkLen: totalSeedLen });\n const mlkemSeed = expanded.subarray(0, mlkemSeedLen);\n const curveSeed = expanded.subarray(mlkemSeedLen, totalSeedLen);\n return concatBytes(mlkemSeed, curveSeed) as TRet<Uint8Array>;\n },\n (pk: TArg<Uint8Array[]>, ct: TArg<Uint8Array[]>, ss: TArg<Uint8Array[]>) =>\n sha3_256(concatBytes(ss[0], ss[1], ct[1], pk[1], asciiToBytes(label))),\n mlkem,\n curveKem\n );\n}\n\n/** P-256 + ML-KEM-768 hybrid preset. */\nexport const ml_kem768_p256: TRet<KEM> = /* @__PURE__ */ (() =>\n concreteHybridKem('MLKEM768-P256', ml_kem768, p256, 128))();\n\n/** P-384 + ML-KEM-1024 hybrid preset. */\nexport const ml_kem1024_p384: TRet<KEM> = /* @__PURE__ */ (() =>\n concreteHybridKem('MLKEM1024-P384', ml_kem1024, p384, 48))();\n\n// Legacy aliases\n/** Legacy alias for `ml_kem768_x25519`. */\nexport const XWing: TRet<KEM> = /* @__PURE__ */ (() => ml_kem768_x25519)();\n/** Legacy alias for `ml_kem768_x25519`. */\nexport const MLKEM768X25519: TRet<KEM> = /* @__PURE__ */ (() => ml_kem768_x25519)();\n/** Legacy alias for `ml_kem768_p256`. */\nexport const MLKEM768P256: TRet<KEM> = /* @__PURE__ */ (() => ml_kem768_p256)();\n/** Legacy alias for `ml_kem1024_p384`. */\nexport const MLKEM1024P384: TRet<KEM> = /* @__PURE__ */ (() => ml_kem1024_p384)();\n/** Legacy alias for `QSF_ml_kem768_p256`. */\nexport const QSFMLKEM768P256: TRet<KEM> = /* @__PURE__ */ (() => QSF_ml_kem768_p256)();\n/** Legacy alias for `QSF_ml_kem1024_p384`. */\nexport const QSFMLKEM1024P384: TRet<KEM> = /* @__PURE__ */ (() => QSF_ml_kem1024_p384)();\n/** Legacy alias for `KitchenSink_ml_kem768_x25519`. */\nexport const KitchenSinkMLKEM768X25519: TRet<KEM> = /* @__PURE__ */ (() =>\n KitchenSink_ml_kem768_x25519)();\n","import { chacha20poly1305 } from '@noble/ciphers/chacha.js';\n\nimport { AeadVerificationError } from './errors';\n\nexport interface ChaCha20Poly1305EncryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly plaintext: Uint8Array;\n}\n\nexport interface ChaCha20Poly1305DecryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly ciphertext: Uint8Array;\n}\n\nexport function chacha20Poly1305Encrypt(opts: ChaCha20Poly1305EncryptOpts): Uint8Array {\n return chacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);\n}\n\nexport function chacha20Poly1305Decrypt(opts: ChaCha20Poly1305DecryptOpts): Uint8Array {\n try {\n return chacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);\n } catch (cause) {\n throw new AeadVerificationError('chacha20-poly1305 decrypt failed', { cause });\n }\n}\n","import { xchacha20poly1305 } from '@noble/ciphers/chacha.js';\n\nimport { AeadVerificationError } from './errors';\n\nexport interface XChaCha20Poly1305EncryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly plaintext: Uint8Array;\n}\n\nexport interface XChaCha20Poly1305DecryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly ciphertext: Uint8Array;\n}\n\nexport function xchacha20Poly1305Encrypt(opts: XChaCha20Poly1305EncryptOpts): Uint8Array {\n return xchacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);\n}\n\nexport function xchacha20Poly1305Decrypt(opts: XChaCha20Poly1305DecryptOpts): Uint8Array {\n try {\n return xchacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);\n } catch (cause) {\n throw new AeadVerificationError('xchacha20-poly1305 decrypt failed', { cause });\n }\n}\n","import { hkdf } from '@noble/hashes/hkdf.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\n\nexport interface HkdfSha256Opts {\n readonly ikm: Uint8Array;\n readonly salt: Uint8Array;\n readonly info: Uint8Array;\n readonly length: number;\n}\n\nexport function hkdfSha256(opts: HkdfSha256Opts): Uint8Array {\n return hkdf(sha256, opts.ikm, opts.salt, opts.info, opts.length);\n}\n","import { XWing } from '@noble/post-quantum/hybrid.js';\n\n// X-Wing (ML-KEM-768 + X25519) hybrid KEM per draft-connolly-cfrg-xwing-kem-06.\n// `XWing` is @noble/post-quantum's alias for `ml_kem768_x25519`. We expose it\n// through opts-object wrappers that pin the wire lengths and map noble's field\n// names onto the project's vocabulary.\n//\n// Unlike the bare X25519 KEM, there is no contributory-behaviour rejection to\n// translate: X-Wing combines the ML-KEM and X25519 shared secrets through a\n// SHA3-256 combiner that also binds the X25519 ephemeral and recipient public\n// keys, and ML-KEM's implicit rejection already yields a constant-work\n// pseudorandom secret on a malformed ciphertext. Decapsulation therefore never\n// throws on attacker-supplied wire data — a wrong shared secret is the correct,\n// indistinguishable failure mode, and callers MUST treat it as a non-match\n// rather than expecting an exception.\n\nexport const MLKEM768X25519_PUBLIC_KEY_LENGTH = 1216 as const;\nexport const MLKEM768X25519_ENC_LENGTH = 1120 as const;\nexport const MLKEM768X25519_SHARED_SECRET_LENGTH = 32 as const;\nexport const MLKEM768X25519_SEED_LENGTH = 32 as const;\nexport const MLKEM768X25519_ESEED_LENGTH = 64 as const;\n\nexport interface Mlkem768X25519KeyPair {\n // The 32-byte root seed IS the secret key in draft-06: the ML-KEM coins and\n // the X25519 scalar are re-expanded from it via SHAKE-256 at decapsulation.\n readonly secretSeed: Uint8Array;\n readonly publicKey: Uint8Array;\n}\n\nexport interface Mlkem768X25519EncapsulateOpts {\n readonly publicKey: Uint8Array;\n // Optional 64-byte encapsulation randomness (msgRand). When supplied the\n // ciphertext and shared secret are fully deterministic; a 32-byte value is\n // rejected by noble, so we pin the length here too.\n readonly eseed?: Uint8Array;\n}\n\nexport interface Mlkem768X25519Encapsulation {\n readonly enc: Uint8Array;\n readonly ss: Uint8Array;\n}\n\nexport interface Mlkem768X25519DecapsulateOpts {\n readonly secretSeed: Uint8Array;\n readonly enc: Uint8Array;\n}\n\nexport function mlkem768x25519Keygen(seed: Uint8Array): Mlkem768X25519KeyPair {\n if (seed.length !== MLKEM768X25519_SEED_LENGTH) {\n throw new Error(\n `mlkem768x25519 seed must be ${MLKEM768X25519_SEED_LENGTH} bytes, got ${seed.length}`,\n );\n }\n const { secretKey, publicKey } = XWing.keygen(seed);\n return { secretSeed: secretKey, publicKey };\n}\n\nexport function mlkem768x25519Encapsulate(\n opts: Mlkem768X25519EncapsulateOpts,\n): Mlkem768X25519Encapsulation {\n if (opts.publicKey.length !== MLKEM768X25519_PUBLIC_KEY_LENGTH) {\n throw new Error(\n `mlkem768x25519 public key must be ${MLKEM768X25519_PUBLIC_KEY_LENGTH} bytes, got ${opts.publicKey.length}`,\n );\n }\n if (opts.eseed !== undefined && opts.eseed.length !== MLKEM768X25519_ESEED_LENGTH) {\n throw new Error(\n `mlkem768x25519 eseed must be ${MLKEM768X25519_ESEED_LENGTH} bytes, got ${opts.eseed.length}`,\n );\n }\n const { cipherText, sharedSecret } = XWing.encapsulate(opts.publicKey, opts.eseed);\n return { enc: cipherText, ss: sharedSecret };\n}\n\nexport function mlkem768x25519Decapsulate(opts: Mlkem768X25519DecapsulateOpts): Uint8Array {\n // Pre-check both lengths before calling noble: decapsulation must perform a\n // constant amount of work for any caller-supplied ciphertext (implicit\n // rejection), which requires the inputs to be the exact expected sizes.\n if (opts.secretSeed.length !== MLKEM768X25519_SEED_LENGTH) {\n throw new Error(\n `mlkem768x25519 secret seed must be ${MLKEM768X25519_SEED_LENGTH} bytes, got ${opts.secretSeed.length}`,\n );\n }\n if (opts.enc.length !== MLKEM768X25519_ENC_LENGTH) {\n throw new Error(\n `mlkem768x25519 enc must be ${MLKEM768X25519_ENC_LENGTH} bytes, got ${opts.enc.length}`,\n );\n }\n // noble's signature is decapsulate(cipherText, secretKey) — ciphertext first.\n return XWing.decapsulate(opts.enc, opts.secretSeed);\n}\n","import { x25519 } from '@noble/curves/ed25519.js';\n\n// RFC 7748 §6.1 contributory-behaviour rejection: a small-order (low-order)\n// Montgomery `u` coordinate makes the X25519 shared secret all-zero, which\n// @noble/curves refuses with `Error: invalid private or public key received`.\n// We rethrow that as a *typed* error so callers can distinguish a structurally\n// valid-but-malicious peer public key (a property of attacker-supplied wire\n// data — trial-decrypt MUST treat the slot as a non-match, not crash) from\n// genuine caller misuse such as a wrong-length key (which @noble raises as a\n// RangeError and which we deliberately let propagate untouched).\nexport class X25519LowOrderPointError extends Error {\n readonly code = 'X25519_LOW_ORDER_POINT' as const;\n constructor(options?: { cause?: unknown }) {\n super('x25519 ECDH rejected: peer public key is a small-order point', options);\n this.name = 'X25519LowOrderPointError';\n }\n}\n\n// @noble/curves v2 signals a small-order/all-zero shared secret with this exact\n// message. Matching on it (rather than the broad Error class) keeps unrelated\n// failures — e.g. a future internal assertion — surfacing as themselves.\nconst NOBLE_LOW_ORDER_MESSAGE = 'invalid private or public key received';\n\nexport interface X25519KeyPair {\n readonly secretKey: Uint8Array;\n readonly publicKey: Uint8Array;\n}\n\nexport interface X25519PublicKeyOpts {\n readonly secretKey: Uint8Array;\n}\n\nexport interface X25519EcdhOpts {\n readonly secretKey: Uint8Array;\n readonly theirPublicKey: Uint8Array;\n}\n\nexport function x25519Keygen(): X25519KeyPair {\n return x25519.keygen();\n}\n\nexport function x25519PublicKey(opts: X25519PublicKeyOpts): Uint8Array {\n return x25519.getPublicKey(opts.secretKey);\n}\n\nexport function x25519Ecdh(opts: X25519EcdhOpts): Uint8Array {\n try {\n return x25519.getSharedSecret(opts.secretKey, opts.theirPublicKey);\n } catch (e) {\n // Translate ONLY the contributory-check rejection into our typed error.\n // A wrong-length key throws a RangeError from @noble's length assertion;\n // that is caller misuse, not malicious wire data, so it must propagate.\n if (e instanceof Error && e.message === NOBLE_LOW_ORDER_MESSAGE) {\n throw new X25519LowOrderPointError({ cause: e });\n }\n throw e;\n }\n}\n","// Sealed-PoE error taxonomy (wire-shape + partitioning-oracle pre-checks).\n\nexport type EciesSealedPoeErrorCode =\n | 'ENC_SLOTS_EMPTY'\n | 'ENC_SLOTS_REQUIRED'\n | 'ENC_SLOTS_MAC_REQUIRED'\n | 'ENC_SLOTS_MAC_INVALID_LENGTH'\n | 'KEM_EPK_LENGTH_MISMATCH'\n | 'KEM_CT_LENGTH_MISMATCH'\n | 'INVALID_CEK_LENGTH'\n | 'NONCE_LENGTH_MISMATCH'\n | 'INVALID_EPHEMERAL_SECRET_LENGTH'\n | 'EPHEMERAL_SECRETS_COUNT_MISMATCH'\n | 'UNSUPPORTED_ENC_VERSION'\n | 'UNSUPPORTED_AEAD_ALG'\n | 'UNSUPPORTED_KEM_ALG'\n | 'INVALID_ENVELOPE_SHAPE'\n | 'INVALID_RECIPIENT_KEY'\n | 'WRAP_LENGTH_MISMATCH';\n\nexport class EciesSealedPoeError extends Error {\n readonly code: EciesSealedPoeErrorCode;\n\n constructor(code: EciesSealedPoeErrorCode, message: string, options?: { cause?: unknown }) {\n super(message, options);\n this.name = 'EciesSealedPoeError';\n this.code = code;\n }\n}\n","// Single source of truth for two seams that wrap, unwrap, and the wire encoder\n// MUST agree on byte-for-byte:\n//\n// 1. How the 1120-byte X-Wing `enc` is split into the ≤ 64-byte byte-string\n// chunks the Cardano ledger requires (`kem_ct`), and the inverse join.\n// 2. The canonical-CBOR serialization of the slot array that feeds slots_mac.\n//\n// Keeping both here means the producer (wrap) and the verifier (unwrap), as well\n// as the downstream record encoder, cannot diverge on the bytes the MAC commits\n// to — the single highest correctness risk for the hybrid branch, since a\n// divergence would leave the ML-KEM ciphertext unauthenticated.\n\nimport { encodeCanonicalCbor, type CanonicalCborValue } from '../cbor/canonical';\n\nimport type { Mlkem768X25519Slot, X25519Slot } from './wrap';\n\n// The envelope-level KEM discriminator.\nexport type SealedKem = 'x25519' | 'mlkem768x25519';\n\n// Cardano ledger CDDL caps every `transaction_metadatum` byte string at 64\n// bytes, so any value larger than 64 bytes is carried as an array of ≤ 64-byte\n// chunks (the `bytes-chunk-array` wire form). This is the identical split rule\n// the record encoder applies to chunked COSE bytes.\nconst CHUNK_MAX_BYTES = 64;\n\n// Split a logical byte string into ≤ 64-byte chunks. Used for the X-Wing\n// `enc` → `kem_ct` wire form. Subarrays are views over the input, never copies.\nexport function chunkKemCt(value: Uint8Array): Uint8Array[] {\n if (value.length === 0) {\n throw new Error('chunkKemCt: refusing to chunk an empty byte string');\n }\n const chunks: Uint8Array[] = [];\n for (let i = 0; i < value.length; i += CHUNK_MAX_BYTES) {\n chunks.push(value.subarray(i, Math.min(i + CHUNK_MAX_BYTES, value.length)));\n }\n return chunks;\n}\n\n// Inverse of chunkKemCt: concatenate the chunked `kem_ct` back into the flat\n// X-Wing `enc`. Performs NO length validation — the caller (unwrap) gates the\n// reassembled length against MLKEM768X25519_ENC_LENGTH before any decapsulation.\nexport function joinKemCt(chunks: ReadonlyArray<Uint8Array>): Uint8Array {\n let total = 0;\n for (const c of chunks) total += c.length;\n const out = new Uint8Array(total);\n let offset = 0;\n for (const c of chunks) {\n out.set(c, offset);\n offset += c.length;\n }\n return out;\n}\n\n// KEM-driven slot serialization for the slots_mac input.\n//\n// • x25519: each slot → { epk: bstr, wrap: bstr }\n// • mlkem768x25519: each slot → { kem_ct: [ bstr, ... ], wrap: bstr }\n//\n// The hybrid form uses the SAME chunked-array shape as the wire encoder, so the\n// MAC commits to the ciphertext exactly as it appears on-chain. Returns the\n// canonical-CBOR bytes ready for HMAC.\nexport function slotsToMacCbor(\n slots: ReadonlyArray<X25519Slot | Mlkem768X25519Slot>,\n kem: SealedKem,\n): Uint8Array {\n let value: CanonicalCborValue;\n if (kem === 'x25519') {\n value = (slots as ReadonlyArray<X25519Slot>).map((s) => ({ epk: s.epk, wrap: s.wrap }));\n } else {\n value = (slots as ReadonlyArray<Mlkem768X25519Slot>).map((s) => ({\n // Canonicalize the chunk boundaries before the MAC commits to them:\n // reassemble the logical ciphertext and re-split into canonical ≤ 64-byte\n // chunks. The on-wire `kem_ct` array is a transport detail (the Cardano\n // ledger's 64-byte metadatum cap), and a hostile or non-canonical chunking\n // ([1, 63, …] instead of [64, …]) reassembles to the SAME bytes — so the\n // MAC must be invariant to it. Committing to the verbatim wire chunks would\n // let an attacker re-chunk an honest envelope and break the slots_mac match\n // for an honest recipient. Honest (already-64B-chunked) records are\n // unchanged; a real byte flip still changes the reassembled bytes and is\n // still rejected.\n kem_ct: chunkKemCt(joinKemCt(s.kem_ct)),\n wrap: s.wrap,\n }));\n }\n return encodeCanonicalCbor(value);\n}\n","// Multi-recipient sealed-PoE wrap (age-style ECIES + AEAD-bound slots).\n// Wire-field names are fixed by the standard: scheme, aead, kem, nonce, slots, slots_mac.\n//\n// Two KEM branches share one envelope shape, discriminated on the envelope-level\n// `kem` field:\n//\n// • kem: 'x25519' — classical age-style ECIES. Per-slot epk(32) + wrap(48).\n// • kem: 'mlkem768x25519' — X-Wing hybrid (ML-KEM-768 + X25519). Per-slot the\n// 1120-byte X-Wing enc carried as a chunked byte-string\n// array (`kem_ct`) + wrap(48). No per-slot epk.\n//\n// The slot type is a discriminated union so every consumer is forced — at compile\n// time — to branch on the KEM before touching kem-specific fields.\n\nimport { randomBytes } from '@noble/ciphers/utils.js';\nimport { hmac } from '@noble/hashes/hmac.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\n\nimport { chacha20Poly1305Encrypt } from '../aead/chacha20-poly1305';\nimport { xchacha20Poly1305Encrypt } from '../aead/xchacha20-poly1305';\nimport { hkdfSha256 } from '../kdf/hkdf';\nimport {\n mlkem768x25519Encapsulate,\n MLKEM768X25519_ENC_LENGTH,\n MLKEM768X25519_ESEED_LENGTH,\n MLKEM768X25519_PUBLIC_KEY_LENGTH,\n} from '../kem/mlkem768x25519';\nimport { x25519Ecdh, x25519PublicKey } from '../kem/x25519';\n\nimport { EciesSealedPoeError } from './errors';\nimport { chunkKemCt, slotsToMacCbor, type SealedKem } from './slots-codec';\n\n// HKDF info strings — fixed protocol labels for KEK derivation and the slot MAC.\n// Byte-length invariants enforce that the SCREAMING_SNAKE constants stay in sync\n// with the on-wire ASCII literals every conformant verifier hashes against.\nexport const CARDANO_POE_HKDF_INFO_KEK: Uint8Array = new TextEncoder().encode('cardano-poe-kek-v1');\n// Hybrid (X-Wing) per-slot KEK label. Distinct from the classical label so a\n// KEK derived under one KEM can never collide with the other. Reused verbatim as\n// the per-slot wrap AEAD AAD, exactly as the classical path reuses its own label.\nexport const CARDANO_POE_HKDF_INFO_KEK_MLKEM768X25519: Uint8Array = new TextEncoder().encode(\n 'cardano-poe-kek-mlkem768x25519-v1',\n);\nexport const CARDANO_POE_HKDF_INFO_SLOTS_MAC: Uint8Array = new TextEncoder().encode(\n 'cardano-poe-slots-mac-v1',\n);\n\nconst ZERO_NONCE_12: Uint8Array = new Uint8Array(12);\nconst EMPTY_SALT: Uint8Array = new Uint8Array(0);\nconst X25519_PUBLIC_KEY_LENGTH = 32 as const;\nconst X25519_SECRET_KEY_LENGTH = 32 as const;\nconst CEK_LENGTH = 32 as const;\nconst NONCE_LENGTH = 24 as const;\nconst WRAP_LENGTH = 48 as const;\nconst SLOTS_MAC_LENGTH = 32 as const;\n\nif (CARDANO_POE_HKDF_INFO_KEK.length !== 18) {\n throw new Error('CARDANO_POE_HKDF_INFO_KEK byte-length invariant violated (expected 18)');\n}\nif (CARDANO_POE_HKDF_INFO_KEK_MLKEM768X25519.length !== 33) {\n throw new Error(\n 'CARDANO_POE_HKDF_INFO_KEK_MLKEM768X25519 byte-length invariant violated (expected 33)',\n );\n}\nif (CARDANO_POE_HKDF_INFO_SLOTS_MAC.length !== 24) {\n throw new Error('CARDANO_POE_HKDF_INFO_SLOTS_MAC byte-length invariant violated (expected 24)');\n}\nif (ZERO_NONCE_12.length !== 12) {\n throw new Error('ZERO_NONCE_12 byte-length invariant violated (expected 12)');\n}\n\n// Classical per-slot wire shape: { epk: bstr(32), wrap: bstr(48) }.\nexport interface X25519Slot {\n readonly epk: Uint8Array;\n readonly wrap: Uint8Array;\n}\n\n// Hybrid per-slot wire shape: { kem_ct: [ bstr .size (1..64) ], wrap: bstr(48) }.\n// `kem_ct` is the 1120-byte X-Wing enc carried as a chunked byte-string array\n// (the Cardano ledger caps any single metadatum bstr at 64 bytes). There is NO\n// per-slot epk and NO per-slot kem field — the KEM identifier is hoisted to\n// envelope scope (every slot shares it).\nexport interface Mlkem768X25519Slot {\n readonly kem_ct: ReadonlyArray<Uint8Array>;\n readonly wrap: Uint8Array;\n}\n\n// Back-compat alias retired: callers branch on the envelope `kem` and use the\n// concrete slot type. The discriminated `SealedEnvelope` below is the only\n// shape consumers should depend on.\n\n// Sealed envelope wire shape (discriminated on `kem`).\nexport type SealedEnvelope =\n | {\n readonly scheme: 1;\n readonly aead: 'xchacha20-poly1305';\n readonly kem: 'x25519';\n readonly nonce: Uint8Array;\n readonly slots: ReadonlyArray<X25519Slot>;\n readonly slots_mac: Uint8Array;\n }\n | {\n readonly scheme: 1;\n readonly aead: 'xchacha20-poly1305';\n readonly kem: 'mlkem768x25519';\n readonly nonce: Uint8Array;\n readonly slots: ReadonlyArray<Mlkem768X25519Slot>;\n readonly slots_mac: Uint8Array;\n };\n\nexport interface SealedPoeOutput {\n readonly envelope: SealedEnvelope;\n readonly ciphertext: Uint8Array;\n}\n\nexport interface WrapArgs {\n readonly plaintext: Uint8Array;\n readonly recipientPublicKeys: ReadonlyArray<Uint8Array>;\n // KEM branch selector. Defaults to 'x25519' for the classical path. The\n // recipient public-key length is validated against the chosen KEM.\n readonly kem?: SealedKem;\n readonly cek?: Uint8Array;\n readonly nonce?: Uint8Array;\n // Deterministic X25519 ephemeral scalars — x25519 branch only.\n readonly ephemeralSecrets?: ReadonlyArray<Uint8Array>;\n // Deterministic X-Wing encapsulation randomness (64 bytes each) — hybrid\n // branch only. One per recipient, parallel to recipientPublicKeys.\n readonly eseeds?: ReadonlyArray<Uint8Array>;\n readonly skipShuffle?: boolean;\n}\n\nfunction concat(a: Uint8Array, b: Uint8Array): Uint8Array {\n const out = new Uint8Array(a.length + b.length);\n out.set(a, 0);\n out.set(b, a.length);\n return out;\n}\n\n// Anonymity invariant: wire ordering MUST NOT encode \"primary\n// recipient first\". A CSPRNG-keyed Fisher-Yates shuffle uniformly permutes the\n// slot array so trial-decrypt order leaks no recipient identity. The\n// slot-set HMAC is computed AFTER this shuffle, binding the on-wire order.\n//\n// Draw an unbiased index in [0, m) from a CSPRNG uint32 via rejection sampling.\n// A plain `u32 % m` skews toward the low residues whenever `m` does not divide\n// 2^32 evenly: the values [0, 2^32 mod m) each occur one extra time. This\n// function exists purely to produce a UNIFORM permutation, so the bias — though\n// cryptographically negligible — is exactly the property we cannot tolerate.\n// We reject any draw landing in the final partial block [limit, 2^32) and\n// redraw, leaving only the residues that map uniformly onto [0, m).\n// Exported so the rejection-bound arithmetic can be asserted directly in tests\n// without relying on a flaky statistical-distribution check.\nexport function uniformIndexBelow(m: number): number {\n // 2^32 mod m, computed without overflowing the 32-bit space.\n const limit = 0x1_0000_0000 - (0x1_0000_0000 % m);\n const buf = new Uint32Array(1);\n let x: number;\n do {\n crypto.getRandomValues(buf);\n x = buf[0] as number;\n } while (x >= limit);\n return x % m;\n}\n\nfunction csprngShuffle<T>(arr: T[]): void {\n for (let i = arr.length - 1; i > 0; i--) {\n const j = uniformIndexBelow(i + 1);\n const tmp = arr[i] as T;\n arr[i] = arr[j] as T;\n arr[j] = tmp;\n }\n}\n\n// Wrap the CEK for one classical recipient: age-style ECIES stanza.\nfunction wrapSlotX25519(args: {\n pubR: Uint8Array;\n privEph: Uint8Array | undefined;\n cek: Uint8Array;\n slotIdx: number;\n}): X25519Slot {\n const privEph = args.privEph ?? randomBytes(X25519_SECRET_KEY_LENGTH);\n if (privEph.length !== X25519_SECRET_KEY_LENGTH) {\n throw new EciesSealedPoeError(\n 'INVALID_EPHEMERAL_SECRET_LENGTH',\n `ephemeralSecrets[${args.slotIdx}] MUST be exactly ${X25519_SECRET_KEY_LENGTH} bytes, got ${privEph.length}`,\n );\n }\n const epk = x25519PublicKey({ secretKey: privEph });\n const shared = x25519Ecdh({ secretKey: privEph, theirPublicKey: args.pubR });\n // age v1 stanza salt is `epk || pub_R`.\n const kek = hkdfSha256({\n ikm: shared,\n salt: concat(epk, args.pubR),\n info: CARDANO_POE_HKDF_INFO_KEK,\n length: 32,\n });\n // Per-slot wrap AAD MUST be the 18-byte ASCII literal of the KEK info\n // string (never empty AAD).\n const wrap = chacha20Poly1305Encrypt({\n key: kek,\n nonce: ZERO_NONCE_12,\n aad: CARDANO_POE_HKDF_INFO_KEK,\n plaintext: args.cek,\n });\n if (wrap.length !== WRAP_LENGTH) {\n throw new Error(`internal: wrap.length=${wrap.length}, expected ${WRAP_LENGTH}`);\n }\n return { epk, wrap };\n}\n\n// Wrap the CEK for one hybrid recipient: X-Wing encapsulation → HKDF → AEAD.\n// The KEK info label doubles as the wrap AEAD AAD, mirroring the classical path.\nfunction wrapSlotMlkem768X25519(args: {\n pubR: Uint8Array;\n eseed: Uint8Array | undefined;\n cek: Uint8Array;\n}): Mlkem768X25519Slot {\n const { enc, ss } = mlkem768x25519Encapsulate({\n publicKey: args.pubR,\n ...(args.eseed !== undefined ? { eseed: args.eseed } : {}),\n });\n if (enc.length !== MLKEM768X25519_ENC_LENGTH) {\n throw new Error(`internal: enc.length=${enc.length}, expected ${MLKEM768X25519_ENC_LENGTH}`);\n }\n const kek = hkdfSha256({\n ikm: ss,\n salt: EMPTY_SALT,\n info: CARDANO_POE_HKDF_INFO_KEK_MLKEM768X25519,\n length: 32,\n });\n const wrap = chacha20Poly1305Encrypt({\n key: kek,\n nonce: ZERO_NONCE_12,\n aad: CARDANO_POE_HKDF_INFO_KEK_MLKEM768X25519,\n plaintext: args.cek,\n });\n if (wrap.length !== WRAP_LENGTH) {\n throw new Error(`internal: wrap.length=${wrap.length}, expected ${WRAP_LENGTH}`);\n }\n return { kem_ct: chunkKemCt(enc), wrap };\n}\n\nexport function eciesSealedPoeWrap(args: WrapArgs): SealedPoeOutput {\n const { plaintext, recipientPublicKeys } = args;\n const kem: SealedKem = args.kem ?? 'x25519';\n const n = recipientPublicKeys.length;\n\n // There is no fixed upper bound on slot count; the producer SDK polices the\n // per-record byte budget. Only the lower bound is enforced here.\n if (n < 1) {\n throw new EciesSealedPoeError(\n 'ENC_SLOTS_EMPTY',\n `recipientPublicKeys.length=${n} must be >= 1`,\n );\n }\n\n const expectedPubLen =\n kem === 'x25519' ? X25519_PUBLIC_KEY_LENGTH : MLKEM768X25519_PUBLIC_KEY_LENGTH;\n for (let i = 0; i < n; i++) {\n const pub = recipientPublicKeys[i];\n if (pub === undefined || pub.length !== expectedPubLen) {\n throw new EciesSealedPoeError(\n 'KEM_EPK_LENGTH_MISMATCH',\n `recipientPublicKeys[${i}] MUST be exactly ${expectedPubLen} bytes for kem='${kem}'`,\n );\n }\n }\n\n if (kem === 'x25519') {\n if (args.eseeds !== undefined) {\n throw new EciesSealedPoeError(\n 'EPHEMERAL_SECRETS_COUNT_MISMATCH',\n \"eseeds is an X-Wing (mlkem768x25519) override and MUST NOT be supplied for kem='x25519'\",\n );\n }\n if (args.ephemeralSecrets !== undefined && args.ephemeralSecrets.length !== n) {\n throw new EciesSealedPoeError(\n 'EPHEMERAL_SECRETS_COUNT_MISMATCH',\n `ephemeralSecrets.length=${args.ephemeralSecrets.length} must match recipientPublicKeys.length=${n}`,\n );\n }\n } else {\n if (args.ephemeralSecrets !== undefined) {\n throw new EciesSealedPoeError(\n 'EPHEMERAL_SECRETS_COUNT_MISMATCH',\n \"ephemeralSecrets is an X25519 override and MUST NOT be supplied for kem='mlkem768x25519'\",\n );\n }\n if (args.eseeds !== undefined) {\n if (args.eseeds.length !== n) {\n throw new EciesSealedPoeError(\n 'EPHEMERAL_SECRETS_COUNT_MISMATCH',\n `eseeds.length=${args.eseeds.length} must match recipientPublicKeys.length=${n}`,\n );\n }\n for (let i = 0; i < n; i++) {\n const eseed = args.eseeds[i]!;\n if (eseed.length !== MLKEM768X25519_ESEED_LENGTH) {\n throw new EciesSealedPoeError(\n 'INVALID_EPHEMERAL_SECRET_LENGTH',\n `eseeds[${i}] MUST be exactly ${MLKEM768X25519_ESEED_LENGTH} bytes, got ${eseed.length}`,\n );\n }\n }\n }\n }\n\n const cek = args.cek ?? randomBytes(CEK_LENGTH);\n const nonce = args.nonce ?? randomBytes(NONCE_LENGTH);\n if (cek.length !== CEK_LENGTH) {\n throw new EciesSealedPoeError(\n 'INVALID_CEK_LENGTH',\n `cek MUST be exactly ${CEK_LENGTH} bytes, got ${cek.length}`,\n );\n }\n if (nonce.length !== NONCE_LENGTH) {\n throw new EciesSealedPoeError(\n 'NONCE_LENGTH_MISMATCH',\n `nonce MUST be exactly ${NONCE_LENGTH} bytes, got ${nonce.length}`,\n );\n }\n\n let envelope: SealedEnvelope;\n if (kem === 'x25519') {\n const slots: X25519Slot[] = [];\n for (let i = 0; i < n; i++) {\n slots.push(\n wrapSlotX25519({\n pubR: recipientPublicKeys[i]!,\n privEph: args.ephemeralSecrets ? (args.ephemeralSecrets[i] as Uint8Array) : undefined,\n cek,\n slotIdx: i,\n }),\n );\n }\n // Anonymity invariant (see csprngShuffle comment).\n if (args.skipShuffle !== true) {\n csprngShuffle(slots);\n }\n const slotsMac = computeSlotsMac(cek, slots, 'x25519');\n envelope = {\n scheme: 1,\n aead: 'xchacha20-poly1305',\n kem: 'x25519',\n nonce,\n slots,\n slots_mac: slotsMac,\n };\n } else {\n const slots: Mlkem768X25519Slot[] = [];\n for (let i = 0; i < n; i++) {\n slots.push(\n wrapSlotMlkem768X25519({\n pubR: recipientPublicKeys[i]!,\n eseed: args.eseeds ? (args.eseeds[i] as Uint8Array) : undefined,\n cek,\n }),\n );\n }\n if (args.skipShuffle !== true) {\n csprngShuffle(slots);\n }\n const slotsMac = computeSlotsMac(cek, slots, 'mlkem768x25519');\n envelope = {\n scheme: 1,\n aead: 'xchacha20-poly1305',\n kem: 'mlkem768x25519',\n nonce,\n slots,\n slots_mac: slotsMac,\n };\n }\n\n // Content AEAD AAD is `nonce || slots_mac` (24 + 32 = 56 B).\n const adContent = concat(nonce, envelope.slots_mac);\n const ciphertext = xchacha20Poly1305Encrypt({\n key: cek,\n nonce,\n aad: adContent,\n plaintext,\n });\n\n return { envelope, ciphertext };\n}\n\n// Slot-set MAC binds canonical-CBOR(slots) to the CEK.\n// The slot→CBOR serialization is KEM-driven (`slotsToMacCbor`) so the hybrid\n// kem_ct is authenticated by slots_mac exactly as the classical epk is.\nfunction computeSlotsMac(\n cek: Uint8Array,\n slots: ReadonlyArray<X25519Slot | Mlkem768X25519Slot>,\n kem: SealedKem,\n): Uint8Array {\n const hmacKey = hkdfSha256({\n ikm: cek,\n salt: EMPTY_SALT,\n info: CARDANO_POE_HKDF_INFO_SLOTS_MAC,\n length: 32,\n });\n const slotsCbor = slotsToMacCbor(slots, kem);\n const slotsMac = hmac(sha256, hmacKey, slotsCbor);\n if (slotsMac.length !== SLOTS_MAC_LENGTH) {\n throw new Error(`internal: slots_mac.length=${slotsMac.length}, expected ${SLOTS_MAC_LENGTH}`);\n }\n return slotsMac;\n}\n","// Raised by the high-level helpers (`publishSealed`, `publishMerkle`) when\n// one or more files uploaded via /poe/uploads come back with `ok: false`.\n//\n// The error carries the full `UploadsResponse` so callers can:\n// - retry just the failed indices (use `failedIndices` to subset their input)\n// - inspect per-file `error.code` / `error.detail` for diagnostics\n// - see which files DID land (already-uploaded files are billed and the\n// URIs remain valid; reuploading them would double-charge)\n\nimport type { UploadFailureEntry, UploadsResponse } from './types';\n\nexport class PartialUploadError extends Error {\n public readonly response: UploadsResponse;\n public readonly failed: ReadonlyArray<UploadFailureEntry>;\n\n constructor(response: UploadsResponse) {\n const failed = response.uploads.filter((u): u is UploadFailureEntry => u.ok === false);\n super(\n `${failed.length} of ${response.uploads.length} upload(s) failed: ${failed\n .map((f) => `[${f.idx}] ${f.error.code} — ${f.error.detail}`)\n .join('; ')}`,\n );\n this.name = 'PartialUploadError';\n this.response = response;\n this.failed = failed;\n }\n\n /** Convenience: the `idx` of every failed entry, in input order. */\n get failedIndices(): ReadonlyArray<number> {\n return this.failed.map((f) => f.idx);\n }\n}\n","// High-level publish helpers — collapse the new uploads + publish flow into\n// single calls for the three common shapes:\n//\n// 1. `publishContent({content, signer?})` — anchor a single content blob by\n// its `sha2-256` (or `blake2b-256`) digest. No Arweave, no /uploads —\n// the record is constructed entirely client-side and posted directly to\n// /publish.\n//\n// 2. `publishSealed({content, recipients, signer?})` — encrypt the content\n// to the recipient X25519 public keys (age-style sealed envelope),\n// upload the ciphertext to Arweave via /uploads, build a CIP-309 record\n// with the resulting `ar://` URI, sign, and post to /publish.\n//\n// 3. `publishMerkle({leaves, signer?})` — anchor an arbitrary number of leaf\n// hashes under a single RFC 9162 §2.1.1 root, with the leaves-list CBOR\n// uploaded to Arweave via /uploads. The Merkle root + leaf_count are\n// bound into the on-chain record via `merkle[0]`.\n//\n// Signer architecture: the SDK does NOT hold identity keys (privacy contract\n// in `off-host-sign.ts`). The helpers take an optional `Signer` that owns the\n// Ed25519 private key (in-memory `@noble/ed25519`, AWS KMS, GCP HSM, …). The\n// helper builds the canonical `Sig_structure`, hands the bytes to the signer,\n// and never sees the private key. When `signer` is omitted the record\n// publishes unsigned (conformance profile `core` for hash-only;\n// `sealed` for sealed envelopes).\n\nimport {\n sha256,\n blake2b256,\n merkleSha2256Root,\n MERKLE_ALG_ID,\n} from '@cardanowall/crypto-core/hash';\nimport { encodeLeavesList } from '@cardanowall/crypto-core/merkle';\nimport { eciesSealedPoeWrap } from '@cardanowall/crypto-core/sealed-poe';\nimport {\n chunkUri,\n encodePoeRecord,\n type EncryptionEnvelope,\n type MerkleCommit,\n type PoeRecord,\n} from '@cardanowall/poe-standard';\n\nimport { assembleCoseSign1, prepareSigStructure } from './off-host-sign';\nimport { PartialUploadError } from './partial-upload-error';\nimport { parseHttpError } from './parse-http-error';\nimport type {\n FetchImpl,\n PublishContentInput,\n PublishMerkleInput,\n PublishMerkleResponse,\n PublishPrehashedInput,\n PublishResponse,\n PublishSealedInput,\n Signer,\n SupportedHashAlg,\n UploadsResponse,\n UploadSuccessEntry,\n} from './types';\n\nconst ED25519_PUBLIC_KEY_LENGTH = 32;\nconst ED25519_SIGNATURE_LENGTH = 64;\nconst X25519_PUBLIC_KEY_LENGTH = 32;\nconst MLKEM768X25519_PUBLIC_KEY_LENGTH = 1216;\nconst LEAF_DIGEST_LENGTH = 32;\nconst STORAGE_TARGET_ARWEAVE = 'arweave' as const;\n\nexport interface ResolvedPublishConfig {\n readonly apiKey: string | undefined;\n readonly baseUrl: string;\n readonly fetch: FetchImpl;\n}\n\nexport class PublishError extends Error {\n readonly code:\n | 'INVALID_SIGNER_PUBKEY'\n | 'INVALID_SIGNER_SIGNATURE'\n | 'INVALID_LEAVES'\n | 'INVALID_DIGEST'\n | 'INVALID_RECIPIENT'\n | 'UNSUPPORTED_HASH_ALG';\n\n constructor(code: PublishError['code'], message: string) {\n super(message);\n this.name = 'PublishError';\n this.code = code;\n }\n}\n\nfunction bytesToHex(bytes: Uint8Array): string {\n return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');\n}\n\nfunction hexToBytes(hex: string): Uint8Array {\n if (hex.length % 2 !== 0) {\n throw new PublishError('INVALID_LEAVES', `hex string has odd length: ${hex.length}`);\n }\n const out = new Uint8Array(hex.length / 2);\n for (let i = 0; i < out.length; i++) {\n const byte = parseInt(hex.slice(i * 2, i * 2 + 2), 16);\n if (Number.isNaN(byte)) {\n throw new PublishError('INVALID_LEAVES', `invalid hex byte at offset ${i * 2}`);\n }\n out[i] = byte;\n }\n return out;\n}\n\nfunction toBytes(content: Uint8Array | string): Uint8Array {\n if (typeof content === 'string') return new TextEncoder().encode(content);\n return content;\n}\n\n// Allocate a fresh Uint8Array (with a concrete ArrayBuffer backing, not\n// ArrayBufferLike) so the result satisfies the strict `Uint8Array<ArrayBuffer>`\n// generic that Zod infers for hash-digest schemas. @noble/hashes returns the\n// generic `Uint8Array<ArrayBufferLike>` shape; an explicit clone collapses it\n// to the strict variant without `as` casts.\nfunction cloneToOwnedBuffer(src: Uint8Array): Uint8Array<ArrayBuffer> {\n const out = new Uint8Array(new ArrayBuffer(src.length));\n out.set(src);\n return out;\n}\n\nfunction hashContent(bytes: Uint8Array, alg: SupportedHashAlg): Uint8Array<ArrayBuffer> {\n if (alg === 'sha2-256') return cloneToOwnedBuffer(sha256(bytes));\n if (alg === 'blake2b-256') return cloneToOwnedBuffer(blake2b256(bytes));\n throw new PublishError(\n 'UNSUPPORTED_HASH_ALG',\n `hashAlg must be 'sha2-256' or 'blake2b-256', got '${alg as string}'`,\n );\n}\n\nfunction assertSigner(signer: Signer): void {\n if (\n !(signer.signerPubkey instanceof Uint8Array) ||\n signer.signerPubkey.length !== ED25519_PUBLIC_KEY_LENGTH\n ) {\n throw new PublishError(\n 'INVALID_SIGNER_PUBKEY',\n `signer.signerPubkey must be a Uint8Array(${ED25519_PUBLIC_KEY_LENGTH})`,\n );\n }\n if (typeof signer.sign !== 'function') {\n throw new PublishError('INVALID_SIGNER_PUBKEY', 'signer.sign must be a function');\n }\n}\n\nfunction buildJsonHeaders(apiKey: string | undefined, idempotencyKey?: string): Headers {\n const headers = new Headers({ 'content-type': 'application/json', accept: 'application/json' });\n if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`);\n if (idempotencyKey !== undefined) headers.set('idempotency-key', idempotencyKey);\n return headers;\n}\n\nfunction buildMultipartHeaders(apiKey: string | undefined, idempotencyKey?: string): Headers {\n const headers = new Headers({ accept: 'application/json' });\n if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`);\n if (idempotencyKey !== undefined) headers.set('idempotency-key', idempotencyKey);\n return headers;\n}\n\nasync function readJson(response: Response): Promise<unknown> {\n const text = await response.text();\n if (text.length === 0) return null;\n try {\n return JSON.parse(text);\n } catch {\n return null;\n }\n}\n\nfunction parseRetryAfter(header: string | null): number | undefined {\n if (header === null) return undefined;\n const parsed = Number(header);\n return Number.isFinite(parsed) ? parsed : undefined;\n}\n\nasync function throwIfNotOk(response: Response): Promise<void> {\n if (response.ok) return;\n const body = await readJson(response);\n const requestId = response.headers.get('x-request-id') ?? undefined;\n const retryAfterSeconds = parseRetryAfter(response.headers.get('retry-after'));\n throw parseHttpError({ httpStatus: response.status, body, requestId, retryAfterSeconds });\n}\n\n/**\n * Sign a record path-1 (in-memory Ed25519 / KMS / HSM) and return the final\n * canonical-CBOR bytes ready for /publish. The signature is embedded into\n * the record's `sigs[]` field — the wire `signatures` parameter on /publish\n * is reserved for the path-2 wallet flow (CIP-30 cose_key sidecar).\n */\nasync function signAndEncodeRecord(record: PoeRecord, signer: Signer): Promise<Uint8Array> {\n const { sigStructureBytes } = prepareSigStructure({\n record,\n signerPubkey: signer.signerPubkey,\n });\n const signature = await signer.sign(sigStructureBytes);\n if (!(signature instanceof Uint8Array) || signature.length !== ED25519_SIGNATURE_LENGTH) {\n throw new PublishError(\n 'INVALID_SIGNER_SIGNATURE',\n `signer.sign() must return a Uint8Array(${ED25519_SIGNATURE_LENGTH}); got length ${\n signature instanceof Uint8Array ? signature.length : 'non-Uint8Array'\n }`,\n );\n }\n const { sigEntry } = assembleCoseSign1({\n record,\n signerPubkey: signer.signerPubkey,\n signature,\n });\n const signed: PoeRecord = { ...record, sigs: [sigEntry] };\n return encodePoeRecord(signed);\n}\n\nasync function encodeRecord(record: PoeRecord, signer: Signer | undefined): Promise<Uint8Array> {\n if (signer === undefined) return encodePoeRecord(record);\n return signAndEncodeRecord(record, signer);\n}\n\nasync function postPublish(\n config: ResolvedPublishConfig,\n recordBytesHex: string,\n quoteId: string,\n idempotencyKey: string | undefined,\n): Promise<PublishResponse> {\n const body = { record: recordBytesHex, quote_id: quoteId };\n const response = await config.fetch(`${config.baseUrl}/api/v1/poe/publish`, {\n method: 'POST',\n headers: buildJsonHeaders(config.apiKey, idempotencyKey),\n body: JSON.stringify(body),\n });\n await throwIfNotOk(response);\n const parsed = (await readJson(response)) as Omit<PublishResponse, 'dedup_hit'>;\n return { ...parsed, dedup_hit: response.status === 200 };\n}\n\nasync function postUploads(\n config: ResolvedPublishConfig,\n blobs: ReadonlyArray<Uint8Array>,\n idempotencyKey: string | undefined,\n): Promise<UploadsResponse> {\n const form = new FormData();\n form.append('target', STORAGE_TARGET_ARWEAVE);\n for (let idx = 0; idx < blobs.length; idx++) {\n const bytes = blobs[idx]!;\n form.append(\n `file_${idx}`,\n new Blob([bytes as unknown as ArrayBuffer], { type: 'application/octet-stream' }),\n `file_${idx}.bin`,\n );\n }\n const response = await config.fetch(`${config.baseUrl}/api/v1/poe/uploads`, {\n method: 'POST',\n headers: buildMultipartHeaders(config.apiKey, idempotencyKey),\n body: form,\n });\n await throwIfNotOk(response);\n const result = (await readJson(response)) as UploadsResponse;\n const anyFailed = result.uploads.some((u) => u.ok === false);\n if (anyFailed) {\n throw new PartialUploadError(result);\n }\n return result;\n}\n\n/**\n * Hash-only PoE — anchor a single content blob's digest, optionally with\n * one path-1 signature. No Arweave, no /uploads.\n */\nexport async function publishContent(\n config: ResolvedPublishConfig,\n input: PublishContentInput,\n): Promise<PublishResponse> {\n if (input.signer !== undefined) assertSigner(input.signer);\n const hashAlg: SupportedHashAlg = input.hashAlg ?? 'sha2-256';\n const contentBytes = toBytes(input.content);\n const digest = hashContent(contentBytes, hashAlg);\n\n const record: PoeRecord = {\n v: 1,\n items: [{ hashes: { [hashAlg]: digest } }],\n };\n const recordBytes = await encodeRecord(record, input.signer);\n return postPublish(config, bytesToHex(recordBytes), input.quoteId, input.idempotencyKey);\n}\n\n// `sha2-256` and `blake2b-256` both produce 32-byte digests. Kept as a\n// per-alg map for forward-compat when wider hash registries land.\nconst DIGEST_BYTE_LENGTH: Record<SupportedHashAlg, number> = {\n 'sha2-256': 32,\n 'blake2b-256': 32,\n};\n\n/**\n * Hash-already-computed PoE — anchor a precomputed content digest (the user\n * already has it), optionally signed. The SDK does not re-hash; it\n * constructs a single-item record with the supplied digests in\n * `items[0].hashes`.\n */\nexport async function publishPrehashed(\n config: ResolvedPublishConfig,\n input: PublishPrehashedInput,\n): Promise<PublishResponse> {\n if (input.signer !== undefined) assertSigner(input.signer);\n const entries = Object.entries(input.hashes) as Array<[SupportedHashAlg, string | undefined]>;\n const present = entries.filter(([, hex]) => typeof hex === 'string' && hex.length > 0) as Array<\n [SupportedHashAlg, string]\n >;\n if (present.length === 0) {\n throw new PublishError(\n 'INVALID_DIGEST',\n 'publishPrehashed requires at least one digest in `hashes`',\n );\n }\n const decoded: Record<string, Uint8Array<ArrayBuffer>> = {};\n for (const [alg, hex] of present) {\n if (!(alg in DIGEST_BYTE_LENGTH)) {\n throw new PublishError(\n 'UNSUPPORTED_HASH_ALG',\n `unsupported hash algorithm '${alg as string}' (expected 'sha2-256' or 'blake2b-256')`,\n );\n }\n const bytes = hexToBytes(hex);\n const expected = DIGEST_BYTE_LENGTH[alg];\n if (bytes.length !== expected) {\n throw new PublishError(\n 'INVALID_DIGEST',\n `hashes[${alg}] must be a ${expected}-byte digest (got ${bytes.length} bytes)`,\n );\n }\n decoded[alg] = cloneToOwnedBuffer(bytes);\n }\n\n const record: PoeRecord = {\n v: 1,\n items: [{ hashes: decoded }],\n };\n const recordBytes = await encodeRecord(record, input.signer);\n return postPublish(config, bytesToHex(recordBytes), input.quoteId, input.idempotencyKey);\n}\n\n/**\n * Sealed-PoE: encrypt content to N X25519 recipients (age-style envelope),\n * upload the ciphertext to Arweave, build a single-item record with the\n * resulting `ar://` URI and the sealed envelope in `items[0].enc`, sign\n * (optional), and post to /publish.\n *\n * The plaintext content-hash is bound into `items[0].hashes` so any verifier\n * that successfully decrypts the ciphertext can reconstruct the plaintext\n * and prove the chain of custody from the on-chain hash to the decrypted\n * bytes.\n */\nexport async function publishSealed(\n config: ResolvedPublishConfig,\n input: PublishSealedInput,\n): Promise<PublishResponse> {\n if (input.signer !== undefined) assertSigner(input.signer);\n if (input.recipients.length < 1) {\n throw new PublishError(\n 'INVALID_RECIPIENT',\n 'publishSealed requires at least one recipient public key',\n );\n }\n // Default to the post-quantum-safe X-Wing hybrid KEM; x25519 is the explicit\n // classical opt-out. The recipient length guard is KEM-aware: 32 B for\n // x25519, 1216 B for the hybrid path.\n const kem = input.kem ?? 'mlkem768x25519';\n const expectedRecipientLength =\n kem === 'x25519' ? X25519_PUBLIC_KEY_LENGTH : MLKEM768X25519_PUBLIC_KEY_LENGTH;\n for (let i = 0; i < input.recipients.length; i++) {\n const pub = input.recipients[i]!;\n if (!(pub instanceof Uint8Array) || pub.length !== expectedRecipientLength) {\n throw new PublishError(\n 'INVALID_RECIPIENT',\n `recipients[${i}] must be a ${expectedRecipientLength}-byte public key for kem='${kem}'`,\n );\n }\n }\n\n const hashAlg: SupportedHashAlg = input.hashAlg ?? 'sha2-256';\n const plaintext = toBytes(input.content);\n const plaintextDigest = hashContent(plaintext, hashAlg);\n\n // Encrypt the plaintext to the recipient public keys under the chosen KEM.\n const sealed = eciesSealedPoeWrap({\n plaintext,\n recipientPublicKeys: input.recipients.map((r) => r),\n kem,\n });\n\n // Upload the ciphertext to Arweave.\n const uploadsResp = await postUploads(config, [sealed.ciphertext], input.idempotencyKey);\n const uri = (uploadsResp.uploads[0] as UploadSuccessEntry).uri;\n\n // Build the sealed record: one item with the plaintext-bind hash, the\n // `ar://<tx>` URI of the ciphertext, and the discriminated envelope shape.\n // Narrow on the envelope `kem` to emit the correct per-slot fields:\n // classical slots carry `{ epk, wrap }`, hybrid slots carry chunked\n // `{ kem_ct, wrap }`.\n const env = sealed.envelope;\n const slots =\n env.kem === 'mlkem768x25519'\n ? env.slots.map((s) => ({\n kem_ct: s.kem_ct.map((c) => cloneToOwnedBuffer(c)),\n wrap: cloneToOwnedBuffer(s.wrap),\n }))\n : env.slots.map((s) => ({\n epk: cloneToOwnedBuffer(s.epk),\n wrap: cloneToOwnedBuffer(s.wrap),\n }));\n const envelope: EncryptionEnvelope = {\n scheme: 1,\n aead: env.aead,\n kem: env.kem,\n nonce: cloneToOwnedBuffer(env.nonce),\n slots,\n slots_mac: cloneToOwnedBuffer(env.slots_mac),\n };\n\n const record: PoeRecord = {\n v: 1,\n items: [\n {\n hashes: { [hashAlg]: plaintextDigest },\n uris: [chunkUri(uri)],\n enc: envelope,\n },\n ],\n };\n const recordBytes = await encodeRecord(record, input.signer);\n return postPublish(config, bytesToHex(recordBytes), input.quoteId, input.idempotencyKey);\n}\n\n/**\n * Merkle batch publish via /uploads + /publish — N leaves under one\n * transaction. The leaves-list CBOR is uploaded to Arweave as a single\n * blob; the on-chain record carries\n * `merkle[0] = { alg: 'rfc9162-sha256', root, leaf_count, uris: [ar://<tx>] }`.\n *\n * Only `sha2-256` leaves are supported because `rfc9162-sha256` is the only\n * registered tree algorithm and its underlying hash is SHA-256 (32-byte\n * leaves).\n */\nexport async function publishMerkle(\n config: ResolvedPublishConfig,\n input: PublishMerkleInput,\n): Promise<PublishMerkleResponse> {\n if (input.signer !== undefined) assertSigner(input.signer);\n if (input.hashAlg !== undefined && input.hashAlg !== 'sha2-256') {\n throw new PublishError(\n 'UNSUPPORTED_HASH_ALG',\n `publishMerkle only supports 'sha2-256' leaves; got '${input.hashAlg as string}'`,\n );\n }\n if (input.leaves.length < 1) {\n throw new PublishError('INVALID_LEAVES', 'publishMerkle requires at least one leaf hash');\n }\n\n const leaves: Uint8Array[] = input.leaves.map((leaf, idx) => {\n const bytes = typeof leaf === 'string' ? hexToBytes(leaf) : leaf;\n if (!(bytes instanceof Uint8Array) || bytes.length !== LEAF_DIGEST_LENGTH) {\n throw new PublishError(\n 'INVALID_LEAVES',\n `leaves[${idx}] must be a ${LEAF_DIGEST_LENGTH}-byte sha2-256 digest`,\n );\n }\n return bytes;\n });\n\n const root = cloneToOwnedBuffer(merkleSha2256Root(leaves));\n const leavesListCbor = encodeLeavesList({ leaves, root });\n\n // Upload the leaves-list to Arweave.\n const uploadsResp = await postUploads(config, [leavesListCbor], input.idempotencyKey);\n const uri = (uploadsResp.uploads[0] as UploadSuccessEntry).uri;\n\n // Build the on-chain record with the resulting `ar://` URI.\n const merkleEntry: MerkleCommit = {\n alg: MERKLE_ALG_ID,\n root,\n leaf_count: leaves.length,\n uris: [chunkUri(uri)],\n };\n const record: PoeRecord = { v: 1, merkle: [merkleEntry] };\n const recordBytes = await encodeRecord(record, input.signer);\n const published = await postPublish(\n config,\n bytesToHex(recordBytes),\n input.quoteId,\n input.idempotencyKey,\n );\n\n return {\n id: published.id,\n tx_hash: published.tx_hash,\n status: published.status,\n root: bytesToHex(root),\n leaf_count: leaves.length,\n ar_uri: uri,\n balance_after_usd_micros: published.balance_after_usd_micros,\n };\n}\n","// Low-level wrappers over the public mutating /api/v1/poe/* surface:\n//\n// POST /api/v1/poe/quote — lock a USD price for a publish\n// POST /api/v1/poe/uploads — multipart binary upload to a backend\n// POST /api/v1/poe/publish — single finalised record (JSON)\n// POST /api/v1/poe/publish-batch — 1..50 finalised records (JSON)\n//\n// Plus high-level helpers that compose the above into common flows:\n//\n// publishContent({content, quoteId, signer?}) — hash-only\n// publishPrehashed({hashes, quoteId, signer?}) — caller already holds digest\n// publishSealed({content, recipients, quoteId, signer?}) — encrypt + uploads + publish\n// publishMerkle({leaves, quoteId, signer?}) — uploads + publish, Merkle root\n\nimport { bytesToHex } from '../hex';\nimport { readJson, throwIfNotOk } from './http-helpers';\nimport {\n publishContent as publishContentImpl,\n publishMerkle as publishMerkleImpl,\n publishPrehashed as publishPrehashedImpl,\n publishSealed as publishSealedImpl,\n type ResolvedPublishConfig,\n} from './publish';\nimport type {\n FetchImpl,\n PublishBatchInput,\n PublishBatchResponse,\n PublishContentInput,\n PublishInput,\n PublishMerkleInput,\n PublishMerkleResponse,\n PublishPrehashedInput,\n PublishResponse,\n PublishSealedInput,\n QuoteInput,\n QuoteResponse,\n UploadsInput,\n UploadsResponse,\n} from './types';\n\ninterface ResolvedConfig {\n readonly apiKey: string | undefined;\n readonly baseUrl: string;\n readonly fetch: FetchImpl;\n}\n\nfunction buildJsonHeaders(args: {\n apiKey: string | undefined;\n idempotencyKey?: string | undefined;\n}): Headers {\n const headers = new Headers({ 'content-type': 'application/json', accept: 'application/json' });\n if (args.apiKey !== undefined) headers.set('authorization', `Bearer ${args.apiKey}`);\n if (args.idempotencyKey !== undefined) headers.set('idempotency-key', args.idempotencyKey);\n return headers;\n}\n\nfunction buildMultipartHeaders(args: {\n apiKey: string | undefined;\n idempotencyKey?: string | undefined;\n}): Headers {\n // Do NOT set content-type — the runtime emits the multipart boundary header\n // automatically when the body is a FormData. Forcing it here would emit a\n // boundary-less content-type that the server rejects.\n const headers = new Headers({ accept: 'application/json' });\n if (args.apiKey !== undefined) headers.set('authorization', `Bearer ${args.apiKey}`);\n if (args.idempotencyKey !== undefined) headers.set('idempotency-key', args.idempotencyKey);\n return headers;\n}\n\nfunction toHex(record: Uint8Array | string): string {\n return typeof record === 'string' ? record : bytesToHex(record);\n}\n\nexport class PoeNamespace {\n private readonly config: ResolvedConfig;\n\n constructor(config: ResolvedConfig) {\n this.config = config;\n }\n\n /**\n * Request an opaque price lock for an upcoming /publish call. The gateway\n * prices the described publish from the supplied byte counts, records the\n * lock, and returns a sealed price token: `quote_id`, the total `amount` in\n * `currency`, and an `expires_at`. The gateway's pricing internals are\n * deliberately NOT part of the response.\n *\n * `amount` is a decimal string; promote it to `BigInt` (or a decimal type)\n * at the application boundary if you need exact arithmetic.\n *\n * Pass the returned `quote_id` to `publish()` (or one of the high-level\n * `publishContent` / `publishSealed` / `publishMerkle` helpers).\n */\n async quote(input: QuoteInput): Promise<QuoteResponse> {\n const body = {\n record_bytes: input.recordBytes,\n recipient_count: input.recipientCount,\n file_bytes_total: input.fileBytesTotal,\n };\n const response = await this.config.fetch(`${this.config.baseUrl}/api/v1/poe/quote`, {\n method: 'POST',\n headers: buildJsonHeaders({ apiKey: this.config.apiKey }),\n body: JSON.stringify(body),\n });\n await throwIfNotOk(response);\n return (await readJson(response)) as QuoteResponse;\n }\n\n /**\n * Upload 1..32 binary files to a storage backend. Returns one entry per file\n * — successful entries carry the `ar://` URI + content hash, failed entries\n * carry an error code / detail so the caller can retry just the failed\n * indices.\n *\n * Billing: free. The storage cost is part of the publish quote (POST\n * /api/v1/poe/quote → POST /api/v1/poe/publish) and is debited once at\n * publish time against the locked price snapshot.\n *\n * On HTTP-level failure (auth, rate limit, malformed request) this throws\n * a typed `Cip309HttpError` subclass. Per-file failures inside a 200\n * response are NOT thrown by `uploads()` itself — the response body is\n * returned verbatim so the caller can decide how to react. The\n * higher-level helpers (`publishSealed`, `publishMerkle`) treat any failed\n * file as a `PartialUploadError`.\n */\n async uploads(input: UploadsInput): Promise<UploadsResponse> {\n const form = new FormData();\n form.append('target', input.target);\n for (let idx = 0; idx < input.data.length; idx++) {\n const bytes = input.data[idx]!;\n // Uint8Array is a valid Blob source in every runtime that ships\n // FormData (browser, undici, node 20+); cast through `unknown` keeps\n // strict-mode TS happy without dragging in lib.dom.iterable.d.ts here.\n form.append(\n `file_${idx}`,\n new Blob([bytes as unknown as ArrayBuffer], { type: 'application/octet-stream' }),\n `file_${idx}.bin`,\n );\n }\n const headers = buildMultipartHeaders({\n apiKey: this.config.apiKey,\n idempotencyKey: input.idempotencyKey,\n });\n const response = await this.config.fetch(`${this.config.baseUrl}/api/v1/poe/uploads`, {\n method: 'POST',\n headers,\n body: form,\n });\n await throwIfNotOk(response);\n return (await readJson(response)) as UploadsResponse;\n }\n\n /**\n * Submit a single finalised canonical-CBOR record to Cardano. Caller is\n * responsible for constructing the record bytes (use `publishContent` /\n * `publishSealed` / `publishMerkle` for the assisted flows) and for\n * acquiring a `quote_id` via `quote()` first.\n *\n * Returns 202 (`dedup_hit: false`) on freshly enqueued records, or 200\n * (`dedup_hit: true`) when the same record bytes were previously submitted\n * by this account. Dedup hits debit nothing.\n */\n async publish(input: PublishInput): Promise<PublishResponse> {\n const body: { record: string; quote_id: string; signatures?: ReadonlyArray<unknown> } = {\n record: toHex(input.record),\n quote_id: input.quoteId,\n };\n if (input.signatures !== undefined) body.signatures = input.signatures;\n const response = await this.config.fetch(`${this.config.baseUrl}/api/v1/poe/publish`, {\n method: 'POST',\n headers: buildJsonHeaders({\n apiKey: this.config.apiKey,\n idempotencyKey: input.idempotencyKey,\n }),\n body: JSON.stringify(body),\n });\n await throwIfNotOk(response);\n const parsed = (await readJson(response)) as Omit<PublishResponse, 'dedup_hit'>;\n return { ...parsed, dedup_hit: response.status === 200 };\n }\n\n /**\n * Submit 1..50 finalised records as independent Cardano transactions.\n * Each entry carries its own `quote_id` — request quotes ahead of time\n * with one `quote()` call per record. Returns 200 with `results[]` —\n * successful entries land alongside failed ones; per-record errors do NOT\n * roll back the batch.\n */\n async publishBatch(input: PublishBatchInput): Promise<PublishBatchResponse> {\n const body = {\n records: input.records.map((r) => ({\n record: toHex(r.record),\n quote_id: r.quoteId,\n ...(r.signatures !== undefined ? { signatures: r.signatures } : {}),\n })),\n };\n const response = await this.config.fetch(`${this.config.baseUrl}/api/v1/poe/publish-batch`, {\n method: 'POST',\n headers: buildJsonHeaders({\n apiKey: this.config.apiKey,\n idempotencyKey: input.idempotencyKey,\n }),\n body: JSON.stringify(body),\n });\n await throwIfNotOk(response);\n return (await readJson(response)) as PublishBatchResponse;\n }\n\n /**\n * High-level hash-only publish: hash the supplied content, build a\n * single-item CIP-309 record, optionally sign it with the caller-supplied\n * signer, and submit. No Arweave, no storage round-trip — anchors the\n * digest only.\n */\n async publishContent(input: PublishContentInput): Promise<PublishResponse> {\n return publishContentImpl(this.config as ResolvedPublishConfig, input);\n }\n\n /**\n * Hash-already-computed publish: caller already holds the digest(s) — e.g.\n * the CLI `--hash <hex>` mode, an air-gapped offline hashing flow, or any\n * pipeline that proxies digests from another tool. No client-side hashing.\n */\n async publishPrehashed(input: PublishPrehashedInput): Promise<PublishResponse> {\n return publishPrehashedImpl(this.config as ResolvedPublishConfig, input);\n }\n\n /**\n * Sealed-PoE: encrypt the supplied content to the recipient X25519 public\n * keys (age-style sealed envelope), upload the ciphertext to Arweave via\n * /uploads, build a CIP-309 record with the resulting `ar://` URI, sign\n * it (optional), and submit via /publish.\n *\n * The sender SHOULD include their own X25519 public key in `recipients`\n * to retain decrypt access — the SDK does NOT inject the sender silently.\n */\n async publishSealed(input: PublishSealedInput): Promise<PublishResponse> {\n return publishSealedImpl(this.config as ResolvedPublishConfig, input);\n }\n\n /**\n * Merkle batch publish: compute the RFC 9162 §2.1.1 root over N\n * caller-supplied 32-byte leaf hashes, upload the canonical leaves-list\n * CBOR to Arweave via /uploads, bind the root + leaf_count into\n * `merkle[0]` of an on-chain record, optionally sign, and submit.\n *\n * Returns the on-chain id + tx hash + root + leaf count + the canonical\n * `ar://<tx>` URI of the leaves-list. Anyone with that URI can later\n * fetch the leaves-list, recompute the root, and prove inclusion of any\n * leaf via `merkleSha2256VerifyInclusion`.\n */\n async publishMerkle(input: PublishMerkleInput): Promise<PublishMerkleResponse> {\n return publishMerkleImpl(this.config as ResolvedPublishConfig, input);\n }\n}\n","// `client.records.*` wraps the open-standard indexer read surface:\n//\n// GET /api/v1/records → records.list(input?)\n// GET /api/v1/records/{tx_hash} → records.get(txHash)\n// POST /api/v1/records/{tx_hash}/verify → records.verify(txHash, input)\n//\n// The PoE namespace owns the mutation methods (uploads, publish,\n// publishBatch + the high-level publishContent/publishSealed/publishMerkle\n// helpers); reads and verifications live here under Records — same tag\n// grouping the OpenAPI registry uses (`tags: ['Records']` on these\n// operationIds).\n//\n// Auth is optional: chain data is public. When an API key is configured the\n// SDK forwards it as `Authorization: Bearer …` so owner-only fields\n// (currently just `account_id`) surface for the caller's own rows, and so the\n// `sealed` list filter can resolve records addressed to the caller.\n\nimport { readJson, throwIfNotOk } from './http-helpers';\nimport type {\n FetchImpl,\n PoeVerifyInput,\n RecordResource,\n RecordsListInput,\n RecordsListResponse,\n} from './types';\nimport type { VerifyReport } from '../verifier/types';\n\ninterface ResolvedConfig {\n readonly apiKey: string | undefined;\n readonly baseUrl: string;\n readonly fetch: FetchImpl;\n}\n\nfunction buildHeaders(apiKey: string | undefined): Headers {\n const headers = new Headers({\n 'content-type': 'application/json',\n accept: 'application/json',\n });\n if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`);\n return headers;\n}\n\n/**\n * Derive the chain tip from a record page as `max(block_height +\n * num_confirmations - 1)` over the rows that carry a block height. Returns\n * `null` for an empty page or one with no anchored rows.\n */\nfunction deriveTipBlockHeight(records: ReadonlyArray<RecordResource>): number | null {\n let tip: number | null = null;\n for (const r of records) {\n if (r.block_height === null) continue;\n const candidate = r.block_height + r.num_confirmations - 1;\n tip = tip === null ? candidate : Math.max(tip, candidate);\n }\n return tip;\n}\n\nexport class RecordsNamespace {\n private readonly config: ResolvedConfig;\n\n constructor(config: ResolvedConfig) {\n this.config = config;\n }\n\n /**\n * List records as a paginated `RecordsListResponse` whose `data[]` entries\n * are the same `RecordResource` projection `get()` returns.\n *\n * Pass `{ sealed: true }` to restrict the page to sealed records addressed\n * to the authenticated caller (the gateway resolves the recipient from the\n * bearer identity); omit it to list every record the caller may read. Page\n * with `{ cursor: previous.next_cursor }` until `has_more` is false.\n */\n async list(input?: RecordsListInput): Promise<RecordsListResponse> {\n const params = new URLSearchParams();\n if (input?.sealed === true) params.set('sealed', 'true');\n if (input?.limit !== undefined) params.set('limit', String(input.limit));\n if (input?.cursor !== undefined && input.cursor !== null) {\n params.set('cursor', input.cursor);\n }\n const query = params.toString();\n const url = `${this.config.baseUrl}/api/v1/records${query === '' ? '' : `?${query}`}`;\n const response = await this.config.fetch(url, {\n method: 'GET',\n headers: buildHeaders(this.config.apiKey),\n });\n await throwIfNotOk(response);\n const page = (await readJson(response)) as RecordsListResponse;\n // A gateway that reports `tip_block_height` populates confirmation data\n // directly; otherwise derive it from the page rows so sealed-record sync\n // has a tip to compute confirmation depth against.\n if (page.tip_block_height === undefined || page.tip_block_height === null) {\n return { ...page, tip_block_height: deriveTipBlockHeight(page.data) };\n }\n return page;\n }\n\n /**\n * Fetch a record by Cardano transaction hash. Returns the JSON\n * `RecordResource` projection — same shape every `records.list` page entry\n * carries inside `data[]`.\n *\n * 404 (RecordNotFoundError) on tx_hashes the indexer has not seen, OR on\n * un-anchored rows when the caller is not their owner (oracle-safe\n * indistinguishable response per the route's privacy invariant).\n */\n async get(txHash: string): Promise<RecordResource> {\n const response = await this.config.fetch(\n `${this.config.baseUrl}/api/v1/records/${encodeURIComponent(txHash)}`,\n {\n method: 'GET',\n headers: buildHeaders(this.config.apiKey),\n },\n );\n await throwIfNotOk(response);\n return (await readJson(response)) as RecordResource;\n }\n\n /**\n * Run the canonical CIP-309 verifier against the record at `txHash`.\n * Returns the same `VerifyReport` shape the standalone verifier emits —\n * `VerifyReport` IS the wire body of this endpoint, with no transformer in\n * between.\n *\n * Auth required (Bearer with `poe:read` scope, or NextAuth session\n * cookie). Optional `verify_uris` toggles URI hash-equivalence checks;\n * `decryption[]` drives trial-decrypt of sealed envelopes per item.\n */\n async verify(txHash: string, input?: PoeVerifyInput): Promise<VerifyReport> {\n const response = await this.config.fetch(\n `${this.config.baseUrl}/api/v1/records/${encodeURIComponent(txHash)}/verify`,\n {\n method: 'POST',\n headers: buildHeaders(this.config.apiKey),\n body: JSON.stringify(input ?? {}),\n },\n );\n await throwIfNotOk(response);\n return (await readJson(response)) as VerifyReport;\n }\n}\n","import { AccountNamespace } from './account';\nimport { InvalidClientConfigError } from './invalid-client-config-error';\nimport { PoeNamespace } from './poe';\nimport { RecordsNamespace } from './records';\nimport type { Cip309ClientConfig, FetchImpl } from './types';\n\nfunction resolveFetch(provided: FetchImpl | undefined): FetchImpl {\n if (provided !== undefined) return provided;\n if (typeof globalThis.fetch === 'function') {\n // Bind to preserve `this` inside Node/browser fetch implementations.\n return globalThis.fetch.bind(globalThis);\n }\n throw new Error(\n 'Cip309Client: no fetch implementation available. Pass `fetch` in the config or run on a platform with globalThis.fetch.',\n );\n}\n\n/**\n * Resolves the gateway base URL the client targets.\n *\n * `baseUrl` is REQUIRED and is used verbatim — the client is gateway-agnostic\n * and binds to no particular deployment. A missing or empty `baseUrl` is a\n * configuration error: there is nowhere to send requests. Trailing slashes are\n * stripped so callers may pass `https://gw.example.com/` or\n * `https://gw.example.com` interchangeably.\n *\n * The `apiKey`, when present, is an OPAQUE bearer token forwarded verbatim as\n * `Authorization: Bearer <apiKey>`. It is never parsed, validated, or used to\n * infer the URL — any CIP-309 gateway may issue keys in its own format. Omit it\n * for anonymous read-only usage.\n */\nfunction resolveBaseUrl(config: Cip309ClientConfig): string {\n const baseUrl = config.baseUrl?.trim();\n if (baseUrl === undefined || baseUrl === '') {\n throw new InvalidClientConfigError(\n 'Cip309Client: baseUrl is required. Pass the base URL of the CIP-309 ' +\n 'gateway you are targeting (e.g. https://gateway.example.com).',\n );\n }\n return baseUrl.replace(/\\/$/, '');\n}\n\nexport class Cip309Client {\n public readonly poe: PoeNamespace;\n public readonly records: RecordsNamespace;\n public readonly account: AccountNamespace;\n\n /**\n * Construct a client against a CIP-309 gateway.\n *\n * `config.baseUrl` is required — there is no default deployment. The\n * `config.apiKey`, when supplied, is an opaque bearer token sent verbatim as\n * `Authorization: Bearer <apiKey>`; omit it for anonymous read-only access.\n *\n * PoE submissions debit the gateway's own balance model. Acquire a price lock\n * via `client.poe.quote(...)` first; the resulting `quote_id` is consumed by\n * the publish call.\n */\n constructor(config: Cip309ClientConfig) {\n const fetchImpl = resolveFetch(config.fetch);\n const baseUrl = resolveBaseUrl(config);\n const resolved = { apiKey: config.apiKey, baseUrl, fetch: fetchImpl };\n this.poe = new PoeNamespace(resolved);\n this.records = new RecordsNamespace(resolved);\n this.account = new AccountNamespace(resolved);\n }\n}\n"]}