@qntx/wallet-worker 0.7.0 → 0.8.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.
package/README.md CHANGED
@@ -27,6 +27,7 @@ const wallet = await createWebWallet({
27
27
  vaultSalt: "example/vault",
28
28
  },
29
29
  autoLock: { platform: "web", idleMinutes: 15 }, // 5 | 15 | 60; max session 12 h
30
+ verifyEvent, // verifies every event the kernel signs; pass your app's verifier
30
31
  });
31
32
 
32
33
  const created = await wallet.wallets.createWithPasskey({ name: "Example Wallet" });
@@ -50,6 +51,7 @@ import { createNodeWallet, createSoftwarePasskey } from "@qntx/wallet-worker/nod
50
51
  const wallet = await createNodeWallet({
51
52
  passkey: { rpId: "qntx.test", rpName: "test", walletSalt: "t/wallet", vaultSalt: "t/vault" },
52
53
  autoLock: { platform: "web", idleMinutes: 15 },
54
+ verifyEvent,
53
55
  authenticator: createSoftwarePasskey(seed),
54
56
  });
55
57
  ```
package/dist/browser.mjs CHANGED
@@ -224,6 +224,7 @@ async function createWebWallet(options) {
224
224
  factors: "software",
225
225
  spawn,
226
226
  passkey: options.passkey,
227
+ verifyEvent: options.verifyEvent,
227
228
  ceremony,
228
229
  setTimer: (delayMs, fn) => {
229
230
  const timer = globalThis.setTimeout(fn, delayMs);
@@ -1 +1 @@
1
- {"version":3,"file":"browser.mjs","names":[],"sources":["../src/browser/passkey.ts","../src/browser/index.ts"],"sourcesContent":["/**\n * The WebAuthn passkey ceremony (main thread only): `navigator.credentials` with the PRF extension.\n * A wallet passkey derives the wallet seed from `PRF(walletSalt)`; the vault factor derives the\n * vault KEK from `PRF(vaultSalt)` on the same credential. Every ceremony output is checked for\n * `prf.enabled`, PRF results and the user-verification flag — a credential that cannot prove UV\n * never reaches the kernel.\n */\nimport { WalletError } from \"@qntx/wallet\";\nimport { PASSKEY_PURPOSE_VAULT, PASSKEY_PURPOSE_WALLET } from \"@qntx/wallet/host\";\nimport type { PasskeyCeremony, PrfResult, PrfSalts } from \"@qntx/wallet/host\";\n\nexport type WebAuthnRp = {\n /** RP id — the origin's effective domain, e.g. `location.hostname`. */\n readonly id: string;\n readonly name: string;\n};\n\nconst UV_FLAG = 0x04; // authenticator-data flags byte (offset 32), bit 2 = user verified\nconst BE_FLAG = 0x08; // bit 3 = credential is backup-eligible (multi-device capable)\nconst BS_FLAG = 0x10; // bit 4 = credential is currently backed up (synced)\n\nfunction factorUnavailable(message: string): WalletError {\n return new WalletError(\"factor-unavailable\", message);\n}\n\nfunction cancelled(message: string): WalletError {\n return new WalletError(\"cancelled\", message);\n}\n\nfunction mapCeremonyError(error: unknown): never {\n if (\n error instanceof DOMException &&\n (error.name === \"NotAllowedError\" || error.name === \"AbortError\")\n ) {\n throw cancelled(\"the passkey ceremony was cancelled\");\n }\n if (error instanceof WalletError) {\n throw error;\n }\n throw factorUnavailable(\"the passkey ceremony failed\");\n}\n\n/** The UV bit of an authenticator-data blob (flags byte at offset 32, bit 0x04). */\nfunction userVerified(authenticatorData: ArrayBuffer): boolean {\n return (\n authenticatorData.byteLength > 32 &&\n // oxlint-disable-next-line eslint/no-bitwise -- flags bit test\n (new DataView(authenticatorData).getUint8(32) & UV_FLAG) !== 0\n );\n}\n\n/**\n * Whether the credential is backed up off-device (flags byte at offset 32): synced requires both BE\n * (the credential is eligible for backup) and BS (the authenticator actually backs it up).\n */\nfunction credentialSynced(authenticatorData: ArrayBuffer): boolean {\n return (\n authenticatorData.byteLength > 32 &&\n // oxlint-disable-next-line eslint/no-bitwise -- flags bit test\n (new DataView(authenticatorData).getUint8(32) & (BE_FLAG | BS_FLAG)) === (BE_FLAG | BS_FLAG)\n );\n}\n\nfunction prfEval(salts: PrfSalts): AuthenticationExtensionsPRFValues {\n // The constructor copy narrows Uint8Array<ArrayBufferLike> to Uint8Array<ArrayBuffer> —\n // the DOM BufferSource does not admit shared-array-buffer views.\n return salts.second === undefined\n ? { first: new Uint8Array(salts.first) }\n : { first: new Uint8Array(salts.first), second: new Uint8Array(salts.second) };\n}\n\n/** A detached copy of a BufferSource — the returned Uint8Array owns its own fresh buffer. */\nfunction copyBufferSource(source: BufferSource): Uint8Array {\n const view =\n source instanceof ArrayBuffer\n ? new Uint8Array(source)\n : new Uint8Array(source.buffer, source.byteOffset, source.byteLength);\n return new Uint8Array(view);\n}\n\nfunction readPrfResult(\n credential: PublicKeyCredential,\n results: AuthenticationExtensionsPRFValues | undefined,\n wantSecond: boolean,\n synced: boolean,\n userHandle?: ArrayBuffer,\n): PrfResult {\n const first = results?.first;\n const second = results?.second;\n if (first === undefined || (wantSecond && second === undefined)) {\n throw factorUnavailable(\"the passkey produced no PRF output\");\n }\n // Copy into fresh Uint8Arrays and drop every reference to the extension's ArrayBuffers.\n return {\n credentialId: copyBufferSource(credential.rawId),\n first: copyBufferSource(first),\n ...(second === undefined ? {} : { second: copyBufferSource(second) }),\n ...(userHandle === undefined ? {} : { userHandle: copyBufferSource(userHandle) }),\n synced,\n };\n}\n\nfunction checkCredential(credential: unknown): PublicKeyCredential {\n if (!(credential instanceof PublicKeyCredential)) {\n throw factorUnavailable(\"the authenticator returned no public-key credential\");\n }\n return credential;\n}\n\n/**\n * The WebAuthn ceremony. `create` registers a resident credential with `prf.eval` over the salts;\n * when the extension returns no results a single assertion over the new credential produces them.\n * `assert` runs a discoverable (or allowCredentials-scoped) get with the same eval shape.\n */\nexport function createWebAuthnPasskey(rp: WebAuthnRp): PasskeyCeremony {\n async function assert(input: {\n readonly credentialIds?: ReadonlyArray<Uint8Array>;\n readonly salts: PrfSalts;\n }): Promise<PrfResult> {\n let credential: PublicKeyCredential;\n try {\n credential = checkCredential(\n await navigator.credentials.get({\n publicKey: {\n rpId: rp.id,\n challenge: crypto.getRandomValues(new Uint8Array(32)),\n userVerification: \"required\",\n ...(input.credentialIds === undefined\n ? {}\n : {\n allowCredentials: input.credentialIds.map((id) => ({\n type: \"public-key\" as const,\n id: new Uint8Array(id),\n })),\n }),\n extensions: { prf: { eval: prfEval(input.salts) } },\n },\n }),\n );\n } catch (error) {\n mapCeremonyError(error);\n }\n const { response } = credential;\n if (!(response instanceof AuthenticatorAssertionResponse)) {\n throw factorUnavailable(\"the authenticator returned no assertion response\");\n }\n if (!userVerified(response.authenticatorData)) {\n throw factorUnavailable(\"the passkey assertion was not user-verified\");\n }\n return readPrfResult(\n credential,\n credential.getClientExtensionResults().prf?.results,\n input.salts.second !== undefined,\n credentialSynced(response.authenticatorData),\n response.userHandle ?? undefined,\n );\n }\n\n return {\n create: async (input) => {\n const userId = new Uint8Array(16);\n userId[0] = input.purpose === \"wallet\" ? PASSKEY_PURPOSE_WALLET : PASSKEY_PURPOSE_VAULT;\n crypto.getRandomValues(userId.subarray(1));\n let credential: PublicKeyCredential;\n try {\n credential = checkCredential(\n await navigator.credentials.create({\n publicKey: {\n rp: { id: rp.id, name: rp.name },\n user: {\n id: userId,\n name: input.name,\n displayName: input.name,\n },\n challenge: crypto.getRandomValues(new Uint8Array(32)),\n pubKeyCredParams: [\n { type: \"public-key\", alg: -7 },\n { type: \"public-key\", alg: -257 },\n ],\n authenticatorSelection: {\n residentKey: \"required\",\n userVerification: \"required\",\n },\n extensions: { prf: { eval: prfEval(input.salts) } },\n },\n }),\n );\n } catch (error) {\n mapCeremonyError(error);\n }\n const { response } = credential;\n if (!(response instanceof AuthenticatorAttestationResponse)) {\n throw factorUnavailable(\"the authenticator returned no attestation response\");\n }\n const extensions = credential.getClientExtensionResults();\n if (extensions.prf?.enabled !== true) {\n throw factorUnavailable(\"the authenticator does not support the PRF extension\");\n }\n if (extensions.prf.results === undefined) {\n // Some authenticators omit creation-time PRF outputs: one assertion over the new\n // credential produces them (it is that response whose UV and BE/BS flags get read).\n return assert({\n credentialIds: [copyBufferSource(credential.rawId)],\n salts: input.salts,\n });\n }\n const authenticatorData = response.getAuthenticatorData();\n if (!userVerified(authenticatorData)) {\n throw factorUnavailable(\"the passkey creation was not user-verified\");\n }\n return readPrfResult(\n credential,\n extensions.prf.results,\n input.salts.second !== undefined,\n credentialSynced(authenticatorData),\n );\n },\n assert,\n };\n}\n","/**\n * The Web host: `createWebWallet` spawns the bundled kernel worker (dist/kernel.worker.js) whose\n * vault lives in the worker's own IndexedDB. Lock destroys the worker; the vault survives in\n * IndexedDB, so the respawned worker reloads it. `pagehide` locks immediately; the idle timer is\n * refreshed by user-presence signals, not request traffic. The WebAuthn ceremony runs on the main\n * thread.\n */\nimport { WalletError } from \"@qntx/wallet\";\nimport type { Wallet, WalletOptions } from \"@qntx/wallet\";\nimport { connectKernel } from \"@qntx/wallet/host\";\nimport type { MainPort } from \"@qntx/wallet/host\";\nimport type { ProtocolValue } from \"@qntx/wallet/protocol\";\n\nimport type { BrowserWorkerInit } from \"../shared.ts\";\nimport { createWebAuthnPasskey } from \"./passkey.ts\";\n\nconst LISTENER_OPTIONS = { capture: true, passive: true };\nconst ACTIVITY_EVENTS = [\"pointerdown\", \"pointermove\", \"keydown\", \"wheel\", \"touchstart\"];\n\n/**\n * The browser's user-presence source: direct input on the page, or returning to a visible tab.\n * Capture-phase passive listeners do a single boolean check per event — high-frequency signals\n * (scroll, pointermove) cost nothing. Returns the unsubscribe the wallet owns.\n */\nfunction subscribePresence(onActivity: () => void): () => void {\n for (const type of ACTIVITY_EVENTS) {\n globalThis.addEventListener(type, onActivity, LISTENER_OPTIONS);\n }\n const onVisible = (): void => {\n if (document.visibilityState === \"visible\") {\n onActivity();\n }\n };\n document.addEventListener(\"visibilitychange\", onVisible);\n return () => {\n for (const type of ACTIVITY_EVENTS) {\n globalThis.removeEventListener(type, onActivity, LISTENER_OPTIONS);\n }\n document.removeEventListener(\"visibilitychange\", onVisible);\n };\n}\n\nasync function bestEffort(task: () => Promise<unknown>): Promise<void> {\n try {\n await task();\n } catch {\n // Lifecycle hooks and persistence hints must never surface failures.\n }\n}\n\nexport async function createWebWallet(options: WalletOptions): Promise<Wallet> {\n const { autoLock } = options;\n if (autoLock.platform !== \"web\") {\n throw new WalletError(\"invalid-input\", \"createWebWallet accepts only the web autoLock policy\");\n }\n // The page sends only the idle choice; the worker owns the maximum session duration.\n const init: BrowserWorkerInit = { t: \"init\", idleMinutes: autoLock.idleMinutes };\n const ceremony = createWebAuthnPasskey({\n id: options.passkey.rpId,\n name: options.passkey.rpName,\n });\n\n const spawn = (): MainPort => {\n // Keep this exact `new Worker(new URL(\"…\", import.meta.url))` shape: bundlers (Vite,\n // webpack, Parcel, esbuild-based tools) recognize only this literal form as a worker entry and\n // bundle the kernel with its chunks and spark.wasm. A precomputed URL is copied as one opaque\n // asset, and the worker then dies loading its first chunk.\n const worker = new Worker(new URL(\"kernel.worker.js\", import.meta.url), {\n type: \"module\",\n name: \"qntx-wallet\",\n });\n // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Worker.postMessage takes a transfer list, not an origin\n worker.postMessage(init); // the kernel validates it strictly before startKernel\n return {\n post: (message: ProtocolValue, transfer: ArrayBuffer[]) => {\n worker.postMessage(message, transfer);\n },\n listen: (handler) => {\n const listener = (event: MessageEvent): void => {\n handler(event.data);\n };\n worker.addEventListener(\"message\", listener);\n return () => {\n worker.removeEventListener(\"message\", listener);\n };\n },\n onExit: (handler) => {\n // Web Workers have no exit event; error/messageerror are the only death signals.\n const listener = (): void => {\n handler(\"crashed\");\n };\n worker.addEventListener(\"error\", listener);\n worker.addEventListener(\"messageerror\", listener);\n return () => {\n worker.removeEventListener(\"error\", listener);\n worker.removeEventListener(\"messageerror\", listener);\n };\n },\n terminate: () => {\n worker.terminate();\n },\n };\n };\n\n const wallet = await connectKernel({\n factors: \"software\",\n spawn,\n passkey: options.passkey,\n ceremony,\n setTimer: (delayMs, fn) => {\n const timer = globalThis.setTimeout(fn, delayMs);\n return () => {\n globalThis.clearTimeout(timer);\n };\n },\n presence: subscribePresence,\n onDestroy: () => {\n globalThis.removeEventListener(\"pagehide\", onPageHide);\n },\n });\n\n // Page lifecycle: lock on pagehide. A BFCache-frozen page still runs the queued locked-event\n // handler on restore, so the wallet respawns then — terminating the worker directly would\n // leave the wallet pointing at a dead port.\n const onPageHide = (): void => {\n void bestEffort(async () => wallet.vault.lock());\n };\n globalThis.addEventListener(\"pagehide\", onPageHide);\n\n // Ask the browser to persist the vault once it exists — a denied request is fine.\n let persisted = false;\n const unsubscribe = wallet.subscribe(() => {\n if (!persisted && wallet.getSnapshot().vault !== \"uninitialized\") {\n persisted = true;\n unsubscribe();\n void bestEffort(async () => navigator.storage.persist());\n }\n });\n\n return wallet;\n}\n\nexport { createWebAuthnPasskey } from \"./passkey.ts\";\nexport type { WebAuthnRp } from \"./passkey.ts\";\n"],"mappings":";;;;;;;;;;AAiBA,MAAM,UAAU;AAIhB,SAAS,kBAAkB,SAA8B;CACvD,OAAO,IAAI,YAAY,sBAAsB,OAAO;AACtD;AAEA,SAAS,UAAU,SAA8B;CAC/C,OAAO,IAAI,YAAY,aAAa,OAAO;AAC7C;AAEA,SAAS,iBAAiB,OAAuB;CAC/C,IACE,iBAAiB,iBAChB,MAAM,SAAS,qBAAqB,MAAM,SAAS,eAEpD,MAAM,UAAU,oCAAoC;CAEtD,IAAI,iBAAiB,aACnB,MAAM;CAER,MAAM,kBAAkB,6BAA6B;AACvD;;AAGA,SAAS,aAAa,mBAAyC;CAC7D,OACE,kBAAkB,aAAa,OAE9B,IAAI,SAAS,iBAAiB,CAAC,CAAC,SAAS,EAAE,IAAI,aAAa;AAEjE;;;;;AAMA,SAAS,iBAAiB,mBAAyC;CACjE,OACE,kBAAkB,aAAa,OAE9B,IAAI,SAAS,iBAAiB,CAAC,CAAC,SAAS,EAAE,IAAK,QAAyB;AAE9E;AAEA,SAAS,QAAQ,OAAoD;CAGnE,OAAO,MAAM,WAAW,KAAA,IACpB,EAAE,OAAO,IAAI,WAAW,MAAM,KAAK,EAAE,IACrC;EAAE,OAAO,IAAI,WAAW,MAAM,KAAK;EAAG,QAAQ,IAAI,WAAW,MAAM,MAAM;CAAE;AACjF;;AAGA,SAAS,iBAAiB,QAAkC;CAC1D,MAAM,OACJ,kBAAkB,cACd,IAAI,WAAW,MAA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B,WAAW,QAAQ;IAC3C,aAAa;KACX,OAAO,oBAAoB,WAAW,QAAQ;IAChD;GACF;GACA,SAAS,YAAY;IAEnB,MAAM,iBAAuB;KAC3B,QAAQ,SAAS;IACnB;IACA,OAAO,iBAAiB,SAAS,QAAQ;IACzC,OAAO,iBAAiB,gBAAgB,QAAQ;IAChD,aAAa;KACX,OAAO,oBAAoB,SAAS,QAAQ;KAC5C,OAAO,oBAAoB,gBAAgB,QAAQ;IACrD;GACF;GACA,iBAAiB;IACf,OAAO,UAAU;GACnB;EACF;CACF;CAEA,MAAM,SAAS,MAAM,cAAc;EACjC,SAAS;EACT;EACA,SAAS,QAAQ;EACjB;EACA,WAAW,SAAS,OAAO;GACzB,MAAM,QAAQ,WAAW,WAAW,IAAI,OAAO;GAC/C,aAAa;IACX,WAAW,aAAa,KAAK;GAC/B;EACF;EACA,UAAU;EACV,iBAAiB;GACf,WAAW,oBAAoB,YAAY,UAAU;EACvD;CACF,CAAC;CAKD,MAAM,mBAAyB;EAC7B,WAAgB,YAAY,OAAO,MAAM,KAAK,CAAC;CACjD;CACA,WAAW,iBAAiB,YAAY,UAAU;CAGlD,IAAI,YAAY;CAChB,MAAM,cAAc,OAAO,gBAAgB;EACzC,IAAI,CAAC,aAAa,OAAO,YAAY,CAAC,CAAC,UAAU,iBAAiB;GAChE,YAAY;GACZ,YAAY;GACZ,WAAgB,YAAY,UAAU,QAAQ,QAAQ,CAAC;EACzD;CACF,CAAC;CAED,OAAO;AACT"}
1
+ {"version":3,"file":"browser.mjs","names":[],"sources":["../src/browser/passkey.ts","../src/browser/index.ts"],"sourcesContent":["/**\n * The WebAuthn passkey ceremony (main thread only): `navigator.credentials` with the PRF extension.\n * A wallet passkey derives the wallet seed from `PRF(walletSalt)`; the vault factor derives the\n * vault KEK from `PRF(vaultSalt)` on the same credential. Every ceremony output is checked for\n * `prf.enabled`, PRF results and the user-verification flag — a credential that cannot prove UV\n * never reaches the kernel.\n */\nimport { WalletError } from \"@qntx/wallet\";\nimport { PASSKEY_PURPOSE_VAULT, PASSKEY_PURPOSE_WALLET } from \"@qntx/wallet/host\";\nimport type { PasskeyCeremony, PrfResult, PrfSalts } from \"@qntx/wallet/host\";\n\nexport type WebAuthnRp = {\n /** RP id — the origin's effective domain, e.g. `location.hostname`. */\n readonly id: string;\n readonly name: string;\n};\n\nconst UV_FLAG = 0x04; // authenticator-data flags byte (offset 32), bit 2 = user verified\nconst BE_FLAG = 0x08; // bit 3 = credential is backup-eligible (multi-device capable)\nconst BS_FLAG = 0x10; // bit 4 = credential is currently backed up (synced)\n\nfunction factorUnavailable(message: string): WalletError {\n return new WalletError(\"factor-unavailable\", message);\n}\n\nfunction cancelled(message: string): WalletError {\n return new WalletError(\"cancelled\", message);\n}\n\nfunction mapCeremonyError(error: unknown): never {\n if (\n error instanceof DOMException &&\n (error.name === \"NotAllowedError\" || error.name === \"AbortError\")\n ) {\n throw cancelled(\"the passkey ceremony was cancelled\");\n }\n if (error instanceof WalletError) {\n throw error;\n }\n throw factorUnavailable(\"the passkey ceremony failed\");\n}\n\n/** The UV bit of an authenticator-data blob (flags byte at offset 32, bit 0x04). */\nfunction userVerified(authenticatorData: ArrayBuffer): boolean {\n return (\n authenticatorData.byteLength > 32 &&\n // oxlint-disable-next-line eslint/no-bitwise -- flags bit test\n (new DataView(authenticatorData).getUint8(32) & UV_FLAG) !== 0\n );\n}\n\n/**\n * Whether the credential is backed up off-device (flags byte at offset 32): synced requires both BE\n * (the credential is eligible for backup) and BS (the authenticator actually backs it up).\n */\nfunction credentialSynced(authenticatorData: ArrayBuffer): boolean {\n return (\n authenticatorData.byteLength > 32 &&\n // oxlint-disable-next-line eslint/no-bitwise -- flags bit test\n (new DataView(authenticatorData).getUint8(32) & (BE_FLAG | BS_FLAG)) === (BE_FLAG | BS_FLAG)\n );\n}\n\nfunction prfEval(salts: PrfSalts): AuthenticationExtensionsPRFValues {\n // The constructor copy narrows Uint8Array<ArrayBufferLike> to Uint8Array<ArrayBuffer> —\n // the DOM BufferSource does not admit shared-array-buffer views.\n return salts.second === undefined\n ? { first: new Uint8Array(salts.first) }\n : { first: new Uint8Array(salts.first), second: new Uint8Array(salts.second) };\n}\n\n/** A detached copy of a BufferSource — the returned Uint8Array owns its own fresh buffer. */\nfunction copyBufferSource(source: BufferSource): Uint8Array {\n const view =\n source instanceof ArrayBuffer\n ? new Uint8Array(source)\n : new Uint8Array(source.buffer, source.byteOffset, source.byteLength);\n return new Uint8Array(view);\n}\n\nfunction readPrfResult(\n credential: PublicKeyCredential,\n results: AuthenticationExtensionsPRFValues | undefined,\n wantSecond: boolean,\n synced: boolean,\n userHandle?: ArrayBuffer,\n): PrfResult {\n const first = results?.first;\n const second = results?.second;\n if (first === undefined || (wantSecond && second === undefined)) {\n throw factorUnavailable(\"the passkey produced no PRF output\");\n }\n // Copy into fresh Uint8Arrays and drop every reference to the extension's ArrayBuffers.\n return {\n credentialId: copyBufferSource(credential.rawId),\n first: copyBufferSource(first),\n ...(second === undefined ? {} : { second: copyBufferSource(second) }),\n ...(userHandle === undefined ? {} : { userHandle: copyBufferSource(userHandle) }),\n synced,\n };\n}\n\nfunction checkCredential(credential: unknown): PublicKeyCredential {\n if (!(credential instanceof PublicKeyCredential)) {\n throw factorUnavailable(\"the authenticator returned no public-key credential\");\n }\n return credential;\n}\n\n/**\n * The WebAuthn ceremony. `create` registers a resident credential with `prf.eval` over the salts;\n * when the extension returns no results a single assertion over the new credential produces them.\n * `assert` runs a discoverable (or allowCredentials-scoped) get with the same eval shape.\n */\nexport function createWebAuthnPasskey(rp: WebAuthnRp): PasskeyCeremony {\n async function assert(input: {\n readonly credentialIds?: ReadonlyArray<Uint8Array>;\n readonly salts: PrfSalts;\n }): Promise<PrfResult> {\n let credential: PublicKeyCredential;\n try {\n credential = checkCredential(\n await navigator.credentials.get({\n publicKey: {\n rpId: rp.id,\n challenge: crypto.getRandomValues(new Uint8Array(32)),\n userVerification: \"required\",\n ...(input.credentialIds === undefined\n ? {}\n : {\n allowCredentials: input.credentialIds.map((id) => ({\n type: \"public-key\" as const,\n id: new Uint8Array(id),\n })),\n }),\n extensions: { prf: { eval: prfEval(input.salts) } },\n },\n }),\n );\n } catch (error) {\n mapCeremonyError(error);\n }\n const { response } = credential;\n if (!(response instanceof AuthenticatorAssertionResponse)) {\n throw factorUnavailable(\"the authenticator returned no assertion response\");\n }\n if (!userVerified(response.authenticatorData)) {\n throw factorUnavailable(\"the passkey assertion was not user-verified\");\n }\n return readPrfResult(\n credential,\n credential.getClientExtensionResults().prf?.results,\n input.salts.second !== undefined,\n credentialSynced(response.authenticatorData),\n response.userHandle ?? undefined,\n );\n }\n\n return {\n create: async (input) => {\n const userId = new Uint8Array(16);\n userId[0] = input.purpose === \"wallet\" ? PASSKEY_PURPOSE_WALLET : PASSKEY_PURPOSE_VAULT;\n crypto.getRandomValues(userId.subarray(1));\n let credential: PublicKeyCredential;\n try {\n credential = checkCredential(\n await navigator.credentials.create({\n publicKey: {\n rp: { id: rp.id, name: rp.name },\n user: {\n id: userId,\n name: input.name,\n displayName: input.name,\n },\n challenge: crypto.getRandomValues(new Uint8Array(32)),\n pubKeyCredParams: [\n { type: \"public-key\", alg: -7 },\n { type: \"public-key\", alg: -257 },\n ],\n authenticatorSelection: {\n residentKey: \"required\",\n userVerification: \"required\",\n },\n extensions: { prf: { eval: prfEval(input.salts) } },\n },\n }),\n );\n } catch (error) {\n mapCeremonyError(error);\n }\n const { response } = credential;\n if (!(response instanceof AuthenticatorAttestationResponse)) {\n throw factorUnavailable(\"the authenticator returned no attestation response\");\n }\n const extensions = credential.getClientExtensionResults();\n if (extensions.prf?.enabled !== true) {\n throw factorUnavailable(\"the authenticator does not support the PRF extension\");\n }\n if (extensions.prf.results === undefined) {\n // Some authenticators omit creation-time PRF outputs: one assertion over the new\n // credential produces them (it is that response whose UV and BE/BS flags get read).\n return assert({\n credentialIds: [copyBufferSource(credential.rawId)],\n salts: input.salts,\n });\n }\n const authenticatorData = response.getAuthenticatorData();\n if (!userVerified(authenticatorData)) {\n throw factorUnavailable(\"the passkey creation was not user-verified\");\n }\n return readPrfResult(\n credential,\n extensions.prf.results,\n input.salts.second !== undefined,\n credentialSynced(authenticatorData),\n );\n },\n assert,\n };\n}\n","/**\n * The Web host: `createWebWallet` spawns the bundled kernel worker (dist/kernel.worker.js) whose\n * vault lives in the worker's own IndexedDB. Lock destroys the worker; the vault survives in\n * IndexedDB, so the respawned worker reloads it. `pagehide` locks immediately; the idle timer is\n * refreshed by user-presence signals, not request traffic. The WebAuthn ceremony runs on the main\n * thread.\n */\nimport { WalletError } from \"@qntx/wallet\";\nimport type { Wallet, WalletOptions } from \"@qntx/wallet\";\nimport { connectKernel } from \"@qntx/wallet/host\";\nimport type { MainPort } from \"@qntx/wallet/host\";\nimport type { ProtocolValue } from \"@qntx/wallet/protocol\";\n\nimport type { BrowserWorkerInit } from \"../shared.ts\";\nimport { createWebAuthnPasskey } from \"./passkey.ts\";\n\nconst LISTENER_OPTIONS = { capture: true, passive: true };\nconst ACTIVITY_EVENTS = [\"pointerdown\", \"pointermove\", \"keydown\", \"wheel\", \"touchstart\"];\n\n/**\n * The browser's user-presence source: direct input on the page, or returning to a visible tab.\n * Capture-phase passive listeners do a single boolean check per event — high-frequency signals\n * (scroll, pointermove) cost nothing. Returns the unsubscribe the wallet owns.\n */\nfunction subscribePresence(onActivity: () => void): () => void {\n for (const type of ACTIVITY_EVENTS) {\n globalThis.addEventListener(type, onActivity, LISTENER_OPTIONS);\n }\n const onVisible = (): void => {\n if (document.visibilityState === \"visible\") {\n onActivity();\n }\n };\n document.addEventListener(\"visibilitychange\", onVisible);\n return () => {\n for (const type of ACTIVITY_EVENTS) {\n globalThis.removeEventListener(type, onActivity, LISTENER_OPTIONS);\n }\n document.removeEventListener(\"visibilitychange\", onVisible);\n };\n}\n\nasync function bestEffort(task: () => Promise<unknown>): Promise<void> {\n try {\n await task();\n } catch {\n // Lifecycle hooks and persistence hints must never surface failures.\n }\n}\n\nexport async function createWebWallet(options: WalletOptions): Promise<Wallet> {\n const { autoLock } = options;\n if (autoLock.platform !== \"web\") {\n throw new WalletError(\"invalid-input\", \"createWebWallet accepts only the web autoLock policy\");\n }\n // The page sends only the idle choice; the worker owns the maximum session duration.\n const init: BrowserWorkerInit = { t: \"init\", idleMinutes: autoLock.idleMinutes };\n const ceremony = createWebAuthnPasskey({\n id: options.passkey.rpId,\n name: options.passkey.rpName,\n });\n\n const spawn = (): MainPort => {\n // Keep this exact `new Worker(new URL(\"…\", import.meta.url))` shape: bundlers (Vite,\n // webpack, Parcel, esbuild-based tools) recognize only this literal form as a worker entry and\n // bundle the kernel with its chunks and spark.wasm. A precomputed URL is copied as one opaque\n // asset, and the worker then dies loading its first chunk.\n const worker = new Worker(new URL(\"kernel.worker.js\", import.meta.url), {\n type: \"module\",\n name: \"qntx-wallet\",\n });\n // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Worker.postMessage takes a transfer list, not an origin\n worker.postMessage(init); // the kernel validates it strictly before startKernel\n return {\n post: (message: ProtocolValue, transfer: ArrayBuffer[]) => {\n worker.postMessage(message, transfer);\n },\n listen: (handler) => {\n const listener = (event: MessageEvent): void => {\n handler(event.data);\n };\n worker.addEventListener(\"message\", listener);\n return () => {\n worker.removeEventListener(\"message\", listener);\n };\n },\n onExit: (handler) => {\n // Web Workers have no exit event; error/messageerror are the only death signals.\n const listener = (): void => {\n handler(\"crashed\");\n };\n worker.addEventListener(\"error\", listener);\n worker.addEventListener(\"messageerror\", listener);\n return () => {\n worker.removeEventListener(\"error\", listener);\n worker.removeEventListener(\"messageerror\", listener);\n };\n },\n terminate: () => {\n worker.terminate();\n },\n };\n };\n\n const wallet = await connectKernel({\n factors: \"software\",\n spawn,\n passkey: options.passkey,\n verifyEvent: options.verifyEvent,\n ceremony,\n setTimer: (delayMs, fn) => {\n const timer = globalThis.setTimeout(fn, delayMs);\n return () => {\n globalThis.clearTimeout(timer);\n };\n },\n presence: subscribePresence,\n onDestroy: () => {\n globalThis.removeEventListener(\"pagehide\", onPageHide);\n },\n });\n\n // Page lifecycle: lock on pagehide. A BFCache-frozen page still runs the queued locked-event\n // handler on restore, so the wallet respawns then — terminating the worker directly would\n // leave the wallet pointing at a dead port.\n const onPageHide = (): void => {\n void bestEffort(async () => wallet.vault.lock());\n };\n globalThis.addEventListener(\"pagehide\", onPageHide);\n\n // Ask the browser to persist the vault once it exists — a denied request is fine.\n let persisted = false;\n const unsubscribe = wallet.subscribe(() => {\n if (!persisted && wallet.getSnapshot().vault !== \"uninitialized\") {\n persisted = true;\n unsubscribe();\n void bestEffort(async () => navigator.storage.persist());\n }\n });\n\n return wallet;\n}\n\nexport { createWebAuthnPasskey } from \"./passkey.ts\";\nexport type { WebAuthnRp } from \"./passkey.ts\";\n"],"mappings":";;;;;;;;;;AAiBA,MAAM,UAAU;AAIhB,SAAS,kBAAkB,SAA8B;CACvD,OAAO,IAAI,YAAY,sBAAsB,OAAO;AACtD;AAEA,SAAS,UAAU,SAA8B;CAC/C,OAAO,IAAI,YAAY,aAAa,OAAO;AAC7C;AAEA,SAAS,iBAAiB,OAAuB;CAC/C,IACE,iBAAiB,iBAChB,MAAM,SAAS,qBAAqB,MAAM,SAAS,eAEpD,MAAM,UAAU,oCAAoC;CAEtD,IAAI,iBAAiB,aACnB,MAAM;CAER,MAAM,kBAAkB,6BAA6B;AACvD;;AAGA,SAAS,aAAa,mBAAyC;CAC7D,OACE,kBAAkB,aAAa,OAE9B,IAAI,SAAS,iBAAiB,CAAC,CAAC,SAAS,EAAE,IAAI,aAAa;AAEjE;;;;;AAMA,SAAS,iBAAiB,mBAAyC;CACjE,OACE,kBAAkB,aAAa,OAE9B,IAAI,SAAS,iBAAiB,CAAC,CAAC,SAAS,EAAE,IAAK,QAAyB;AAE9E;AAEA,SAAS,QAAQ,OAAoD;CAGnE,OAAO,MAAM,WAAW,KAAA,IACpB,EAAE,OAAO,IAAI,WAAW,MAAM,KAAK,EAAE,IACrC;EAAE,OAAO,IAAI,WAAW,MAAM,KAAK;EAA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file": "browser-1vpxmqwn.js",
70
74
  "sha256": "e5afdeec6f071a87cb6a1040ddeb1b9995d53ed2cea239f8f9424dc1d3df601b"
@@ -74,27 +78,23 @@
74
78
  "sha256": "e25059b920fe661a3c6624032a69c40dc001d1b5829a5f54b7f23db52387f6c7"
75
79
  },
