@hauska-sdk/core 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,433 @@
1
+ # API Reference
2
+
3
+ Complete API documentation for the CNS Protocol Core SDK.
4
+
5
+ ## CNSSDK
6
+
7
+ The main class for the unified CNS Protocol SDK.
8
+
9
+ ### Constructor
10
+
11
+ ```typescript
12
+ new CNSSDK(config: CNSSDKConfig)
13
+ ```
14
+
15
+ **Parameters:**
16
+ - `config` - SDK configuration object
17
+
18
+ **Example:**
19
+ ```typescript
20
+ const sdk = new CNSSDK({
21
+ vda: {
22
+ storageAdapter: new PostgreSQLStorageAdapter({ /* ... */ }),
23
+ },
24
+ payment: {
25
+ storage: new PostgreSQLStorageAdapter({ /* ... */ }),
26
+ },
27
+ retrieval: {
28
+ pinata: {
29
+ pinataJwt: process.env.PINATA_JWT!,
30
+ },
31
+ },
32
+ });
33
+ ```
34
+
35
+ ### Methods
36
+
37
+ #### `getOrCreateWallet(options: WalletOptions): Promise<Wallet>`
38
+
39
+ Gets or creates a wallet for a user.
40
+
41
+ **Parameters:**
42
+ - `options.userId` - User ID (required)
43
+ - `options.password` - Password for wallet encryption (required)
44
+ - `options.autoCreate` - Whether to create wallet if it doesn't exist (default: `true`)
45
+
46
+ **Returns:** `Promise<Wallet>`
47
+
48
+ **Throws:** `WalletError` if wallet creation fails
49
+
50
+ **Example:**
51
+ ```typescript
52
+ const wallet = await sdk.getOrCreateWallet({
53
+ userId: "user-123",
54
+ password: "secure-password",
55
+ });
56
+ ```
57
+
58
+ #### `purchaseAndMintVDA(options: PurchaseAndMintVDAOptions): Promise<{payment: PaymentRecord, vda: VDA, wallet: {address: string}}>`
59
+
60
+ Purchases and mints a VDA in a single operation.
61
+
62
+ **Parameters:**
63
+ - `options.wallet` - Wallet options
64
+ - `options.amount` - Payment amount (string)
65
+ - `options.currency` - Payment currency (e.g., "USDC")
66
+ - `options.method` - Payment method ("crypto" | "fiat")
67
+ - `options.vdaParams` - VDA minting parameters
68
+ - `options.paymentMetadata` - Optional payment metadata
69
+ - `options.vdaMetadata` - Optional VDA metadata
70
+
71
+ **Returns:** `Promise<{payment: PaymentRecord, vda: VDA, wallet: {address: string}}>`
72
+
73
+ **Throws:** `PaymentError` or `VDAError` on failure
74
+
75
+ **Example:**
76
+ ```typescript
77
+ const result = await sdk.purchaseAndMintVDA({
78
+ wallet: { userId: "user", password: "pass" },
79
+ amount: "5000",
80
+ currency: "USDC",
81
+ method: "crypto",
82
+ vdaParams: {
83
+ assetType: "blueprint",
84
+ address: "123 Main St",
85
+ spoke: "architect",
86
+ },
87
+ });
88
+ ```
89
+
90
+ #### `createDataRoom(options: CreateDataRoomOptions): Promise<{vda: VDA, document?: UploadDocumentResult, accessPass?: VDA}>`
91
+
92
+ Creates a data room (VDA + optional document + optional access pass).
93
+
94
+ **Parameters:**
95
+ - `options.ownerWallet` - Wallet options for the owner
96
+ - `options.vdaParams` - VDA parameters
97
+ - `options.document` - Optional document to upload
98
+ - `options.accessPass` - Optional access pass configuration
99
+
100
+ **Returns:** `Promise<{vda: VDA, document?: UploadDocumentResult, accessPass?: VDA}>`
101
+
102
+ **Throws:** `VDAError` or `RetrievalError` on failure
103
+
104
+ **Example:**
105
+ ```typescript
106
+ const dataRoom = await sdk.createDataRoom({
107
+ ownerWallet: { userId: "owner", password: "pass" },
108
+ vdaParams: {
109
+ assetType: "deed",
110
+ address: "123 Main St",
111
+ spoke: "real-estate",
112
+ },
113
+ document: {
114
+ content: Buffer.from("..."),
115
+ encrypt: true,
116
+ },
117
+ accessPass: {
118
+ recipientWallet: { userId: "buyer", password: "pass" },
119
+ permissions: ["view", "download"],
120
+ expiry: Date.now() + 30 * 24 * 3600000,
121
+ },
122
+ });
123
+ ```
124
+
125
+ #### `retrieveDocument(options: RetrieveDocumentOptions): Promise<Buffer>`
126
+
127
+ Retrieves a document with full access control, decryption, and watermarking.
128
+
129
+ **Parameters:**
130
+ - `options.wallet` - Wallet options for the requester
131
+ - `options.cid` - IPFS Content Identifier
132
+ - `options.requiredPermissions` - Required permissions (default: `["view"]`)
133
+ - `options.decrypt` - Whether to decrypt (default: `false`)
134
+ - `options.watermark` - Whether to watermark (default: `false`)
135
+
136
+ **Returns:** `Promise<Buffer>`
137
+
138
+ **Throws:** `RetrievalError` if access is denied or document not found
139
+
140
+ **Example:**
141
+ ```typescript
142
+ const document = await sdk.retrieveDocument({
143
+ wallet: { userId: "user", password: "pass" },
144
+ cid: "QmTest123...",
145
+ requiredPermissions: ["view"],
146
+ decrypt: true,
147
+ watermark: true,
148
+ });
149
+ ```
150
+
151
+ #### `getPaymentSDK(): PaymentSDK`
152
+
153
+ Gets the underlying Payment SDK instance.
154
+
155
+ **Returns:** `PaymentSDK`
156
+
157
+ #### `getVDASDK(): VDASDK`
158
+
159
+ Gets the underlying VDA SDK instance.
160
+
161
+ **Returns:** `VDASDK`
162
+
163
+ #### `getRetrievalSDK(): RetrievalSDK`
164
+
165
+ Gets the underlying Retrieval SDK instance.
166
+
167
+ **Returns:** `RetrievalSDK`
168
+
169
+ #### `getWalletManager(): WalletManager`
170
+
171
+ Gets the wallet manager instance.
172
+
173
+ **Returns:** `WalletManager`
174
+
175
+ ## Types
176
+
177
+ ### CNSSDKConfig
178
+
179
+ SDK configuration object.
180
+
181
+ ```typescript
182
+ interface CNSSDKConfig {
183
+ walletManager?: WalletManager;
184
+ payment?: Omit<PaymentSDKConfig, "walletManager">;
185
+ vda: Omit<VDASDKConfig, "walletManager">;
186
+ retrieval: Omit<RetrievalSDKConfig, "vdaSdk">;
187
+ logHook?: LogHook;
188
+ metricsHook?: MetricsHook;
189
+ }
190
+ ```
191
+
192
+ ### WalletOptions
193
+
194
+ Wallet creation/retrieval options.
195
+
196
+ ```typescript
197
+ interface WalletOptions {
198
+ userId: string;
199
+ password: string;
200
+ autoCreate?: boolean;
201
+ }
202
+ ```
203
+
204
+ ### PurchaseAndMintVDAOptions
205
+
206
+ Options for purchasing and minting a VDA.
207
+
208
+ ```typescript
209
+ interface PurchaseAndMintVDAOptions {
210
+ wallet: WalletOptions;
211
+ amount: string;
212
+ currency: string;
213
+ method: "crypto" | "fiat";
214
+ vdaParams: {
215
+ assetType: AssetType;
216
+ ownerWallet?: string;
217
+ spoke: SpokeType;
218
+ address?: string;
219
+ legalDesc?: string;
220
+ patientId?: string;
221
+ api14?: string;
222
+ [key: string]: any;
223
+ };
224
+ paymentMetadata?: Record<string, any>;
225
+ vdaMetadata?: Record<string, any>;
226
+ }
227
+ ```
228
+
229
+ ### CreateDataRoomOptions
230
+
231
+ Options for creating a data room.
232
+
233
+ ```typescript
234
+ interface CreateDataRoomOptions {
235
+ ownerWallet: WalletOptions;
236
+ vdaParams: {
237
+ assetType: AssetType;
238
+ spoke: SpokeType;
239
+ address?: string;
240
+ legalDesc?: string;
241
+ patientId?: string;
242
+ api14?: string;
243
+ [key: string]: any;
244
+ };
245
+ document?: {
246
+ content: Buffer;
247
+ name?: string;
248
+ encrypt?: boolean;
249
+ };
250
+ accessPass?: {
251
+ recipientWallet: WalletOptions;
252
+ permissions: string[];
253
+ expiry: number;
254
+ };
255
+ }
256
+ ```
257
+
258
+ ### RetrieveDocumentOptions
259
+
260
+ Options for retrieving a document.
261
+
262
+ ```typescript
263
+ interface RetrieveDocumentOptions {
264
+ wallet: WalletOptions;
265
+ cid: string;
266
+ requiredPermissions?: string[];
267
+ decrypt?: boolean;
268
+ watermark?: boolean;
269
+ }
270
+ ```
271
+
272
+ ## Error Handling
273
+
274
+ The SDK uses structured error types for better error handling.
275
+
276
+ ### Error Types
277
+
278
+ #### SDKError
279
+
280
+ Base error class for all SDK errors.
281
+
282
+ ```typescript
283
+ class SDKError extends Error {
284
+ code: string;
285
+ context?: Record<string, any>;
286
+ statusCode?: number;
287
+ retryable: boolean;
288
+ }
289
+ ```
290
+
291
+ #### PaymentError
292
+
293
+ Payment-related errors.
294
+
295
+ ```typescript
296
+ class PaymentError extends SDKError {
297
+ // HTTP 402 status code
298
+ }
299
+ ```
300
+
301
+ #### VDAError
302
+
303
+ VDA-related errors.
304
+
305
+ ```typescript
306
+ class VDAError extends SDKError {
307
+ // HTTP 400 status code
308
+ }
309
+ ```
310
+
311
+ #### RetrievalError
312
+
313
+ Retrieval-related errors.
314
+
315
+ ```typescript
316
+ class RetrievalError extends SDKError {
317
+ // HTTP 404 status code
318
+ }
319
+ ```
320
+
321
+ #### WalletError
322
+
323
+ Wallet-related errors.
324
+
325
+ ```typescript
326
+ class WalletError extends SDKError {
327
+ // HTTP 401 status code
328
+ }
329
+ ```
330
+
331
+ #### ConfigurationError
332
+
333
+ Configuration-related errors.
334
+
335
+ ```typescript
336
+ class ConfigurationError extends SDKError {
337
+ // HTTP 500 status code
338
+ }
339
+ ```
340
+
341
+ ### Error Codes
342
+
343
+ Common error codes:
344
+
345
+ - `PAYMENT_VERIFICATION_FAILED` - Payment verification failed
346
+ - `VDA_NOT_FOUND` - VDA not found
347
+ - `VDA_ACCESS_DENIED` - Access denied to VDA
348
+ - `RETRIEVAL_DOCUMENT_NOT_FOUND` - Document not found
349
+ - `RETRIEVAL_ACCESS_DENIED` - Access denied to document
350
+ - `WALLET_NOT_FOUND` - Wallet not found
351
+ - `CONFIG_MISSING_REQUIRED` - Required configuration missing
352
+
353
+ ### Error Handling Example
354
+
355
+ ```typescript
356
+ import {
357
+ SDKError,
358
+ PaymentError,
359
+ VDAError,
360
+ RetrievalError,
361
+ isSDKError,
362
+ isRetryableError,
363
+ } from "@hauska-sdk/core";
364
+
365
+ try {
366
+ await sdk.purchaseAndMintVDA({ /* ... */ });
367
+ } catch (error) {
368
+ if (isSDKError(error)) {
369
+ console.error(`Error code: ${error.code}`);
370
+ console.error(`Context:`, error.context);
371
+
372
+ if (error instanceof PaymentError) {
373
+ // Handle payment error
374
+ } else if (error instanceof VDAError) {
375
+ // Handle VDA error
376
+ }
377
+
378
+ if (isRetryableError(error)) {
379
+ // Retry logic
380
+ }
381
+ } else {
382
+ // Handle unexpected error
383
+ }
384
+ }
385
+ ```
386
+
387
+ ## Logging and Monitoring
388
+
389
+ ### Log Hook
390
+
391
+ ```typescript
392
+ type LogHook = (level: "info" | "warn" | "error", message: string, data?: any) => void;
393
+ ```
394
+
395
+ **Example:**
396
+ ```typescript
397
+ const sdk = new CNSSDK({
398
+ // ... config
399
+ logHook: (level, message, data) => {
400
+ console.log(`[${level}] ${message}`, data);
401
+ },
402
+ });
403
+ ```
404
+
405
+ ### Metrics Hook
406
+
407
+ ```typescript
408
+ type MetricsHook = (event: string, data?: Record<string, any>) => void;
409
+ ```
410
+
411
+ **Example:**
412
+ ```typescript
413
+ const sdk = new CNSSDK({
414
+ // ... config
415
+ metricsHook: (event, data) => {
416
+ analytics.track(event, data);
417
+ },
418
+ });
419
+ ```
420
+
421
+ **Common Events:**
422
+ - `cns_sdk_initialized` - SDK initialized
423
+ - `payment_sdk_initialized` - Payment SDK initialized
424
+ - `vda_sdk_initialized` - VDA SDK initialized
425
+ - `retrieval_sdk_initialized` - Retrieval SDK initialized
426
+ - `wallet_retrieved_or_created` - Wallet operation
427
+
428
+ ## Related Documentation
429
+
430
+ - [Payment SDK API](../payment/README.md)
431
+ - [VDA SDK API](../vda/README.md)
432
+ - [Retrieval SDK API](../retrieval/README.md)
433
+ - [Wallet Management API](../wallet/README.md)