wallet-connect-button-react 1.1.6 → 1.1.7

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.
package/dist/index.js CHANGED
@@ -110,8 +110,11 @@ function useSearchParams() {
110
110
  return [searchParams, setSearchParams, removeSearchParam];
111
111
  }
112
112
 
113
- function constructURI(clientId, session_type) {
114
- var request_uri = "https://issuance.wallet-connect.eu/disclosure/".concat(clientId, "/request_uri?session_type=").concat(session_type);
113
+ // Global cache to prevent duplicate requests
114
+ var credentialsCache = new Map();
115
+ function constructURI(clientId, session_type, walletConnectHost) {
116
+ var baseHost = walletConnectHost || "https://issuance.wallet-connect.eu";
117
+ var request_uri = "".concat(baseHost, "/disclosure/").concat(clientId, "/request_uri?session_type=").concat(session_type);
115
118
  var request_uri_method = "post";
116
119
  var client_id_uri = "".concat(clientId, ".example.com");
117
120
  return "walletdebuginteraction://wallet.edi.rijksoverheid.nl/disclosure_based_issuance?request_uri=".concat(encodeURIComponent(request_uri), "&request_uri_method=").concat(request_uri_method, "&client_id=").concat(client_id_uri);
@@ -123,8 +126,8 @@ function WalletConnectButton(_a) {
123
126
  var _c = react.useState(false), loading = _c[0], setLoading = _c[1];
124
127
  var _d = react.useState(null), error = _d[0], setError = _d[1];
125
128
  var buttonRef = react.useRef(null);
126
- var sameDeviceUl = constructURI(clientId, "same_device");
127
- var crossDeviceUl = constructURI(clientId, "cross_device");
129
+ var sameDeviceUl = constructURI(clientId, "same_device", walletConnectHost);
130
+ var crossDeviceUl = constructURI(clientId, "cross_device", walletConnectHost);
128
131
  react.useEffect(function () {
129
132
  // Dynamically import the web component
130
133
  var loadWebComponent = function () { return __awaiter(_this, void 0, void 0, function () {
@@ -141,6 +144,8 @@ function WalletConnectButton(_a) {
141
144
  if (button) {
142
145
  button.addEventListener("success", handleSuccess);
143
146
  button.addEventListener("failed", handleFailed);
147
+ // Add click listener directly to the element
148
+ button.addEventListener("click", handleButtonClick);
144
149
  }
145
150
  return [3 /*break*/, 3];
146
151
  case 2:
@@ -158,9 +163,131 @@ function WalletConnectButton(_a) {
158
163
  if (button) {
159
164
  button.removeEventListener("success", handleSuccess);
160
165
  button.removeEventListener("failed", handleFailed);
166
+ button.removeEventListener("click", handleButtonClick);
161
167
  }
162
168
  };
163
169
  }, []);
170
+ var fetchRequestedCredentials = function () { return __awaiter(_this, void 0, void 0, function () {
171
+ var cacheKey, cached, fetchPromise;
172
+ var _this = this;
173
+ return __generator(this, function (_a) {
174
+ switch (_a.label) {
175
+ case 0:
176
+ if (!apiKey || !clientId)
177
+ return [2 /*return*/, []];
178
+ cacheKey = "".concat(clientId, "-").concat(walletConnectHost || "default");
179
+ cached = credentialsCache.get(cacheKey);
180
+ if (cached === null || cached === void 0 ? void 0 : cached.data) {
181
+ return [2 /*return*/, cached.data];
182
+ }
183
+ if (!(cached === null || cached === void 0 ? void 0 : cached.promise)) return [3 /*break*/, 2];
184
+ return [4 /*yield*/, cached.promise];
185
+ case 1: return [2 /*return*/, _a.sent()];
186
+ case 2:
187
+ fetchPromise = (function () { return __awaiter(_this, void 0, void 0, function () {
188
+ var baseUrl, url, headers, response, credentials, error_2;
189
+ var _a, _b;
190
+ return __generator(this, function (_c) {
191
+ switch (_c.label) {
192
+ case 0:
193
+ _c.trys.push([0, 2, , 3]);
194
+ baseUrl = walletConnectHost || "https://wallet-connect.eu";
195
+ url = "".concat(baseUrl, "/api/client/").concat(clientId, "/requested-credentials");
196
+ headers = { 'Authorization': "Bearer ".concat(apiKey) };
197
+ return [4 /*yield*/, axios.get(url, { headers: headers })];
198
+ case 1:
199
+ response = _c.sent();
200
+ credentials = ((_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.requestedCredentials) || [];
201
+ // Cache the result
202
+ credentialsCache.set(cacheKey, { data: credentials });
203
+ return [2 /*return*/, credentials];
204
+ case 2:
205
+ error_2 = _c.sent();
206
+ // Remove failed request from cache
207
+ credentialsCache.delete(cacheKey);
208
+ throw error_2;
209
+ case 3: return [2 /*return*/];
210
+ }
211
+ });
212
+ }); })();
213
+ // Cache the promise to prevent duplicate requests
214
+ credentialsCache.set(cacheKey, { promise: fetchPromise });
215
+ return [4 /*yield*/, fetchPromise];
216
+ case 3: return [2 /*return*/, _a.sent()];
217
+ }
218
+ });
219
+ }); };
220
+ var injectCredentialsIntoShadowDOM = function (credentials, retryCount) {
221
+ if (retryCount === void 0) { retryCount = 0; }
222
+ var maxRetries = 10;
223
+ var walletButton = buttonRef.current;
224
+ if (!walletButton || !walletButton.shadowRoot) {
225
+ return;
226
+ }
227
+ // Remove any existing credential info
228
+ var existingCredentials = walletButton.shadowRoot.querySelector('.required-credentials');
229
+ if (existingCredentials) {
230
+ existingCredentials.remove();
231
+ }
232
+ if (credentials.length === 0)
233
+ return;
234
+ // Look for the modal and website section
235
+ var modal = walletButton.shadowRoot.querySelector('.modal');
236
+ if (!modal) {
237
+ // Retry if modal not found yet
238
+ if (retryCount < maxRetries) {
239
+ setTimeout(function () {
240
+ injectCredentialsIntoShadowDOM(credentials, retryCount + 1);
241
+ }, 200);
242
+ return;
243
+ }
244
+ return;
245
+ }
246
+ var websiteSection = modal.querySelector('.website');
247
+ // Determine language and translations
248
+ var isNL = lang === 'nl';
249
+ var headerText = isNL ? 'Benodigde Attestaties:' : 'Required Credentials:';
250
+ var getLinkText = isNL ? '→ Verkrijg attestatie' : '→ Get credential';
251
+ // Create credential info element
252
+ var credentialsDiv = document.createElement('div');
253
+ credentialsDiv.className = 'required-credentials';
254
+ credentialsDiv.innerHTML = "\n <div style=\"\n background: #f8f9fa;\n border: 1px solid #e9ecef;\n border-radius: 6px;\n padding: 12px;\n font-family: inherit;\n font-size: 13px;\n line-height: 1.4;\n \">\n <div style=\"margin: 0 0 8px 0; color: #212529; font-size: 14px; font-weight: 600;\">".concat(headerText, "</div>\n ").concat(credentials.map(function (credential) {
255
+ var credentialName = isNL ? credential.credentialName.nl : credential.credentialName.en;
256
+ return "\n <div style=\"margin-bottom: 6px; display: flex; align-items: center; flex-wrap: wrap; gap: 8px;\">\n <span style=\"color: #495057; font-weight: 500;\">".concat(credentialName, "</span>\n ").concat(credential.websiteUrl ? "\n <a href=\"".concat(credential.websiteUrl, "\" target=\"_blank\" rel=\"noopener noreferrer\" style=\"\n color: #0066cc;\n text-decoration: none;\n font-size: 12px;\n white-space: nowrap;\n \">").concat(getLinkText, "</a>\n ") : '', "\n </div>\n ");
257
+ }).join(''), "\n </div>\n ");
258
+ // Insert the credentials div after the website section
259
+ if (websiteSection) {
260
+ websiteSection.insertAdjacentElement('afterend', credentialsDiv);
261
+ }
262
+ else {
263
+ // Fallback: insert at the beginning of modal
264
+ modal.insertBefore(credentialsDiv, modal.firstChild);
265
+ }
266
+ };
267
+ var handleButtonClick = function (_event) { return __awaiter(_this, void 0, void 0, function () {
268
+ var credentials_1, error_3;
269
+ return __generator(this, function (_a) {
270
+ switch (_a.label) {
271
+ case 0:
272
+ _a.trys.push([0, 2, , 3]);
273
+ return [4 /*yield*/, fetchRequestedCredentials()];
274
+ case 1:
275
+ credentials_1 = _a.sent();
276
+ if (credentials_1 && credentials_1.length > 0) {
277
+ // Inject credentials into the shadow DOM with multiple attempts
278
+ setTimeout(function () {
279
+ injectCredentialsIntoShadowDOM(credentials_1);
280
+ }, 100);
281
+ }
282
+ return [3 /*break*/, 3];
283
+ case 2:
284
+ error_3 = _a.sent();
285
+ console.error('Failed to fetch credentials:', error_3);
286
+ return [3 /*break*/, 3];
287
+ case 3: return [2 /*return*/];
288
+ }
289
+ });
290
+ }); };
164
291
  // Function to handle the 'success' event
165
292
  var handleSuccess = function (e) {
166
293
  var customEvent = e;
@@ -218,7 +345,7 @@ function WalletConnectButton(_a) {
218
345
  if (error) {
219
346
  return (jsxRuntime.jsx("div", { className: "attributes", children: jsxRuntime.jsxs("div", { className: "verification-card", children: [jsxRuntime.jsx("h2", { children: "Error" }), jsxRuntime.jsxs("p", { children: ["An error occurred while verifying your attributes: ", error] })] }) }));
220
347
  }
221
- return (jsxRuntime.jsx("nl-wallet-button", { ref: buttonRef, text: label, usecase: issuance ? "" : clientId, "start-url": "".concat(walletConnectHost || "https://wallet-connect.eu", "/api/create-session?lang=en&return_url=").concat(encodeURIComponent(window.location.href)), lang: lang || "nl", "same-device-ul": sameDeviceUl, "cross-device-ul": crossDeviceUl, "help-base-url": helpBaseUrl }));
348
+ return (jsxRuntime.jsx("nl-wallet-button", { ref: buttonRef, text: label, usecase: issuance ? "" : clientId, "start-url": "".concat(walletConnectHost || "https://wallet-connect.eu", "/api/create-session?lang=en&return_url=").concat(encodeURIComponent(window.location.href)), lang: lang || "nl", "same-device-ul": sameDeviceUl, "cross-device-ul": crossDeviceUl, "help-base-url": helpBaseUrl, onClick: handleButtonClick }));
222
349
  }
223
350
 
224
351
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};