wallet-connect-button-react 1.1.6 → 1.1.8
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/WalletConnectButton.d.ts +4 -2
- package/dist/index.esm.js +154 -11
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +154 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -110,21 +110,41 @@ function useSearchParams() {
|
|
|
110
110
|
return [searchParams, setSearchParams, removeSearchParam];
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
// Global cache to prevent duplicate requests
|
|
114
|
+
var credentialsCache = new Map();
|
|
115
|
+
function getDefaultHost(useLocalWcServer, business, issuance) {
|
|
116
|
+
// If useLocalWcServer is set, use local server
|
|
117
|
+
if (useLocalWcServer) {
|
|
118
|
+
if (business) {
|
|
119
|
+
return issuance ? 'http://localhost:4007' : 'http://bw.localhost:3021';
|
|
120
|
+
}
|
|
121
|
+
return issuance ? 'http://localhost:3007' : 'http://localhost:3021';
|
|
122
|
+
}
|
|
123
|
+
// Otherwise use remote servers
|
|
124
|
+
if (business) {
|
|
125
|
+
return issuance ? 'https://bw.issuance.wallet-connect.eu' : 'https://bw.wallet-connect.eu';
|
|
126
|
+
}
|
|
127
|
+
return issuance ? 'https://issuance.wallet-connect.eu' : 'https://wallet-connect.eu';
|
|
128
|
+
}
|
|
129
|
+
function constructURI(clientId, session_type, walletConnectHost, business) {
|
|
130
|
+
var request_uri = "".concat(walletConnectHost, "/disclosure/").concat(clientId, "/request_uri?session_type=").concat(session_type);
|
|
115
131
|
var request_uri_method = "post";
|
|
116
132
|
var client_id_uri = "".concat(clientId, ".example.com");
|
|
117
|
-
|
|
133
|
+
var deepLinkScheme = business
|
|
134
|
+
? 'businesswalletdebuginteraction://wallet.kvk.rijksoverheid.nl'
|
|
135
|
+
: 'walletdebuginteraction://wallet.edi.rijksoverheid.nl';
|
|
136
|
+
return "".concat(deepLinkScheme, "/disclosure_based_issuance?request_uri=").concat(encodeURIComponent(request_uri), "&request_uri_method=").concat(request_uri_method, "&client_id=").concat(client_id_uri);
|
|
118
137
|
}
|
|
119
138
|
function WalletConnectButton(_a) {
|
|
120
139
|
var _this = this;
|
|
121
|
-
var label = _a.label, clientId = _a.clientId, onSuccess = _a.onSuccess, apiKey = _a.apiKey,
|
|
122
|
-
var
|
|
123
|
-
var
|
|
124
|
-
var
|
|
140
|
+
var label = _a.label, clientId = _a.clientId, onSuccess = _a.onSuccess, apiKey = _a.apiKey, _b = _a.useLocalWcServer, useLocalWcServer = _b === void 0 ? false : _b, _c = _a.business, business = _c === void 0 ? false : _c, lang = _a.lang, helpBaseUrl = _a.helpBaseUrl, _d = _a.issuance, issuance = _d === void 0 ? false : _d;
|
|
141
|
+
var _e = useSearchParams(), searchParams = _e[0], setSearchParams = _e[1], removeSearchParam = _e[2];
|
|
142
|
+
var _f = react.useState(false), loading = _f[0], setLoading = _f[1];
|
|
143
|
+
var _g = react.useState(null), error = _g[0], setError = _g[1];
|
|
125
144
|
var buttonRef = react.useRef(null);
|
|
126
|
-
var
|
|
127
|
-
var
|
|
145
|
+
var walletConnectHost = getDefaultHost(useLocalWcServer, business, issuance);
|
|
146
|
+
var sameDeviceUl = constructURI(clientId, "same_device", walletConnectHost, business);
|
|
147
|
+
var crossDeviceUl = constructURI(clientId, "cross_device", walletConnectHost, business);
|
|
128
148
|
react.useEffect(function () {
|
|
129
149
|
// Dynamically import the web component
|
|
130
150
|
var loadWebComponent = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -141,6 +161,8 @@ function WalletConnectButton(_a) {
|
|
|
141
161
|
if (button) {
|
|
142
162
|
button.addEventListener("success", handleSuccess);
|
|
143
163
|
button.addEventListener("failed", handleFailed);
|
|
164
|
+
// Add click listener directly to the element
|
|
165
|
+
button.addEventListener("click", handleButtonClick);
|
|
144
166
|
}
|
|
145
167
|
return [3 /*break*/, 3];
|
|
146
168
|
case 2:
|
|
@@ -158,9 +180,130 @@ function WalletConnectButton(_a) {
|
|
|
158
180
|
if (button) {
|
|
159
181
|
button.removeEventListener("success", handleSuccess);
|
|
160
182
|
button.removeEventListener("failed", handleFailed);
|
|
183
|
+
button.removeEventListener("click", handleButtonClick);
|
|
161
184
|
}
|
|
162
185
|
};
|
|
163
186
|
}, []);
|
|
187
|
+
var fetchRequestedCredentials = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
188
|
+
var cacheKey, cached, fetchPromise;
|
|
189
|
+
var _this = this;
|
|
190
|
+
return __generator(this, function (_a) {
|
|
191
|
+
switch (_a.label) {
|
|
192
|
+
case 0:
|
|
193
|
+
if (!apiKey || !clientId)
|
|
194
|
+
return [2 /*return*/, []];
|
|
195
|
+
cacheKey = "".concat(clientId, "-").concat(walletConnectHost);
|
|
196
|
+
cached = credentialsCache.get(cacheKey);
|
|
197
|
+
if (cached === null || cached === void 0 ? void 0 : cached.data) {
|
|
198
|
+
return [2 /*return*/, cached.data];
|
|
199
|
+
}
|
|
200
|
+
if (!(cached === null || cached === void 0 ? void 0 : cached.promise)) return [3 /*break*/, 2];
|
|
201
|
+
return [4 /*yield*/, cached.promise];
|
|
202
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
203
|
+
case 2:
|
|
204
|
+
fetchPromise = (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
205
|
+
var url, headers, response, credentials, error_2;
|
|
206
|
+
var _a, _b;
|
|
207
|
+
return __generator(this, function (_c) {
|
|
208
|
+
switch (_c.label) {
|
|
209
|
+
case 0:
|
|
210
|
+
_c.trys.push([0, 2, , 3]);
|
|
211
|
+
url = "".concat(walletConnectHost, "/api/client/").concat(clientId, "/requested-credentials");
|
|
212
|
+
headers = { 'Authorization': "Bearer ".concat(apiKey) };
|
|
213
|
+
return [4 /*yield*/, axios.get(url, { headers: headers })];
|
|
214
|
+
case 1:
|
|
215
|
+
response = _c.sent();
|
|
216
|
+
credentials = ((_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.requestedCredentials) || [];
|
|
217
|
+
// Cache the result
|
|
218
|
+
credentialsCache.set(cacheKey, { data: credentials });
|
|
219
|
+
return [2 /*return*/, credentials];
|
|
220
|
+
case 2:
|
|
221
|
+
error_2 = _c.sent();
|
|
222
|
+
// Remove failed request from cache
|
|
223
|
+
credentialsCache.delete(cacheKey);
|
|
224
|
+
throw error_2;
|
|
225
|
+
case 3: return [2 /*return*/];
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}); })();
|
|
229
|
+
// Cache the promise to prevent duplicate requests
|
|
230
|
+
credentialsCache.set(cacheKey, { promise: fetchPromise });
|
|
231
|
+
return [4 /*yield*/, fetchPromise];
|
|
232
|
+
case 3: return [2 /*return*/, _a.sent()];
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}); };
|
|
236
|
+
var injectCredentialsIntoShadowDOM = function (credentials, retryCount) {
|
|
237
|
+
if (retryCount === void 0) { retryCount = 0; }
|
|
238
|
+
var maxRetries = 10;
|
|
239
|
+
var walletButton = buttonRef.current;
|
|
240
|
+
if (!walletButton || !walletButton.shadowRoot) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
// Remove any existing credential info
|
|
244
|
+
var existingCredentials = walletButton.shadowRoot.querySelector('.required-credentials');
|
|
245
|
+
if (existingCredentials) {
|
|
246
|
+
existingCredentials.remove();
|
|
247
|
+
}
|
|
248
|
+
if (credentials.length === 0)
|
|
249
|
+
return;
|
|
250
|
+
// Look for the modal and website section
|
|
251
|
+
var modal = walletButton.shadowRoot.querySelector('.modal');
|
|
252
|
+
if (!modal) {
|
|
253
|
+
// Retry if modal not found yet
|
|
254
|
+
if (retryCount < maxRetries) {
|
|
255
|
+
setTimeout(function () {
|
|
256
|
+
injectCredentialsIntoShadowDOM(credentials, retryCount + 1);
|
|
257
|
+
}, 200);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
var websiteSection = modal.querySelector('.website');
|
|
263
|
+
// Determine language and translations
|
|
264
|
+
var isNL = lang === 'nl';
|
|
265
|
+
var headerText = isNL ? 'Benodigde Attestaties:' : 'Required Credentials:';
|
|
266
|
+
var getLinkText = isNL ? '→ Verkrijg attestatie' : '→ Get credential';
|
|
267
|
+
// Create credential info element
|
|
268
|
+
var credentialsDiv = document.createElement('div');
|
|
269
|
+
credentialsDiv.className = 'required-credentials';
|
|
270
|
+
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) {
|
|
271
|
+
var credentialName = isNL ? credential.credentialName.nl : credential.credentialName.en;
|
|
272
|
+
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 ");
|
|
273
|
+
}).join(''), "\n </div>\n ");
|
|
274
|
+
// Insert the credentials div after the website section
|
|
275
|
+
if (websiteSection) {
|
|
276
|
+
websiteSection.insertAdjacentElement('afterend', credentialsDiv);
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
// Fallback: insert at the beginning of modal
|
|
280
|
+
modal.insertBefore(credentialsDiv, modal.firstChild);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
var handleButtonClick = function (_event) { return __awaiter(_this, void 0, void 0, function () {
|
|
284
|
+
var credentials_1, error_3;
|
|
285
|
+
return __generator(this, function (_a) {
|
|
286
|
+
switch (_a.label) {
|
|
287
|
+
case 0:
|
|
288
|
+
_a.trys.push([0, 2, , 3]);
|
|
289
|
+
return [4 /*yield*/, fetchRequestedCredentials()];
|
|
290
|
+
case 1:
|
|
291
|
+
credentials_1 = _a.sent();
|
|
292
|
+
if (credentials_1 && credentials_1.length > 0) {
|
|
293
|
+
// Inject credentials into the shadow DOM with multiple attempts
|
|
294
|
+
setTimeout(function () {
|
|
295
|
+
injectCredentialsIntoShadowDOM(credentials_1);
|
|
296
|
+
}, 100);
|
|
297
|
+
}
|
|
298
|
+
return [3 /*break*/, 3];
|
|
299
|
+
case 2:
|
|
300
|
+
error_3 = _a.sent();
|
|
301
|
+
console.error('Failed to fetch credentials:', error_3);
|
|
302
|
+
return [3 /*break*/, 3];
|
|
303
|
+
case 3: return [2 /*return*/];
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}); };
|
|
164
307
|
// Function to handle the 'success' event
|
|
165
308
|
var handleSuccess = function (e) {
|
|
166
309
|
var customEvent = e;
|
|
@@ -184,7 +327,7 @@ function WalletConnectButton(_a) {
|
|
|
184
327
|
if (!session_token)
|
|
185
328
|
return;
|
|
186
329
|
setLoading(true);
|
|
187
|
-
var baseUrl = apiKey ?
|
|
330
|
+
var baseUrl = apiKey ? walletConnectHost : "";
|
|
188
331
|
var url = baseUrl + "/api/disclosed-attributes?session_token=".concat(session_token, "&client_id=").concat(clientId);
|
|
189
332
|
if (nonce)
|
|
190
333
|
url = "".concat(url, "&nonce=").concat(nonce);
|
|
@@ -218,7 +361,7 @@ function WalletConnectButton(_a) {
|
|
|
218
361
|
if (error) {
|
|
219
362
|
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
363
|
}
|
|
221
|
-
return (jsxRuntime.jsx("nl-wallet-button", { ref: buttonRef, text: label, usecase: issuance ? "" : clientId, "start-url": "".concat(walletConnectHost
|
|
364
|
+
return (jsxRuntime.jsx("nl-wallet-button", { ref: buttonRef, text: label, usecase: issuance ? "" : clientId, "start-url": "".concat(walletConnectHost, "/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
365
|
}
|
|
223
366
|
|
|
224
367
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|