@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
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OID4VCHandler = void 0;
4
+ exports.createOID4VCHandler = createOID4VCHandler;
5
+ /**
6
+ * Built-in handler for OID4VC (OpenID for Verifiable Credentials) URIs
7
+ *
8
+ * This is a generic handler that can be configured with app-specific callbacks
9
+ * for importing credentials. The handler itself only handles protocol detection
10
+ * and delegates the actual import logic to the configured callback.
11
+ *
12
+ * ## Example Usage
13
+ *
14
+ * ```typescript
15
+ * import { OID4VCHandler } from '@docknetwork/wallet-sdk-core/lib/qr-handlers/builtin';
16
+ * import { getCredentialProvider } from '@docknetwork/wallet-sdk-react-native';
17
+ *
18
+ * const handler = new OID4VCHandler({
19
+ * onImportCredential: async (uri, context) => {
20
+ * try {
21
+ * // Use SDK to import credential
22
+ * await getCredentialProvider().importCredentialFromURI({
23
+ * uri,
24
+ * didProvider: getDIDProvider(),
25
+ * getAuthCode: async (authUrl) => {
26
+ * // App-specific auth handling
27
+ * return await showAuthWebView(authUrl);
28
+ * },
29
+ * });
30
+ *
31
+ * return { success: true };
32
+ * } catch (error) {
33
+ * return {
34
+ * success: false,
35
+ * error: error instanceof Error ? error : new Error(String(error)),
36
+ * };
37
+ * }
38
+ * },
39
+ * });
40
+ *
41
+ * processor.registerHandler(handler);
42
+ * ```
43
+ *
44
+ * ## Handler Priority
45
+ *
46
+ * Default priority: 5 (very high)
47
+ * This ensures OID4VC URIs are checked before other credential handlers.
48
+ *
49
+ * @category Built-in Handlers
50
+ */
51
+ class OID4VCHandler {
52
+ id = 'oid4vc';
53
+ priority;
54
+ uriPrefixes;
55
+ onImportCredential;
56
+ constructor(config) {
57
+ if (!config.onImportCredential) {
58
+ throw new Error('onImportCredential callback is required');
59
+ }
60
+ this.priority = config.priority ?? 5;
61
+ this.uriPrefixes = config.uriPrefixes ?? ['openid-credential-offer://'];
62
+ this.onImportCredential = config.onImportCredential;
63
+ }
64
+ /**
65
+ * Check if this is an OID4VC URI
66
+ *
67
+ * Matches URIs that start with any of the configured prefixes.
68
+ * By default, matches: openid-credential-offer://
69
+ *
70
+ * @param context - The QR code context
71
+ * @returns True if this handler can process the URI
72
+ */
73
+ canHandle(context) {
74
+ return this.uriPrefixes.some(prefix => context.data.startsWith(prefix));
75
+ }
76
+ /**
77
+ * Process the OID4VC credential offer URI
78
+ *
79
+ * Delegates to the configured onImportCredential callback for actual processing.
80
+ * This allows apps to implement their own navigation, UI, and error handling.
81
+ *
82
+ * @param context - The QR code context
83
+ * @returns Result of the processing
84
+ */
85
+ async handle(context) {
86
+ try {
87
+ const result = await this.onImportCredential(context.data, context);
88
+ return {
89
+ success: result.success,
90
+ data: result.credential,
91
+ error: result.error,
92
+ metadata: {
93
+ type: 'oid4vc',
94
+ uri: context.data,
95
+ ...result.metadata,
96
+ },
97
+ };
98
+ }
99
+ catch (error) {
100
+ const err = error instanceof Error ? error : new Error(String(error));
101
+ return {
102
+ success: false,
103
+ error: err,
104
+ metadata: {
105
+ type: 'oid4vc',
106
+ uri: context.data,
107
+ },
108
+ };
109
+ }
110
+ }
111
+ }
112
+ exports.OID4VCHandler = OID4VCHandler;
113
+ /**
114
+ * Create an OID4VC handler with custom configuration
115
+ *
116
+ * This is a convenience factory function for creating an OID4VC handler.
117
+ *
118
+ * @param config - Handler configuration
119
+ * @returns Configured OID4VC handler
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * const handler = createOID4VCHandler({
124
+ * onImportCredential: async (uri) => {
125
+ * // Your import logic
126
+ * return { success: true };
127
+ * },
128
+ * });
129
+ * ```
130
+ */
131
+ function createOID4VCHandler(config) {
132
+ return new OID4VCHandler(config);
133
+ }
134
+ //# sourceMappingURL=oid4vc-handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oid4vc-handler.js","sourceRoot":"","sources":["../../../src/qr-handlers/builtin/oid4vc-handler.ts"],"names":[],"mappings":";;;AAiMA,kDAIC;AA1ID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,MAAa,aAAa;IACxB,EAAE,GAAG,QAAQ,CAAC;IACd,QAAQ,CAAS;IAET,WAAW,CAAW;IACtB,kBAAkB,CAGO;IAEjC,YAAY,MAA2B;QACrC,IAAI,CAAC,MAAM,CAAC,kBAAkB,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;QAC7D,CAAC;QAED,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,IAAI,CAAC,4BAA4B,CAAC,CAAC;QACxE,IAAI,CAAC,kBAAkB,GAAG,MAAM,CAAC,kBAAkB,CAAC;IACtD,CAAC;IAED;;;;;;;;OAQG;IACH,SAAS,CAAC,OAAsB;QAC9B,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1E,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,MAAM,CAAC,OAAsB;QACjC,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YAEpE,OAAO;gBACL,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,IAAI,EAAE,MAAM,CAAC,UAAU;gBACvB,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,QAAQ,EAAE;oBACR,IAAI,EAAE,QAAQ;oBACd,GAAG,EAAE,OAAO,CAAC,IAAI;oBACjB,GAAG,MAAM,CAAC,QAAQ;iBACnB;aACF,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,GAAG,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;YACtE,OAAO;gBACL,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,GAAG;gBACV,QAAQ,EAAE;oBACR,IAAI,EAAE,QAAQ;oBACd,GAAG,EAAE,OAAO,CAAC,IAAI;iBAClB;aACF,CAAC;QACJ,CAAC;IACH,CAAC;CACF;AApED,sCAoEC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,mBAAmB,CACjC,MAA2B;IAE3B,OAAO,IAAI,aAAa,CAAC,MAAM,CAAC,CAAC;AACnC,CAAC"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * QR Code Handler System
3
+ *
4
+ * This module provides a generic, extensible system for processing QR codes
5
+ * in decentralized identity wallet applications.
6
+ *
7
+ * ## Overview
8
+ *
9
+ * The QR handler system is built around these core concepts:
10
+ *
11
+ * - **QRCodeContext**: Contains parsed QR data (raw string, JSON, URLs)
12
+ * - **QRCodeHandler**: Interface for implementing specific QR code processors
13
+ * - **QRCodeProcessor**: Manages handler registration and execution
14
+ * - **ProcessOptions**: Configuration for how QR codes are processed
15
+ *
16
+ * ## Basic Usage
17
+ *
18
+ * ```typescript
19
+ * import { createQRCodeProcessor } from '@docknetwork/wallet-sdk-core';
20
+ *
21
+ * // Create processor
22
+ * const processor = createQRCodeProcessor();
23
+ *
24
+ * // Register handlers
25
+ * processor.registerHandler({
26
+ * id: 'my-handler',
27
+ * priority: 10,
28
+ * canHandle: (context) => context.data.startsWith('myprotocol://'),
29
+ * handle: async (context) => {
30
+ * // Process the QR code
31
+ * return { success: true };
32
+ * }
33
+ * });
34
+ *
35
+ * // Process QR code
36
+ * const result = await processor.process(scannedData);
37
+ * ```
38
+ *
39
+ * ## Custom Handlers
40
+ *
41
+ * Handlers can be implemented as classes or objects:
42
+ *
43
+ * ```typescript
44
+ * class MyCustomHandler implements QRCodeHandler {
45
+ * id = 'my-custom-handler';
46
+ * priority = 20;
47
+ *
48
+ * canHandle(context: QRCodeContext): boolean {
49
+ * return context.jsonData?.type === 'my-type';
50
+ * }
51
+ *
52
+ * async handle(context: QRCodeContext): Promise<QRCodeHandlerResult> {
53
+ * // Your processing logic
54
+ * return { success: true, data: processedData };
55
+ * }
56
+ * }
57
+ *
58
+ * processor.registerHandler(new MyCustomHandler());
59
+ * ```
60
+ *
61
+ * ## Built-in Handlers
62
+ *
63
+ * The SDK provides built-in handlers for common protocols:
64
+ * - OID4VC (OpenID for Verifiable Credentials)
65
+ * - OID4VP (OpenID for Verifiable Presentations)
66
+ * - DIDComm (Decentralized Identity Communication)
67
+ *
68
+ * These handlers are available in separate modules and can be imported
69
+ * and registered as needed.
70
+ *
71
+ * @module qr-handlers
72
+ */
73
+ export * from './types';
74
+ export * from './processor';
75
+ export * from './builtin';
76
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/qr-handlers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuEG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC"}
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ /**
3
+ * QR Code Handler System
4
+ *
5
+ * This module provides a generic, extensible system for processing QR codes
6
+ * in decentralized identity wallet applications.
7
+ *
8
+ * ## Overview
9
+ *
10
+ * The QR handler system is built around these core concepts:
11
+ *
12
+ * - **QRCodeContext**: Contains parsed QR data (raw string, JSON, URLs)
13
+ * - **QRCodeHandler**: Interface for implementing specific QR code processors
14
+ * - **QRCodeProcessor**: Manages handler registration and execution
15
+ * - **ProcessOptions**: Configuration for how QR codes are processed
16
+ *
17
+ * ## Basic Usage
18
+ *
19
+ * ```typescript
20
+ * import { createQRCodeProcessor } from '@docknetwork/wallet-sdk-core';
21
+ *
22
+ * // Create processor
23
+ * const processor = createQRCodeProcessor();
24
+ *
25
+ * // Register handlers
26
+ * processor.registerHandler({
27
+ * id: 'my-handler',
28
+ * priority: 10,
29
+ * canHandle: (context) => context.data.startsWith('myprotocol://'),
30
+ * handle: async (context) => {
31
+ * // Process the QR code
32
+ * return { success: true };
33
+ * }
34
+ * });
35
+ *
36
+ * // Process QR code
37
+ * const result = await processor.process(scannedData);
38
+ * ```
39
+ *
40
+ * ## Custom Handlers
41
+ *
42
+ * Handlers can be implemented as classes or objects:
43
+ *
44
+ * ```typescript
45
+ * class MyCustomHandler implements QRCodeHandler {
46
+ * id = 'my-custom-handler';
47
+ * priority = 20;
48
+ *
49
+ * canHandle(context: QRCodeContext): boolean {
50
+ * return context.jsonData?.type === 'my-type';
51
+ * }
52
+ *
53
+ * async handle(context: QRCodeContext): Promise<QRCodeHandlerResult> {
54
+ * // Your processing logic
55
+ * return { success: true, data: processedData };
56
+ * }
57
+ * }
58
+ *
59
+ * processor.registerHandler(new MyCustomHandler());
60
+ * ```
61
+ *
62
+ * ## Built-in Handlers
63
+ *
64
+ * The SDK provides built-in handlers for common protocols:
65
+ * - OID4VC (OpenID for Verifiable Credentials)
66
+ * - OID4VP (OpenID for Verifiable Presentations)
67
+ * - DIDComm (Decentralized Identity Communication)
68
+ *
69
+ * These handlers are available in separate modules and can be imported
70
+ * and registered as needed.
71
+ *
72
+ * @module qr-handlers
73
+ */
74
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
75
+ if (k2 === undefined) k2 = k;
76
+ var desc = Object.getOwnPropertyDescriptor(m, k);
77
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
78
+ desc = { enumerable: true, get: function() { return m[k]; } };
79
+ }
80
+ Object.defineProperty(o, k2, desc);
81
+ }) : (function(o, m, k, k2) {
82
+ if (k2 === undefined) k2 = k;
83
+ o[k2] = m[k];
84
+ }));
85
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
86
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
87
+ };
88
+ Object.defineProperty(exports, "__esModule", { value: true });
89
+ __exportStar(require("./types"), exports);
90
+ __exportStar(require("./processor"), exports);
91
+ __exportStar(require("./builtin"), exports);
92
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/qr-handlers/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuEG;;;;;;;;;;;;;;;;AAEH,0CAAwB;AACxB,8CAA4B;AAC5B,4CAA0B"}
@@ -0,0 +1,110 @@
1
+ import { QRCodeHandler, QRCodeHandlerResult, QRCodeProcessor, ProcessOptions } from './types';
2
+ /**
3
+ * Default implementation of QRCodeProcessor
4
+ *
5
+ * This processor manages a registry of QR code handlers and executes them
6
+ * in priority order to process scanned QR codes. It provides a flexible,
7
+ * extensible system for handling various types of QR codes in a wallet application.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const processor = new DefaultQRCodeProcessor();
12
+ *
13
+ * // Register handlers
14
+ * processor.registerHandler(new OID4VCHandler());
15
+ * processor.registerHandler(new CredentialHandler());
16
+ *
17
+ * // Process QR code
18
+ * const result = await processor.process(scannedData);
19
+ * if (result.success) {
20
+ * console.log('QR code processed:', result.data);
21
+ * } else {
22
+ * console.error('Failed to process QR code:', result.error);
23
+ * }
24
+ * ```
25
+ */
26
+ export declare class DefaultQRCodeProcessor implements QRCodeProcessor {
27
+ private handlers;
28
+ /**
29
+ * Register a new QR code handler
30
+ *
31
+ * @param handler - The handler to register
32
+ * @throws Error if a handler with the same ID is already registered
33
+ */
34
+ registerHandler(handler: QRCodeHandler): void;
35
+ /**
36
+ * Unregister a QR code handler by its ID
37
+ *
38
+ * @param id - The ID of the handler to unregister
39
+ * @returns True if the handler was found and removed, false otherwise
40
+ */
41
+ unregisterHandler(id: string): boolean;
42
+ /**
43
+ * Get all registered handlers sorted by priority
44
+ *
45
+ * @returns Array of registered handlers sorted by priority (lowest first)
46
+ */
47
+ getHandlers(): QRCodeHandler[];
48
+ /**
49
+ * Get a specific handler by its ID
50
+ *
51
+ * @param id - The ID of the handler to retrieve
52
+ * @returns The handler if found, undefined otherwise
53
+ */
54
+ getHandler(id: string): QRCodeHandler | undefined;
55
+ /**
56
+ * Clear all registered handlers
57
+ */
58
+ clearHandlers(): void;
59
+ /**
60
+ * Process QR code data through registered handlers
61
+ *
62
+ * This method:
63
+ * 1. Prepares the context from raw QR data
64
+ * 2. Executes handlers in priority order
65
+ * 3. Returns the first successful result (or continues if stopOnFirstSuccess is false)
66
+ * 4. Returns an error result if no handler can process the data
67
+ *
68
+ * @param data - Raw QR code data string
69
+ * @param options - Processing options
70
+ * @returns Result of the processing
71
+ */
72
+ process(data: string, options?: ProcessOptions): Promise<QRCodeHandlerResult>;
73
+ /**
74
+ * Default context preparation function
75
+ *
76
+ * This method attempts to parse the raw QR data as JSON or URL.
77
+ * Override this by providing a custom prepareContext function in ProcessOptions.
78
+ *
79
+ * @param data - Raw QR code data string
80
+ * @returns Prepared context object
81
+ */
82
+ private defaultPrepareContext;
83
+ /**
84
+ * Execute a promise with a timeout
85
+ *
86
+ * @param promise - Promise to execute
87
+ * @param timeoutMs - Timeout in milliseconds
88
+ * @returns Result of the promise
89
+ * @throws Error if the promise times out
90
+ */
91
+ private withTimeout;
92
+ }
93
+ /**
94
+ * Create a new QR code processor instance
95
+ *
96
+ * This is a convenience factory function for creating a processor.
97
+ *
98
+ * @param handlers - Optional array of handlers to register immediately
99
+ * @returns New processor instance with handlers registered
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * const processor = createQRCodeProcessor([
104
+ * new OID4VCHandler(),
105
+ * new CredentialHandler(),
106
+ * ]);
107
+ * ```
108
+ */
109
+ export declare function createQRCodeProcessor(handlers?: QRCodeHandler[]): QRCodeProcessor;
110
+ //# sourceMappingURL=processor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"processor.d.ts","sourceRoot":"","sources":["../../src/qr-handlers/processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,cAAc,EACf,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,sBAAuB,YAAW,eAAe;IAC5D,OAAO,CAAC,QAAQ,CAAyC;IAEzD;;;;;OAKG;IACH,eAAe,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI;IAU7C;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAItC;;;;OAIG;IACH,WAAW,IAAI,aAAa,EAAE;IAM9B;;;;;OAKG;IACH,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS;IAIjD;;OAEG;IACH,aAAa,IAAI,IAAI;IAIrB;;;;;;;;;;;;OAYG;IACG,OAAO,CACX,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,mBAAmB,CAAC;IAsH/B;;;;;;;;OAQG;YACW,qBAAqB;IA0BnC;;;;;;;OAOG;YACW,WAAW;CAc1B;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CACnC,QAAQ,CAAC,EAAE,aAAa,EAAE,GACzB,eAAe,CAUjB"}
@@ -0,0 +1,251 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultQRCodeProcessor = void 0;
4
+ exports.createQRCodeProcessor = createQRCodeProcessor;
5
+ /**
6
+ * Default implementation of QRCodeProcessor
7
+ *
8
+ * This processor manages a registry of QR code handlers and executes them
9
+ * in priority order to process scanned QR codes. It provides a flexible,
10
+ * extensible system for handling various types of QR codes in a wallet application.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const processor = new DefaultQRCodeProcessor();
15
+ *
16
+ * // Register handlers
17
+ * processor.registerHandler(new OID4VCHandler());
18
+ * processor.registerHandler(new CredentialHandler());
19
+ *
20
+ * // Process QR code
21
+ * const result = await processor.process(scannedData);
22
+ * if (result.success) {
23
+ * console.log('QR code processed:', result.data);
24
+ * } else {
25
+ * console.error('Failed to process QR code:', result.error);
26
+ * }
27
+ * ```
28
+ */
29
+ class DefaultQRCodeProcessor {
30
+ handlers = new Map();
31
+ /**
32
+ * Register a new QR code handler
33
+ *
34
+ * @param handler - The handler to register
35
+ * @throws Error if a handler with the same ID is already registered
36
+ */
37
+ registerHandler(handler) {
38
+ if (this.handlers.has(handler.id)) {
39
+ throw new Error(`Handler with id "${handler.id}" is already registered. ` +
40
+ `Please use a unique ID or unregister the existing handler first.`);
41
+ }
42
+ this.handlers.set(handler.id, handler);
43
+ }
44
+ /**
45
+ * Unregister a QR code handler by its ID
46
+ *
47
+ * @param id - The ID of the handler to unregister
48
+ * @returns True if the handler was found and removed, false otherwise
49
+ */
50
+ unregisterHandler(id) {
51
+ return this.handlers.delete(id);
52
+ }
53
+ /**
54
+ * Get all registered handlers sorted by priority
55
+ *
56
+ * @returns Array of registered handlers sorted by priority (lowest first)
57
+ */
58
+ getHandlers() {
59
+ return Array.from(this.handlers.values()).sort((a, b) => (a.priority ?? 100) - (b.priority ?? 100));
60
+ }
61
+ /**
62
+ * Get a specific handler by its ID
63
+ *
64
+ * @param id - The ID of the handler to retrieve
65
+ * @returns The handler if found, undefined otherwise
66
+ */
67
+ getHandler(id) {
68
+ return this.handlers.get(id);
69
+ }
70
+ /**
71
+ * Clear all registered handlers
72
+ */
73
+ clearHandlers() {
74
+ this.handlers.clear();
75
+ }
76
+ /**
77
+ * Process QR code data through registered handlers
78
+ *
79
+ * This method:
80
+ * 1. Prepares the context from raw QR data
81
+ * 2. Executes handlers in priority order
82
+ * 3. Returns the first successful result (or continues if stopOnFirstSuccess is false)
83
+ * 4. Returns an error result if no handler can process the data
84
+ *
85
+ * @param data - Raw QR code data string
86
+ * @param options - Processing options
87
+ * @returns Result of the processing
88
+ */
89
+ async process(data, options = {}) {
90
+ const { timeout = 30000, stopOnFirstSuccess = true, prepareContext = this.defaultPrepareContext.bind(this), onError, onSuccess, } = options;
91
+ // Prepare context from raw data
92
+ let context;
93
+ try {
94
+ context = await this.withTimeout(prepareContext(data), timeout);
95
+ }
96
+ catch (error) {
97
+ return {
98
+ success: false,
99
+ error: error instanceof Error ? error : new Error(String(error)),
100
+ metadata: { phase: 'context-preparation' },
101
+ };
102
+ }
103
+ // Get sorted handlers
104
+ const handlers = this.getHandlers();
105
+ if (handlers.length === 0) {
106
+ return {
107
+ success: false,
108
+ error: new Error('No handlers registered'),
109
+ metadata: { phase: 'handler-execution' },
110
+ };
111
+ }
112
+ let lastError;
113
+ const attemptedHandlers = [];
114
+ // Execute handlers in priority order
115
+ for (const handler of handlers) {
116
+ try {
117
+ // Check if handler can process this data
118
+ const canHandle = await this.withTimeout(Promise.resolve(handler.canHandle(context)), timeout);
119
+ if (!canHandle) {
120
+ continue;
121
+ }
122
+ attemptedHandlers.push(handler.id);
123
+ // Execute handler
124
+ const result = await this.withTimeout(handler.handle(context), timeout);
125
+ // Call success callback if provided
126
+ if (result.success && onSuccess) {
127
+ try {
128
+ onSuccess(result, handler);
129
+ }
130
+ catch (callbackError) {
131
+ console.error(`Success callback error for handler ${handler.id}:`, callbackError);
132
+ }
133
+ }
134
+ // Stop on first success if configured
135
+ if (result.success && stopOnFirstSuccess) {
136
+ return {
137
+ ...result,
138
+ metadata: {
139
+ ...result.metadata,
140
+ handlerId: handler.id,
141
+ attemptedHandlers,
142
+ },
143
+ };
144
+ }
145
+ // Store error if handler failed
146
+ if (!result.success && result.error) {
147
+ lastError = result.error;
148
+ }
149
+ }
150
+ catch (error) {
151
+ const handlerError = error instanceof Error ? error : new Error(String(error));
152
+ lastError = handlerError;
153
+ // Call error callback if provided
154
+ if (onError) {
155
+ try {
156
+ onError(handlerError, handler);
157
+ }
158
+ catch (callbackError) {
159
+ console.error(`Error callback error for handler ${handler.id}:`, callbackError);
160
+ }
161
+ }
162
+ // Continue to next handler
163
+ continue;
164
+ }
165
+ }
166
+ // No handler could process the data
167
+ return {
168
+ success: false,
169
+ error: lastError ||
170
+ new Error(`No handler could process the QR code. Attempted handlers: ${attemptedHandlers.join(', ') || 'none'}`),
171
+ metadata: {
172
+ phase: 'handler-execution',
173
+ attemptedHandlers,
174
+ },
175
+ };
176
+ }
177
+ /**
178
+ * Default context preparation function
179
+ *
180
+ * This method attempts to parse the raw QR data as JSON or URL.
181
+ * Override this by providing a custom prepareContext function in ProcessOptions.
182
+ *
183
+ * @param data - Raw QR code data string
184
+ * @returns Prepared context object
185
+ */
186
+ async defaultPrepareContext(data) {
187
+ const context = { data };
188
+ // Try to parse as URL
189
+ try {
190
+ // Basic URL validation
191
+ if (data.startsWith('http://') || data.startsWith('https://')) {
192
+ const url = new URL(data);
193
+ context.url = data;
194
+ context.parsedUrl = data;
195
+ }
196
+ }
197
+ catch {
198
+ // Not a valid URL, continue
199
+ }
200
+ // Try to parse as JSON
201
+ try {
202
+ const parsed = JSON.parse(data);
203
+ context.jsonData = parsed;
204
+ }
205
+ catch {
206
+ // Not valid JSON, continue
207
+ }
208
+ return context;
209
+ }
210
+ /**
211
+ * Execute a promise with a timeout
212
+ *
213
+ * @param promise - Promise to execute
214
+ * @param timeoutMs - Timeout in milliseconds
215
+ * @returns Result of the promise
216
+ * @throws Error if the promise times out
217
+ */
218
+ async withTimeout(promise, timeoutMs) {
219
+ return Promise.race([
220
+ promise,
221
+ new Promise((_, reject) => setTimeout(() => reject(new Error(`Operation timed out after ${timeoutMs}ms`)), timeoutMs)),
222
+ ]);
223
+ }
224
+ }
225
+ exports.DefaultQRCodeProcessor = DefaultQRCodeProcessor;
226
+ /**
227
+ * Create a new QR code processor instance
228
+ *
229
+ * This is a convenience factory function for creating a processor.
230
+ *
231
+ * @param handlers - Optional array of handlers to register immediately
232
+ * @returns New processor instance with handlers registered
233
+ *
234
+ * @example
235
+ * ```typescript
236
+ * const processor = createQRCodeProcessor([
237
+ * new OID4VCHandler(),
238
+ * new CredentialHandler(),
239
+ * ]);
240
+ * ```
241
+ */
242
+ function createQRCodeProcessor(handlers) {
243
+ const processor = new DefaultQRCodeProcessor();
244
+ if (handlers) {
245
+ for (const handler of handlers) {
246
+ processor.registerHandler(handler);
247
+ }
248
+ }
249
+ return processor;
250
+ }
251
+ //# sourceMappingURL=processor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"processor.js","sourceRoot":"","sources":["../../src/qr-handlers/processor.ts"],"names":[],"mappings":";;;AA0SA,sDAYC;AA9SD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,sBAAsB;IACzB,QAAQ,GAA+B,IAAI,GAAG,EAAE,CAAC;IAEzD;;;;;OAKG;IACH,eAAe,CAAC,OAAsB;QACpC,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CACb,oBAAoB,OAAO,CAAC,EAAE,2BAA2B;gBACvD,kEAAkE,CACrE,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,QAA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