import { ProvingConfig, Client, WalletActions, RecordProvider } from '@provablehq/veil-core'; import { ApiAuthConfig } from '@provablehq/sdk'; /** * Credentials for the legacy JWT model of the Provable API. * * Optional everywhere: the default gateway needs no consumer and mints no JWT. * A caller who targets a legacy gateway such as `api.provable.com` passes the * pair and the session mints short-lived JWTs from it at that gateway. Nothing * registers new consumers anymore. A provisioned key for the default gateway * goes through {@link ProvableKeyedAuth} instead. * * @property consumerId Consumer id. Forms the path segment when minting JWTs. * @property apiKey API key. Sent as `X-Provable-API-Key` on the mint. */ type ProvableApiCredentials = { consumerId: string; apiKey: string; }; /** * Persists Provable API credentials between runs. * * Implemented by the caller — a file, a keychain, `localStorage`, or a secret * manager are all valid. A session reads through `load` on first use and never * writes: nothing registers anymore, so `save` is only called by callers who * seed a store themselves. * * @property load Reads stored credentials, or `undefined` when none are held. * @property save Writes credentials. Only reached by a caller seeding the store. * * @example * const store: ProvableCredentialStore = { * load: async () => JSON.parse(await readFile(path, 'utf8')).provableApi, * save: async (c) => writeFile(path, JSON.stringify({ provableApi: c }), { mode: 0o600 }), * } */ type ProvableCredentialStore = { load: () => Promise | ProvableApiCredentials | undefined; save: (credentials: ProvableApiCredentials) => Promise | void; }; /** * Builds a credential store that keeps credentials for the life of the process. * * The default when a client is given no credentials and no store. Suited to any * runtime, since it touches no storage. * * @param initial Optional credentials to start with, so a caller can seed the * store from an environment variable. * @returns A store backed by a closure variable. * * @example * const store = memoryCredentialStore() * // or seeded: * const seeded = memoryCredentialStore({ consumerId, apiKey }) */ declare function memoryCredentialStore(initial?: ProvableApiCredentials): ProvableCredentialStore; /** * Provisioned-key authentication for the Provable API gateway. * * The keyed variant of the Provable SDK's `ApiAuthConfig`, derived rather * than restated so the two cannot drift — values of this type pass straight * into the SDK's `RecordScanner` and delegated proving as their `auth` * option, where the SDK applies the header default (`DEFAULT_API_KEY_HEADER`). * * The gateway is unauthenticated by default, so a key is optional. When an * operator hands one out, every request carries it verbatim in a header. * Nothing registers, persists, or refreshes, and a rejected request (401) * means the key is invalid or revoked — retrying cannot help, and only the * operator can issue a replacement. * * Mutually exclusive with the session options (`credentials`, `store`, * `username`, `session`): those belong to the legacy JWT model. Combining * them throws at construction. * * @example * const auth: ProvableKeyedAuth = { mode: 'api-key', value: process.env.PROVABLE_API_KEY! } */ type ProvableKeyedAuth = Extract; /** * A minted Provable API JWT and its expiry. * * Structurally identical to the Provable SDK's `JWTData` and * `RecordScannerJWTData`, so a value of this type passes directly as their * `jwtData` option. * * @property jwt The `Authorization` header value, verbatim as issued by the * API (Bearer-prefixed). * @property expiration Expiry as milliseconds since the Unix epoch. */ type ProvableJwt = { jwt: string; expiration: number; }; /** * The consumers a session has been wired into. * * Reported by {@link authenticateProvableApi} so a caller can tell which paths * one client's session reaches. * * @property proving Whether a proving configuration carries this session. * @property recordScanning Whether a record provider carries this session. */ type ProvableSessionConsumers = { proving: boolean; recordScanning: boolean; }; /** * A Provable API session: the configured credentials plus a cached, refreshing JWT. * * Built by `createProvingConfig`, `createRemoteScanner`, and * `createAleoClient` from the credential options they are given — a caller * configures credentials and does not construct this directly. With a * credential pair and a legacy gateway to mint at, the session mints and * refreshes JWTs; otherwise it is inert, since the default gateway needs * nothing. * * @property registeredConsumer Always false; nothing registers anymore. * @property getCredentials Resolves the supplied or stored credentials, or * `undefined` when the client holds none. * @property getJwt Returns a JWT valid for at least the expiry margin, minting * or refreshing as needed, or `undefined` when there are no credentials, no * legacy gateway to mint at, or the gateway answered the mint with 404. * @property consumers Which consumers carry this session. Advisory reporting; * nothing reads it to make decisions. * @property attach Records that a consumer now carries this session. Called by * the factories during wiring. */ type ProvableSession = { registeredConsumer: () => boolean; getCredentials: (options?: { username?: string; }) => Promise; getJwt: (options?: { forceRefresh?: boolean; }) => Promise; consumers: ProvableSessionConsumers; attach: (consumer: keyof ProvableSessionConsumers) => void; }; /** * Options for {@link registerProvableApi}. * * @property username Handle the retired flow registered under. Ignored. * @property baseUrl Ignored; kept so existing calls compile. * @property transport Ignored; kept so existing calls compile. */ type RegisterProvableApiParameters = { username: string; baseUrl?: string; transport?: typeof fetch; }; /** * Options for {@link createProvableSession}. * * @property credentials Optional credentials to mint from. Take precedence * over `store`, so an operator can inject a rotated pair without clearing * persisted state first. * @property store Optional store to read credentials from. Never written. * @property username Ignored; nothing registers anymore. * @property baseUrl Optional root of a legacy gateway that serves `/jwts`, * such as `https://api.provable.com`. Without it the session mints nothing, * because the default gateway has no JWT route. The factories derive it from * the prover or scanner URL the caller configured. * @property transport Optional fetch-compatible transport for the mint. * Defaults to the global `fetch`. Applies when a caller intercepts or * instruments HTTP — a proxy, a recorder, a test stub. */ type CreateProvableSessionOptions = { credentials?: ProvableApiCredentials; store?: ProvableCredentialStore; username?: string | (() => string); baseUrl?: string; transport?: typeof fetch; }; /** * Options for {@link authenticateProvableApi}. * * @property username Ignored; nothing registers anymore. * @property forceRefresh Mint a fresh JWT even when the cached one is still * valid. Defaults to false. Applies when recovering from a rejected token. * No effect without credentials. */ type AuthenticateProvableApiParameters = { username?: string; forceRefresh?: boolean; }; /** * Result of {@link authenticateProvableApi}. * * @property credentials The credentials the client was configured with, or * `undefined` when it holds none. * @property expiration Expiry of the minted JWT, as milliseconds since the * Unix epoch, or `undefined` when nothing mints one. * @property registered Always false; nothing registers anymore. * @property applied Which paths the client's session reaches. All false for a * keyed client or a client built without a session. */ type AuthenticateProvableApiReturnType = { credentials: ProvableApiCredentials | undefined; expiration: number | undefined; registered: boolean; applied: ProvableSessionConsumers; }; /** * The Provable API authentication action, merged into a client by `extend`. * * @property authenticateProvableApi Resolves the client's Provable API session. */ type ProvableApiActions = { authenticateProvableApi: (params?: AuthenticateProvableApiParameters) => Promise; }; /** * A wallet client carrying the Provable API authentication action. * * Composed inside the client's action set rather than intersected onto * `WalletClient`, so a caller who extends further — adding DEX actions, for * example — keeps `authenticateProvableApi` in the resulting type. `extend` * carries forward only what sits in the action set, so an outer intersection * would be dropped on the next call. * * The wallet half is restated rather than derived. `Omit` reads better and was tried first, but `keyof Client` resolves to * `never` against core's built declarations — so the Omit keeps every base field, * violates the `Extended` constraint, and silently collapses to a type missing * every wallet action. It typechecks against core's source and fails only for * consumers, which is the worst place to find out. * * Keep this in step if core changes what a wallet client carries; a * `WalletClientActions` export from core would remove the duplication safely. */ type ProvableWalletClient = Client; /** * A proving configuration carrying the Provable API session. * * `createProvingConfig` returns this shape. Core types `Client.proving` as the * bare {@link ProvingConfig} and never reads binding-specific fields — `url` and * `apiKey` already travel the same way — so the session rides along without a * core change, and {@link authenticateProvableApi} narrows to read it. * * @property session The session shared with record scanning, or `undefined` * when the client was configured without credentials. * @property keyedAuth The provisioned-key auth the client was configured * with, or `undefined` under the session model. Mutually exclusive with * `session`. */ type ProvingConfigWithSession = ProvingConfig & { session?: ProvableSession | undefined; keyedAuth?: ProvableKeyedAuth | undefined; }; /** * Formerly registered a Provable API consumer. Now a no-op. * * The default gateway needs no consumer, and the legacy gateway issues no new * ones through this SDK. Resolves without contacting the network. * * @deprecated Consumer registration is retired. Remove the call; pass a * consumer pair the caller already holds, or a provisioned key through * `auth`. * @param params Ignored. * @returns `undefined`. * * @example * const credentials = await registerProvableApi({ username: 'my-bot-42' }) * // credentials is undefined */ declare function registerProvableApi(params: RegisterProvableApiParameters): Promise; /** * Builds a Provable API session that mints JWTs from the credentials it is given. * * Credentials come from `credentials` or, failing that, from the store. With a * pair and a `baseUrl` the session mints a JWT on first use and refreshes it * near expiry, single-flighting concurrent mints so a cold client that proves * and scans together mints once. Without either it is inert: `getJwt` resolves * to `undefined` and nothing is requested, because the default gateway needs * no token. A root that answers the mint with 404 has no JWT route — a devnode, * a self-hosted edge — and the session goes inert from then on rather than * failing a client that never needed a token. Nothing registers a consumer in * any case. * * @param options Credential source, legacy mint root, and transport. * @returns A session for `createProvingConfig`, `createRemoteScanner`, and * `createAleoClient` to share. * * @example * const session = createProvableSession({ * credentials: { consumerId, apiKey }, * baseUrl: 'https://api.provable.com', * }) * const jwt = await session.getJwt() */ declare function createProvableSession(options?: CreateProvableSessionOptions): ProvableSession; /** * Resolves the Provable API session backing delegated proving and record scanning. * * With a credential pair aimed at a legacy gateway this mints the JWT eagerly, * so a bad key fails before a transaction is built, and reports the expiry. * Otherwise it is a no-op: the default gateway needs no token, so a keyed, * credential-less, or default-gateway client resolves immediately with no * expiry. Never throws for lack of a session, and never registers a consumer. * * @param client Any client. * @param params Optional forced refresh; `username` is ignored. * @returns The configured credentials or `undefined`, the JWT expiry or * `undefined`, `registered` false, and which paths the session reaches. * @throws When a configured pair fails to mint. * * @example * const { expiration, applied } = await client.authenticateProvableApi() */ declare function authenticateProvableApi(client: Client, params?: AuthenticateProvableApiParameters): Promise; /** * Builds the Provable API auth decorator for `client.extend()`. * * `createAleoClient` applies this already. Applies directly when composing a * client by hand from `createWalletClient` and a proving configuration. * * @returns A decorator: pass it to `client.extend(...)`. * * @example * const client = createWalletClient({ account, transport, proving }) * .extend(provableApiActions()) * await client.authenticateProvableApi() */ declare function provableApiActions(): (client: Client) => ProvableApiActions; export { type AuthenticateProvableApiParameters as A, type CreateProvableSessionOptions as C, type ProvableSession as P, type RegisterProvableApiParameters as R, type ProvableKeyedAuth as a, type ProvingConfigWithSession as b, type ProvableCredentialStore as c, type ProvableWalletClient as d, type AuthenticateProvableApiReturnType as e, type ProvableApiActions as f, type ProvableApiCredentials as g, type ProvableJwt as h, type ProvableSessionConsumers as i, authenticateProvableApi as j, createProvableSession as k, memoryCredentialStore as m, provableApiActions as p, registerProvableApi as r };