76
80
  {
77
- "file": "worker-366p4fej.js",
78
- "sha256": "449410efad818a8f1dcfd02840375c1748c61294ad172e4c6c57e9d1d7c81283"
81
+ "file": "node-gsare3mw.js",
82
+ "sha256": "163a8aeb01c8d699a7567eecb90c6d5c859151d1e7322c3be0487eb13940b9c7"
79
83
  },
80
84
  {
81
- "file": "kernel.worker.js",
82
- "sha256": "d51780e0e77202b349d0b2966222877b11e7561f8719d89af4d7ef62a5e6bc42"
85
+ "file": "worker-366p4fej.js",
86
+ "sha256": "449410efad818a8f1dcfd02840375c1748c61294ad172e4c6c57e9d1d7c81283"
83
87
  },
84
88
  {
85
89
  "file": "signer-gs57sjgx.js",
86
90
  "sha256": "e83ab645d9efc41a821e572768e4b28a7902336ccfa692c2101755284b2a7d42"
87
91
  },
88
- {
89
- "file": "worker-gsare3mw.js",
90
- "sha256": "163a8aeb01c8d699a7567eecb90c6d5c859151d1e7322c3be0487eb13940b9c7"
91
- },
92
92
  {
93
93
  "file": "node-366p4fej.js",
94
94
  "sha256": "449410efad818a8f1dcfd02840375c1748c61294ad172e4c6c57e9d1d7c81283"
95
95
  },
