@docknetwork/wallet-sdk-core 1.5.11 → 1.7.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.
Files changed (95) hide show
  1. package/generate-docs.js +33 -0
  2. package/jsdoc.conf.json +28 -0
  3. package/lib/biometric-provider.d.ts +123 -31
  4. package/lib/biometric-provider.d.ts.map +1 -1
  5. package/lib/biometric-provider.js +146 -7
  6. package/lib/biometric-provider.js.map +1 -1
  7. package/lib/cloud-wallet.d.ts +7 -3
  8. package/lib/cloud-wallet.d.ts.map +1 -1
  9. package/lib/cloud-wallet.js +29 -18
  10. package/lib/cloud-wallet.js.map +1 -1
  11. package/lib/credential-provider.d.ts +63 -30
  12. package/lib/credential-provider.d.ts.map +1 -1
  13. package/lib/credential-provider.js +220 -13
  14. package/lib/credential-provider.js.map +1 -1
  15. package/lib/credentials/oidvc.d.ts +1 -1
  16. package/lib/credentials/oidvc.d.ts.map +1 -1
  17. package/lib/credentials/oidvc.js +8 -9
  18. package/lib/credentials/oidvc.js.map +1 -1
  19. package/lib/did-provider.d.ts +102 -36
  20. package/lib/did-provider.d.ts.map +1 -1
  21. package/lib/did-provider.js +186 -28
  22. package/lib/did-provider.js.map +1 -1
  23. package/lib/ecosystem-tools.js +5 -6
  24. package/lib/ecosystem-tools.js.map +1 -1
  25. package/lib/helpers.js +6 -6
  26. package/lib/helpers.js.map +1 -1
  27. package/lib/message-provider.d.ts +39 -13
  28. package/lib/message-provider.d.ts.map +1 -1
  29. package/lib/message-provider.js +147 -21
  30. package/lib/message-provider.js.map +1 -1
  31. package/lib/messages/message-helpers.js +6 -6
  32. package/lib/messages/message-helpers.js.map +1 -1
  33. package/lib/network-resolver.d.ts +1 -1
  34. package/lib/network-resolver.js +5 -5
  35. package/lib/network-resolver.js.map +1 -1
  36. package/lib/qr-handlers/builtin/index.d.ts +30 -0
  37. package/lib/qr-handlers/builtin/index.d.ts.map +1 -0
  38. package/lib/qr-handlers/builtin/index.js +46 -0
  39. package/lib/qr-handlers/builtin/index.js.map +1 -0
  40. package/lib/qr-handlers/builtin/oid4vc-handler.d.ts +137 -0
  41. package/lib/qr-handlers/builtin/oid4vc-handler.d.ts.map +1 -0
  42. package/lib/qr-handlers/builtin/oid4vc-handler.js +134 -0
  43. package/lib/qr-handlers/builtin/oid4vc-handler.js.map +1 -0
  44. package/lib/qr-handlers/index.d.ts +76 -0
  45. package/lib/qr-handlers/index.d.ts.map +1 -0
  46. package/lib/qr-handlers/index.js +92 -0
  47. package/lib/qr-handlers/index.js.map +1 -0
  48. package/lib/qr-handlers/processor.d.ts +110 -0
  49. package/lib/qr-handlers/processor.d.ts.map +1 -0
  50. package/lib/qr-handlers/processor.js +251 -0
  51. package/lib/qr-handlers/processor.js.map +1 -0
  52. package/lib/qr-handlers/types.d.ts +205 -0
  53. package/lib/qr-handlers/types.d.ts.map +1 -0
  54. package/lib/qr-handlers/types.js +10 -0
  55. package/lib/qr-handlers/types.js.map +1 -0
  56. package/lib/types.d.ts +610 -14
  57. package/lib/types.d.ts.map +1 -1
  58. package/lib/types.js +16 -0
  59. package/lib/types.js.map +1 -1
  60. package/lib/verification-controller.d.ts +0 -1
  61. package/lib/verification-controller.d.ts.map +1 -1
  62. package/lib/verification-controller.js +12 -5
  63. package/lib/verification-controller.js.map +1 -1
  64. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.d.ts +0 -1
  65. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.d.ts.map +1 -1
  66. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.js +3 -3
  67. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.js.map +1 -1
  68. package/lib/wallet-wasm.d.ts.map +1 -1
  69. package/lib/wallet-wasm.js +13 -14
  70. package/lib/wallet-wasm.js.map +1 -1
  71. package/lib/wallet.d.ts +36 -20
  72. package/lib/wallet.d.ts.map +1 -1
  73. package/lib/wallet.js +174 -28
  74. package/lib/wallet.js.map +1 -1
  75. package/package.json +19 -11
  76. package/src/biometric-provider.ts +157 -42
  77. package/src/cloud-wallet.ts +13 -0
  78. package/src/credential-provider.test.ts +220 -1
  79. package/src/credential-provider.ts +222 -25
  80. package/src/credentials/oidvc.test.ts +1 -1
  81. package/src/credentials/oidvc.ts +1 -1
  82. package/src/did-provider.ts +183 -34
  83. package/src/message-provider.ts +149 -35
  84. package/src/qr-handlers/builtin/index.ts +30 -0
  85. package/src/qr-handlers/builtin/oid4vc-handler.ts +198 -0
  86. package/src/qr-handlers/index.ts +76 -0
  87. package/src/qr-handlers/processor.test.ts +514 -0
  88. package/src/qr-handlers/processor.ts +311 -0
  89. package/src/qr-handlers/types.ts +228 -0
  90. package/src/types.ts +666 -11
  91. package/src/verification-controller.test.ts +1 -2
  92. package/src/verification-controller.ts +9 -1
  93. package/src/wallet-wasm.ts +1 -3
  94. package/src/wallet.ts +173 -24
  95. package/tsconfig.build.tsbuildinfo +1 -1
