@uverify/sdk 0.1.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.
@@ -0,0 +1,437 @@
1
+ /**
2
+ * A single certificate entry as stored on-chain and returned by the verification endpoints.
3
+ */
4
+ interface CertificateResponse {
5
+ /** SHA-256 or SHA-512 hash of the certified data. */
6
+ hash: string;
7
+ /** Cardano address that issued the certificate. */
8
+ address: string;
9
+ /** Hash of the block that contains this certificate. */
10
+ blockHash: string;
11
+ /** Block height at which the certificate was recorded. */
12
+ blockNumber: number;
13
+ /** Transaction hash on the Cardano blockchain. */
14
+ transactionHash: string;
15
+ /** Slot number at which the certificate was recorded. */
16
+ slot: number;
17
+ /** ISO-8601 timestamp of the block. */
18
+ creationTime: string;
19
+ /** Optional metadata attached to the certificate. */
20
+ metadata?: string;
21
+ /** Optional issuer identifier. */
22
+ issuer?: string;
23
+ }
24
+ /**
25
+ * Certificate data provided when building a new certification transaction.
26
+ */
27
+ interface CertificateData {
28
+ /** SHA-256 or SHA-512 hash of the data to be certified. */
29
+ hash: string;
30
+ /** Optional metadata string to attach to the certificate. */
31
+ metadata?: string;
32
+ /**
33
+ * Hashing algorithm used to produce the hash.
34
+ * Common values: "SHA-256", "SHA-512".
35
+ */
36
+ algorithm?: string;
37
+ }
38
+
39
+ /**
40
+ * The type of transaction to build.
41
+ *
42
+ * - `default` — standard certificate issuance against an existing state.
43
+ * - `bootstrap` — initialise a new state (fork) from a bootstrap datum.
44
+ * - `custom` — custom certificate transaction.
45
+ * - `burn_state` — burn the state token and close the workspace.
46
+ * - `burn_bootstrap` — burn a bootstrap datum token.
47
+ * - `init` — deploy the initial bootstrap datum.
48
+ * - `deploy` — deploy a library proxy contract.
49
+ */
50
+ type TransactionType = 'default' | 'bootstrap' | 'custom' | 'burn_state' | 'burn_bootstrap' | 'init' | 'deploy';
51
+ /** Status information returned alongside an unsigned transaction. */
52
+ interface BuildStatus {
53
+ message: string;
54
+ code: number;
55
+ }
56
+ /** Request body for `POST /api/v1/transaction/build`. */
57
+ interface BuildTransactionRequest {
58
+ /** Array of certificates to issue in this transaction. */
59
+ certificates: CertificateData[];
60
+ /** Transaction type. */
61
+ type: TransactionType;
62
+ /** Cardano address of the signer / payer. */
63
+ address: string;
64
+ /**
65
+ * Bootstrap datum reference. Required when `type` is `bootstrap`
66
+ * or when you want to specify which bootstrap datum to fork from.
67
+ */
68
+ bootstrapDatum?: Record<string, unknown>;
69
+ /** Existing state ID to update. Required for `default` and `burn_state` types. */
70
+ stateId?: string;
71
+ }
72
+ /** Response body for `POST /api/v1/transaction/build`. */
73
+ interface BuildTransactionResponse {
74
+ /** CBOR-hex encoded unsigned transaction, ready to be signed. */
75
+ unsignedTransaction: string;
76
+ /** Echoes back the transaction type. */
77
+ type: TransactionType;
78
+ /** Build status with a human-readable message and code. */
79
+ status: BuildStatus;
80
+ }
81
+
82
+ /**
83
+ * Available actions for user state management.
84
+ *
85
+ * - `USER_INFO` — retrieve current state information.
86
+ * - `INVALIDATE_STATE` — mark the current state as invalid.
87
+ * - `OPT_OUT` — opt out and remove the user's state.
88
+ */
89
+ type UserAction = 'USER_INFO' | 'INVALIDATE_STATE' | 'OPT_OUT';
90
+ /** Request body for `POST /api/v1/user/request/action`. */
91
+ interface UserActionRequest {
92
+ /** Cardano address of the user. */
93
+ address: string;
94
+ /** The action to perform. */
95
+ action: UserAction;
96
+ /** State ID to act on (required for INVALIDATE_STATE and OPT_OUT). */
97
+ stateId?: string;
98
+ }
99
+ /**
100
+ * Response from `POST /api/v1/user/request/action`.
101
+ * Contains a server-side signed message that the user must countersign
102
+ * before calling `POST /api/v1/user/state/action`.
103
+ */
104
+ interface UserActionRequestResponse {
105
+ address: string;
106
+ action: UserAction;
107
+ /** Server signature over the message. */
108
+ signature: string;
109
+ /** UNIX timestamp of the request. */
110
+ timestamp: number;
111
+ /** Message that the user must sign with their wallet. */
112
+ message: string;
113
+ status: number;
114
+ error?: string;
115
+ }
116
+ /** Request body for `POST /api/v1/user/state/action`. */
117
+ interface ExecuteUserActionRequest {
118
+ address: string;
119
+ action: UserAction;
120
+ stateId?: string;
121
+ /** Message returned by the request/action step. */
122
+ message: string;
123
+ /** Server signature from the request/action step. */
124
+ signature: string;
125
+ /** CIP-30 wallet signature over the message. */
126
+ userSignature: string;
127
+ /** Hex-encoded public key of the signing key. */
128
+ userPublicKey: string;
129
+ /** Timestamp from the request/action step. */
130
+ timestamp: number;
131
+ }
132
+ /** An on-chain certificate entry inside a state datum. */
133
+ interface UVerifyCertificate {
134
+ hash: string;
135
+ algorithm: string;
136
+ issuer: string;
137
+ extra: string[];
138
+ }
139
+ /** On-chain state datum representing a user's issuing workspace. */
140
+ interface UserState {
141
+ /** Unique identifier of this state (derived from an output reference). */
142
+ id: string;
143
+ /** Payment credential of the owner. */
144
+ owner: string;
145
+ fee: number;
146
+ feeInterval: number;
147
+ feeReceivers: string[];
148
+ /** UNIX timestamp — expiry of this state. */
149
+ ttl: number;
150
+ /** Certificates remaining before renewal is required. */
151
+ countdown: number;
152
+ certificates: UVerifyCertificate[];
153
+ batchSize: number;
154
+ bootstrapDatumName: string;
155
+ keepAsOracle: boolean;
156
+ }
157
+ /** Response from `POST /api/v1/user/state/action`. */
158
+ interface ExecuteUserActionResponse {
159
+ state?: UserState;
160
+ status: number;
161
+ error?: string;
162
+ /** Unsigned transaction hex when the action requires an on-chain transaction. */
163
+ unsignedTransaction?: string;
164
+ }
165
+
166
+ /**
167
+ * Callback used by high-level helpers for message-based user state actions.
168
+ *
169
+ * Receives the raw challenge message string from the server and must return
170
+ * a CIP-30-compatible data signature object (`{ key, signature }`).
171
+ *
172
+ * The callback is wallet-agnostic — pass any implementation that speaks
173
+ * CIP-30 (e.g. mesh.js, blaze, lucid, or a raw browser wallet API).
174
+ *
175
+ * @example CIP-30 browser wallet
176
+ * ```ts
177
+ * const signMessage: MessageSignCallback = async (message) => {
178
+ * const hexAddress = await api.getChangeAddress();
179
+ * const hexMessage = Array.from(message)
180
+ * .map((c) => c.charCodeAt(0).toString(16).padStart(2, '0'))
181
+ * .join('');
182
+ * return api.signData(hexAddress, hexMessage); // { key, signature }
183
+ * };
184
+ * ```
185
+ */
186
+ type MessageSignCallback = (message: string) => Promise<{
187
+ key: string;
188
+ signature: string;
189
+ }>;
190
+ /**
191
+ * Callback used by high-level helpers that require signing a transaction.
192
+ *
193
+ * Receives the CBOR-hex unsigned transaction and must return the CBOR-hex
194
+ * witness set after signing.
195
+ *
196
+ * @example CIP-30 browser wallet
197
+ * ```ts
198
+ * const signTx: TransactionSignCallback = (unsignedTx) =>
199
+ * api.signTx(unsignedTx, true); // partial = true
200
+ * ```
201
+ *
202
+ * @example mesh.js
203
+ * ```ts
204
+ * const signTx: TransactionSignCallback = (unsignedTx) =>
205
+ * wallet.signTx(unsignedTx, true);
206
+ * ```
207
+ */
208
+ type TransactionSignCallback = (unsignedTx: string) => Promise<string>;
209
+ /**
210
+ * Low-level API surface, accessible via {@link UVerifyClient.core}.
211
+ *
212
+ * Use these methods when you need fine-grained control over the request/response
213
+ * cycle (e.g. multi-sig flows, custom submission logic). For most use-cases
214
+ * the high-level helpers on {@link UVerifyClient} are sufficient.
215
+ */
216
+ interface UVerifyCore {
217
+ /**
218
+ * Build an unsigned transaction for certificate issuance or state management.
219
+ *
220
+ * The returned `unsignedTransaction` is a CBOR-hex encoded transaction that
221
+ * must be signed by the user's wallet before being submitted via
222
+ * {@link submitTransaction}.
223
+ */
224
+ buildTransaction(request: BuildTransactionRequest): Promise<BuildTransactionResponse>;
225
+ /**
226
+ * Submit a signed transaction to the Cardano blockchain.
227
+ *
228
+ * @param transaction - CBOR-hex encoded signed transaction.
229
+ * @param witnessSet - Optional separate witness set in CBOR-hex.
230
+ */
231
+ submitTransaction(transaction: string, witnessSet?: string): Promise<void>;
232
+ /**
233
+ * Create a server-signed action request that the user must countersign.
234
+ *
235
+ * This is step 1 of the two-step user-state-action flow.
236
+ */
237
+ requestUserAction(request: UserActionRequest): Promise<UserActionRequestResponse>;
238
+ /**
239
+ * Execute a user state action using the signatures from both parties.
240
+ *
241
+ * This is step 2 of the two-step flow.
242
+ */
243
+ executeUserAction(request: ExecuteUserActionRequest): Promise<ExecuteUserActionResponse>;
244
+ }
245
+ interface UVerifyClientOptions {
246
+ /** Base URL of the UVerify API. Defaults to `https://api.uverify.io`. */
247
+ baseUrl?: string;
248
+ /** Optional HTTP headers added to every request (e.g. an API key). */
249
+ headers?: Record<string, string>;
250
+ /**
251
+ * Default callback for message signing, used by {@link UVerifyClient.getUserInfo},
252
+ * {@link UVerifyClient.invalidateState}, and {@link UVerifyClient.optOut} when no
253
+ * per-call callback is provided.
254
+ *
255
+ * @example CIP-30 browser wallet
256
+ * ```ts
257
+ * signMessage: async (message) => {
258
+ * const hexAddress = await api.getChangeAddress();
259
+ * const hexMessage = Array.from(message)
260
+ * .map((c) => c.charCodeAt(0).toString(16).padStart(2, '0'))
261
+ * .join('');
262
+ * return api.signData(hexAddress, hexMessage);
263
+ * }
264
+ * ```
265
+ */
266
+ signMessage?: MessageSignCallback;
267
+ /**
268
+ * Default callback for transaction signing, used by {@link UVerifyClient.issueCertificates}
269
+ * when no per-call callback is provided.
270
+ *
271
+ * @example CIP-30 browser wallet
272
+ * ```ts
273
+ * signTx: (unsignedTx) => api.signTx(unsignedTx, true)
274
+ * ```
275
+ */
276
+ signTx?: TransactionSignCallback;
277
+ }
278
+ /**
279
+ * Main entry point for the UVerify SDK.
280
+ *
281
+ * @example
282
+ * ```ts
283
+ * import { UVerifyClient } from '@uverify/sdk';
284
+ *
285
+ * const client = new UVerifyClient();
286
+ *
287
+ * const certificates = await client.verify('a3b4c5...');
288
+ * console.log(certificates);
289
+ * ```
290
+ */
291
+ declare class UVerifyClient {
292
+ private readonly baseUrl;
293
+ private readonly defaultHeaders;
294
+ private readonly defaultSignMessage?;
295
+ private readonly defaultSignTx?;
296
+ /**
297
+ * Low-level API access. Use these methods when you need full control over
298
+ * the request/response cycle. For most use-cases the high-level helpers
299
+ * (e.g. {@link issueCertificates}, {@link getUserInfo}) are sufficient.
300
+ *
301
+ * @example
302
+ * ```ts
303
+ * const { unsignedTransaction } = await client.core.buildTransaction({ ... });
304
+ * const witnessSet = await wallet.signTx(unsignedTransaction, true);
305
+ * await client.core.submitTransaction(unsignedTransaction, witnessSet);
306
+ * ```
307
+ */
308
+ readonly core: UVerifyCore;
309
+ constructor(options?: UVerifyClientOptions);
310
+ private request;
311
+ private get;
312
+ private post;
313
+ /**
314
+ * Retrieve all on-chain certificates associated with the given data hash.
315
+ *
316
+ * @param hash - SHA-256 or SHA-512 hex hash of the data to look up.
317
+ * @returns Array of certificates, or an empty array if none exist.
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * const certs = await client.verify('a3b4c5d6...');
322
+ * if (certs.length > 0) {
323
+ * console.log('First issued at block', certs[0].blockNumber);
324
+ * }
325
+ * ```
326
+ */
327
+ verify(hash: string): Promise<CertificateResponse[]>;
328
+ /**
329
+ * Retrieve a single certificate by its Cardano transaction hash and data hash.
330
+ *
331
+ * @param transactionHash - 64-character hex transaction hash.
332
+ * @param dataHash - SHA-256 or SHA-512 hex hash of the certified data.
333
+ */
334
+ verifyByTransaction(transactionHash: string, dataHash: string): Promise<CertificateResponse>;
335
+ private _buildTransaction;
336
+ private _submitTransaction;
337
+ private _requestUserAction;
338
+ private _executeUserAction;
339
+ /**
340
+ * Issue certificates in a single step.
341
+ *
342
+ * Builds the unsigned transaction, passes it to `signCallback` to obtain a
343
+ * witness set, then submits the signed transaction to the blockchain.
344
+ *
345
+ * This is the recommended way to issue certificates when you control the full
346
+ * flow in one place. For more control (e.g. multi-sig flows) use
347
+ * {@link buildTransaction} and {@link submitTransaction} separately.
348
+ *
349
+ * @param address Cardano address of the signer / payer.
350
+ * @param stateId ID of the issuing state.
351
+ * @param certificates One or more certificates to certify.
352
+ * @param signCallback Callback that receives the unsigned transaction and
353
+ * returns the CIP-30 witness set CBOR hex.
354
+ *
355
+ * @example CIP-30 browser wallet
356
+ * ```ts
357
+ * await client.issueCertificates(
358
+ * 'addr1...',
359
+ * 'my-state-id',
360
+ * [{ hash: 'sha256-hash-of-doc', algorithm: 'SHA-256' }],
361
+ * (unsignedTx) => api.signTx(unsignedTx, true),
362
+ * );
363
+ * ```
364
+ *
365
+ * @example mesh.js
366
+ * ```ts
367
+ * await client.issueCertificates(
368
+ * 'addr1...', 'my-state-id',
369
+ * [{ hash: 'sha256-hash-of-doc', algorithm: 'SHA-256' }],
370
+ * (unsignedTx) => wallet.signTx(unsignedTx, true),
371
+ * );
372
+ * ```
373
+ */
374
+ issueCertificates(address: string, certificates: CertificateData[], signCallback?: TransactionSignCallback, stateId?: string): Promise<void>;
375
+ /**
376
+ * Retrieve the current state information for a user in a single step.
377
+ *
378
+ * Internally calls {@link requestUserAction} to obtain a server-signed
379
+ * challenge, invokes `signCallback` so the user's wallet can countersign,
380
+ * then calls {@link executeUserAction} and returns the resolved state.
381
+ *
382
+ * @param address Cardano address of the user.
383
+ * @param signCallback Callback that signs the challenge message and returns
384
+ * the CIP-30 `{ key, signature }` data signature object.
385
+ *
386
+ * @example CIP-30 browser wallet
387
+ * ```ts
388
+ * const state = await client.getUserInfo('addr1...', async (message) => {
389
+ * const hexAddress = await api.getChangeAddress();
390
+ * const hexMessage = Array.from(message)
391
+ * .map((c) => c.charCodeAt(0).toString(16).padStart(2, '0'))
392
+ * .join('');
393
+ * return api.signData(hexAddress, hexMessage);
394
+ * });
395
+ * console.log('Certificates left before renewal:', state?.countdown);
396
+ * ```
397
+ */
398
+ getUserInfo(address: string, signCallback?: MessageSignCallback): Promise<UserState | undefined>;
399
+ /**
400
+ * Invalidate a user state in a single step.
401
+ *
402
+ * @param address Cardano address of the user.
403
+ * @param stateId ID of the state to invalidate.
404
+ * @param signCallback Callback that signs the challenge message.
405
+ * Falls back to the `signMessage` set in the constructor.
406
+ */
407
+ invalidateState(address: string, stateId: string, signCallback?: MessageSignCallback): Promise<ExecuteUserActionResponse>;
408
+ /**
409
+ * Opt out of UVerify in a single step, removing the user's state.
410
+ *
411
+ * @param address Cardano address of the user.
412
+ * @param stateId ID of the state to remove.
413
+ * @param signCallback Callback that signs the challenge message.
414
+ * Falls back to the `signMessage` set in the constructor.
415
+ */
416
+ optOut(address: string, stateId: string, signCallback?: MessageSignCallback): Promise<ExecuteUserActionResponse>;
417
+ /**
418
+ * Internal helper that runs the two-step user state action flow:
419
+ * request challenge → user signs → execute action.
420
+ */
421
+ private _performUserStateAction;
422
+ private _resolveMessageCallback;
423
+ private _resolveTxCallback;
424
+ }
425
+
426
+ /** Thrown when the UVerify API returns a non-2xx response. */
427
+ declare class UVerifyApiError extends Error {
428
+ readonly statusCode: number;
429
+ readonly responseBody?: unknown | undefined;
430
+ constructor(message: string, statusCode: number, responseBody?: unknown | undefined);
431
+ }
432
+ /** Thrown when a required parameter is missing or invalid. */
433
+ declare class UVerifyValidationError extends Error {
434
+ constructor(message: string);
435
+ }
436
+
437
+ export { type BuildStatus, type BuildTransactionRequest, type BuildTransactionResponse, type CertificateData, type CertificateResponse, type ExecuteUserActionRequest, type ExecuteUserActionResponse, type MessageSignCallback, type TransactionSignCallback, type TransactionType, UVerifyApiError, type UVerifyCertificate, UVerifyClient, type UVerifyClientOptions, type UVerifyCore, UVerifyValidationError, type UserAction, type UserActionRequest, type UserActionRequestResponse, type UserState };