import type { ProductId } from "@parity/truapi"; import type { PermissionAuthorizationRuntime } from "./worker-permission-authorization.js"; /** Cancellable handle on one live render stream inside the core. */ export interface WorkerRendererSubscription { cancel(): void; free(): void; } /** One product-scoped core inside the worker. */ export interface WorkerProductRuntime { receiveFrame(frame: Uint8Array): Promise; /** Drop cached contact handles after a contact is removed or blocked. */ notifyContactsChanged(): void; dispose(): void; free(): void; /** Throws when the connection may not reach Chat. */ publishChatAction(action: Uint8Array): void; /** * Publish one action triggered inside a product-rendered body, as a * SCALE-encoded `HostRendererActionSubscribeItem`. Throws when the * connection may not reach the product's renderer. */ publishRendererAction(item: Uint8Array): void; /** * Start the host-initiated render subscription for one body. `request` is a * SCALE-encoded `ProductRendererRenderRequest`. `onUpdate` receives each * SCALE-encoded `RendererNode`, then exactly one of `onComplete` (last tree * stands) or `onError` (the product could not serve the render; the last * tree is partial). Terminals never arrive during the call itself; a request * the core refuses outright throws instead. */ render(request: Uint8Array, onUpdate: (node: Uint8Array) => void, onComplete: () => void, onError: (reason: string) => void): WorkerRendererSubscription; } /** What the host does with a product's worker after demand on it changed. */ export type WorkerTransition = "Start" | "Stop"; /** The long-lived runtime product cores are created from, in either role. */ export interface WorkerHostRuntime extends PermissionAuthorizationRuntime { productRuntime(product: unknown, coreCallbacks: unknown): WorkerProductRuntime; disconnectSession(): Promise; notifyContactsChanged(): void; /** Only on a core built with `test-host`. */ setSubmitPreimagesLocally?(local: boolean): void; /** * Take one reference on the product's worker. The first one reports * `"Start"` through the runtime's `requestWorkerTransition` callback. */ acquireWorker(productId: ProductId): void; /** * Release one reference. The last one reports `"Stop"` the same way. */ releaseWorker(productId: ProductId): void; /** * Forget one product: its grants, Allow once answers and keys, and the * values it wrote to product storage. */ clearProductState(productId: ProductId): Promise; free(): void; } /** The pairing-host runtime: the user's keys live in a paired wallet. */ export interface WorkerPairingHostRuntime extends WorkerHostRuntime { cancelPairing(): void; notifySessionStoreChanged(): void; sessionChatIdentityKey(): Uint8Array | undefined; deviceStatementKey(): Uint8Array | undefined; deviceEncryptionKey(): Promise; productSubtreePublicKey(productId: ProductId, timeoutMs?: number): Promise; activateStoredSession(): Promise; activateExternalSession(blob: Uint8Array): Promise; resetSessionState(): Promise; } /** * The signing-host runtime, present only in the `testing` WASM bundle. * * A signing host owns the user's keys and establishes sessions from local * entropy rather than by pairing with a wallet. The production `web` bundle is * built without it on purpose, so this is optional on the module surface. */ export interface WorkerSigningHostRuntime extends WorkerHostRuntime { /** Derived from the active session root, so it never waits. */ productSubtreePublicKey(productId: ProductId): Uint8Array | undefined; activateLocalSession(secret: Uint8Array): Promise; /** Only on a core built with `wasm-signing-host`. */ setGrantAllowancesUnchecked?(granted: boolean): void; /** Only on a core built with `wasm-signing-host`. */ setWithheldResources?(tags: string[]): void; /** * Activate and give the session a display name, which is what * `account.get_user_id` answers with. Optional: a core built before this * entry point existed exposes only {@link activateLocalSession}. */ activateLocalSessionWithIdentity?(secret: Uint8Array, liteUsername?: string | null): Promise; } /** Module surface the wasm-pack glue exports. */ export interface WasmModuleShape { default: (input?: unknown) => Promise; /** Validate and normalize an identifier using the core's canonical parser. */ parseProductId: (value: string) => ProductId; /** * Each runtime is built with `create`, which resolves once the core has read * its stack record from the host's core storage, or after a few seconds * without it. */ WasmPairingHostRuntime: { create(callbacks: unknown, hostConfig: unknown): Promise; }; /** Only in the `testing` bundle; see {@link WorkerSigningHostRuntime}. */ WasmSigningHostRuntime?: { create(callbacks: unknown, hostConfig: unknown): Promise; }; WasmProductRuntime: { create(callbacks: unknown, runtimeConfig: unknown): Promise; }; setLogLevel?: (level: string) => void; /** * Derive a product account public key from that product's hard-subtree * public key and a SCALE-encoded `DerivationIndex`. Pure: no runtime or * session needed, so a host can call it after `default()` alone. */ deriveProductAccountPublicKey: (productSubtreePublicKey: Uint8Array, derivationIndex: Uint8Array) => Uint8Array; /** SS58 address for a product account public key, at the core's prefix. */ productAccountAddress: (publicKey: Uint8Array) => string; /** * The core's own `TRUAPI_WIRE_SCHEMA_HASH`, exported by `truapi`'s wasm * bridge. Optional because `dist/wasm/web/` is gitignored and built by hand, so * a stale bundle predating the export is a normal state to find at runtime; a * core that cannot vouch for its table streams frames without a `schema` stamp * and the debugger groups them without decoding. */ wireSchemaHash?: () => string; }