@@ -1,7 +1,18 @@
1
- import {IWallet} from './types';
1
+ /**
2
+ * @module did-provider
3
+ * @description DID (Decentralized Identifier) management functionality for the Truvera Wallet SDK.
4
+ * This module provides functions for creating, importing, exporting, and managing DIDs.
5
+ */
6
+
7
+ import {IWallet, IDIDProvider} from './types';
8
+ export type {IDIDProvider};
2
9
  import {didServiceRPC} from '@docknetwork/wallet-sdk-wasm/src/services/dids/index';
3
10
  import assert from 'assert';
4
11
 
12
+ /**
13
+ * Internal function to import a DID from an encrypted wallet JSON
14
+ * @private
15
+ */
5
16
  export async function importDID({
6
17
  wallet,
7
18
  encryptedJSONWallet,
@@ -30,15 +41,12 @@ export async function importDID({
30
41
  });
31
42
 
32
43
  for (const doc of docs) {
33
- const existingDocs = await wallet.query({
34
- id: doc.id,
35
- });
44
+ const existingDoc = await wallet.getDocumentById(doc.id);
36
45
 
37
- if (existingDocs.length === 0) {
46
+ if (!existingDoc) {
38
47
  await wallet.addDocument(doc);
39
48
  } else if (
40
- existingDocs.length > 0 &&
41
- existingDocs[0].type === 'DIDResolutionResponse'
49
+ existingDoc.type === 'DIDResolutionResponse'
42
50
  ) {
43
51
  throw new Error('DID already exists in wallet');
44
52
  }
@@ -54,14 +62,15 @@ export async function importDID({
54
62
  }
55
63
  }
56
64
 
65
+ /**
66
+ * Internal function to edit a DID document's name
67
+ * @private
68
+ */
57
69
  async function editDID({wallet, id, name}){
58
70
  if (typeof id === 'string' && id.length > 0) {
59
- const docs = await wallet.query({
60
- id,
61
- });
62
- if (docs.length === 1) {
63
- const doc = docs[0];
64
- await wallet.update({
71
+ const doc = await wallet.getDocumentById(id);
72
+ if (doc) {
73
+ await wallet.updateDocument({
65
74
  ...doc,
66
75
  name,
67
76
  });
@@ -71,19 +80,27 @@ async function editDID({wallet, id, name}){
71
80
  }
72
81
  };
73
82
 
83
+ /**
84
+ * Internal function to delete a DID from the wallet
85
+ * @private
86
+ */
74
87
  async function deleteDID({wallet, id}){
75
88
  if (typeof id === 'string' && id.length > 0) {
76
- return await wallet.remove(id);
89
+ return await wallet.removeDocument(id);
77
90
  } else {
78
91
  throw Error('Document ID is not set');
79
92
  }
80
93
  }
81
94
 
95
+ /**
96
+ * Internal function to export a DID and its correlated documents
97
+ * @private
98
+ */
82
99
  async function exportDID({wallet, id, password }){
83
100
  const existingDoc = await wallet.getDocumentById(id);
84
101
  if (existingDoc) {
85
102
  const allCorrelationDocuments = (
86
- await wallet.resolveCorrelations(id)
103
+ await wallet.getDocumentCorrelations(id)
87
104
  ).filter(doc => doc && doc?.id !== existingDoc.id);
88
105
  const documentsToExport = [existingDoc, ...allCorrelationDocuments];
89
106
 
@@ -99,6 +116,10 @@ async function exportDID({wallet, id, password }){
99
116
  throw new Error('DID Document not found');
100
117
  }
101
118
 
119
+ /**
120
+ * Internal function to create a DID document from a keypair document
121
+ * @private
122
+ */
102
123
  export const createDIDKeyDocument = async (
103
124
  keypairDoc: any,
104
125
  didDocParams: any = {},
@@ -126,6 +147,10 @@ export const createDIDKeyDocument = async (
126
147
  };
127
148
  };
128
149
 
150
+ /**
151
+ * Internal function to create a new DID:key with an associated keypair
152
+ * @private
153
+ */
129
154
  export async function createDIDKey({wallet, name, derivePath=undefined, type=undefined}) {
130
155
  assert(!!wallet, 'wallet is required');
131
156
  assert(!!name, 'name is required');
@@ -146,10 +171,8 @@ export async function createDIDKey({wallet, name, derivePath=undefined, type=und
146
171
  }
147
172
 
148
173
  /**
149
- * Get DIDs list
150
- *
151
- * @param param0
152
- * @returns
174
+ * Internal function to retrieve all DIDs stored in the wallet
175
+ * @private
153
176
  */
154
177
  export async function getAll({wallet}) {
155
178
  assert(!!wallet, 'wallet is required');
@@ -157,12 +180,20 @@ export async function getAll({wallet}) {
157
180
  return dids;
158
181
  }
159
182
 
183
+ /**
184
+ * Internal function to get the default DID from the wallet
185
+ * @private
186
+ */
160
187
  export async function getDefaultDID({wallet}) {
161
188
  assert(!!wallet, 'wallet is required');
162
189
  const allDids = await getAll({ wallet });
163
190
  return allDids[0]?.didDocument.id;
164
191
  }
165
192
 
193
+ /**
194
+ * Internal function to retrieve all keypairs associated with DIDs
195
+ * @private
196
+ */
166
197
  export async function getDIDKeyPairs({wallet}) {
167
198
  assert(!!wallet, 'wallet is required');
168
199
  const didDocs = await getAll({wallet});
@@ -174,6 +205,7 @@ export async function getDIDKeyPairs({wallet}) {
174
205
  return keyPairs;
175
206
  }
176
207
 
208
+
177
209
  export async function ensureDID({wallet}) {
178
210
  assert(!!wallet, 'wallet is required');
179
211
  const dids = await getAll({wallet});
@@ -182,49 +214,166 @@ export async function ensureDID({wallet}) {
182
214
  }
183
215
  }
184
216
 
185
- export interface IDIDProvider {
186
- importDID(params: {
187
- encryptedJSONWallet: any;
188
- password: string;
189
- }): Promise<void>;
190
- createDIDKey(params: {name: string, derivePath?:string, type?: string}): Promise<any>;
191
- editDID(params: {id: string; name: string}): Promise<void>;
192
- deleteDID(params: {id: string;}): Promise<void>;
193
- exportDID(params: {id: string; password: string}): Promise<void>;
194
- getAll(): Promise<any>;
195
- getDIDKeyPairs(): Promise<any>;
196
- ensureDID(): Promise<any>;
197
- getDefaultDID(): Promise<string>;
198
- }
199
217
 
218
+ /**
219
+ * Creates a DID provider instance bound to a wallet
220
+ * @param {Object} params - Provider configuration
221
+ * @param {IWallet} params.wallet - The wallet instance to bind the provider to
222
+ * @returns {IDIDProvider} A DID provider instance with all DID management methods
223
+ * @see {@link IDIDProvider} - The interface defining all available DID provider methods
224
+ * @example
225
+ * import { createDIDProvider } from '@docknetwork/wallet-sdk-core';
226
+ *
227
+ * const didProvider = createDIDProvider({wallet});
228
+ *
229
+ * // Create a new DID
230
+ * const {keyDoc, didDocumentResolution} = await didProvider.createDIDKey({
231
+ * name: 'My DID'
232
+ * });
233
+ *
234
+ * // Get all DIDs
235
+ * const allDIDs = await didProvider.getAll();
236
+ *
237
+ * // Export a DID
238
+ * const exportedDID = await didProvider.exportDID({
239
+ * id: didDocumentResolution.id,
240
+ * password: 'mypassword'
241
+ * });
242
+ */
200
243
  export function createDIDProvider({wallet}): IDIDProvider {
201
- return {
244
+ const provider = {
245
+ /**
246
+ * Imports a DID from an encrypted wallet JSON
247
+ * @memberof IDIDProvider
248
+ * @param {Object} params - Import parameters
249
+ * @param {any} params.encryptedJSONWallet - The encrypted wallet JSON containing the DID
250
+ * @param {string} params.password - Password to decrypt the wallet
251
+ * @returns {Promise<any[]>} Array of imported documents
252
+ * @throws {Error} If password is incorrect or DID already exists in wallet
253
+ * @example
254
+ * const importedDocs = await didProvider.importDID({
255
+ * encryptedJSONWallet: encryptedBackup,
256
+ * password: 'mypassword'
257
+ * });
258
+ */
202
259
  async importDID({encryptedJSONWallet, password}) {
203
260
  return importDID({wallet, encryptedJSONWallet, password});
204
261
  },
262
+ /**
263
+ * Creates a new DID:key with an associated keypair
264
+ * @memberof IDIDProvider
265
+ * @param {Object} params - Creation parameters
266
+ * @param {string} params.name - The name for the new DID
267
+ * @param {string} [params.derivePath] - Optional derivation path for the keypair
268
+ * @param {string} [params.type] - Optional key type specification
269
+ * @returns {Promise<{keyDoc: any, didDocumentResolution: any}>} The created keypair and DID document
270
+ * @throws {Error} If name is not provided
271
+ * @example
272
+ * const {keyDoc, didDocumentResolution} = await didProvider.createDIDKey({
273
+ * name: 'My Identity DID',
274
+ * derivePath: "m/44'/60'/0'/0/0"
275
+ * });
276
+ */
205
277
  async createDIDKey({name, derivePath, type}) {
206
278
  return createDIDKey({wallet, name, derivePath, type});
207
279
  },
280
+ /**
281
+ * Edits a DID document's name
282
+ * @memberof IDIDProvider
283
+ * @param {Object} params - Edit parameters
284
+ * @param {string} params.id - The ID of the DID document to edit
285
+ * @param {string} params.name - The new name for the DID
286
+ * @returns {Promise<void>}
287
+ * @throws {Error} If document ID is not set or document not found
288
+ * @example
289
+ * await didProvider.editDID({
290
+ * id: 'did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK',
291
+ * name: 'Updated DID Name'
292
+ * });
293
+ */
208
294
  async editDID(params) {
209
295
  return editDID({wallet, ...params});
210
296
  },
297
+ /**
298
+ * Deletes a DID from the wallet
299
+ * @memberof IDIDProvider
300
+ * @param {Object} params - Delete parameters
301
+ * @param {string} params.id - The ID of the DID document to delete
302
+ * @returns {Promise<void>}
303
+ * @throws {Error} If document ID is not set
304
+ * @example
305
+ * await didProvider.deleteDID({
306
+ * id: 'did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK'
307
+ * });
308
+ */
211
309
  async deleteDID(params) {
212
310
  return deleteDID({wallet, ...params});
213
311
  },
312
+ /**
313
+ * Exports a DID and its correlated documents as an encrypted JSON
314
+ * @memberof IDIDProvider
315
+ * @param {Object} params - Export parameters
316
+ * @param {string} params.id - The ID of the DID document to export
317
+ * @param {string} params.password - Password for encryption
318
+ * @returns {Promise<any>} Encrypted wallet JSON containing the DID and correlations
319
+ * @throws {Error} If DID document or keypair not found
320
+ * @example
321
+ * const exportedDID = await didProvider.exportDID({
322
+ * id: 'did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK',
323
+ * password: 'mypassword'
324
+ * });
325
+ */
214
326
  async exportDID(params) {
215
327
  return exportDID({wallet, ...params});
216
328
  },
329
+ /**
330
+ * Retrieves all DIDs stored in the wallet
331
+ * @memberof IDIDProvider
332
+ * @returns {Promise<any[]>} Array of DID resolution response documents
333
+ * @example
334
+ * const allDIDs = await didProvider.getAll();
335
+ * console.log(`Found ${allDIDs.length} DIDs in wallet`);
336
+ */
217
337
  async getAll() {
218
338
  return getAll({wallet});
219
339
  },
340
+ /**
341
+ * Retrieves all keypairs associated with DIDs in the wallet
342
+ * @memberof IDIDProvider
343
+ * @returns {Promise<any[]>} Array of keypair documents
344
+ * @example
345
+ * const keyPairs = await didProvider.getDIDKeyPairs();
346
+ * console.log(`Found ${keyPairs.length} DID keypairs`);
347
+ */
220
348
  async getDIDKeyPairs() {
221
349
  return getDIDKeyPairs({wallet});
222
350
  },
351
+ /**
352
+ * Ensures at least one DID exists in the wallet, creating a default if none exist
353
+ * @memberof IDIDProvider
354
+ * @returns {Promise<{keyDoc: any, didDocumentResolution: any}|void>} The created DID if one was created, undefined otherwise
355
+ * @example
356
+ * // Ensure wallet has at least one DID
357
+ * await didProvider.ensureDID();
358
+ */
223
359
  async ensureDID() {
224
360
  return ensureDID({wallet});
225
361
  },
362
+ /**
363
+ * Gets the default DID from the wallet (first DID if exists)
364
+ * @memberof IDIDProvider
365
+ * @returns {Promise<string|undefined>} The default DID identifier or undefined if no DIDs exist
366
+ * @example
367
+ * const defaultDID = await didProvider.getDefaultDID();
368
+ * if (defaultDID) {
369
+ * console.log(`Default DID: ${defaultDID}`);
370
+ * }
371
+ */
226
372
  async getDefaultDID() {
227
373
  return getDefaultDID({wallet});
228
374
  }
229
375
  };
376
+
377
+
378
+ return provider;
230
379
  }
@@ -1,23 +1,22 @@
1
+ /**
2
+ * @module message-provider
3
+ * @description DIDComm message management functionality for the Truvera Wallet SDK.
4
+ * This module provides functions for sending, receiving, and processing DIDComm messages.
5
+ */
6
+
1
7
  import {logger} from '@docknetwork/wallet-sdk-data-store/src/logger';
2
8
  import {IDIDProvider} from './did-provider';
3
9
  import {WalletDocumentTypes, captureException} from './helpers';
4
- import {IWallet} from './types';
10
+ import {IWallet, IMessageProvider} from './types';
11
+ export type {IMessageProvider};
5
12
  import {relayService as defaultRelayService} from '@docknetwork/wallet-sdk-wasm/src/services/relay-service';
6
13
 
7
14
  const FETCH_MESSAGE_LIMIT = 10;
8
15
 
9
- export interface IMessageProvider {
10
- sendMessage: (message: any) => Promise<void>;
11
- fetchMessages: () => Promise<void>;
12
- processDIDCommMessages: () => Promise<void>;
13
- startAutoFetch: (timeout?: number) => () => void;
14
- addMessageListener: (handler: () => void) => () => void;
15
- waitForMessage: () => Promise<any>;
16
- markMessageAsRead: (messageId: string) => Promise<void>;
17
- clearCache: () => Promise<void>;
18
- processMessageRecurrentJob: () => Promise<void>;
19
- }
20
-
16
+ /**
17
+ * Internal function to retrieve key pair documents for a DID
18
+ * @private
19
+ */
21
20
  async function getKeyPairDocs(didProvider: IDIDProvider, did?: string) {
22
21
  try {
23
22
  const keyPairDocs = await didProvider.getDIDKeyPairs();
@@ -30,6 +29,37 @@ async function getKeyPairDocs(didProvider: IDIDProvider, did?: string) {
30
29
  }
31
30
  }
32
31
 
32
+ /**
33
+ * Creates a message provider instance bound to a wallet and DID provider
34
+ * @param {Object} params - Provider configuration
35
+ * @param {IWallet} params.wallet - The wallet instance to use for message storage
36
+ * @param {IDIDProvider} params.didProvider - The DID provider instance to use for key management
37
+ * @param {any} [params.relayService] - Optional relay service implementation (defaults to built-in service)
38
+ * @returns {IMessageProvider} A message provider instance with all DIDComm message management methods
39
+ * @see {@link IMessageProvider} - The interface defining all available message provider methods
40
+ * @example
41
+ * import { createMessageProvider } from '@docknetwork/wallet-sdk-core';
42
+ *
43
+ * const messageProvider = createMessageProvider({
44
+ * wallet,
45
+ * didProvider
46
+ * });
47
+ *
48
+ * // Send a message
49
+ * await messageProvider.sendMessage({
50
+ * did: 'did:key:sender123',
51
+ * recipientDid: 'did:key:recipient456',
52
+ * message: { hello: 'world' }
53
+ * });
54
+ *
55
+ * // Start auto-fetching messages
56
+ * const stopAutoFetch = messageProvider.startAutoFetch(5000);
57
+ *
58
+ * // Add message listener
59
+ * const removeListener = messageProvider.addMessageListener((message) => {
60
+ * console.log('Received message:', message);
61
+ * });
62
+ */
33
63
  export function createMessageProvider({
34
64
  wallet,
35
65
  didProvider,
@@ -38,7 +68,7 @@ export function createMessageProvider({
38
68
  wallet: IWallet;
39
69
  didProvider: IDIDProvider;
40
70
  relayService?: any;
41
- }) {
71
+ }): IMessageProvider {
42
72
  async function markMessageAsRead(messageId: string) {
43
73
  try {
44
74
  const message = await wallet.getDocumentById(messageId);
@@ -225,38 +255,49 @@ export function createMessageProvider({
225
255
  }
226
256
 
227
257
  return {
258
+ /**
259
+ * Sends a DIDComm message to a recipient
260
+ * @memberof IMessageProvider
261
+ * @param {Object} params - Message parameters
262
+ * @param {string} [params.from] - Sender DID identifier
263
+ * @param {string} [params.to] - Recipient DID identifier
264
+ * @param {any} [params.message] - Message payload to send
265
+ * @param {string} [params.type] - DIDComm message type
266
+ * @param {string} [params.did] - @deprecated Use 'from' instead - Sender DID identifier
267
+ * @param {string} [params.recipientDid] - @deprecated Use 'to' instead - Recipient DID identifier
268
+ * @param {any} [params.body] - @deprecated Use 'message' instead - Message payload to send
269
+ * @returns {Promise<any>} Result of sending the message
270
+ * @throws {Error} If sender DID not found or message sending fails
271
+ * @example
272
+ * await messageProvider.sendMessage({
273
+ * from: 'did:key:sender123',
274
+ * to: 'did:key:recipient456',
275
+ * message: { hello: 'world' },
276
+ * type: 'basic-message'
277
+ * });
278
+ *
279
+ */
228
280
  async sendMessage({
229
- did,
230
- recipientDid,
231
- message,
232
- // didcomm message parameters
281
+ // Recommended parameters
233
282
  from,
234
283
  to,
235
- body,
284
+ message,
236
285
  type,
286
+ // Deprecated parameters
287
+ did,
288
+ recipientDid,
289
+ body,
237
290
  }) {
238
- // TODO: rename relay service parameters to make it easier to understand
239
- if (from) {
240
- did = from;
241
- }
242
-
243
- if (to) {
244
- recipientDid = to;
245
- }
246
-
247
- if (!message && body) {
248
- message = body;
249
- }
250
291
 
251
292
  try {
252
- const keyPairDoc = await getKeyPairDocs(didProvider, did);
293
+ const keyPairDoc = await getKeyPairDocs(didProvider, from || did);
253
294
  if (!keyPairDoc) {
254
295
  throw new Error(`${did} not found in didDocs`);
255
296
  }
256
- await relayService.sendMessage({
297
+ return await relayService.sendMessage({
257
298
  keyPairDoc,
258
- message,
259
- recipientDid,
299
+ message: message || body,
300
+ recipientDid: to || recipientDid,
260
301
  type,
261
302
  });
262
303
  } catch (error) {
@@ -264,6 +305,14 @@ export function createMessageProvider({
264
305
  throw new Error(`Failed to send message: ${error.message}`);
265
306
  }
266
307
  },
308
+ /**
309
+ * Waits for the next incoming message
310
+ * @memberof IMessageProvider
311
+ * @returns {Promise<any>} Promise that resolves with the next received message
312
+ * @example
313
+ * const nextMessage = await messageProvider.waitForMessage();
314
+ * console.log('Received message:', nextMessage);
315
+ */
267
316
  waitForMessage() {
268
317
  return new Promise((resolve: any) => {
269
318
  let removeListener = addMessageListener(async message => {
@@ -272,6 +321,16 @@ export function createMessageProvider({
272
321
  });
273
322
  });
274
323
  },
324
+ /**
325
+ * Starts automatic message fetching at regular intervals
326
+ * @memberof IMessageProvider
327
+ * @param {number} [timeout=2000] - Interval in milliseconds between fetch operations
328
+ * @returns {Function} Function to stop the auto-fetch process
329
+ * @example
330
+ * const stopAutoFetch = messageProvider.startAutoFetch(5000);
331
+ * // Later, stop auto-fetching
332
+ * stopAutoFetch();
333
+ */
275
334
  startAutoFetch(timeout = 2000) {
276
335
  clearInterval(listenerIntervalId);
277
336
  listenerIntervalId = setInterval(async () => {
@@ -281,6 +340,14 @@ export function createMessageProvider({
281
340
 
282
341
  return () => clearInterval(listenerIntervalId);
283
342
  },
343
+ /**
344
+ * Clears all cached messages from the wallet
345
+ * @memberof IMessageProvider
346
+ * @returns {Promise<void>}
347
+ * @example
348
+ * await messageProvider.clearCache();
349
+ * console.log('All messages cleared');
350
+ */
284
351
  clearCache: async () => {
285
352
  return Promise.all(
286
353
  (
@@ -291,10 +358,57 @@ export function createMessageProvider({
291
358
  }),
292
359
  );
293
360
  },
361
+ /**
362
+ * Fetches new messages from the relay service
363
+ * @memberof IMessageProvider
364
+ * @returns {Promise<void>}
365
+ * @throws {Error} If message fetching fails
366
+ * @example
367
+ * await messageProvider.fetchMessages();
368
+ * console.log('Messages fetched successfully');
369
+ */
294
370
  fetchMessages,
371
+ /**
372
+ * Adds a listener for when messages are decrypted
373
+ * @memberof IMessageProvider
374
+ * @param {Function} handler - Callback function to handle decrypted messages
375
+ * @returns {Function} Function to remove the listener
376
+ * @example
377
+ * const removeListener = messageProvider.addMessageListener((message) => {
378
+ * console.log('New message received:', message);
379
+ * });
380
+ * // Later, remove the listener
381
+ * removeListener();
382
+ */
295
383
  addMessageListener,
384
+ /**
385
+ * Processes stored DIDComm messages and decrypts them
386
+ * @memberof IMessageProvider
387
+ * @returns {Promise<void>}
388
+ * @throws {Error} If message processing fails
389
+ * @example
390
+ * await messageProvider.processDIDCommMessages();
391
+ * console.log('Messages processed successfully');
392
+ */
296
393
  processDIDCommMessages,
394
+ /**
395
+ * Starts the recurrent message processing job
396
+ * @memberof IMessageProvider
397
+ * @returns {Promise<void>}
398
+ * @example
399
+ * await messageProvider.processMessageRecurrentJob();
400
+ */
297
401
  processMessageRecurrentJob,
402
+ /**
403
+ * Marks a message as read and removes it from storage
404
+ * @memberof IMessageProvider
405
+ * @param {string} messageId - The ID of the message to mark as read
406
+ * @returns {Promise<void>}
407
+ * @throws {Error} If message is not found or not a DIDComm message
408
+ * @example
409
+ * await messageProvider.markMessageAsRead('message-id-123');
410
+ * console.log('Message marked as read');
411
+ */
298
412
  markMessageAsRead,
299
413
  } as any;
300
414
  }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Built-in QR Code Handlers
3
+ *
4
+ * This module provides pre-built handlers for common protocols used in
5
+ * decentralized identity wallets. These handlers can be used directly or
6
+ * extended for custom behavior.
7
+ *
8
+ * ## Available Handlers
9
+ *
10
+ * - **OID4VCHandler**: OpenID for Verifiable Credentials (OID4VC)
11
+ *
12
+ * ## Usage
13
+ *
14
+ * ```typescript
15
+ * import { OID4VCHandler } from '@docknetwork/wallet-sdk-core/src/qr-handlers/builtin';
16
+ *
17
+ * const handler = new OID4VCHandler({
18
+ * onImportCredential: async (uri) => {
19
+ * // Your credential import logic
20
+ * return { success: true };
21
+ * },
22
+ * });
23
+ *
24
+ * processor.registerHandler(handler);
25
+ * ```
26
+ *
27
+ * @module qr-handlers/builtin
28
+ */
29
+
30
+ export * from './oid4vc-handler';