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