@docknetwork/wallet-sdk-core 1.5.14 → 1.7.6

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 (92) hide show
  1. package/.claude/settings.local.json +12 -0
  2. package/generate-docs.js +33 -0
  3. package/jsdoc.conf.json +28 -0
  4. package/lib/biometric-provider.d.ts +124 -32
  5. package/lib/biometric-provider.d.ts.map +1 -1
  6. package/lib/biometric-provider.js +146 -7
  7. package/lib/biometric-provider.js.map +1 -1
  8. package/lib/cloud-wallet.d.ts +8 -6
  9. package/lib/cloud-wallet.d.ts.map +1 -1
  10. package/lib/cloud-wallet.js +41 -57
  11. package/lib/cloud-wallet.js.map +1 -1
  12. package/lib/credential-provider.d.ts +58 -33
  13. package/lib/credential-provider.d.ts.map +1 -1
  14. package/lib/credential-provider.js +212 -11
  15. package/lib/credential-provider.js.map +1 -1
  16. package/lib/credentials/oidvc.js +4 -5
  17. package/lib/credentials/oidvc.js.map +1 -1
  18. package/lib/did-provider.d.ts +102 -36
  19. package/lib/did-provider.d.ts.map +1 -1
  20. package/lib/did-provider.js +185 -27
  21. package/lib/did-provider.js.map +1 -1
  22. package/lib/ecosystem-tools.js +4 -5
  23. package/lib/ecosystem-tools.js.map +1 -1
  24. package/lib/helpers.js +6 -6
  25. package/lib/helpers.js.map +1 -1
  26. package/lib/message-provider.d.ts +39 -13
  27. package/lib/message-provider.d.ts.map +1 -1
  28. package/lib/message-provider.js +173 -22
  29. package/lib/message-provider.js.map +1 -1
  30. package/lib/messages/message-helpers.js +6 -6
  31. package/lib/messages/message-helpers.js.map +1 -1
  32. package/lib/network-resolver.js +5 -5
  33. package/lib/network-resolver.js.map +1 -1
  34. package/lib/qr-handlers/builtin/index.d.ts +30 -0
  35. package/lib/qr-handlers/builtin/index.d.ts.map +1 -0
  36. package/lib/qr-handlers/builtin/index.js +46 -0
  37. package/lib/qr-handlers/builtin/index.js.map +1 -0
  38. package/lib/qr-handlers/builtin/oid4vc-handler.d.ts +137 -0
  39. package/lib/qr-handlers/builtin/oid4vc-handler.d.ts.map +1 -0
  40. package/lib/qr-handlers/builtin/oid4vc-handler.js +134 -0
  41. package/lib/qr-handlers/builtin/oid4vc-handler.js.map +1 -0
  42. package/lib/qr-handlers/index.d.ts +76 -0
  43. package/lib/qr-handlers/index.d.ts.map +1 -0
  44. package/lib/qr-handlers/index.js +92 -0
  45. package/lib/qr-handlers/index.js.map +1 -0
  46. package/lib/qr-handlers/processor.d.ts +110 -0
  47. package/lib/qr-handlers/processor.d.ts.map +1 -0
  48. package/lib/qr-handlers/processor.js +251 -0
  49. package/lib/qr-handlers/processor.js.map +1 -0
  50. package/lib/qr-handlers/types.d.ts +205 -0
  51. package/lib/qr-handlers/types.d.ts.map +1 -0
  52. package/lib/qr-handlers/types.js +10 -0
  53. package/lib/qr-handlers/types.js.map +1 -0
  54. package/lib/types.d.ts +613 -13
  55. package/lib/types.d.ts.map +1 -1
  56. package/lib/types.js +16 -0
  57. package/lib/types.js.map +1 -1
  58. package/lib/verification-controller.d.ts +3 -4
  59. package/lib/verification-controller.d.ts.map +1 -1
  60. package/lib/verification-controller.js +10 -3
  61. package/lib/verification-controller.js.map +1 -1
  62. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.d.ts +0 -1
  63. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.d.ts.map +1 -1
  64. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.js +2 -2
  65. package/lib/wallet-to-wallet-verification/walletToWalletVerificationProvider.js.map +1 -1
  66. package/lib/wallet-wasm.d.ts +2 -2
  67. package/lib/wallet-wasm.d.ts.map +1 -1
  68. package/lib/wallet-wasm.js +15 -17
  69. package/lib/wallet-wasm.js.map +1 -1
  70. package/lib/wallet.d.ts +36 -20
  71. package/lib/wallet.d.ts.map +1 -1
  72. package/lib/wallet.js +172 -26
  73. package/lib/wallet.js.map +1 -1
  74. package/package.json +19 -11
  75. package/src/biometric-provider.ts +157 -42
  76. package/src/cloud-wallet.ts +21 -60
  77. package/src/credential-provider.test.ts +191 -1
  78. package/src/credential-provider.ts +208 -27
  79. package/src/did-provider.ts +183 -34
  80. package/src/message-provider.ts +177 -38
  81. package/src/qr-handlers/builtin/index.ts +30 -0
  82. package/src/qr-handlers/builtin/oid4vc-handler.ts +198 -0
  83. package/src/qr-handlers/index.ts +76 -0
  84. package/src/qr-handlers/processor.test.ts +514 -0
  85. package/src/qr-handlers/processor.ts +311 -0
  86. package/src/qr-handlers/types.ts +228 -0
  87. package/src/types.ts +671 -11
  88. package/src/verification-controller.test.ts +1 -2
  89. package/src/verification-controller.ts +14 -2
  90. package/src/wallet-wasm.ts +5 -8
  91. package/src/wallet.ts +173 -24
  92. 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<any>;
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);
@@ -213,6 +243,8 @@ export function createMessageProvider({
213
243
  }
214
244
 
215
245
  let listenerIntervalId = null;
246
+ let processMessageTimeoutId = null;
247
+ let stopped = false;
216
248
 
217
249
  const processMessageInterval = 3000;
218
250
 
@@ -220,43 +252,68 @@ export function createMessageProvider({
220
252
  try {
221
253
  await processDIDCommMessages();
222
254
  } finally {
223
- setTimeout(processMessageRecurrentJob, processMessageInterval);
255
+ if (!stopped) {
256
+ processMessageTimeoutId = setTimeout(processMessageRecurrentJob, processMessageInterval);
257
+ }
258
+ }
259
+ }
260
+
261
+ function stop() {
262
+ stopped = true;
263
+ if (listenerIntervalId) {
264
+ clearInterval(listenerIntervalId);
265
+ listenerIntervalId = null;
266
+ }
267
+ if (processMessageTimeoutId) {
268
+ clearTimeout(processMessageTimeoutId);
269
+ processMessageTimeoutId = null;
224
270
  }
225
271
  }
226
272
 
227
273
  return {
274
+ /**
275
+ * Sends a DIDComm message to a recipient
276
+ * @memberof IMessageProvider
277
+ * @param {Object} params - Message parameters
278
+ * @param {string} [params.from] - Sender DID identifier
279
+ * @param {string} [params.to] - Recipient DID identifier
280
+ * @param {any} [params.message] - Message payload to send
281
+ * @param {string} [params.type] - DIDComm message type
282
+ * @param {string} [params.did] - @deprecated Use 'from' instead - Sender DID identifier
283
+ * @param {string} [params.recipientDid] - @deprecated Use 'to' instead - Recipient DID identifier
284
+ * @param {any} [params.body] - @deprecated Use 'message' instead - Message payload to send
285
+ * @returns {Promise<any>} Result of sending the message
286
+ * @throws {Error} If sender DID not found or message sending fails
287
+ * @example
288
+ * await messageProvider.sendMessage({
289
+ * from: 'did:key:sender123',
290
+ * to: 'did:key:recipient456',
291
+ * message: { hello: 'world' },
292
+ * type: 'basic-message'
293
+ * });
294
+ *
295
+ */
228
296
  async sendMessage({
229
- did,
230
- recipientDid,
231
- message,
232
- // didcomm message parameters
297
+ // Recommended parameters
233
298
  from,
234
299
  to,
235
- body,
300
+ message,
236
301
  type,
302
+ // Deprecated parameters
303
+ did,
304
+ recipientDid,
305
+ body,
237
306
  }) {
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
307
 
251
308
  try {
252
- const keyPairDoc = await getKeyPairDocs(didProvider, did);
309
+ const keyPairDoc = await getKeyPairDocs(didProvider, from || did);
253
310
  if (!keyPairDoc) {
254
311
  throw new Error(`${did} not found in didDocs`);
255
312
  }
256
313
  return await relayService.sendMessage({
257
314
  keyPairDoc,
258
- message,
259
- recipientDid,
315
+ message: message || body,
316
+ recipientDid: to || recipientDid,
260
317
  type,
261
318
  });
262
319
  } catch (error) {
@@ -264,6 +321,14 @@ export function createMessageProvider({
264
321
  throw new Error(`Failed to send message: ${error.message}`);
265
322
  }
266
323
  },
324
+ /**
325
+ * Waits for the next incoming message
326
+ * @memberof IMessageProvider
327
+ * @returns {Promise<any>} Promise that resolves with the next received message
328
+ * @example
329
+ * const nextMessage = await messageProvider.waitForMessage();
330
+ * console.log('Received message:', nextMessage);
331
+ */
267
332
  waitForMessage() {
268
333
  return new Promise((resolve: any) => {
269
334
  let removeListener = addMessageListener(async message => {
@@ -272,15 +337,41 @@ export function createMessageProvider({
272
337
  });
273
338
  });
274
339
  },
340
+ /**
341
+ * Starts automatic message fetching at regular intervals
342
+ * @memberof IMessageProvider
343
+ * @param {number} [timeout=2000] - Interval in milliseconds between fetch operations
344
+ * @returns {Function} Function to stop the auto-fetch process
345
+ * @example
346
+ * const stopAutoFetch = messageProvider.startAutoFetch(5000);
347
+ * // Later, stop auto-fetching
348
+ * stopAutoFetch();
349
+ */
275
350
  startAutoFetch(timeout = 2000) {
276
351
  clearInterval(listenerIntervalId);
352
+ stopped = false;
277
353
  listenerIntervalId = setInterval(async () => {
278
- await fetchMessages();
279
- await processDIDCommMessages();
354
+ try {
355
+ await fetchMessages();
356
+ await processDIDCommMessages();
357
+ } catch (err) {
358
+ logger.debug(`Auto-fetch error: ${err.message}`);
359
+ }
280
360
  }, timeout);
281
361
 
282
- return () => clearInterval(listenerIntervalId);
362
+ return () => {
363
+ clearInterval(listenerIntervalId);
364
+ listenerIntervalId = null;
365
+ };
283
366
  },
367
+ /**
368
+ * Clears all cached messages from the wallet
369
+ * @memberof IMessageProvider
370
+ * @returns {Promise<void>}
371
+ * @example
372
+ * await messageProvider.clearCache();
373
+ * console.log('All messages cleared');
374
+ */
284
375
  clearCache: async () => {
285
376
  return Promise.all(
286
377
  (
@@ -291,10 +382,58 @@ export function createMessageProvider({
291
382
  }),
292
383
  );
293
384
  },
385
+ /**
386
+ * Fetches new messages from the relay service
387
+ * @memberof IMessageProvider
388
+ * @returns {Promise<void>}
389
+ * @throws {Error} If message fetching fails
390
+ * @example
391
+ * await messageProvider.fetchMessages();
392
+ * console.log('Messages fetched successfully');
393
+ */
294
394
  fetchMessages,
395
+ /**
396
+ * Adds a listener for when messages are decrypted
397
+ * @memberof IMessageProvider
398
+ * @param {Function} handler - Callback function to handle decrypted messages
399
+ * @returns {Function} Function to remove the listener
400
+ * @example
401
+ * const removeListener = messageProvider.addMessageListener((message) => {
402
+ * console.log('New message received:', message);
403
+ * });
404
+ * // Later, remove the listener
405
+ * removeListener();
406
+ */
295
407
  addMessageListener,
408
+ /**
409
+ * Processes stored DIDComm messages and decrypts them
410
+ * @memberof IMessageProvider
411
+ * @returns {Promise<void>}
412
+ * @throws {Error} If message processing fails
413
+ * @example
414
+ * await messageProvider.processDIDCommMessages();
415
+ * console.log('Messages processed successfully');
416
+ */
296
417
  processDIDCommMessages,
418
+ /**
419
+ * Starts the recurrent message processing job
420
+ * @memberof IMessageProvider
421
+ * @returns {Promise<void>}
422
+ * @example
423
+ * await messageProvider.processMessageRecurrentJob();
424
+ */
297
425
  processMessageRecurrentJob,
426
+ /**
427
+ * Marks a message as read and removes it from storage
428
+ * @memberof IMessageProvider
429
+ * @param {string} messageId - The ID of the message to mark as read
430
+ * @returns {Promise<void>}
431
+ * @throws {Error} If message is not found or not a DIDComm message
432
+ * @example
433
+ * await messageProvider.markMessageAsRead('message-id-123');
434
+ * console.log('Message marked as read');
435
+ */
298
436
  markMessageAsRead,
437
+ stop,
299
438
  } as any;
300
439
  }
@@ -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';