96
96
  {
97
- "file": "node-gsare3mw.js",
97
+ "file": "worker-gsare3mw.js",
98
98
  "sha256": "163a8aeb01c8d699a7567eecb90c6d5c859151d1e7322c3be0487eb13940b9c7"
99
99
  }
100
100
  ]
package/dist/node.d.mts CHANGED
@@ -1,12 +1,10 @@
1
- import { AutoLockPolicy, PasskeyConfig, Wallet } from "@qntx/wallet";
1
+ import { Wallet, WalletOptions } from "@qntx/wallet";
2
2
  import { PasskeyCeremony } from "@qntx/wallet/host";
3
3
  import { Worker } from "node:worker_threads";
4
4
  export declare function createSoftwarePasskey(seed: Uint8Array, options?: {
5
5
  readonly synced?: boolean;
6
6
  }): PasskeyCeremony;
7
- export type NodeWalletOptions = {
8
- readonly passkey: PasskeyConfig;
9
- readonly autoLock: AutoLockPolicy;
7
+ export type NodeWalletOptions = WalletOptions & {
10
8
  readonly authenticator?: PasskeyCeremony;
11
9
  /**
12
10
  * Test/debug hook: the worker file location and a spawn observer (count spawns, watch 'exit').
package/dist/node.mjs CHANGED
@@ -347,6 +347,7 @@ async function createNodeWallet(options) {
347
347
  factors: "software",
348
348
  spawn,
349
349
  passkey: options.passkey,
350
+ verifyEvent: options.verifyEvent,
350
351
  ceremony: options.authenticator,
351
352
  setTimer: (delayMs, fn) => {
352
353
  const timer = setTimeout(fn, delayMs);
package/dist/node.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"node.mjs","names":[],"sources":["../../wallet-kernel/src/errors.ts","../../wallet-kernel/src/vault/memory.ts","../src/shared.ts","../src/node/store-server.ts","../src/node/software-passkey.ts","../src/node/index.ts"],"sourcesContent":["import type { WalletErrorCode } from \"@qntx/wallet/types\";\n\nexport type KernelErrorCode = WalletErrorCode;\n\n/**\n * The only error type kernel code throws. Its code is the public `WalletErrorCode`: the kernel\n * returns it verbatim in the error payload. Messages are static strings: they must never echo user\n * input or secret contents.\n */\nexport class KernelError extends Error {\n readonly code: KernelErrorCode;\n readonly retryAt: number | undefined;\n /**\n * True when the error proves the stored vault itself is corrupt — the kernel latches the\n * corrupted state on it. Ordinary failures (a wrong factor, a bad param) leave it false.\n */\n readonly corrupted: boolean;\n\n constructor(code: KernelErrorCode, message: string, retryAt?: number, corrupted = false) {\n super(message);\n this.name = \"KernelError\";\n this.code = code;\n this.retryAt = retryAt;\n this.corrupted = corrupted;\n }\n}\n","import { KernelError } from \"../errors.ts\";\nimport type {\n RecordStore,\n StoredFactor,\n StoredRecord,\n StoreOp,\n VaultImage,\n VaultMeta,\n} from \"./store.ts\";\n\nfunction cloneRecord(record: StoredRecord): StoredRecord {\n return { ...record, sealed: Uint8Array.from(record.sealed) };\n}\n\nfunction cloneFactor(factor: StoredFactor): StoredFactor {\n return { ...factor, sealed: Uint8Array.from(factor.sealed) };\n}\n\nfunction cloneMeta(meta: VaultMeta): VaultMeta {\n return { ...meta };\n}\n\nexport function cloneImage(image: VaultImage): VaultImage {\n return {\n meta: image.meta === undefined ? undefined : cloneMeta(image.meta),\n records: image.records.map(cloneRecord),\n factors: image.factors.map(cloneFactor),\n };\n}\n\nfunction conflict(message: string): KernelError {\n return new KernelError(\"conflict\", `vault store: ${message}`);\n}\n\nfunction requireMeta(meta: VaultMeta | undefined): VaultMeta {\n if (meta === undefined) {\n throw conflict(\"vault is not initialized\");\n }\n return meta;\n}\n\n/**\n * In-memory RecordStore for the Node host and kernel tests. Loads and commits deep-copy every\n * Uint8Array, so callers can never mutate stored state. `commit` applies ops to a clone and swaps\n * atomically: a failing precondition (`init`, `expect-revision`, a counter op on an empty store)\n * leaves the store untouched. Content ops bump `meta.revision` once per commit; an `init` commit\n * writes its meta verbatim (revision stays as the initializer set it).\n */\n/**\n * Applies a commit's ops to a vault image and returns the next image — the single implementation of\n * record/factor op semantics shared by the memory store and the file-backed store in\n *\n * @qntx/wallet-native.\n */\nexport function applyStoreOps(image: VaultImage, ops: ReadonlyArray<StoreOp>): VaultImage {\n let { meta } = image;\n let { records } = image;\n let { factors } = image;\n let bump = false;\n let initialized = false;\n for (const op of ops) {\n switch (op.op) {\n case \"init\":\n if (meta !== undefined || records.length > 0 || factors.length > 0) {\n throw conflict(\"vault already initialized\");\n }\n meta = cloneMeta(op.meta);\n initialized = true;\n break;\n case \"expect-revision\":\n if (meta === undefined || meta.revision !== op.revision) {\n throw conflict(\"vault changed underneath\");\n }\n break;\n case \"set-key-id\":\n meta = { ...requireMeta(meta), keyId: op.keyId };\n bump = true;\n break;\n case \"record-failure\": {\n const current = requireMeta(meta);\n meta = {\n ...current,\n failedAttempts: current.failedAttempts + 1,\n lastFailureAt: op.at,\n };\n break;\n }\n case \"reset-failures\":\n meta = { ...requireMeta(meta), failedAttempts: 0, lastFailureAt: 0 };\n break;\n case \"put-record\":\n records = [\n ...records.filter(\n (record) => !(record.kind === op.record.kind && record.id === op.record.id),\n ),\n cloneRecord(op.record),\n ];\n bump = true;\n break;\n case \"delete-record\":\n records = records.filter((record) => !(record.kind === op.kind && record.id === op.id));\n bump = true;\n break;\n case \"put-factor\":\n factors = [\n ...factors.filter((factor) => factor.id !== op.factor.id),\n cloneFactor(op.factor),\n ];\n bump = true;\n break;\n case \"delete-factor\":\n factors = factors.filter((factor) => factor.id !== op.id);\n bump = true;\n break;\n case \"clear\":\n meta = undefined;\n records = [];\n factors = [];\n break;\n }\n }\n if (bump && !initialized && meta !== undefined) {\n meta = { ...meta, revision: meta.revision + 1 };\n }\n return { meta, records, factors };\n}\n\nexport function createMemoryStore(): RecordStore {\n let image: VaultImage = { meta: undefined, records: [], factors: [] };\n const listeners = new Set<() => void>();\n\n return {\n async load() {\n return cloneImage(image);\n },\n async commit(ops) {\n const next = applyStoreOps(image, ops);\n image = next;\n for (const listener of listeners) {\n listener();\n }\n },\n watch(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n };\n}\n","/**\n * The store RPC shared by the worker (`src/worker/store-client.ts`) and the main side\n * (`src/node/store-server.ts`). The vault image travels as plain structured-clone data — it is\n * ciphertext plus public headers only, never decrypted secrets. This file stays platform-neutral:\n * the port shape is injected on both sides.\n */\nimport type { KernelErrorCode, StoreOp, VaultImage } from \"@qntx/wallet-kernel\";\n\nexport type StorePort = {\n readonly post: (message: unknown) => void;\n readonly listen: (handler: (message: unknown) => void) => () => void;\n};\n\nexport type StoreRequest =\n | { readonly t: \"load\"; readonly id: number }\n | { readonly t: \"commit\"; readonly id: number; readonly ops: ReadonlyArray<StoreOp> };\n\nexport type StoreReply =\n | { readonly t: \"loaded\"; readonly id: number; readonly image: WireImage }\n | { readonly t: \"committed\"; readonly id: number }\n | { readonly t: \"failed\"; readonly id: number; readonly code: KernelErrorCode }\n | { readonly t: \"changed\" };\n\n/** Structured clone preserves `undefined` properties, so `meta` travels as-is. */\ntype WireImage = VaultImage;\n\nexport function encodeImage(image: VaultImage): WireImage {\n return { meta: image.meta, factors: image.factors, records: image.records };\n}\n\nexport { assertVaultImage } from \"@qntx/wallet-kernel\";\n\nexport type IdleMinutes = 5 | 15 | 60;\n\nexport function isIdleMinutes(value: unknown): value is IdleMinutes {\n return value === 5 || value === 15 || value === 60;\n}\n\nexport function idleMsOf(minutes: IdleMinutes): number {\n return minutes * 60_000;\n}\n\n/** The fixed maximum session duration owned by the worker entries (never a host input). */\nexport const MAX_SESSION_MS: number = 12 * 3_600_000;\n\n/**\n * The first message the Web host posts to `kernel.worker.js`: the session policy. The browser\n * worker's vault lives in IndexedDB inside the worker itself, so unlike Node there is no store port\n * — policy is all the kernel needs before `startKernel` runs.\n */\nexport type BrowserWorkerInit = {\n readonly t: \"init\";\n readonly idleMinutes: IdleMinutes;\n};\n\nfunction plainInitObject(value: unknown): Record<string, unknown> | undefined {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return undefined;\n }\n if (Object.getPrototypeOf(value) !== Object.prototype) {\n return undefined;\n }\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- narrowed to a plain object above\n return value as Record<string, unknown>;\n}\n\n/** Strict init-message validation; anything else must not start the kernel. */\nexport function isBrowserWorkerInit(value: unknown): value is BrowserWorkerInit {\n const init = plainInitObject(value);\n return (\n init !== undefined &&\n Object.keys(init).sort().join(\",\") === \"idleMinutes,t\" &&\n init[\"t\"] === \"init\" &&\n isIdleMinutes(init[\"idleMinutes\"])\n );\n}\n\n/**\n * Strict `workerData` validation for the Node kernel worker: `{ idleMinutes, storePort }` and\n * nothing else. `storePort` is checked structurally (the real type lives behind the worker_threads\n * boundary); a wrong shape must not start the kernel.\n */\nexport function isNodeWorkerData(\n value: unknown,\n): value is { readonly idleMinutes: IdleMinutes; readonly storePort: unknown } {\n const data = plainInitObject(value);\n return (\n data !== undefined &&\n Object.keys(data).sort().join(\",\") === \"idleMinutes,storePort\" &&\n isIdleMinutes(data[\"idleMinutes\"]) &&\n typeof data[\"storePort\"] === \"object\" &&\n data[\"storePort\"] !== null\n );\n}\n","/**\n * Main-side half of the store RPC: serves the shared in-memory RecordStore to the worker and pushes\n * \"changed\" notices for commits that did NOT come through this port (the kernel's own commits must\n * not echo back as external changes).\n */\nimport type { MessagePort } from \"node:worker_threads\";\n\nimport { KernelError } from \"@qntx/wallet-kernel\";\nimport type { KernelErrorCode, RecordStore } from \"@qntx/wallet-kernel\";\n\nimport type { StoreReply, StoreRequest } from \"../shared.ts\";\nimport { encodeImage } from \"../shared.ts\";\n\n// Internal wire between this package's two halves — malformed payloads get dropped.\nfunction isStoreRequest(raw: unknown): raw is StoreRequest {\n if (typeof raw !== \"object\" || raw === null || Array.isArray(raw)) {\n return false;\n }\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- narrowed to a plain object above\n const request = raw as Record<string, unknown>;\n if (typeof request[\"id\"] !== \"number\") {\n return false;\n }\n return request[\"t\"] === \"load\" || (request[\"t\"] === \"commit\" && Array.isArray(request[\"ops\"]));\n}\n\nexport function serveStore(store: RecordStore, port: MessagePort): () => void {\n let ownCommits = 0;\n\n const post = (reply: StoreReply): void => {\n port.postMessage(reply);\n };\n\n // A failed load/commit replies \"failed\" with the KernelError code (or \"unavailable\" for a\n // non-kernel failure) — a request that hangs would deadlock the kernel's sequential queue.\n const failed = (id: number, error: unknown): void => {\n const code: KernelErrorCode = error instanceof KernelError ? error.code : \"unavailable\";\n post({ t: \"failed\", id, code });\n };\n\n const answer = async (request: StoreRequest): Promise<void> => {\n if (request.t === \"load\") {\n try {\n post({ t: \"loaded\", id: request.id, image: encodeImage(await store.load()) });\n } catch (error) {\n failed(request.id, error);\n }\n return;\n }\n ownCommits++;\n try {\n await store.commit(request.ops);\n } catch (error) {\n ownCommits--; // a failed commit fires no watch — do not eat the next external notice\n failed(request.id, error);\n return;\n }\n post({ t: \"committed\", id: request.id });\n };\n\n const onMessage = (raw: unknown): void => {\n if (!isStoreRequest(raw)) {\n return;\n }\n void (async (): Promise<void> => {\n try {\n await answer(raw);\n } catch {\n // A dead worker cannot be answered; the exit path handles it.\n }\n })();\n };\n\n const unwatch = store.watch(() => {\n if (ownCommits > 0) {\n ownCommits--;\n return;\n }\n post({ t: \"changed\" });\n });\n\n port.on(\"message\", onMessage);\n port.start();\n\n return () => {\n unwatch();\n port.off(\"message\", onMessage);\n port.close();\n };\n}\n","/**\n * CreateSoftwarePasskey — a TEST-ONLY PasskeyCeremony. There is no authenticator, no user presence\n * check, no credential store: the \"passkey\" is a deterministic function of `seed`, so anyone\n * holding the seed owns every wallet it ever protects. Use it in tests and demos only.\n *\n * CredentialId = SHA-256(seed ‖ \"credential\" ‖ u32be(n)) (n increments per create) PRF(salt) =\n * HMAC-SHA256(HMAC-SHA256(seed, credentialId), SHA-256(UTF-8(\"WebAuthn PRF\") ‖ 0x00 ‖ salt))\n * UserHandle = purpose byte ‖ SHA-256(seed ‖ \"user-handle\" ‖ credentialId)[0:15] — the same\n * 16-byte, purpose-prefixed shape the browser create path puts in `user.id`.\n */\nimport { hmac } from \"@noble/hashes/hmac.js\";\nimport { sha256 } from \"@noble/hashes/sha2.js\";\nimport { bytesToHex, concatBytes, utf8ToBytes } from \"@noble/hashes/utils.js\";\n\nimport { WalletError } from \"@qntx/wallet\";\nimport { PASSKEY_PURPOSE_VAULT, PASSKEY_PURPOSE_WALLET } from \"@qntx/wallet/host\";\nimport type { PasskeyCeremony, PrfResult, PrfSalts } from \"@qntx/wallet/host\";\n\nexport function createSoftwarePasskey(\n seed: Uint8Array,\n options?: { readonly synced?: boolean },\n): PasskeyCeremony {\n const synced = options?.synced ?? true; // software credentials pretend to be synced by default\n let counter = 0;\n let lastCreated: Uint8Array | undefined;\n const known = new Map<string, { credentialId: Uint8Array; userHandle: Uint8Array }>();\n\n const prf = (credentialId: Uint8Array, salt: Uint8Array): Uint8Array => {\n const key = hmac(sha256, seed, credentialId);\n const message = sha256(concatBytes(utf8ToBytes(\"WebAuthn PRF\"), new Uint8Array([0]), salt));\n return hmac(sha256, key, message);\n };\n\n const result = (\n credentialId: Uint8Array,\n salts: PrfSalts,\n userHandle?: Uint8Array,\n ): PrfResult => ({\n credentialId: new Uint8Array(credentialId),\n first: prf(credentialId, salts.first),\n ...(salts.second === undefined ? {} : { second: prf(credentialId, salts.second) }),\n ...(userHandle === undefined ? {} : { userHandle: new Uint8Array(userHandle) }),\n synced,\n });\n\n return {\n // oxlint-disable-next-line typescript/require-await -- the ceremony contract is async; this test double computes synchronously\n create: async ({ purpose, salts }) => {\n const counterBytes = new Uint8Array(4);\n new DataView(counterBytes.buffer).setUint32(0, counter);\n counter++;\n const credentialId = sha256(concatBytes(seed, utf8ToBytes(\"credential\"), counterBytes));\n const purposeByte = purpose === \"wallet\" ? PASSKEY_PURPOSE_WALLET : PASSKEY_PURPOSE_VAULT;\n const userHandle = new Uint8Array(16);\n userHandle[0] = purposeByte;\n userHandle.set(\n sha256(concatBytes(seed, utf8ToBytes(\"user-handle\"), credentialId)).subarray(0, 15),\n 1,\n );\n known.set(bytesToHex(credentialId), { credentialId, userHandle });\n lastCreated = credentialId;\n return result(credentialId, salts);\n },\n // oxlint-disable-next-line typescript/require-await -- the ceremony contract is async; this test double computes synchronously\n assert: async ({ credentialIds, salts }) => {\n const credentialId =\n credentialIds === undefined\n ? lastCreated\n : credentialIds.find((id) => known.has(bytesToHex(id)));\n if (credentialId === undefined) {\n throw new WalletError(\n \"factor-unavailable\",\n credentialIds === undefined\n ? \"no software credential exists yet\"\n : \"unknown software credential\",\n );\n }\n const entry = known.get(bytesToHex(credentialId));\n return result(credentialId, salts, entry?.userHandle);\n },\n };\n}\n","/**\n * The Node host: `createNodeWallet` spawns the bundled kernel worker (dist/kernel.node.js) and\n * keeps the vault image — ciphertext + public headers, never secrets — in the main thread's\n * in-memory store, served to each worker over a MessageChannel. That is what lets a lock destroy\n * the whole worker without losing the vault.\n */\nimport { MessageChannel, Worker } from \"node:worker_threads\";\n\nimport { WalletError } from \"@qntx/wallet\";\nimport type { AutoLockPolicy, PasskeyConfig, Wallet } from \"@qntx/wallet\";\nimport { createMemoryStore } from \"@qntx/wallet-kernel\";\nimport { connectKernel } from \"@qntx/wallet/host\";\nimport type { MainPort, PasskeyCeremony } from \"@qntx/wallet/host\";\nimport type { ProtocolValue } from \"@qntx/wallet/protocol\";\n\nimport type { NodeWorkerData } from \"../worker/node.ts\";\nimport { serveStore } from \"./store-server.ts\";\n\nexport { createSoftwarePasskey } from \"./software-passkey.ts\";\n\nexport type NodeWalletOptions = {\n readonly passkey: PasskeyConfig;\n readonly autoLock: AutoLockPolicy;\n readonly authenticator?: PasskeyCeremony;\n /**\n * Test/debug hook: the worker file location and a spawn observer (count spawns, watch 'exit').\n * Defaults to the bundled dist/kernel.node.js next to this module.\n */\n readonly kernelUrl?: string | URL;\n readonly onSpawn?: (worker: Worker) => void;\n};\n\nexport async function createNodeWallet(options: NodeWalletOptions): Promise<Wallet> {\n const { autoLock } = options;\n if (autoLock.platform !== \"web\") {\n throw new WalletError(\"invalid-input\", \"createNodeWallet accepts only the web autoLock policy\");\n }\n // The host sends only the idle choice; the worker owns the maximum session duration.\n const workerData = { idleMinutes: autoLock.idleMinutes };\n const store = createMemoryStore(); // ciphertext + public headers only — safe in the main thread\n const kernelUrl = options.kernelUrl ?? new URL(\"kernel.node.js\", import.meta.url);\n\n const spawn = (): MainPort => {\n const channel = new MessageChannel();\n const stopStore = serveStore(store, channel.port1);\n const worker = new Worker(kernelUrl, {\n workerData: { ...workerData, storePort: channel.port2 } satisfies NodeWorkerData,\n transferList: [channel.port2],\n });\n options.onSpawn?.(worker);\n return {\n post: (message: ProtocolValue, transfer: ArrayBuffer[]) => {\n worker.postMessage(message, transfer);\n },\n listen: (handler) => {\n const listener = (message: unknown) => {\n handler(message);\n };\n worker.on(\"message\", listener);\n return () => {\n worker.off(\"message\", listener);\n };\n },\n onExit: (handler) => {\n const listener = () => {\n handler(\"crashed\");\n };\n worker.once(\"exit\", listener);\n return () => {\n worker.off(\"exit\", listener);\n };\n },\n terminate: () => {\n stopStore();\n void worker.terminate();\n },\n };\n };\n\n return connectKernel({\n factors: \"software\",\n spawn,\n passkey: options.passkey,\n ceremony: options.authenticator,\n setTimer: (delayMs, fn) => {\n const timer = setTimeout(fn, delayMs);\n return () => {\n clearTimeout(timer);\n };\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;AASA,IAAa,cAAb,cAAiC,MAAM;CACrC;CACA;;;;;CAKA;CAEA,YAAY,MAAuB,SAAiB,SAAkB,YAAY,OAAO;EACvF,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,UAAU;EACf,KAAK,YAAY;CACnB;AACF;;ACfA,SAAS,YAAY,QAAoC;CACvD,OAAO;EAAE,GAAG;EAAQ,QAAQ,WAAW,KAAK,OAAO,MAAM;CAAE;AAC7D;AAEA,SAAS,YAAY,QAAoC;CACvD,OAAO;EAAE,GAAG;EAAQ,QAAQ,WAAW,KAAK,OAAO,MAAM;CAAE;AAC7D;AAEA,SAAS,UAAU,MAA4B;CAC7C,OAAO,EAAE,GAAG,KAAK;AACnB;AAEA,SAAgB,WAAW,OAA+B;CACxD,OAAO;EACL,MAAM,MAAM,SAAS,KAAA,IAAY,KAAA,IAAY,UAAU,MAAM,IAAI;EACjE,SAAS,MAAM,QAAQ,IAAI,WAAW;EACtC,SAAS,MAAM,QAAQ,IAAI,WAAW;CACxC;AACF;AAEA,SAAS,SAAS,SAA8B;CAC9C,OAAO,IAAI,YAAY,YAAY,gBAAgB,SAAS;AAC9D;AAEA,SAAS,YAAY,MAAwC;CAC3D,IAAI,SAAS,KAAA,GACX,MAAM,SAAS,0BAA0B;CAE3C,OAAO;AACT;;;;;;;;;;;;;;AAeA,SAAgB,cAAc,OAAmB,KAAyC;CACxF,IAAI,EAAE,SAAS;CACf,IAAI,EAAE,YAAY;CAClB,IA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5B,MAAM,UAAU,IAAI,eAAe;EACnC,MAAM,YAAY,WAAW,OAAO,QAAQ,KAAK;EACjD,MAAM,SAAS,IAAI,OAAO,WAAW;GACnC,YAAY;IAAE,GAAG;IAAY,WAAW,QAAQ;GAAM;GACtD,cAAc,CAAC,QAAQ,KAAK;EAC9B,CAAC;EACD,QAAQ,UAAU,MAAM;EACxB,OAAO;GACL,OAAO,SAAwB,aAA4B;IACzD,OAAO,YAAY,SAAS,QAAQ;GACtC;GACA,SAAS,YAAY;IACnB,MAAM,YAAY,YAAqB;KACrC,QAAQ,OAAO;IACjB;IACA,OAAO,GAAG,WAAW,QAAQ;IAC7B,aAAa;KACX,OAAO,IAAI,WAAW,QAAQ;IAChC;GACF;GACA,SAAS,YAAY;IACnB,MAAM,iBAAiB;KACrB,QAAQ,SAAS;IACnB;IACA,OAAO,KAAK,QAAQ,QAAQ;IAC5B,aAAa;KACX,OAAO,IAAI,QAAQ,QAAQ;IAC7B;GACF;GACA,iBAAiB;IACf,UAAU;IACV,OAAY,UAAU;GACxB;EACF;CACF;CAEA,OAAO,cAAc;EACnB,SAAS;EACT;EACA,SAAS,QAAQ;EACjB,UAAU,QAAQ;EAClB,WAAW,SAAS,OAAO;GACzB,MAAM,QAAQ,WAAW,IAAI,OAAO;GACpC,aAAa;IACX,aAAa,KAAK;GACpB;EACF;CACF,CAAC;AACH"}
1
+ {"version":3,"file":"node.mjs","names":[],"sources":["../../wallet-kernel/src/errors.ts","../../wallet-kernel/src/vault/memory.ts","../src/shared.ts","../src/node/store-server.ts","../src/node/software-passkey.ts","../src/node/index.ts"],"sourcesContent":["import type { WalletErrorCode } from \"@qntx/wallet/types\";\n\nexport type KernelErrorCode = WalletErrorCode;\n\n/**\n * The only error type kernel code throws. Its code is the public `WalletErrorCode`: the kernel\n * returns it verbatim in the error payload. Messages are static strings: they must never echo user\n * input or secret contents.\n */\nexport class KernelError extends Error {\n readonly code: KernelErrorCode;\n readonly retryAt: number | undefined;\n /**\n * True when the error proves the stored vault itself is corrupt — the kernel latches the\n * corrupted state on it. Ordinary failures (a wrong factor, a bad param) leave it false.\n */\n readonly corrupted: boolean;\n\n constructor(code: KernelErrorCode, message: string, retryAt?: number, corrupted = false) {\n super(message);\n this.name = \"KernelError\";\n this.code = code;\n this.retryAt = retryAt;\n this.corrupted = corrupted;\n }\n}\n","import { KernelError } from \"../errors.ts\";\nimport type {\n RecordStore,\n StoredFactor,\n StoredRecord,\n StoreOp,\n VaultImage,\n VaultMeta,\n} from \"./store.ts\";\n\nfunction cloneRecord(record: StoredRecord): StoredRecord {\n return { ...record, sealed: Uint8Array.from(record.sealed) };\n}\n\nfunction cloneFactor(factor: StoredFactor): StoredFactor {\n return { ...factor, sealed: Uint8Array.from(factor.sealed) };\n}\n\nfunction cloneMeta(meta: VaultMeta): VaultMeta {\n return { ...meta };\n}\n\nexport function cloneImage(image: VaultImage): VaultImage {\n return {\n meta: image.meta === undefined ? undefined : cloneMeta(image.meta),\n records: image.records.map(cloneRecord),\n factors: image.factors.map(cloneFactor),\n };\n}\n\nfunction conflict(message: string): KernelError {\n return new KernelError(\"conflict\", `vault store: ${message}`);\n}\n\nfunction requireMeta(meta: VaultMeta | undefined): VaultMeta {\n if (meta === undefined) {\n throw conflict(\"vault is not initialized\");\n }\n return meta;\n}\n\n/**\n * In-memory RecordStore for the Node host and kernel tests. Loads and commits deep-copy every\n * Uint8Array, so callers can never mutate stored state. `commit` applies ops to a clone and swaps\n * atomically: a failing precondition (`init`, `expect-revision`, a counter op on an empty store)\n * leaves the store untouched. Content ops bump `meta.revision` once per commit; an `init` commit\n * writes its meta verbatim (revision stays as the initializer set it).\n */\n/**\n * Applies a commit's ops to a vault image and returns the next image — the single implementation of\n * record/factor op semantics shared by the memory store and the file-backed store in\n *\n * @qntx/wallet-native.\n */\nexport function applyStoreOps(image: VaultImage, ops: ReadonlyArray<StoreOp>): VaultImage {\n let { meta } = image;\n let { records } = image;\n let { factors } = image;\n let bump = false;\n let initialized = false;\n for (const op of ops) {\n switch (op.op) {\n case \"init\":\n if (meta !== undefined || records.length > 0 || factors.length > 0) {\n throw conflict(\"vault already initialized\");\n }\n meta = cloneMeta(op.meta);\n initialized = true;\n break;\n case \"expect-revision\":\n if (meta === undefined || meta.revision !== op.revision) {\n throw conflict(\"vault changed underneath\");\n }\n break;\n case \"set-key-id\":\n meta = { ...requireMeta(meta), keyId: op.keyId };\n bump = true;\n break;\n case \"record-failure\": {\n const current = requireMeta(meta);\n meta = {\n ...current,\n failedAttempts: current.failedAttempts + 1,\n lastFailureAt: op.at,\n };\n break;\n }\n case \"reset-failures\":\n meta = { ...requireMeta(meta), failedAttempts: 0, lastFailureAt: 0 };\n break;\n case \"put-record\":\n records = [\n ...records.filter(\n (record) => !(record.kind === op.record.kind && record.id === op.record.id),\n ),\n cloneRecord(op.record),\n ];\n bump = true;\n break;\n case \"delete-record\":\n records = records.filter((record) => !(record.kind === op.kind && record.id === op.id));\n bump = true;\n break;\n case \"put-factor\":\n factors = [\n ...factors.filter((factor) => factor.id !== op.factor.id),\n cloneFactor(op.factor),\n ];\n bump = true;\n break;\n case \"delete-factor\":\n factors = factors.filter((factor) => factor.id !== op.id);\n bump = true;\n break;\n case \"clear\":\n meta = undefined;\n records = [];\n factors = [];\n break;\n }\n }\n if (bump && !initialized && meta !== undefined) {\n meta = { ...meta, revision: meta.revision + 1 };\n }\n return { meta, records, factors };\n}\n\nexport function createMemoryStore(): RecordStore {\n let image: VaultImage = { meta: undefined, records: [], factors: [] };\n const listeners = new Set<() => void>();\n\n return {\n async load() {\n return cloneImage(image);\n },\n async commit(ops) {\n const next = applyStoreOps(image, ops);\n image = next;\n for (const listener of listeners) {\n listener();\n }\n },\n watch(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n };\n}\n","/**\n * The store RPC shared by the worker (`src/worker/store-client.ts`) and the main side\n * (`src/node/store-server.ts`). The vault image travels as plain structured-clone data — it is\n * ciphertext plus public headers only, never decrypted secrets. This file stays platform-neutral:\n * the port shape is injected on both sides.\n */\nimport type { KernelErrorCode, StoreOp, VaultImage } from \"@qntx/wallet-kernel\";\n\nexport type StorePort = {\n readonly post: (message: unknown) => void;\n readonly listen: (handler: (message: unknown) => void) => () => void;\n};\n\nexport type StoreRequest =\n | { readonly t: \"load\"; readonly id: number }\n | { readonly t: \"commit\"; readonly id: number; readonly ops: ReadonlyArray<StoreOp> };\n\nexport type StoreReply =\n | { readonly t: \"loaded\"; readonly id: number; readonly image: WireImage }\n | { readonly t: \"committed\"; readonly id: number }\n | { readonly t: \"failed\"; readonly id: number; readonly code: KernelErrorCode }\n | { readonly t: \"changed\" };\n\n/** Structured clone preserves `undefined` properties, so `meta` travels as-is. */\ntype WireImage = VaultImage;\n\nexport function encodeImage(image: VaultImage): WireImage {\n return { meta: image.meta, factors: image.factors, records: image.records };\n}\n\nexport { assertVaultImage } from \"@qntx/wallet-kernel\";\n\nexport type IdleMinutes = 5 | 15 | 60;\n\nexport function isIdleMinutes(value: unknown): value is IdleMinutes {\n return value === 5 || value === 15 || value === 60;\n}\n\nexport function idleMsOf(minutes: IdleMinutes): number {\n return minutes * 60_000;\n}\n\n/** The fixed maximum session duration owned by the worker entries (never a host input). */\nexport const MAX_SESSION_MS: number = 12 * 3_600_000;\n\n/**\n * The first message the Web host posts to `kernel.worker.js`: the session policy. The browser\n * worker's vault lives in IndexedDB inside the worker itself, so unlike Node there is no store port\n * — policy is all the kernel needs before `startKernel` runs.\n */\nexport type BrowserWorkerInit = {\n readonly t: \"init\";\n readonly idleMinutes: IdleMinutes;\n};\n\nfunction plainInitObject(value: unknown): Record<string, unknown> | undefined {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return undefined;\n }\n if (Object.getPrototypeOf(value) !== Object.prototype) {\n return undefined;\n }\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- narrowed to a plain object above\n return value as Record<string, unknown>;\n}\n\n/** Strict init-message validation; anything else must not start the kernel. */\nexport function isBrowserWorkerInit(value: unknown): value is BrowserWorkerInit {\n const init = plainInitObject(value);\n return (\n init !== undefined &&\n Object.keys(init).sort().join(\",\") === \"idleMinutes,t\" &&\n init[\"t\"] === \"init\" &&\n isIdleMinutes(init[\"idleMinutes\"])\n );\n}\n\n/**\n * Strict `workerData` validation for the Node kernel worker: `{ idleMinutes, storePort }` and\n * nothing else. `storePort` is checked structurally (the real type lives behind the worker_threads\n * boundary); a wrong shape must not start the kernel.\n */\nexport function isNodeWorkerData(\n value: unknown,\n): value is { readonly idleMinutes: IdleMinutes; readonly storePort: unknown } {\n const data = plainInitObject(value);\n return (\n data !== undefined &&\n Object.keys(data).sort().join(\",\") === \"idleMinutes,storePort\" &&\n isIdleMinutes(data[\"idleMinutes\"]) &&\n typeof data[\"storePort\"] === \"object\" &&\n data[\"storePort\"] !== null\n );\n}\n","/**\n * Main-side half of the store RPC: serves the shared in-memory RecordStore to the worker and pushes\n * \"changed\" notices for commits that did NOT come through this port (the kernel's own commits must\n * not echo back as external changes).\n */\nimport type { MessagePort } from \"node:worker_threads\";\n\nimport { KernelError } from \"@qntx/wallet-kernel\";\nimport type { KernelErrorCode, RecordStore } from \"@qntx/wallet-kernel\";\n\nimport type { StoreReply, StoreRequest } from \"../shared.ts\";\nimport { encodeImage } from \"../shared.ts\";\n\n// Internal wire between this package's two halves — malformed payloads get dropped.\nfunction isStoreRequest(raw: unknown): raw is StoreRequest {\n if (typeof raw !== \"object\" || raw === null || Array.isArray(raw)) {\n return false;\n }\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- narrowed to a plain object above\n const request = raw as Record<string, unknown>;\n if (typeof request[\"id\"] !== \"number\") {\n return false;\n }\n return request[\"t\"] === \"load\" || (request[\"t\"] === \"commit\" && Array.isArray(request[\"ops\"]));\n}\n\nexport function serveStore(store: RecordStore, port: MessagePort): () => void {\n let ownCommits = 0;\n\n const post = (reply: StoreReply): void => {\n port.postMessage(reply);\n };\n\n // A failed load/commit replies \"failed\" with the KernelError code (or \"unavailable\" for a\n // non-kernel failure) — a request that hangs would deadlock the kernel's sequential queue.\n const failed = (id: number, error: unknown): void => {\n const code: KernelErrorCode = error instanceof KernelError ? error.code : \"unavailable\";\n post({ t: \"failed\", id, code });\n };\n\n const answer = async (request: StoreRequest): Promise<void> => {\n if (request.t === \"load\") {\n try {\n post({ t: \"loaded\", id: request.id, image: encodeImage(await store.load()) });\n } catch (error) {\n failed(request.id, error);\n }\n return;\n }\n ownCommits++;\n try {\n await store.commit(request.ops);\n } catch (error) {\n ownCommits--; // a failed commit fires no watch — do not eat the next external notice\n failed(request.id, error);\n return;\n }\n post({ t: \"committed\", id: request.id });\n };\n\n const onMessage = (raw: unknown): void => {\n if (!isStoreRequest(raw)) {\n return;\n }\n void (async (): Promise<void> => {\n try {\n await answer(raw);\n } catch {\n // A dead worker cannot be answered; the exit path handles it.\n }\n })();\n };\n\n const unwatch = store.watch(() => {\n if (ownCommits > 0) {\n ownCommits--;\n return;\n }\n post({ t: \"changed\" });\n });\n\n port.on(\"message\", onMessage);\n port.start();\n\n return () => {\n unwatch();\n port.off(\"message\", onMessage);\n port.close();\n };\n}\n","/**\n * CreateSoftwarePasskey — a TEST-ONLY PasskeyCeremony. There is no authenticator, no user presence\n * check, no credential store: the \"passkey\" is a deterministic function of `seed`, so anyone\n * holding the seed owns every wallet it ever protects. Use it in tests and demos only.\n *\n * CredentialId = SHA-256(seed ‖ \"credential\" ‖ u32be(n)) (n increments per create) PRF(salt) =\n * HMAC-SHA256(HMAC-SHA256(seed, credentialId), SHA-256(UTF-8(\"WebAuthn PRF\") ‖ 0x00 ‖ salt))\n * UserHandle = purpose byte ‖ SHA-256(seed ‖ \"user-handle\" ‖ credentialId)[0:15] — the same\n * 16-byte, purpose-prefixed shape the browser create path puts in `user.id`.\n */\nimport { hmac } from \"@noble/hashes/hmac.js\";\nimport { sha256 } from \"@noble/hashes/sha2.js\";\nimport { bytesToHex, concatBytes, utf8ToBytes } from \"@noble/hashes/utils.js\";\n\nimport { WalletError } from \"@qntx/wallet\";\nimport { PASSKEY_PURPOSE_VAULT, PASSKEY_PURPOSE_WALLET } from \"@qntx/wallet/host\";\nimport type { PasskeyCeremony, PrfResult, PrfSalts } from \"@qntx/wallet/host\";\n\nexport function createSoftwarePasskey(\n seed: Uint8Array,\n options?: { readonly synced?: boolean },\n): PasskeyCeremony {\n const synced = options?.synced ?? true; // software credentials pretend to be synced by default\n let counter = 0;\n let lastCreated: Uint8Array | undefined;\n const known = new Map<string, { credentialId: Uint8Array; userHandle: Uint8Array }>();\n\n const prf = (credentialId: Uint8Array, salt: Uint8Array): Uint8Array => {\n const key = hmac(sha256, seed, credentialId);\n const message = sha256(concatBytes(utf8ToBytes(\"WebAuthn PRF\"), new Uint8Array([0]), salt));\n return hmac(sha256, key, message);\n };\n\n const result = (\n credentialId: Uint8Array,\n salts: PrfSalts,\n userHandle?: Uint8Array,\n ): PrfResult => ({\n credentialId: new Uint8Array(credentialId),\n first: prf(credentialId, salts.first),\n ...(salts.second === undefined ? {} : { second: prf(credentialId, salts.second) }),\n ...(userHandle === undefined ? {} : { userHandle: new Uint8Array(userHandle) }),\n synced,\n });\n\n return {\n // oxlint-disable-next-line typescript/require-await -- the ceremony contract is async; this test double computes synchronously\n create: async ({ purpose, salts }) => {\n const counterBytes = new Uint8Array(4);\n new DataView(counterBytes.buffer).setUint32(0, counter);\n counter++;\n const credentialId = sha256(concatBytes(seed, utf8ToBytes(\"credential\"), counterBytes));\n const purposeByte = purpose === \"wallet\" ? PASSKEY_PURPOSE_WALLET : PASSKEY_PURPOSE_VAULT;\n const userHandle = new Uint8Array(16);\n userHandle[0] = purposeByte;\n userHandle.set(\n sha256(concatBytes(seed, utf8ToBytes(\"user-handle\"), credentialId)).subarray(0, 15),\n 1,\n );\n known.set(bytesToHex(credentialId), { credentialId, userHandle });\n lastCreated = credentialId;\n return result(credentialId, salts);\n },\n // oxlint-disable-next-line typescript/require-await -- the ceremony contract is async; this test double computes synchronously\n assert: async ({ credentialIds, salts }) => {\n const credentialId =\n credentialIds === undefined\n ? lastCreated\n : credentialIds.find((id) => known.has(bytesToHex(id)));\n if (credentialId === undefined) {\n throw new WalletError(\n \"factor-unavailable\",\n credentialIds === undefined\n ? \"no software credential exists yet\"\n : \"unknown software credential\",\n );\n }\n const entry = known.get(bytesToHex(credentialId));\n return result(credentialId, salts, entry?.userHandle);\n },\n };\n}\n","/**\n * The Node host: `createNodeWallet` spawns the bundled kernel worker (dist/kernel.node.js) and\n * keeps the vault image — ciphertext + public headers, never secrets — in the main thread's\n * in-memory store, served to each worker over a MessageChannel. That is what lets a lock destroy\n * the whole worker without losing the vault.\n */\nimport { MessageChannel, Worker } from \"node:worker_threads\";\n\nimport { WalletError } from \"@qntx/wallet\";\nimport type { Wallet, WalletOptions } from \"@qntx/wallet\";\nimport { createMemoryStore } from \"@qntx/wallet-kernel\";\nimport { connectKernel } from \"@qntx/wallet/host\";\nimport type { MainPort, PasskeyCeremony } from \"@qntx/wallet/host\";\nimport type { ProtocolValue } from \"@qntx/wallet/protocol\";\n\nimport type { NodeWorkerData } from \"../worker/node.ts\";\nimport { serveStore } from \"./store-server.ts\";\n\nexport { createSoftwarePasskey } from \"./software-passkey.ts\";\n\nexport type NodeWalletOptions = WalletOptions & {\n readonly authenticator?: PasskeyCeremony;\n /**\n * Test/debug hook: the worker file location and a spawn observer (count spawns, watch 'exit').\n * Defaults to the bundled dist/kernel.node.js next to this module.\n */\n readonly kernelUrl?: string | URL;\n readonly onSpawn?: (worker: Worker) => void;\n};\n\nexport async function createNodeWallet(options: NodeWalletOptions): Promise<Wallet> {\n const { autoLock } = options;\n if (autoLock.platform !== \"web\") {\n throw new WalletError(\"invalid-input\", \"createNodeWallet accepts only the web autoLock policy\");\n }\n // The host sends only the idle choice; the worker owns the maximum session duration.\n const workerData = { idleMinutes: autoLock.idleMinutes };\n const store = createMemoryStore(); // ciphertext + public headers only — safe in the main thread\n const kernelUrl = options.kernelUrl ?? new URL(\"kernel.node.js\", import.meta.url);\n\n const spawn = (): MainPort => {\n const channel = new MessageChannel();\n const stopStore = serveStore(store, channel.port1);\n const worker = new Worker(kernelUrl, {\n workerData: { ...workerData, storePort: channel.port2 } satisfies NodeWorkerData,\n transferList: [channel.port2],\n });\n options.onSpawn?.(worker);\n return {\n post: (message: ProtocolValue, transfer: ArrayBuffer[]) => {\n worker.postMessage(message, transfer);\n },\n listen: (handler) => {\n const listener = (message: unknown) => {\n handler(message);\n };\n worker.on(\"message\", listener);\n return () => {\n worker.off(\"message\", listener);\n };\n },\n onExit: (handler) => {\n const listener = () => {\n handler(\"crashed\");\n };\n worker.once(\"exit\", listener);\n return () => {\n worker.off(\"exit\", listener);\n };\n },\n terminate: () => {\n stopStore();\n void worker.terminate();\n },\n };\n };\n\n return connectKernel({\n factors: \"software\",\n spawn,\n passkey: options.passkey,\n verifyEvent: options.verifyEvent,\n ceremony: options.authenticator,\n setTimer: (delayMs, fn) => {\n const timer = setTimeout(fn, delayMs);\n return () => {\n clearTimeout(timer);\n };\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;AASA,IAAa,cAAb,cAAiC,MAAM;CACrC;CACA;;;;;CAKA;CAEA,YAAY,MAAuB,SAAiB,SAAkB,YAAY,OAAO;EACvF,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,UAAU;EACf,KAAK,YAAY;CACnB;AACF;;ACfA,SAAS,YAAY,QAAoC;CACvD,OAAO;EAAE,GAAG;EAAQ,QAAQ,WAAW,KAAK,OAAO,MAAM;CAAE;AAC7D;AAEA,SAAS,YAAY,QAAoC;CACvD,OAAO;EAAE,GAAG;EAAQ,QAAQ,WAAW,KAAK,OAAO,MAAM;CAAE;AAC7D;AAEA,SAAS,UAAU,MAA4B;CAC7C,OAAO,EAAE,GAAG,KAAK;AACnB;AAEA,SAAgB,WAAW,OAA+B;CACxD,OAAO;EACL,MAAM,MAAM,SAAS,KAAA,IAAY,KAAA,IAAY,UAAU,MAAM,IAAI;EACjE,SAAS,MAAM,QAAQ,IAA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@
1
1
  {
2
2
  "name": "@qntx/wallet-worker",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Web Worker and worker_threads hosts for @qntx/wallet",
5
5
  "keywords": [
6
6
  "nostr",
@@ -56,8 +56,8 @@
56
56
  "test:consumer": "bun scripts/consumer-smoke.ts"
57
57
  },
58
58
  "dependencies": {
59
- "@noble/hashes": "^2.4.0",
60
- "@scure/base": "^2.4.0"
59
+ "@noble/hashes": "2.4.0",
60
+ "@scure/base": "2.4.0"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@arethetypeswrong/cli": "0.18.5",
@@ -67,8 +67,8 @@
67
67
  "@qntx/spark": "0.5.2",
68
68
  "@qntx/spark-wasm": "0.5.2",
69
69
  "@qntx/tsconfig": "1.0.1",
70
- "@qntx/wallet": "0.7.0",
71
- "@qntx/wallet-kernel": "0.7.0",
70
+ "@qntx/wallet": "0.8.0",
71
+ "@qntx/wallet-kernel": "0.8.0",
72
72
  "@scure/base": "2.4.0",
73
73
  "@scure/btc-signer": "2.4.1",
74
74
  "@types/node": "26.6.5",
@@ -77,7 +77,7 @@
77
77
  "vite-plus": "1.4.0"
78
78
  },
79
79
  "peerDependencies": {
80
- "@qntx/wallet": "0.7.0"
80
+ "@qntx/wallet": "0.8.0"
81
81
  },
82
82
  "devEngines": {
83
83
  "packageManager": {