@provablehq/veil-aleo-sdk 0.6.0 → 0.7.1

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.
@@ -0,0 +1,362 @@
1
+ import { ProvingConfig, Client, WalletActions, RecordProvider } from '@provablehq/veil-core';
2
+ import { ApiAuthConfig } from '@provablehq/sdk';
3
+
4
+ /**
5
+ * Credentials issued by the Provable API for a registered consumer.
6
+ *
7
+ * Authenticate delegated proving and the hosted Record Scanner Service. The
8
+ * pair is minted by {@link registerProvableApi} and exchanged for short-lived
9
+ * JWTs.
10
+ *
11
+ * @property consumerId Consumer id. Forms the path segment when minting JWTs.
12
+ * @property apiKey API key. Returned once at registration and unrecoverable
13
+ * afterward, so a caller MUST persist it.
14
+ */
15
+ type ProvableApiCredentials = {
16
+ consumerId: string;
17
+ apiKey: string;
18
+ };
19
+ /**
20
+ * Persists Provable API credentials between runs.
21
+ *
22
+ * Implemented by the caller — a file, a keychain, `localStorage`, or a secret
23
+ * manager are all valid, and the choice belongs to the runtime rather than to
24
+ * the SDK. A session reads through `load` on first use and writes through
25
+ * `save` exactly once, immediately after registering a new consumer.
26
+ *
27
+ * @property load Reads stored credentials. Returning `undefined` means no
28
+ * consumer is registered yet and triggers registration.
29
+ * @property save Writes credentials. The API key is unrecoverable if this
30
+ * write is lost, so a failure here should propagate rather than be swallowed.
31
+ *
32
+ * @example
33
+ * const store: ProvableCredentialStore = {
34
+ * load: async () => JSON.parse(await readFile(path, 'utf8')).provableApi,
35
+ * save: async (c) => writeFile(path, JSON.stringify({ provableApi: c }), { mode: 0o600 }),
36
+ * }
37
+ */
38
+ type ProvableCredentialStore = {
39
+ load: () => Promise<ProvableApiCredentials | undefined> | ProvableApiCredentials | undefined;
40
+ save: (credentials: ProvableApiCredentials) => Promise<void> | void;
41
+ };
42
+ /**
43
+ * Builds a credential store that keeps credentials for the life of the process.
44
+ *
45
+ * The default when a client is given no credentials and no store, and the right
46
+ * choice for tests and short-lived workers. Suited to any runtime, since it
47
+ * touches no storage.
48
+ *
49
+ * A consumer registered into this store is lost when the process exits, and its
50
+ * API key is issued once — so a process that registers here and runs again
51
+ * registers a second consumer that nobody can reclaim. Anything longer-lived
52
+ * than a single run belongs in a persistent store: `fileCredentialStore` from
53
+ * `@provablehq/veil-aleo-sdk/node`, or a caller-supplied
54
+ * {@link ProvableCredentialStore}.
55
+ *
56
+ * @param initial Optional credentials to start with, so a caller can seed the
57
+ * store from an environment variable and skip registration.
58
+ * @returns A store backed by a closure variable.
59
+ *
60
+ * @example
61
+ * const store = memoryCredentialStore()
62
+ * // or seeded, in which case nothing registers:
63
+ * const seeded = memoryCredentialStore({ consumerId, apiKey })
64
+ */
65
+ declare function memoryCredentialStore(initial?: ProvableApiCredentials): ProvableCredentialStore;
66
+ /**
67
+ * Provisioned-key authentication for the edge Provable API gateway.
68
+ *
69
+ * The keyed variant of the Provable SDK's `ApiAuthConfig`, derived rather
70
+ * than restated so the two cannot drift — values of this type pass straight
71
+ * into the SDK's `RecordScanner` and delegated proving as their `auth`
72
+ * option, where the SDK applies the header default (`DEFAULT_API_KEY_HEADER`).
73
+ *
74
+ * The edge gateway (`edge.provable.com`) runs a different auth model from
75
+ * `api.provable.com`: there is no consumer registration and no JWT minting.
76
+ * An operator hands out API keys, and every request carries the key verbatim
77
+ * in a header. Nothing registers, persists, or refreshes, and a rejected
78
+ * request (401) means the key is invalid or revoked — retrying cannot help,
79
+ * and only the operator can issue a replacement.
80
+ *
81
+ * Mutually exclusive with the session options (`credentials`, `store`,
82
+ * `username`, `session`): those describe the registered-consumer lifecycle,
83
+ * which a provisioned key does not have. Combining them throws at
84
+ * construction.
85
+ *
86
+ * @example
87
+ * const auth: ProvableKeyedAuth = { mode: 'api-key', value: process.env.PROVABLE_API_KEY! }
88
+ */
89
+ type ProvableKeyedAuth = Extract<ApiAuthConfig, {
90
+ mode: 'api-key';
91
+ }>;
92
+ /**
93
+ * A minted Provable API JWT and its expiry.
94
+ *
95
+ * Structurally identical to the Provable SDK's `JWTData` and
96
+ * `RecordScannerJWTData`, so a value of this type passes directly as their
97
+ * `jwtData` option.
98
+ *
99
+ * @property jwt The `Authorization` header value, verbatim as issued by the
100
+ * API (Bearer-prefixed).
101
+ * @property expiration Expiry as milliseconds since the Unix epoch.
102
+ */
103
+ type ProvableJwt = {
104
+ jwt: string;
105
+ expiration: number;
106
+ };
107
+ /**
108
+ * The consumers a session has been wired into.
109
+ *
110
+ * Reported by {@link authenticateProvableApi} so a caller can tell which paths
111
+ * one authentication call actually covers.
112
+ *
113
+ * @property proving Whether a proving configuration carries this session.
114
+ * @property recordScanning Whether a record provider carries this session.
115
+ */
116
+ type ProvableSessionConsumers = {
117
+ proving: boolean;
118
+ recordScanning: boolean;
119
+ };
120
+ /**
121
+ * A live Provable API session: consumer credentials plus a cached, refreshing JWT.
122
+ *
123
+ * Built by `createProvingConfig`, `createRemoteScanner`, and
124
+ * `createAleoClient` from the credential options they are given — a caller
125
+ * configures credentials and does not construct this directly. Sharing one
126
+ * session across delegated proving and record scanning means a single minted
127
+ * JWT and a single refresh policy for both.
128
+ *
129
+ * @property registeredConsumer Reports whether this session registered a new
130
+ * consumer rather than loading an existing one. Only meaningful after
131
+ * credentials have resolved.
132
+ * @property getCredentials Resolves the credentials, registering on first use
133
+ * when neither direct credentials nor a store supply them.
134
+ * @property getJwt Returns a JWT valid for at least the expiry margin,
135
+ * minting or refreshing as needed.
136
+ * @property consumers Which consumers carry this session. Advisory reporting;
137
+ * nothing reads it to make decisions. `recordScanning` is set where a record
138
+ * provider is wired to a client, so sharing one session across several
139
+ * clients under-reports rather than claiming a path a given client lacks.
140
+ * @property attach Records that a consumer now carries this session. Called by
141
+ * the factories during wiring.
142
+ */
143
+ type ProvableSession = {
144
+ registeredConsumer: () => boolean;
145
+ getCredentials: (options?: {
146
+ username?: string;
147
+ }) => Promise<ProvableApiCredentials>;
148
+ getJwt: (options?: {
149
+ forceRefresh?: boolean;
150
+ }) => Promise<ProvableJwt>;
151
+ consumers: ProvableSessionConsumers;
152
+ attach: (consumer: keyof ProvableSessionConsumers) => void;
153
+ };
154
+ /**
155
+ * Options for {@link registerProvableApi}.
156
+ *
157
+ * @property username Handle for the consumer. Globally unique across the
158
+ * Provable API, so a taken name fails the call.
159
+ * @property baseUrl Optional Provable API root. Defaults to
160
+ * `https://api.provable.com`. Applies when targeting a non-production
161
+ * deployment.
162
+ * @property transport Optional fetch-compatible transport for the request.
163
+ * Defaults to the global `fetch`. Applies when a caller intercepts or
164
+ * instruments HTTP — a proxy, a recorder, a test stub.
165
+ */
166
+ type RegisterProvableApiParameters = {
167
+ username: string;
168
+ baseUrl?: string;
169
+ transport?: typeof fetch;
170
+ };
171
+ /**
172
+ * Options for {@link createProvableSession}.
173
+ *
174
+ * @property credentials Optional credentials to use directly. Take precedence
175
+ * over `store`, so an operator can inject a rotated or CI-provided pair
176
+ * without clearing persisted state first.
177
+ * @property store Optional persistence for credentials across runs. Omit for a
178
+ * consumer that lives only as long as the process.
179
+ * @property username Optional handle to register under when neither
180
+ * `credentials` nor `store` yields a pair. A function is called lazily, so a
181
+ * caller can derive the name from an account address that is not known at
182
+ * configuration time. Required only if registration may happen.
183
+ * @property baseUrl Optional Provable API root. Defaults to
184
+ * `https://api.provable.com`.
185
+ * @property transport Optional fetch-compatible transport used for
186
+ * registration and JWT minting. Defaults to the global `fetch`. Applies
187
+ * when a caller intercepts or instruments HTTP — a proxy, a recorder, a
188
+ * test stub.
189
+ */
190
+ type CreateProvableSessionOptions = {
191
+ credentials?: ProvableApiCredentials;
192
+ store?: ProvableCredentialStore;
193
+ username?: string | (() => string);
194
+ baseUrl?: string;
195
+ transport?: typeof fetch;
196
+ };
197
+ /**
198
+ * Options for {@link authenticateProvableApi}.
199
+ *
200
+ * @property username Optional handle to register under when the client's
201
+ * configuration yields no credentials. Overrides the name configured on the
202
+ * session.
203
+ * @property forceRefresh Mint a fresh JWT even when the cached one is still
204
+ * valid. Defaults to false. Applies when recovering from a rejected token.
205
+ */
206
+ type AuthenticateProvableApiParameters = {
207
+ username?: string;
208
+ forceRefresh?: boolean;
209
+ };
210
+ /**
211
+ * Result of {@link authenticateProvableApi}.
212
+ *
213
+ * @property credentials The resolved consumer credentials. Worth persisting
214
+ * when `registered` is true — the API key is unrecoverable afterward.
215
+ * @property expiration Expiry of the minted JWT, as milliseconds since the
216
+ * Unix epoch.
217
+ * @property registered Whether this call registered a new consumer rather than
218
+ * loading an existing one.
219
+ * @property applied Which paths the session reaches. `recordScanning` is false
220
+ * when the client was given a record provider that cannot accept a session —
221
+ * any implementation other than the ones this package builds — in which case
222
+ * that provider keeps using the credentials it was constructed with.
223
+ */
224
+ type AuthenticateProvableApiReturnType = {
225
+ credentials: ProvableApiCredentials;
226
+ expiration: number;
227
+ registered: boolean;
228
+ applied: ProvableSessionConsumers;
229
+ };
230
+ /**
231
+ * The Provable API authentication action, merged into a client by `extend`.
232
+ *
233
+ * @property authenticateProvableApi Resolves the client's Provable API session.
234
+ */
235
+ type ProvableApiActions = {
236
+ authenticateProvableApi: (params?: AuthenticateProvableApiParameters) => Promise<AuthenticateProvableApiReturnType>;
237
+ };
238
+ /**
239
+ * A wallet client carrying the Provable API authentication action.
240
+ *
241
+ * Composed inside the client's action set rather than intersected onto
242
+ * `WalletClient`, so a caller who extends further — adding DEX actions, for
243
+ * example — keeps `authenticateProvableApi` in the resulting type. `extend`
244
+ * carries forward only what sits in the action set, so an outer intersection
245
+ * would be dropped on the next call.
246
+ *
247
+ * The wallet half is restated rather than derived. `Omit<WalletClient, keyof
248
+ * Client>` reads better and was tried first, but `keyof Client` resolves to
249
+ * `never` against core's built declarations — so the Omit keeps every base field,
250
+ * violates the `Extended` constraint, and silently collapses to a type missing
251
+ * every wallet action. It typechecks against core's source and fails only for
252
+ * consumers, which is the worst place to find out.
253
+ *
254
+ * Keep this in step if core changes what a wallet client carries; a
255
+ * `WalletClientActions` export from core would remove the duplication safely.
256
+ */
257
+ type ProvableWalletClient = Client<WalletActions & {
258
+ recordProvider: RecordProvider | undefined;
259
+ } & ProvableApiActions>;
260
+ /**
261
+ * A proving configuration carrying the Provable API session.
262
+ *
263
+ * `createProvingConfig` returns this shape. Core types `Client.proving` as the
264
+ * bare {@link ProvingConfig} and never reads binding-specific fields — `url` and
265
+ * `apiKey` already travel the same way — so the session rides along without a
266
+ * core change, and {@link authenticateProvableApi} narrows to read it.
267
+ *
268
+ * @property session The session shared with record scanning, or `undefined`
269
+ * when the client was configured without credentials.
270
+ * @property keyedAuth The provisioned-key auth the client was configured
271
+ * with, or `undefined` under the session model. Mutually exclusive with
272
+ * `session`.
273
+ */
274
+ type ProvingConfigWithSession = ProvingConfig & {
275
+ session?: ProvableSession | undefined;
276
+ keyedAuth?: ProvableKeyedAuth | undefined;
277
+ };
278
+ /**
279
+ * Registers a Provable API consumer and returns its credentials.
280
+ *
281
+ * Unauthenticated — this is the call that issues the credentials everything
282
+ * else authenticates with. Hits the network.
283
+ *
284
+ * A username is spent once. It is globally unique, the API exposes no endpoint
285
+ * that reads a consumer back, and a duplicate registration answers 409 with
286
+ * nothing usable in it — so a taken name cannot be traded for the credentials it
287
+ * belongs to, and the only remedy is the stored key or a different name.
288
+ *
289
+ * @param params Handle to register under, and optionally a non-default API root.
290
+ * @returns The consumer id and API key. The key is shown only here, so the
291
+ * caller MUST persist it.
292
+ * @throws When the username is already registered, when registration returns any
293
+ * other non-2xx status, or when the response body does not carry a consumer id
294
+ * and key.
295
+ *
296
+ * @example
297
+ * const credentials = await registerProvableApi({ username: 'my-bot-42' })
298
+ * await writeFile('creds.json', JSON.stringify(credentials))
299
+ */
300
+ declare function registerProvableApi(params: RegisterProvableApiParameters): Promise<ProvableApiCredentials>;
301
+ /**
302
+ * Builds a Provable API session that resolves credentials and refreshes its JWT.
303
+ *
304
+ * Credentials resolve on first use — supplied directly, else loaded from the
305
+ * store, else registered and saved. Registration and minting are each
306
+ * single-flighted, so a cold client that proves and scans concurrently
307
+ * registers once and mints once. Pure and local until the first
308
+ * `getCredentials` or `getJwt` call.
309
+ *
310
+ * @param options Credential source, optional persistence, and the name to
311
+ * register under.
312
+ * @returns A session for `createProvingConfig`, `createRemoteScanner`, and
313
+ * `createAleoClient` to share.
314
+ *
315
+ * @example
316
+ * const session = createProvableSession({ store, username: 'my-bot-42' })
317
+ * const { jwt } = await session.getJwt()
318
+ */
319
+ declare function createProvableSession(options?: CreateProvableSessionOptions): ProvableSession;
320
+ /**
321
+ * Resolves the Provable API session backing delegated proving and record scanning.
322
+ *
323
+ * Registers a consumer when the client's configuration yields none, mints a
324
+ * JWT, and leaves both on the session the client's proving configuration and
325
+ * record provider already hold — so proving and scanning authenticate from then
326
+ * on without further setup. Optional: the first prove or scan resolves the same
327
+ * session lazily. Calling it explicitly front-loads registration, surfaces
328
+ * credential failures before a transaction is built, and returns a newly issued
329
+ * API key at the one moment it is recoverable.
330
+ *
331
+ * Hits the network: registration on first run, plus one JWT mint.
332
+ *
333
+ * @param client A client whose proving configuration carries Provable API
334
+ * credentials or a credential store.
335
+ * @param params Optional registration name and forced refresh.
336
+ * @returns The credentials, the JWT expiry, whether a consumer was registered,
337
+ * and which paths the session reaches.
338
+ * @throws When the client has no Provable API session configured, or when
339
+ * registration or minting fails.
340
+ *
341
+ * @example
342
+ * const { credentials, registered } = await client.authenticateProvableApi()
343
+ * if (registered) await store.save(credentials)
344
+ */
345
+ declare function authenticateProvableApi(client: Client, params?: AuthenticateProvableApiParameters): Promise<AuthenticateProvableApiReturnType>;
346
+ /**
347
+ * Builds the Provable API auth decorator for `client.extend()`.
348
+ *
349
+ * `createAleoClient` applies this already. Applies directly when composing a
350
+ * client by hand from `createWalletClient` and a proving configuration built
351
+ * with credentials.
352
+ *
353
+ * @returns A decorator: pass it to `client.extend(...)`.
354
+ *
355
+ * @example
356
+ * const client = createWalletClient({ account, transport, proving })
357
+ * .extend(provableApiActions())
358
+ * await client.authenticateProvableApi()
359
+ */
360
+ declare function provableApiActions(): (client: Client) => ProvableApiActions;
361
+
362
+ 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@provablehq/veil-aleo-sdk",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Local signing and proving for the Veil Aleo SDK, backed by the Provable SDK.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -16,6 +16,10 @@
16
16
  ".": {
17
17
  "types": "./dist/index.d.ts",
18
18
  "import": "./dist/index.js"
19
+ },
20
+ "./node": {
21
+ "types": "./dist/node.d.ts",
22
+ "import": "./dist/node.js"
19
23
  }
20
24
  },
21
25
  "sideEffects": false,
@@ -29,16 +33,16 @@
29
33
  "access": "public"
30
34
  },
31
35
  "peerDependencies": {
32
- "@provablehq/veil-aleo-devnode": "^0.6.0",
33
- "@provablehq/veil-core": "^0.6.0"
36
+ "@provablehq/veil-core": ">=0.6.0 <1.0.0",
37
+ "@provablehq/veil-aleo-devnode": ">=0.6.0 <1.0.0"
34
38
  },
35
39
  "dependencies": {
36
40
  "@noble/hashes": "^1.7.2",
37
- "@provablehq/sdk": "^0.11.4",
41
+ "@provablehq/sdk": "^0.11.9",
38
42
  "@scure/bip39": "^1.4.0"
39
43
  },
40
44
  "devDependencies": {
41
- "@provablehq/veil-leo": "0.6.0"
45
+ "@provablehq/veil-leo": "0.7.1"
42
46
  },
43
47
  "scripts": {
44
48
  "build": "tsup",