@qntx/wallet-worker
v0.16.0
Published
Web Worker and worker_threads hosts for @qntx/wallet
Maintainers
Readme
@qntx/wallet-worker
Web and Node host for @qntx/wallet: spawns the
wallet kernel in a Worker (dist/kernel.worker.js / dist/kernel.node.js, bundled), keeps the
encrypted vault in IndexedDB (Web) or a main-thread store server (Node), and runs the WebAuthn
PRF passkey ceremony on the main thread. Peer dependency: the exact @qntx/wallet release this
kernel was built against — the host/kernel protocol requires it. dist/kernel.manifest.json
records the protocol version, vault format, every pinned kernel dependency's lockfile integrity
and the SHA-256 of each bundle.
npm install @qntx/wallet-worker @qntx/walletWeb
@qntx/wallet-worker/browser:
import { verifyEvent } from "@qntx/nostr";
import { createWebWallet } from "@qntx/wallet-worker/browser";
const wallet = await createWebWallet({
passkey: {
rpId: "wallet.example.com", // permanent, wallet-only — see Security notes
rpName: "Example",
walletSalt: "example/wallet",
vaultSalt: "example/vault",
backupSalt: "example/backup",
},
autoLock: { platform: "web", idleMinutes: 15 }, // 5 | 15 | 60; max session 12 h
verifyEvent, // verifies every event the kernel signs; pass your app's verifier
});
const created = await wallet.wallets.createWithPasskey({ name: "Example Wallet" });
await wallet.vault.unlock({ kind: "passkey" });
import { nostr } from "@qntx/wallet/nostr";
const key = await nostr(wallet).deriveKey(created.walletId, 0);
const signer = nostr(wallet).signer(key.ref);Node (tests and demos)
@qntx/wallet-worker/node runs the same kernel in a node:worker_threads Worker with an
in-memory vault store. createSoftwarePasskey(seed) is a test-only WebAuthn stand-in —
deterministic, no authenticator, anyone holding the seed owns the wallet:
import { verifyEvent } from "@qntx/nostr";
import { createNodeWallet, createSoftwarePasskey } from "@qntx/wallet-worker/node";
// The deterministic seed behind the software passkey — test/dev hosts only.
declare const seed: Uint8Array;
const wallet = await createNodeWallet({
passkey: {
rpId: "qntx.test",
rpName: "test",
walletSalt: "t/wallet",
vaultSalt: "t/vault",
backupSalt: "t/backup",
},
autoLock: { platform: "web", idleMinutes: 15 },
verifyEvent,
authenticator: createSoftwarePasskey(seed),
});Security notes
Secrets exist only inside the kernel Worker;
vault.lock()resolves immediately on the host and destroys the worker — the vault image (ciphertext + public headers) persists in the store.Session limits belong to the worker, not the page:
idleMinutesaccepts 5, 15 or 60 and the absolute session cap is a fixed 12 hours, measured on a monotonic clock. On the Web only user presence (input events, a tab becoming visible) refreshes the idle timer — background request traffic does not; on Node every successful request does.WalletInfo.backupis"mnemonic","passkey"(a synced passkey — prompt the user to export) or"none"(a device-bound passkey — survives only on this device).exportMnemonicupgrades the record to"mnemonic".createSoftwarePasskey(seed, { synced })—synceddefaults totrueand lands in everyPrfResult; pass{ synced: false }to exercise the device-bound path.The
rpIdscope is the wallet's trust boundary: any origin that may claim the RP id (its subdomains, its related origins) can derive a passkey wallet's seed with one passkey prompt. Serve the wallet from a domain of its own and use that domain as therpId.passkey.rpIdand the three salts are permanent identifiers: changing them makes existing passkey factors and passkey-derived wallets unrecoverable. Moving domains needs WebAuthn related origins (/.well-known/webauthn); offerexportMnemonicfor passkey-derived wallets.IndexedDB upgrades are never
blocked: the worker closes its connection onversionchange, and once the database is newer than this build's schema the vault reports{ status: "unsupported", reason: "newer" }— update the app, do not reset.The Node store's RPC failures surface as
unavailableand precondition failures asconflict— neither hangs a kernel call.The Web host is a hot wallet. CSP needs
worker-src 'self'. The host spawns the kernel withnew Worker(new URL("kernel.worker.js", import.meta.url)), which bundlers emit as a worker entry with its chunks andspark.wasm. Consumer setup: Vite —worker: { format: "es" }(required to keep the Spark backend lazy; the default"iife"still runs but ships Spark inside the initial worker) and dev-serveroptimizeDeps.exclude: ["@qntx/wallet-worker"]; webpack 5 — nothing.Trusted Types (
require-trusted-types-for 'script') works with adefaultpolicy that admits the bundled kernel worker. Bundlers recognise the worker only in the literalnew Worker(new URL(…))form, so the library cannot pass a named policy'sTrustedScriptURLitself. Addtrusted-types defaultto the CSP and install the policy beforecreateWebWallet:trustedTypes?.createPolicy("default", { createScriptURL: (input) => { const url = new URL(input, location.href); if (url.origin !== location.origin || !/\/kernel\.worker[^/]*\.js$/.test(url.pathname)) { throw new TypeError(`script URL not allowed: ${input}`); } return url.href; }, });The Playwright suite runs the whole Web host under exactly this CSP and policy.
Custom schemes (Electron
app://): register the schemestandard,secureandsupportFetchAPI(protocol.registerSchemesAsPrivileged) — the vault needs a real origin and a secure context, and Spark needsfetchofspark.wasm. Passkeys are unavailable there: Chromium admits WebAuthn only on https,localhostand extension origins, so create the vault behind a password factor.dist/kernel.manifest.jsonpins the artifacts the package was built from. Consumer bundlers re-bundle the worker, so the shipped bytes differ from the manifest's;spark.wasmis still verified by its integrity constant when the kernel loads it.@qntx/wallet/hostis for host packages only — not application API.
License
MIT OR Apache-2.0
