forked from OrbitChainLabs/OrbitChain-Contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet_connect.html
More file actions
484 lines (441 loc) · 19.4 KB
/
Copy pathwallet_connect.html
File metadata and controls
484 lines (441 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="color-scheme" content="light dark" />
<title>OrbitChain – Wallet Connect</title>
<style>
/* Theme tokens. Both palettes meet WCAG AA: body/label text >= 7:1,
button text >= 6:1, focus ring >= 3:1 against the surface it sits on. */
:root {
color-scheme: light dark;
--bg: #f0f4ff;
--card-bg: #ffffff;
--heading: #1a1a2e;
--text: #3d3d4d;
--muted: #575766; /* 7.0:1 on --card-bg */
--accent: #4451d4; /* 6.2:1 with white text */
--accent-hover: #333fb8;
--accent-text: #ffffff;
--focus-ring: #1a1a2e; /* 15.9:1 on card, 14.5:1 on bg */
--error: #b3261e; /* 6.6:1 on --card-bg */
--ok: #1b7a3d; /* 4.9:1 on --card-bg */
--code-bg: #eef0f6;
--code-text: #1a1a2e;
--border: #9aa0b5;
--option-bg: #fafbff;
--option-border: #c9cde4;
--option-hover: #eef0ff;
--overlay: rgba(20, 20, 40, .45);
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #12141f;
--card-bg: #1e2130;
--heading: #f2f3fa;
--text: #d5d8e6;
--muted: #aab0c5; /* 7.3:1 on --card-bg */
--accent: #a9b2ff; /* 8.9:1 with dark text */
--accent-hover: #c2c9ff;
--accent-text: #12142b;
--focus-ring: #cfd5ff; /* 10.4:1 on card */
--error: #ffb4ab; /* 8.7:1 on --card-bg */
--ok: #7edb9a; /* 9.3:1 on --card-bg */
--code-bg: #12141f;
--code-text: #e8eaf5;
--border: #6a7190;
--option-bg: #262a3d;
--option-border: #4a5070;
--option-hover: #303550;
--overlay: rgba(0, 0, 0, .6);
}
}
body {
font-family: sans-serif;
display: flex; flex-direction: column; align-items: center; justify-content: center;
min-height: 100vh; margin: 0;
background: var(--bg); color: var(--text);
}
.card {
background: var(--card-bg); border-radius: 12px; padding: 2rem 2.5rem;
box-shadow: 0 4px 20px rgba(0,0,0,.15); text-align: center;
min-width: 320px; max-width: 90vw;
}
h1 { margin: 0 0 1.5rem; font-size: 1.4rem; color: var(--heading); }
button {
background: var(--accent); color: var(--accent-text);
border: 1px solid transparent; border-radius: 8px;
padding: .75rem 1.75rem; font-size: 1rem; cursor: pointer;
}
button:hover { background: var(--accent-hover); }
button[disabled] { opacity: .55; cursor: default; }
button.secondary {
background: transparent; color: var(--heading); border-color: var(--border);
}
/* Visible focus indicator (>= 3:1 against every surface it appears on). */
:focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 2px;
border-radius: 4px;
}
/* Motion is opt-in: only animate for users who have not asked for reduced motion. */
@media (prefers-reduced-motion: no-preference) {
button { transition: background .2s, color .2s; }
}
@media (prefers-contrast: more) {
button { border-color: currentColor; }
:focus-visible { outline-width: 3px; }
}
#wallet-info { margin-top: 1.25rem; display: none; text-align: left; }
#wallet-info p { margin: .4rem 0; font-size: .9rem; }
.connected-flag { color: var(--ok); font-weight: 600; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); margin-right: 6px; }
#wallet-address {
font-family: monospace; font-size: .85rem; word-break: break-all;
background: var(--code-bg); color: var(--code-text);
padding: .5rem .75rem; border-radius: 6px; display: block;
}
/* Desktop manual-entry fallback (replaces the old window.prompt()). */
#manual-form { margin-top: 1.25rem; display: none; text-align: left; }
#manual-form label { display: block; font-size: .9rem; font-weight: 600; color: var(--heading); margin-bottom: .3rem; }
#manual-form .hint { font-size: .8rem; color: var(--muted); margin: 0 0 .5rem; }
#manual-address {
width: 100%; box-sizing: border-box; font-family: monospace; font-size: .85rem;
padding: .55rem .6rem; border: 1px solid var(--border); border-radius: 6px;
background: var(--card-bg); color: var(--text);
}
.form-actions { margin-top: .75rem; display: flex; gap: .5rem; }
.form-actions button { padding: .55rem 1.1rem; font-size: .9rem; }
#status { margin-top: .75rem; font-size: .9rem; color: var(--muted); min-height: 1.2em; }
#status.is-error { color: var(--error); font-weight: 600; }
/* Desktop multi-wallet chooser (issue #142) — inline in the card, NOT the
modal picker: the overlay dialog is reserved for the mobile deep-link
flow, and desktop UAs must never see it. */
#desktop-wallets { margin-top: 1.25rem; display: none; text-align: left; }
#desktop-wallets .chooser-title { font-size: .9rem; font-weight: 600; color: var(--heading); margin: 0 0 .5rem; }
/* Mobile wallet picker (modal dialog) */
#wallet-picker-overlay { display: none; position: fixed; inset: 0; background: var(--overlay); align-items: center; justify-content: center; padding: 1rem; z-index: 10; }
#wallet-picker-overlay.open { display: flex; }
#wallet-picker { background: var(--card-bg); border-radius: 12px; padding: 1.5rem; width: 320px; max-width: 100%; text-align: left; }
#wallet-picker h2 { margin: 0 0 .25rem; font-size: 1.1rem; color: var(--heading); }
#wallet-picker .hint { margin: 0 0 1rem; font-size: .8rem; color: var(--muted); }
.wallet-option { display: flex; align-items: center; gap: .6rem; width: 100%; padding: .65rem .75rem; margin-bottom: .5rem; border: 1px solid var(--option-border); border-radius: 8px; background: var(--option-bg); cursor: pointer; font-size: .95rem; color: var(--heading); }
.wallet-option:hover { background: var(--option-hover); border-color: var(--accent); }
.wallet-option .icon { font-size: 1.2rem; }
#pubkey-field { display: none; margin: .5rem 0 .75rem; }
#pubkey-field label { display: block; font-size: .85rem; font-weight: 600; color: var(--heading); margin-bottom: .3rem; }
#pubkey-input { width: 100%; box-sizing: border-box; padding: .6rem .7rem; border: 1px solid var(--border); border-radius: 8px; font-family: monospace; font-size: .82rem; background: var(--card-bg); color: var(--text); }
#wallet-picker-cancel { background: none; border: 1px solid transparent; color: var(--muted); font-size: .85rem; cursor: pointer; margin-top: .25rem; padding: .35rem .6rem; }
#wallet-picker-cancel:hover { color: var(--heading); background: none; }
</style>
</head>
<body>
<main class="card">
<h1><span aria-hidden="true">🌟</span> OrbitChain Wallet Connect</h1>
<button id="connect-btn" type="button" onclick="connectWallet()">Connect Wallet</button>
<div id="wallet-info">
<p class="connected-flag"><span class="dot" aria-hidden="true"></span><strong>Wallet Connected</strong></p>
<p id="address-label">Address:</p>
<div id="wallet-address" role="note" aria-labelledby="address-label"></div>
</div>
<!-- Desktop multi-wallet chooser (issue #142): shown when more than one
wallet extension is detected. A single detected wallet connects
directly; none falls through to the manual form below. -->
<div id="desktop-wallets" role="group" aria-labelledby="desktop-wallets-title">
<p class="chooser-title" id="desktop-wallets-title">Choose a wallet</p>
<div id="desktop-wallet-options"></div>
<button type="button" class="secondary" id="desktop-wallets-cancel">Cancel</button>
</div>
<!-- Desktop fallback: accessible inline form instead of window.prompt(),
so manual entry has real label/error semantics and keyboard focus flow. -->
<form id="manual-form" novalidate>
<label for="manual-address">Stellar public key</label>
<p class="hint" id="manual-address-hint">
Freighter was not detected. Enter your public key manually — it starts
with G and is 56 characters long.
</p>
<input
id="manual-address"
name="manual-address"
type="text"
autocomplete="off"
spellcheck="false"
aria-describedby="manual-address-hint"
aria-required="true"
/>
<div class="form-actions">
<button type="submit">Use this address</button>
<button type="button" class="secondary" id="manual-cancel">Cancel</button>
</div>
</form>
<!-- Single live region: progress and errors are both announced from here.
Errors are written verbatim with a plain "Error:" prefix. -->
<p id="status" role="status" aria-live="polite"></p>
<noscript><p>This page requires JavaScript to connect a wallet.</p></noscript>
</main>
<!-- Mobile wallet picker: shown instead of the Freighter path when
OrbitWalletConnect.isMobile() is true. See wallet_connect.js and
docs/tutorials/dapp-integration.md for the SEP-10 / SEP-0007 flow. -->
<div id="wallet-picker-overlay">
<div id="wallet-picker" role="dialog" aria-modal="true" aria-labelledby="wallet-picker-title" aria-describedby="wallet-picker-hint">
<h2 id="wallet-picker-title">Choose a wallet</h2>
<p class="hint" id="wallet-picker-hint">Detected mobile device. Pick your Stellar wallet to continue.</p>
<div id="wallet-options"></div>
<div id="pubkey-field">
<label for="pubkey-input">Stellar public key</label>
<input id="pubkey-input" type="text" autocomplete="off" spellcheck="false" aria-required="true" placeholder="G…" />
</div>
<div>
<button id="wallet-picker-cancel" type="button" onclick="closeWalletPicker(true)">Cancel</button>
</div>
</div>
</div>
<script src="wallet_connect.js"></script>
<script>
const btn = document.getElementById('connect-btn');
const walletInfo = document.getElementById('wallet-info');
const walletAddress = document.getElementById('wallet-address');
const status = document.getElementById('status');
const pickerOverlay = document.getElementById('wallet-picker-overlay');
const picker = document.getElementById('wallet-picker');
const walletOptions = document.getElementById('wallet-options');
const pubkeyField = document.getElementById('pubkey-field');
const pubkeyInput = document.getElementById('pubkey-input');
const manualForm = document.getElementById('manual-form');
const manualInput = document.getElementById('manual-address');
const manualCancel = document.getElementById('manual-cancel');
let selectedWallet = null;
function setStatus(message) {
status.classList.remove('is-error');
status.textContent = message;
}
// Errors share the #status live region (announced politely, read
// verbatim) with a plain-text prefix instead of an emoji.
function setError(message) {
status.classList.add('is-error');
status.textContent = 'Error: ' + message;
}
function isValidPublicKey(addr) {
return typeof addr === 'string' && /^G[A-Z2-7]{55}$/.test(addr);
}
async function connectWallet() {
setStatus('Connecting…');
btn.disabled = true;
btn.setAttribute('aria-busy', 'true');
try {
if (OrbitWalletConnect.isMobile()) {
openWalletPicker();
return;
}
// Desktop: consult the wallet registry (issue #142). Exactly one
// detected extension keeps the historical UX and connects directly;
// several show the inline chooser; none falls back to manual entry.
const detected = OrbitWalletConnect.detectedDesktopWallets();
if (detected.length === 1) {
const address = await detected[0].connect();
showConnected(address);
} else if (detected.length > 1) {
openDesktopChooser(detected);
} else {
openManualForm();
}
} catch (err) {
setError(err.message);
resetConnectButton();
}
}
function resetConnectButton() {
btn.disabled = false;
btn.removeAttribute('aria-busy');
}
// ── Desktop multi-wallet chooser (issue #142) ─────────────────────────
const desktopWallets = document.getElementById('desktop-wallets');
const desktopWalletOptions = document.getElementById('desktop-wallet-options');
const desktopWalletsCancel = document.getElementById('desktop-wallets-cancel');
function openDesktopChooser(wallets) {
desktopWalletOptions.innerHTML = '';
wallets.forEach((wallet) => {
const el = document.createElement('button');
el.type = 'button';
el.className = 'wallet-option';
el.innerHTML = `<span class="icon" aria-hidden="true">${wallet.icon}</span><span>${wallet.name}</span>`;
el.onclick = () => connectDesktopWallet(wallet);
desktopWalletOptions.appendChild(el);
});
desktopWallets.style.display = 'block';
btn.removeAttribute('aria-busy');
setStatus('Several wallet extensions detected. Choose one to connect.');
const firstOption = desktopWalletOptions.querySelector('button');
if (firstOption) firstOption.focus();
}
function closeDesktopChooser(refocus) {
desktopWallets.style.display = 'none';
desktopWalletOptions.innerHTML = '';
resetConnectButton();
if (refocus) btn.focus();
}
async function connectDesktopWallet(wallet) {
try {
setStatus('Connecting with ' + wallet.name + '…');
const address = await wallet.connect();
showConnected(address);
} catch (err) {
setError(err.message);
}
}
desktopWalletsCancel.addEventListener('click', () => {
closeDesktopChooser(true);
setStatus('Connection cancelled.');
});
// ── Desktop manual-entry fallback ─────────────────────────────────────
function openManualForm() {
btn.setAttribute('aria-busy', 'false');
manualForm.style.display = 'block';
setStatus('Freighter not detected. Enter your Stellar public key below.');
manualInput.focus();
}
function closeManualForm() {
manualForm.style.display = 'none';
manualInput.value = '';
resetConnectButton();
}
manualForm.addEventListener('submit', (event) => {
event.preventDefault();
const addr = manualInput.value.trim();
if (isValidPublicKey(addr)) {
closeManualForm();
showConnected(addr);
} else {
setError('Enter a valid Stellar public key (starts with G, 56 chars).');
manualInput.focus();
}
});
manualCancel.addEventListener('click', () => {
closeManualForm();
setStatus('Connection cancelled.');
btn.focus();
});
// ── Mobile wallet picker (modal dialog semantics) ─────────────────────
function openWalletPicker() {
walletOptions.innerHTML = '';
OrbitWalletConnect.walletsFor('mobile').forEach((wallet) => {
const el = document.createElement('button');
el.type = 'button';
el.className = 'wallet-option';
el.innerHTML = `<span class="icon" aria-hidden="true">${wallet.icon}</span><span>${wallet.name}</span>`;
el.onclick = () => selectWallet(wallet);
walletOptions.appendChild(el);
});
pickerOverlay.classList.add('open');
btn.removeAttribute('aria-busy');
setStatus('');
const firstOption = walletOptions.querySelector('button');
if (firstOption) firstOption.focus();
}
function closeWalletPicker(refocus) {
pickerOverlay.classList.remove('open');
pubkeyField.style.display = 'none';
pubkeyInput.value = '';
selectedWallet = null;
resetConnectButton();
if (refocus) btn.focus();
}
// Keep keyboard focus inside the open dialog; Escape closes it.
pickerOverlay.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
closeWalletPicker(true);
setStatus('Connection cancelled.');
return;
}
if (e.key !== 'Tab') return;
const focusables = Array.from(
picker.querySelectorAll('button, input')
).filter((el) => el.offsetParent !== null && !el.disabled);
if (focusables.length === 0) return;
const first = focusables[0];
const last = focusables[focusables.length - 1];
if (e.shiftKey && document.activeElement === first) {
e.preventDefault();
last.focus();
} else if (!e.shiftKey && document.activeElement === last) {
e.preventDefault();
first.focus();
}
});
function selectWallet(wallet) {
selectedWallet = wallet;
pubkeyField.style.display = 'block';
pubkeyInput.focus();
pubkeyInput.onkeydown = (e) => {
if (e.key === 'Enter') confirmMobileConnect();
};
// Replace the option list with a single confirm affordance.
walletOptions.innerHTML = `<p class="hint">Connecting with <strong>${wallet.name}</strong>. Enter the public key to authenticate, then continue in the app.</p>`;
const confirmBtn = document.createElement('button');
confirmBtn.type = 'button';
confirmBtn.className = 'wallet-option';
confirmBtn.textContent = 'Continue to ' + wallet.name;
confirmBtn.onclick = confirmMobileConnect;
walletOptions.appendChild(confirmBtn);
}
async function confirmMobileConnect() {
if (!selectedWallet) return;
try {
setStatus('Requesting SEP-10 challenge…');
await OrbitWalletConnect.startMobileConnect(selectedWallet, pubkeyInput.value.trim());
// startMobileConnect() redirects the browser to the wallet app; if we
// reach this line the redirect didn't happen (e.g. blocked), so the
// picker stays open for the user to retry.
} catch (err) {
setError(err.message);
btn.disabled = false;
}
}
function showConnected(address) {
closeWalletPicker(false);
closeDesktopChooser(false);
closeManualForm();
walletAddress.textContent = address;
walletInfo.style.display = 'block';
btn.textContent = 'Disconnect';
btn.onclick = disconnectWallet;
resetConnectButton();
setStatus('Wallet connected.');
btn.focus();
}
function disconnectWallet() {
walletInfo.style.display = 'none';
walletAddress.textContent = '';
btn.textContent = 'Connect Wallet';
btn.onclick = connectWallet;
setStatus('Disconnected.');
btn.focus();
}
// ── Resume the mobile deep-link flow on return from the wallet app ────
(function resumeMobileReturnIfAny() {
const result = OrbitWalletConnect.handleMobileReturn();
if (!result) return;
setStatus('Verifying signed challenge…');
btn.disabled = true;
result
.then(({ token }) => {
const address = decodeJwtSubject(token);
showConnected(address || '(see session token)');
})
.catch((err) => {
setError(err.message);
resetConnectButton();
});
})();
function decodeJwtSubject(token) {
try {
const payload = JSON.parse(atob(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')));
return payload.sub;
} catch {
return null;
}
}
</script>
</body>
</html>