@bwalletx/connect 0.5.4 → 0.5.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.
- package/CHANGELOG.md +28 -0
- package/README.md +76 -8
- package/dist/bwalletx-connect.global.js +92 -25
- package/dist/bwalletx-connect.global.js.map +1 -1
- package/dist/{wallets-CbzMtxrf.d.ts → choice-DCh-tgpT.d.cts} +112 -4
- package/dist/{wallets-CbzMtxrf.d.cts → choice-DCh-tgpT.d.ts} +112 -4
- package/dist/chunk-34DKBQNN.js +222 -0
- package/dist/chunk-34DKBQNN.js.map +1 -0
- package/dist/{chunk-QRXFL66W.js → chunk-A2WLGS25.js} +6 -4
- package/dist/chunk-A2WLGS25.js.map +1 -0
- package/dist/chunk-IFGOAROM.js +255 -0
- package/dist/chunk-IFGOAROM.js.map +1 -0
- package/dist/chunk-LS6AYEBO.js +1886 -0
- package/dist/chunk-LS6AYEBO.js.map +1 -0
- package/dist/element-D9Zl-x12.d.cts +46 -0
- package/dist/element-D9Zl-x12.d.ts +46 -0
- package/dist/element.cjs +1147 -61
- package/dist/element.cjs.map +1 -1
- package/dist/element.d.cts +23 -28
- package/dist/element.d.ts +23 -28
- package/dist/element.js +29 -2
- package/dist/element.js.map +1 -1
- package/dist/index.cjs +1469 -453
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +163 -14
- package/dist/index.d.ts +163 -14
- package/dist/index.js +100 -12
- package/dist/index.js.map +1 -1
- package/dist/next.cjs +5 -3
- package/dist/next.cjs.map +1 -1
- package/dist/next.js +1 -1
- package/dist/react.cjs +994 -183
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.cts +75 -8
- package/dist/react.d.ts +75 -8
- package/dist/react.js +121 -40
- package/dist/react.js.map +1 -1
- package/dist/server.cjs +5 -3
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +8 -3
- package/dist/server.d.ts +8 -3
- package/dist/server.js +1 -1
- package/package.json +10 -9
- package/dist/chunk-EXRQJTXF.js +0 -910
- package/dist/chunk-EXRQJTXF.js.map +0 -1
- package/dist/chunk-QRXFL66W.js.map +0 -1
- package/dist/chunk-RIOHG5JH.js +0 -459
- package/dist/chunk-RIOHG5JH.js.map +0 -1
|
@@ -0,0 +1,1886 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ACCESS_PROTOCOL,
|
|
3
|
+
AccessError,
|
|
4
|
+
MAX_HOLDING_PROOFS,
|
|
5
|
+
challengeBytes,
|
|
6
|
+
parseAccessChallenge
|
|
7
|
+
} from "./chunk-MWD4BCO5.js";
|
|
8
|
+
|
|
9
|
+
// src/discover.ts
|
|
10
|
+
import { WalletClient, XDM } from "@bsv/sdk";
|
|
11
|
+
var RDNS_EXTENSION = "com.bwalletx.extension";
|
|
12
|
+
var RDNS_MOBILE = "space.bwallet.mobile";
|
|
13
|
+
var BWALLETX_RDNS = [RDNS_EXTENSION, RDNS_MOBILE];
|
|
14
|
+
var DEFAULT_DISCOVER_MS = 300;
|
|
15
|
+
function discoverBwalletX(ms = DEFAULT_DISCOVER_MS) {
|
|
16
|
+
if (typeof window === "undefined") return Promise.resolve(null);
|
|
17
|
+
return new Promise((resolve) => {
|
|
18
|
+
let done = false;
|
|
19
|
+
let timer;
|
|
20
|
+
const finish = (r) => {
|
|
21
|
+
if (done) return;
|
|
22
|
+
done = true;
|
|
23
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
24
|
+
window.removeEventListener("brc100:announceWallet", onAnnounce);
|
|
25
|
+
resolve(r);
|
|
26
|
+
};
|
|
27
|
+
const onAnnounce = (e) => {
|
|
28
|
+
const d = e.detail;
|
|
29
|
+
const rdns = d?.info?.rdns ?? "";
|
|
30
|
+
if (d?.wallet && BWALLETX_RDNS.includes(rdns)) {
|
|
31
|
+
finish({ wallet: d.wallet, method: rdns === RDNS_EXTENSION ? "extension" : "in-app", rdns });
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
window.addEventListener("brc100:announceWallet", onAnnounce);
|
|
35
|
+
window.dispatchEvent(new Event("brc100:requestWallet"));
|
|
36
|
+
if (done) return;
|
|
37
|
+
if (isFramed()) {
|
|
38
|
+
void probeXdm(Math.max(ms, 1200)).then((w) => finish(w ? { wallet: w, method: "in-app", rdns: null } : null));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
timer = setTimeout(() => {
|
|
42
|
+
timer = void 0;
|
|
43
|
+
finish(null);
|
|
44
|
+
}, ms);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
async function findBwalletX(ms = DEFAULT_DISCOVER_MS) {
|
|
48
|
+
return (await discoverBwalletX(ms))?.wallet ?? null;
|
|
49
|
+
}
|
|
50
|
+
var isFramed = () => {
|
|
51
|
+
try {
|
|
52
|
+
return window.parent !== window;
|
|
53
|
+
} catch {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var BWALLETX_PARENT_ORIGINS = [
|
|
58
|
+
"https://web.bwalletx.com",
|
|
59
|
+
"https://desktop.bwalletx.com",
|
|
60
|
+
"https://bwalletx.com",
|
|
61
|
+
"https://www.bwallet.space",
|
|
62
|
+
"https://bwallet.space",
|
|
63
|
+
"capacitor://localhost",
|
|
64
|
+
// iOS app shell
|
|
65
|
+
"https://localhost",
|
|
66
|
+
// Android app shell
|
|
67
|
+
"tauri://localhost",
|
|
68
|
+
// bWalletX Desktop (macOS)
|
|
69
|
+
"https://tauri.localhost",
|
|
70
|
+
// bWalletX Desktop (Windows)
|
|
71
|
+
"chrome-extension://bombmmcjghjdlbmcclnhjodgcelnehgl"
|
|
72
|
+
// bWalletX for Chrome (fixed id)
|
|
73
|
+
];
|
|
74
|
+
var parentOrigin = () => {
|
|
75
|
+
try {
|
|
76
|
+
const anc = window.location.ancestorOrigins;
|
|
77
|
+
if (anc && anc.length > 0) return anc[0] ?? null;
|
|
78
|
+
if (!document.referrer) return null;
|
|
79
|
+
const u = new URL(document.referrer);
|
|
80
|
+
return `${u.protocol}//${u.host}`;
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
async function probeXdm(ms = 1200, allowed = BWALLETX_PARENT_ORIGINS) {
|
|
86
|
+
const origin = parentOrigin();
|
|
87
|
+
if (!origin || !allowed.includes(origin)) return null;
|
|
88
|
+
try {
|
|
89
|
+
const w = new WalletClient(new XDM(origin));
|
|
90
|
+
const ok = await Promise.race([
|
|
91
|
+
w.isAuthenticated({}).then(
|
|
92
|
+
() => true,
|
|
93
|
+
() => false
|
|
94
|
+
),
|
|
95
|
+
new Promise((r) => setTimeout(() => r(false), ms))
|
|
96
|
+
]);
|
|
97
|
+
return ok ? w : null;
|
|
98
|
+
} catch {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
var warmedAt = 0;
|
|
103
|
+
function prewarmBwalletX() {
|
|
104
|
+
if (typeof window === "undefined" || Date.now() - warmedAt < 2e4) return;
|
|
105
|
+
warmedAt = Date.now();
|
|
106
|
+
void discoverBwalletX(0).then((f) => {
|
|
107
|
+
if (f && f.method === "extension") void f.wallet.isAuthenticated({}).catch(() => {
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
function bindPrewarm(el2) {
|
|
112
|
+
const on = () => prewarmBwalletX();
|
|
113
|
+
const evs = ["pointerenter", "focus", "touchstart"];
|
|
114
|
+
for (const e of evs) el2.addEventListener(e, on, { passive: true });
|
|
115
|
+
return () => evs.forEach((e) => el2.removeEventListener(e, on));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/in-order.ts
|
|
119
|
+
function inOrder(handle, onError) {
|
|
120
|
+
let queue = Promise.resolve();
|
|
121
|
+
return (frame) => {
|
|
122
|
+
queue = queue.then(() => handle(frame)).catch((e) => onError?.(e));
|
|
123
|
+
return queue;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/pair.ts
|
|
128
|
+
import { PrivateKey as PrivateKey2 } from "@bsv/sdk";
|
|
129
|
+
|
|
130
|
+
// src/protocol.ts
|
|
131
|
+
import { PublicKey } from "@bsv/sdk";
|
|
132
|
+
var PAIR_VERSION = "1";
|
|
133
|
+
var PAIR_INFO = "bwallet-pair-v1";
|
|
134
|
+
var PAIR_HOST = "www.bwallet.space";
|
|
135
|
+
var DEFAULT_RELAY = "relay.bwallet.space";
|
|
136
|
+
var QR_LIFETIME_S = 120;
|
|
137
|
+
var enc = new TextEncoder();
|
|
138
|
+
var dec = new TextDecoder();
|
|
139
|
+
var binary = (bytes) => {
|
|
140
|
+
let s = "";
|
|
141
|
+
for (let i = 0; i < bytes.length; i += 32768) s += String.fromCharCode(...bytes.subarray(i, i + 32768));
|
|
142
|
+
return s;
|
|
143
|
+
};
|
|
144
|
+
var b64url = (bytes) => btoa(binary(bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
145
|
+
var fromB64url = (s) => {
|
|
146
|
+
const t = s.replace(/-/g, "+").replace(/_/g, "/");
|
|
147
|
+
const bin = atob(t + "=".repeat((4 - t.length % 4) % 4));
|
|
148
|
+
return Uint8Array.from(bin, (ch) => ch.charCodeAt(0));
|
|
149
|
+
};
|
|
150
|
+
var b64 = (bytes) => btoa(binary(bytes));
|
|
151
|
+
var fromB64 = (s) => Uint8Array.from(atob(s), (ch) => ch.charCodeAt(0));
|
|
152
|
+
var newChannel = () => b64url(crypto.getRandomValues(new Uint8Array(16)));
|
|
153
|
+
function pairUrl(link) {
|
|
154
|
+
const q = new URLSearchParams({ v: link.v, r: link.r, c: link.c, k: link.k, o: link.o, e: String(link.e) });
|
|
155
|
+
return `https://${PAIR_HOST}/pair?${q}`;
|
|
156
|
+
}
|
|
157
|
+
var newClaim = () => b64url(crypto.getRandomValues(new Uint8Array(16)));
|
|
158
|
+
var WALLET_CLAIM_INFO = "bwallet-pair-wallet-claim-v1";
|
|
159
|
+
var hex = (b) => Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
|
|
160
|
+
async function walletClaimFor(channel, sitePub) {
|
|
161
|
+
const d = await crypto.subtle.digest("SHA-256", enc.encode(`${WALLET_CLAIM_INFO}|${channel}|${sitePub}`));
|
|
162
|
+
return b64url(new Uint8Array(d));
|
|
163
|
+
}
|
|
164
|
+
async function claimDigest(claim) {
|
|
165
|
+
return hex(new Uint8Array(await crypto.subtle.digest("SHA-256", enc.encode(claim))));
|
|
166
|
+
}
|
|
167
|
+
var relaySocketUrl = (relay, channel, role, expiry, claim, walletClaimHash) => {
|
|
168
|
+
const local = /^(localhost|127\.0\.0\.1)(:\d+)?$/.test(relay);
|
|
169
|
+
const q = new URLSearchParams({ role });
|
|
170
|
+
if (expiry) q.set("e", String(expiry));
|
|
171
|
+
if (claim) q.set("claim", claim);
|
|
172
|
+
if (walletClaimHash && role === "site") q.set("wc", walletClaimHash);
|
|
173
|
+
return `${local ? "ws" : "wss"}://${relay}/v1/c/${channel}?${q}`;
|
|
174
|
+
};
|
|
175
|
+
async function deriveSession(mine, theirs, channel) {
|
|
176
|
+
const point = mine.deriveSharedSecret(PublicKey.fromString(theirs));
|
|
177
|
+
const ikm = new Uint8Array(point.encode(true));
|
|
178
|
+
const base = await crypto.subtle.importKey("raw", ikm, "HKDF", false, ["deriveBits"]);
|
|
179
|
+
const bits = new Uint8Array(
|
|
180
|
+
await crypto.subtle.deriveBits(
|
|
181
|
+
{ name: "HKDF", hash: "SHA-256", salt: fromB64url(channel), info: enc.encode(PAIR_INFO) },
|
|
182
|
+
base,
|
|
183
|
+
256
|
|
184
|
+
)
|
|
185
|
+
);
|
|
186
|
+
const key = await crypto.subtle.importKey("raw", bits, "AES-GCM", false, ["encrypt", "decrypt"]);
|
|
187
|
+
const h = new Uint8Array(await crypto.subtle.digest("SHA-256", bits));
|
|
188
|
+
const code = String((h[0] << 8 | h[1]) % 1e4).padStart(4, "0");
|
|
189
|
+
return { key, code };
|
|
190
|
+
}
|
|
191
|
+
var Sealer = class {
|
|
192
|
+
constructor(key, me) {
|
|
193
|
+
this.key = key;
|
|
194
|
+
this.me = me;
|
|
195
|
+
}
|
|
196
|
+
key;
|
|
197
|
+
me;
|
|
198
|
+
sent = 0;
|
|
199
|
+
lastSeen = 0;
|
|
200
|
+
async seal(msg) {
|
|
201
|
+
const s = ++this.sent;
|
|
202
|
+
const n = crypto.getRandomValues(new Uint8Array(12));
|
|
203
|
+
const ct = new Uint8Array(
|
|
204
|
+
await crypto.subtle.encrypt(
|
|
205
|
+
{ name: "AES-GCM", iv: n, additionalData: enc.encode(`${this.me}|${s}`) },
|
|
206
|
+
this.key,
|
|
207
|
+
enc.encode(JSON.stringify(msg))
|
|
208
|
+
)
|
|
209
|
+
);
|
|
210
|
+
return { s, n: b64(n), d: b64(ct) };
|
|
211
|
+
}
|
|
212
|
+
/** Decrypts a frame from the other side; null for replays, reflections or tampering. */
|
|
213
|
+
async open(f) {
|
|
214
|
+
if (!Number.isInteger(f.s) || f.s <= this.lastSeen) return null;
|
|
215
|
+
const from = this.me === "site" ? "wallet" : "site";
|
|
216
|
+
try {
|
|
217
|
+
const pt = await crypto.subtle.decrypt(
|
|
218
|
+
{ name: "AES-GCM", iv: fromB64(f.n), additionalData: enc.encode(`${from}|${f.s}`) },
|
|
219
|
+
this.key,
|
|
220
|
+
fromB64(f.d)
|
|
221
|
+
);
|
|
222
|
+
this.lastSeen = f.s;
|
|
223
|
+
return JSON.parse(dec.decode(pt));
|
|
224
|
+
} catch {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/** For resuming a session after a reload: continue counters where they were. */
|
|
229
|
+
get counters() {
|
|
230
|
+
return { sent: this.sent, lastSeen: this.lastSeen };
|
|
231
|
+
}
|
|
232
|
+
restore(c) {
|
|
233
|
+
this.sent = c.sent;
|
|
234
|
+
this.lastSeen = c.lastSeen;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
var isSealed = (x) => !!x && typeof x === "object" && "d" in x && "n" in x && "s" in x;
|
|
238
|
+
|
|
239
|
+
// src/pair.ts
|
|
240
|
+
var DEFAULT_KEY = "bwalletx.connect.pairing.v1";
|
|
241
|
+
var OPEN = 1;
|
|
242
|
+
function memoryStorage() {
|
|
243
|
+
const m = /* @__PURE__ */ new Map();
|
|
244
|
+
return { getItem: (k) => m.get(k) ?? null, setItem: (k, v) => void m.set(k, v), removeItem: (k) => void m.delete(k) };
|
|
245
|
+
}
|
|
246
|
+
function defaultStorage() {
|
|
247
|
+
try {
|
|
248
|
+
if (typeof localStorage !== "undefined") return localStorage;
|
|
249
|
+
} catch {
|
|
250
|
+
}
|
|
251
|
+
return memoryStorage();
|
|
252
|
+
}
|
|
253
|
+
function defaultSocket() {
|
|
254
|
+
return (url) => {
|
|
255
|
+
if (typeof WebSocket === "undefined") throw new Error("No WebSocket here. Pass options.socket.");
|
|
256
|
+
return new WebSocket(url);
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
function parseStored(raw) {
|
|
260
|
+
if (!raw) return null;
|
|
261
|
+
try {
|
|
262
|
+
const p = JSON.parse(raw);
|
|
263
|
+
const n = (x) => Number.isInteger(x) && x >= 0;
|
|
264
|
+
if (p.v !== 1) return null;
|
|
265
|
+
if (typeof p.c !== "string" || !/^[A-Za-z0-9_-]{22}$/.test(p.c)) return null;
|
|
266
|
+
if (typeof p.r !== "string" || !/^[a-z0-9.-]+(:\d+)?$/i.test(p.r)) return null;
|
|
267
|
+
if (typeof p.s !== "string" || !/^[0-9a-f]{64}$/i.test(p.s)) return null;
|
|
268
|
+
if (typeof p.k !== "string" || !/^0[23][0-9a-f]{64}$/i.test(p.k)) return null;
|
|
269
|
+
if (!n(p.sent) || !n(p.lastSeen) || !n(p.pairedAt)) return null;
|
|
270
|
+
if (p.sc !== void 0 && (typeof p.sc !== "string" || !/^[A-Za-z0-9_-]{22,64}$/.test(p.sc))) return null;
|
|
271
|
+
return p;
|
|
272
|
+
} catch {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
var Link = class {
|
|
277
|
+
constructor(p, sealer, storage, key, socket, timeoutMs) {
|
|
278
|
+
this.p = p;
|
|
279
|
+
this.sealer = sealer;
|
|
280
|
+
this.storage = storage;
|
|
281
|
+
this.key = key;
|
|
282
|
+
this.socket = socket;
|
|
283
|
+
this.timeoutMs = timeoutMs;
|
|
284
|
+
}
|
|
285
|
+
p;
|
|
286
|
+
sealer;
|
|
287
|
+
storage;
|
|
288
|
+
key;
|
|
289
|
+
socket;
|
|
290
|
+
timeoutMs;
|
|
291
|
+
ws = null;
|
|
292
|
+
opening = null;
|
|
293
|
+
waiting = /* @__PURE__ */ new Map();
|
|
294
|
+
forgotten = false;
|
|
295
|
+
/** Take over a socket that is already open (the one pairing used). */
|
|
296
|
+
adopt(ws) {
|
|
297
|
+
this.ws = ws;
|
|
298
|
+
this.wire(ws);
|
|
299
|
+
}
|
|
300
|
+
wire(ws) {
|
|
301
|
+
ws.onmessage = inOrder((ev) => this.onFrame(String(ev.data)));
|
|
302
|
+
ws.onerror = null;
|
|
303
|
+
ws.onclose = () => {
|
|
304
|
+
if (this.ws === ws) this.ws = null;
|
|
305
|
+
this.failAll(new Error("The connection to bWalletX closed"));
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
persist() {
|
|
309
|
+
if (this.forgotten) return;
|
|
310
|
+
this.p = { ...this.p, ...this.sealer.counters };
|
|
311
|
+
void Promise.resolve(this.storage.setItem(this.key, JSON.stringify(this.p))).catch(() => {
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
failAll(e) {
|
|
315
|
+
for (const w of this.waiting.values()) {
|
|
316
|
+
clearTimeout(w.timer);
|
|
317
|
+
w.reject(e);
|
|
318
|
+
}
|
|
319
|
+
this.waiting.clear();
|
|
320
|
+
}
|
|
321
|
+
async ensure() {
|
|
322
|
+
if (this.forgotten) throw new Error("This pairing was removed. Pair again.");
|
|
323
|
+
if (this.ws && this.ws.readyState === OPEN) return;
|
|
324
|
+
this.opening ??= this.reopen().finally(() => this.opening = null);
|
|
325
|
+
await this.opening;
|
|
326
|
+
}
|
|
327
|
+
reopen() {
|
|
328
|
+
if (!this.p.sc) {
|
|
329
|
+
this.p = { ...this.p, sc: newClaim() };
|
|
330
|
+
this.persist();
|
|
331
|
+
}
|
|
332
|
+
const e = Math.floor(Date.now() / 1e3) + QR_LIFETIME_S - 10;
|
|
333
|
+
const ws = this.socket(relaySocketUrl(this.p.r, this.p.c, "site", e, this.p.sc));
|
|
334
|
+
return new Promise((resolve, reject) => {
|
|
335
|
+
ws.onopen = () => {
|
|
336
|
+
this.ws = ws;
|
|
337
|
+
this.wire(ws);
|
|
338
|
+
resolve();
|
|
339
|
+
};
|
|
340
|
+
ws.onerror = () => reject(new Error("Could not reach the bWalletX pairing service"));
|
|
341
|
+
ws.onclose = () => reject(new Error("Could not reach the bWalletX pairing service"));
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
async onFrame(raw) {
|
|
345
|
+
let f;
|
|
346
|
+
try {
|
|
347
|
+
f = JSON.parse(raw);
|
|
348
|
+
} catch {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const t = f.t;
|
|
352
|
+
if (t === "relay" && f.error) {
|
|
353
|
+
return this.failAll(new Error(`bWalletX pairing service: ${f.error}`));
|
|
354
|
+
}
|
|
355
|
+
if (!isSealed(f)) return;
|
|
356
|
+
const msg = await this.sealer.open(f);
|
|
357
|
+
if (!msg) return;
|
|
358
|
+
this.persist();
|
|
359
|
+
if (msg.t === "res") {
|
|
360
|
+
const w = this.waiting.get(msg.id);
|
|
361
|
+
if (!w) return;
|
|
362
|
+
clearTimeout(w.timer);
|
|
363
|
+
this.waiting.delete(msg.id);
|
|
364
|
+
if (msg.error) w.reject(Object.assign(new Error(msg.error.message), { code: msg.error.code }));
|
|
365
|
+
else w.resolve(msg.result);
|
|
366
|
+
} else if (msg.t === "close") {
|
|
367
|
+
this.drop();
|
|
368
|
+
this.failAll(new Error("bWalletX disconnected this site. Pair again."));
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
async call(action, params) {
|
|
372
|
+
await this.ensure();
|
|
373
|
+
const ws = this.ws;
|
|
374
|
+
const id = `${action}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
375
|
+
const result = new Promise((resolve, reject) => {
|
|
376
|
+
const timer = setTimeout(() => {
|
|
377
|
+
if (this.waiting.delete(id)) reject(new Error("No answer from bWalletX. Open it, unlocked, and approve."));
|
|
378
|
+
}, this.timeoutMs);
|
|
379
|
+
this.waiting.set(id, { resolve, reject, timer });
|
|
380
|
+
});
|
|
381
|
+
ws.send(JSON.stringify(await this.sealer.seal({ t: "req", id, action, params })));
|
|
382
|
+
this.persist();
|
|
383
|
+
return result;
|
|
384
|
+
}
|
|
385
|
+
close() {
|
|
386
|
+
const ws = this.ws;
|
|
387
|
+
this.ws = null;
|
|
388
|
+
ws?.close();
|
|
389
|
+
}
|
|
390
|
+
drop() {
|
|
391
|
+
this.forgotten = true;
|
|
392
|
+
void Promise.resolve(this.storage.removeItem(this.key)).catch(() => {
|
|
393
|
+
});
|
|
394
|
+
this.close();
|
|
395
|
+
}
|
|
396
|
+
async forget() {
|
|
397
|
+
if (this.forgotten) return;
|
|
398
|
+
try {
|
|
399
|
+
if (this.ws && this.ws.readyState === OPEN) {
|
|
400
|
+
this.ws.send(JSON.stringify(await this.sealer.seal({ t: "close", reason: "site unpaired" })));
|
|
401
|
+
}
|
|
402
|
+
} catch {
|
|
403
|
+
}
|
|
404
|
+
this.drop();
|
|
405
|
+
}
|
|
406
|
+
wallet() {
|
|
407
|
+
return new Proxy({}, {
|
|
408
|
+
get: (_t, method) => {
|
|
409
|
+
if (method === "close") return () => this.close();
|
|
410
|
+
if (method === "forget") return () => this.forget();
|
|
411
|
+
if (typeof method !== "string" || method === "then" || method === "toJSON") return void 0;
|
|
412
|
+
return (params) => this.call(method, params ?? {});
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
function pairBwalletX(opts) {
|
|
418
|
+
const storage = opts.storage === void 0 ? defaultStorage() : opts.storage ?? memoryStorage();
|
|
419
|
+
const key = opts.storageKey ?? DEFAULT_KEY;
|
|
420
|
+
const socket = opts.socket ?? defaultSocket();
|
|
421
|
+
const relay = opts.relay ?? DEFAULT_RELAY;
|
|
422
|
+
const origin = opts.origin ?? (typeof location !== "undefined" ? location.origin : "");
|
|
423
|
+
if (!origin) return Promise.reject(new Error("pairBwalletX needs options.origin outside a browser"));
|
|
424
|
+
const siteKey = PrivateKey2.fromRandom();
|
|
425
|
+
const channel = newChannel();
|
|
426
|
+
const expiry = Math.floor(Date.now() / 1e3) + QR_LIFETIME_S;
|
|
427
|
+
const siteClaim = newClaim();
|
|
428
|
+
const sitePub = siteKey.toPublicKey().toString();
|
|
429
|
+
return walletClaimFor(channel, sitePub).then(claimDigest).then((wc) => new Promise((resolve, reject) => {
|
|
430
|
+
if (opts.signal?.aborted) return reject(new Error("cancelled"));
|
|
431
|
+
let ws;
|
|
432
|
+
try {
|
|
433
|
+
ws = socket(relaySocketUrl(relay, channel, "site", expiry, siteClaim, wc));
|
|
434
|
+
} catch (e) {
|
|
435
|
+
return reject(e);
|
|
436
|
+
}
|
|
437
|
+
let sealer = null;
|
|
438
|
+
let walletKey = "";
|
|
439
|
+
let settled = false;
|
|
440
|
+
const fail = (e) => {
|
|
441
|
+
if (settled) return;
|
|
442
|
+
settled = true;
|
|
443
|
+
clearTimeout(timer);
|
|
444
|
+
ws.close();
|
|
445
|
+
reject(e);
|
|
446
|
+
};
|
|
447
|
+
const timer = setTimeout(() => fail(new Error("The pairing code expired. Show a new one.")), QR_LIFETIME_S * 1e3);
|
|
448
|
+
opts.signal?.addEventListener("abort", () => fail(new Error("cancelled")));
|
|
449
|
+
ws.onerror = () => fail(new Error("Could not reach the bWalletX pairing service"));
|
|
450
|
+
ws.onclose = () => fail(new Error("The connection to bWalletX closed before pairing finished"));
|
|
451
|
+
ws.onmessage = inOrder(async (ev) => {
|
|
452
|
+
if (settled) return;
|
|
453
|
+
let f;
|
|
454
|
+
try {
|
|
455
|
+
f = JSON.parse(String(ev.data));
|
|
456
|
+
} catch {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
if (f.t === "hello" && !sealer) {
|
|
460
|
+
walletKey = f.k;
|
|
461
|
+
const s = await deriveSession(siteKey, walletKey, channel);
|
|
462
|
+
sealer = new Sealer(s.key, "site");
|
|
463
|
+
opts.onCode(s.code);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
if (!sealer || !isSealed(f)) return;
|
|
467
|
+
const msg = await sealer.open(f);
|
|
468
|
+
if (!msg) return;
|
|
469
|
+
if (msg.t === "ready") {
|
|
470
|
+
settled = true;
|
|
471
|
+
clearTimeout(timer);
|
|
472
|
+
const p = { v: 1, c: channel, r: relay, s: siteKey.toHex(), k: walletKey, ...sealer.counters, pairedAt: Date.now(), sc: siteClaim };
|
|
473
|
+
await Promise.resolve(storage.setItem(key, JSON.stringify(p))).catch(() => {
|
|
474
|
+
});
|
|
475
|
+
const link = new Link(p, sealer, storage, key, socket, opts.callTimeoutMs ?? 12e4);
|
|
476
|
+
link.adopt(ws);
|
|
477
|
+
resolve(link.wallet());
|
|
478
|
+
} else if (msg.t === "close") {
|
|
479
|
+
fail(new Error("Pairing was declined in bWalletX"));
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
opts.onLink(pairUrl({ v: PAIR_VERSION, r: relay, c: channel, k: sitePub, o: origin, e: expiry }));
|
|
483
|
+
}));
|
|
484
|
+
}
|
|
485
|
+
async function restorePairing(opts = {}) {
|
|
486
|
+
const storage = opts.storage === void 0 ? defaultStorage() : opts.storage ?? memoryStorage();
|
|
487
|
+
const key = opts.storageKey ?? DEFAULT_KEY;
|
|
488
|
+
const p = parseStored(await Promise.resolve(storage.getItem(key)).catch(() => null));
|
|
489
|
+
if (!p) return null;
|
|
490
|
+
const { key: aes } = await deriveSession(PrivateKey2.fromHex(p.s), p.k, p.c);
|
|
491
|
+
const sealer = new Sealer(aes, "site");
|
|
492
|
+
sealer.restore({ sent: p.sent, lastSeen: p.lastSeen });
|
|
493
|
+
return new Link(p, sealer, storage, key, opts.socket ?? defaultSocket(), opts.callTimeoutMs ?? 12e4).wallet();
|
|
494
|
+
}
|
|
495
|
+
async function clearPairing(opts = {}) {
|
|
496
|
+
const storage = opts.storage === void 0 ? defaultStorage() : opts.storage ?? memoryStorage();
|
|
497
|
+
await Promise.resolve(storage.removeItem(opts.storageKey ?? DEFAULT_KEY)).catch(() => {
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// src/signin.ts
|
|
502
|
+
var LOGIN_PROTOCOL = [2, "bwallet sign in"];
|
|
503
|
+
async function connectBwalletX(opts = {}) {
|
|
504
|
+
const status = (s) => {
|
|
505
|
+
try {
|
|
506
|
+
opts.onStatus?.(s);
|
|
507
|
+
} catch {
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
if (opts.wallet) {
|
|
511
|
+
status("opening");
|
|
512
|
+
return { wallet: opts.wallet, method: "wallet" };
|
|
513
|
+
}
|
|
514
|
+
status("finding");
|
|
515
|
+
const found = await discoverBwalletX(opts.discoverMs ?? DEFAULT_DISCOVER_MS);
|
|
516
|
+
if (found) {
|
|
517
|
+
status("opening");
|
|
518
|
+
return { wallet: found.wallet, method: found.method };
|
|
519
|
+
}
|
|
520
|
+
if (opts.reusePairing !== false) {
|
|
521
|
+
const restored = await restorePairing(opts.pairing ?? {});
|
|
522
|
+
if (restored) {
|
|
523
|
+
status("opening");
|
|
524
|
+
return { wallet: restored, method: "pairing" };
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
if (!opts.pairing) {
|
|
528
|
+
throw Object.assign(new Error("bWalletX was not found in this browser"), { code: "NOT_FOUND" });
|
|
529
|
+
}
|
|
530
|
+
status("pairing");
|
|
531
|
+
return { wallet: await pairBwalletX(opts.pairing), method: "pairing" };
|
|
532
|
+
}
|
|
533
|
+
async function waitIfLocked(wallet) {
|
|
534
|
+
let ok = false;
|
|
535
|
+
try {
|
|
536
|
+
ok = !!(await wallet.isAuthenticated({}))?.authenticated;
|
|
537
|
+
} catch {
|
|
538
|
+
}
|
|
539
|
+
if (!ok) await Promise.resolve().then(() => wallet.waitForAuthentication({})).catch(() => {
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
function challengeWantsIdentityKey(e) {
|
|
543
|
+
const msg = e instanceof Error ? e.message : String(e ?? "");
|
|
544
|
+
return /identityKey/i.test(msg);
|
|
545
|
+
}
|
|
546
|
+
async function signInWithBwalletX(opts) {
|
|
547
|
+
const { wallet, method } = await connectBwalletX(opts);
|
|
548
|
+
if (method !== "pairing" && !opts.skipWaitForAuthentication) await waitIfLocked(wallet);
|
|
549
|
+
const status = () => {
|
|
550
|
+
try {
|
|
551
|
+
opts.onStatus?.("signing");
|
|
552
|
+
} catch {
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
const sign = (c2) => wallet.createSignature({
|
|
556
|
+
protocolID: c2.protocolID ?? LOGIN_PROTOCOL,
|
|
557
|
+
keyID: c2.keyID,
|
|
558
|
+
counterparty: "anyone",
|
|
559
|
+
data: Array.from(new TextEncoder().encode(c2.message))
|
|
560
|
+
});
|
|
561
|
+
const keyFirst = async () => {
|
|
562
|
+
const { publicKey: identityKey2 } = await atStage("identity", () => wallet.getPublicKey({ identityKey: true }));
|
|
563
|
+
status();
|
|
564
|
+
const c2 = await atStage("challenge", () => opts.challenge(identityKey2));
|
|
565
|
+
const { signature: signature2 } = await atStage("signature", () => sign(c2));
|
|
566
|
+
return { identityKey: identityKey2, signature: Array.from(signature2), nonce: c2.nonce, method, wallet };
|
|
567
|
+
};
|
|
568
|
+
if (opts.challengeNeedsIdentityKey && !opts.identityKey) return keyFirst();
|
|
569
|
+
let c;
|
|
570
|
+
try {
|
|
571
|
+
c = await atStage("challenge", () => opts.challenge(opts.identityKey));
|
|
572
|
+
} catch (e) {
|
|
573
|
+
if (!opts.identityKey && challengeWantsIdentityKey(e)) return keyFirst();
|
|
574
|
+
throw e;
|
|
575
|
+
}
|
|
576
|
+
status();
|
|
577
|
+
const [{ publicKey: identityKey }, { signature }] = await Promise.all([
|
|
578
|
+
opts.identityKey ? Promise.resolve({ publicKey: opts.identityKey }) : atStage("identity", () => wallet.getPublicKey({ identityKey: true })),
|
|
579
|
+
atStage("signature", () => sign(c))
|
|
580
|
+
]);
|
|
581
|
+
return { identityKey, signature: Array.from(signature), nonce: c.nonce, method, wallet };
|
|
582
|
+
}
|
|
583
|
+
async function signInAndVerify(opts) {
|
|
584
|
+
const hinted = !opts.identityKey && !!opts.identityKeyHint;
|
|
585
|
+
const r = await signInWithBwalletX({ ...opts, identityKey: opts.identityKey ?? opts.identityKeyHint });
|
|
586
|
+
try {
|
|
587
|
+
return { result: r, verified: await atStage("verify", () => opts.verify(r)) };
|
|
588
|
+
} catch (e) {
|
|
589
|
+
const status = e?.status;
|
|
590
|
+
if (!hinted || status !== 401) throw e;
|
|
591
|
+
const again = await signInWithBwalletX({ ...opts, wallet: r.wallet, identityKey: void 0, skipWaitForAuthentication: true });
|
|
592
|
+
return { result: again, verified: await atStage("verify", () => opts.verify(again)) };
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
async function atStage(stage, run) {
|
|
596
|
+
try {
|
|
597
|
+
return await run();
|
|
598
|
+
} catch (e) {
|
|
599
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
600
|
+
if (!err.stage) Object.assign(err, { stage });
|
|
601
|
+
throw err;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// src/wallets.ts
|
|
606
|
+
import { WalletClient as WalletClient2 } from "@bsv/sdk";
|
|
607
|
+
async function brc100Connected(wallet, o, c = {}) {
|
|
608
|
+
if (c.identity === false) return { wallet, payer: wallet, identityKey: null, optionId: o.id, kind: o.kind };
|
|
609
|
+
await waitIfLocked(wallet);
|
|
610
|
+
const { publicKey } = await wallet.getPublicKey({ identityKey: true });
|
|
611
|
+
return { wallet, payer: wallet, identityKey: publicKey, optionId: o.id, kind: o.kind };
|
|
612
|
+
}
|
|
613
|
+
function brc100Option(wallet, info) {
|
|
614
|
+
const opt = {
|
|
615
|
+
id: info.id,
|
|
616
|
+
name: info.name,
|
|
617
|
+
icon: info.icon,
|
|
618
|
+
rdns: info.rdns,
|
|
619
|
+
kind: "brc100",
|
|
620
|
+
isBwalletX: info.isBwalletX ?? false,
|
|
621
|
+
substrate: info.substrate,
|
|
622
|
+
wallet,
|
|
623
|
+
connect: (c) => brc100Connected(wallet, opt, c)
|
|
624
|
+
};
|
|
625
|
+
return opt;
|
|
626
|
+
}
|
|
627
|
+
function pairingOption(pairing) {
|
|
628
|
+
const opt = {
|
|
629
|
+
id: "bwalletx-pairing",
|
|
630
|
+
name: "bWalletX on your phone",
|
|
631
|
+
kind: "pairing",
|
|
632
|
+
isBwalletX: true,
|
|
633
|
+
substrate: "pairing",
|
|
634
|
+
async connect(c) {
|
|
635
|
+
const p = typeof pairing === "function" ? pairing() : pairing;
|
|
636
|
+
const w = await restorePairing(p) ?? await pairBwalletX(p);
|
|
637
|
+
return brc100Connected(w, opt, c);
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
return opt;
|
|
641
|
+
}
|
|
642
|
+
function handcashAdapter(o) {
|
|
643
|
+
const opt = {
|
|
644
|
+
id: "handcash",
|
|
645
|
+
name: "HandCash",
|
|
646
|
+
icon: o.icon,
|
|
647
|
+
kind: "handcash",
|
|
648
|
+
isBwalletX: false,
|
|
649
|
+
substrate: "adapter",
|
|
650
|
+
async connect() {
|
|
651
|
+
const p = await o.login();
|
|
652
|
+
return {
|
|
653
|
+
wallet: null,
|
|
654
|
+
payer: o.pay ? { pay: o.pay } : null,
|
|
655
|
+
identityKey: p.identityKey ?? null,
|
|
656
|
+
handle: p.handle,
|
|
657
|
+
paymail: p.paymail ?? `${p.handle.replace(/^\$/, "")}@handcash.io`,
|
|
658
|
+
optionId: opt.id,
|
|
659
|
+
kind: "handcash",
|
|
660
|
+
transitional: true
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
return opt;
|
|
665
|
+
}
|
|
666
|
+
var NAMES = { [RDNS_EXTENSION]: "bWalletX", "space.bwallet.mobile": "bWalletX" };
|
|
667
|
+
async function probe(substrate, ms) {
|
|
668
|
+
try {
|
|
669
|
+
const w = new WalletClient2(substrate);
|
|
670
|
+
const ok = await Promise.race([w.isAuthenticated({}).then(() => true), new Promise((r) => setTimeout(() => r(false), ms))]);
|
|
671
|
+
return ok ? w : null;
|
|
672
|
+
} catch {
|
|
673
|
+
return null;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
function cwiIsAnnounced(cwi, announced) {
|
|
677
|
+
return announced.some((o) => o.wallet === cwi || o.rdns === RDNS_MOBILE);
|
|
678
|
+
}
|
|
679
|
+
async function discoverWallets(o = {}) {
|
|
680
|
+
const out = [];
|
|
681
|
+
const ms = o.ms ?? 300;
|
|
682
|
+
if (typeof window !== "undefined") {
|
|
683
|
+
const seen = /* @__PURE__ */ new Map();
|
|
684
|
+
const onAnnounce = (e) => {
|
|
685
|
+
const d = e.detail;
|
|
686
|
+
if (!d?.wallet) return;
|
|
687
|
+
const rdns = d.info?.rdns ?? d.info?.uuid ?? `wallet-${seen.size}`;
|
|
688
|
+
if (seen.has(rdns)) return;
|
|
689
|
+
const isB = BWALLETX_RDNS.includes(rdns);
|
|
690
|
+
const substrate = rdns === RDNS_EXTENSION ? "extension" : rdns === RDNS_MOBILE ? "in-app" : "injected";
|
|
691
|
+
seen.set(rdns, brc100Option(d.wallet, { id: rdns, rdns, name: NAMES[rdns] ?? d.info?.name ?? rdns, icon: d.info?.icon, isBwalletX: isB, substrate }));
|
|
692
|
+
};
|
|
693
|
+
window.addEventListener("brc100:announceWallet", onAnnounce);
|
|
694
|
+
window.dispatchEvent(new Event("brc100:requestWallet"));
|
|
695
|
+
const local = o.probeLocal ? Promise.all([probe("json-api", ms), probe("Cicada", ms)]) : Promise.resolve([null, null]);
|
|
696
|
+
await new Promise((r) => setTimeout(r, seen.size > 0 || window.CWI ? Math.min(ms, 50) : ms));
|
|
697
|
+
window.removeEventListener("brc100:announceWallet", onAnnounce);
|
|
698
|
+
out.push(...seen.values());
|
|
699
|
+
if (![...seen.values()].some((w) => w.isBwalletX) && isFramed()) {
|
|
700
|
+
const x = await probeXdm(Math.max(ms, 1200));
|
|
701
|
+
if (x) out.push(brc100Option(x, { id: "bwalletx-xdm", name: "bWalletX", isBwalletX: true, substrate: "xdm" }));
|
|
702
|
+
}
|
|
703
|
+
const cwi = window.CWI;
|
|
704
|
+
if (cwi && !cwiIsAnnounced(cwi, [...seen.values()])) {
|
|
705
|
+
out.push(brc100Option(cwi, { id: "window.CWI", name: seen.size ? "Another browser wallet" : "Browser wallet", isBwalletX: false, substrate: "window.CWI" }));
|
|
706
|
+
}
|
|
707
|
+
const [json, cicada] = await local;
|
|
708
|
+
if (json) out.push(brc100Option(json, { id: "localhost:3321", name: "Desktop wallet (localhost:3321)", substrate: "json-api" }));
|
|
709
|
+
if (cicada) out.push(brc100Option(cicada, { id: "localhost:3301", name: "Desktop wallet (localhost:3301)", substrate: "cicada" }));
|
|
710
|
+
}
|
|
711
|
+
if (o.pairing) out.push(pairingOption(o.pairing));
|
|
712
|
+
out.push(...o.extra ?? []);
|
|
713
|
+
const rank = (w) => w.isBwalletX ? 0 : w.kind === "handcash" ? 2 : 1;
|
|
714
|
+
return out.map((w, i) => ({ w, i })).sort((a, b) => rank(a.w) - rank(b.w) || a.i - b.i).map((x) => x.w);
|
|
715
|
+
}
|
|
716
|
+
var DESKTOP_OPTION_ID = "desktop";
|
|
717
|
+
function desktopOption(ms = 1500) {
|
|
718
|
+
const opt = {
|
|
719
|
+
id: DESKTOP_OPTION_ID,
|
|
720
|
+
name: "Desktop wallet",
|
|
721
|
+
kind: "brc100",
|
|
722
|
+
isBwalletX: false,
|
|
723
|
+
substrate: "json-api",
|
|
724
|
+
async connect(c) {
|
|
725
|
+
const [json, cicada] = await Promise.all([probe("json-api", ms), probe("Cicada", ms)]);
|
|
726
|
+
const w = json ?? cicada;
|
|
727
|
+
if (!w) throw Object.assign(new Error("No desktop wallet answered on this computer"), { code: "NOT_FOUND" });
|
|
728
|
+
opt.wallet = w;
|
|
729
|
+
opt.substrate = json ? "json-api" : "cicada";
|
|
730
|
+
return brc100Connected(w, opt, c);
|
|
731
|
+
}
|
|
732
|
+
};
|
|
733
|
+
return opt;
|
|
734
|
+
}
|
|
735
|
+
function autoChoice(list, force = false) {
|
|
736
|
+
const detected = list.filter((w) => w.kind === "brc100");
|
|
737
|
+
const others = list.filter((w) => w.kind === "handcash" || w.kind === "custom");
|
|
738
|
+
if (force) return detected[0] ?? list.find((w) => w.kind !== "pairing") ?? list[0] ?? null;
|
|
739
|
+
if (!list.length) return null;
|
|
740
|
+
if (list.length === 1) return list[0];
|
|
741
|
+
if (detected.length === 1 && others.length === 0) return detected[0];
|
|
742
|
+
if (detected.length === 0 && others.length === 0) return list[0];
|
|
743
|
+
return void 0;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
// src/account.ts
|
|
747
|
+
var PAYMAIL_LOOKUP_URL = "https://pay.bwallet.space/api/paymail/lookup?key=";
|
|
748
|
+
var BAVATAR_URL = "https://chat.bwalletx.com/api/bavatar/";
|
|
749
|
+
function shortKey(key) {
|
|
750
|
+
return key.length > 12 ? `${key.slice(0, 6)}\u2026${key.slice(-4)}` : key;
|
|
751
|
+
}
|
|
752
|
+
function accountLabel(a) {
|
|
753
|
+
if (a.paymail && /@bwalletx\.com$/i.test(a.paymail)) return "$" + a.paymail.split("@")[0];
|
|
754
|
+
if (a.handle) return a.handle;
|
|
755
|
+
if (a.paymail) return a.paymail;
|
|
756
|
+
return a.identityKey ? shortKey(a.identityKey) : "";
|
|
757
|
+
}
|
|
758
|
+
function avatarUrlFor(identityKey) {
|
|
759
|
+
return identityKey && /^0[23][0-9a-fA-F]{64}$/.test(identityKey) ? `${BAVATAR_URL}${identityKey.toLowerCase()}.png` : null;
|
|
760
|
+
}
|
|
761
|
+
var cache = /* @__PURE__ */ new Map();
|
|
762
|
+
function lookupPaymail(identityKey, url = PAYMAIL_LOOKUP_URL) {
|
|
763
|
+
if (url === "off" || typeof fetch === "undefined") return Promise.resolve(null);
|
|
764
|
+
const k = `${url}|${identityKey}`;
|
|
765
|
+
let p = cache.get(k);
|
|
766
|
+
if (!p) {
|
|
767
|
+
p = fetch(url + encodeURIComponent(identityKey)).then((r) => r.ok ? r.json() : null).then((j) => j && typeof j.paymail === "string" ? j.paymail : null).catch(() => null);
|
|
768
|
+
cache.set(k, p);
|
|
769
|
+
}
|
|
770
|
+
return p;
|
|
771
|
+
}
|
|
772
|
+
function isUserRejection(e) {
|
|
773
|
+
const code = e?.code;
|
|
774
|
+
if (code === "ERR_PERMISSION_DENIED" || code === 4001) return true;
|
|
775
|
+
const msg = e instanceof Error ? e.message : String(e ?? "");
|
|
776
|
+
return /denied|reject|cancel|declin|refused|not now|user closed/i.test(msg);
|
|
777
|
+
}
|
|
778
|
+
function signInErrorText(e) {
|
|
779
|
+
if (isUserRejection(e)) return "Sign-in cancelled";
|
|
780
|
+
const code = e?.code;
|
|
781
|
+
if (code === "NOT_FOUND") return "bWalletX was not found in this browser";
|
|
782
|
+
const msg = e instanceof Error ? e.message : String(e ?? "");
|
|
783
|
+
if (/lock/i.test(msg)) return "Your wallet is locked. Unlock it and try again";
|
|
784
|
+
if (/timeout|timed out|expired/i.test(msg)) return "The wallet did not answer in time";
|
|
785
|
+
if (/origin/i.test(msg)) return "This page is not on the site\u2019s configured origin";
|
|
786
|
+
return "Sign-in failed";
|
|
787
|
+
}
|
|
788
|
+
async function copyText(text) {
|
|
789
|
+
try {
|
|
790
|
+
await navigator.clipboard.writeText(text);
|
|
791
|
+
return true;
|
|
792
|
+
} catch {
|
|
793
|
+
return false;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
var IDENTITY_HINT_KEY = "bwx:identityKey";
|
|
797
|
+
function rememberedIdentity() {
|
|
798
|
+
try {
|
|
799
|
+
const k = globalThis.localStorage?.getItem(IDENTITY_HINT_KEY) ?? "";
|
|
800
|
+
return /^0[23][0-9a-f]{64}$/i.test(k) ? k : void 0;
|
|
801
|
+
} catch {
|
|
802
|
+
return void 0;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
function rememberIdentity(key) {
|
|
806
|
+
try {
|
|
807
|
+
if (key) globalThis.localStorage?.setItem(IDENTITY_HINT_KEY, key);
|
|
808
|
+
else globalThis.localStorage?.removeItem(IDENTITY_HINT_KEY);
|
|
809
|
+
} catch {
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
// src/choice.ts
|
|
814
|
+
var BWALLETX_TYPE = "bwalletx";
|
|
815
|
+
function substrateOf(o) {
|
|
816
|
+
if (o.substrate) return o.substrate;
|
|
817
|
+
if (o.kind === "pairing") return "pairing";
|
|
818
|
+
if (o.isBwalletX && o.kind === "brc100") return o.rdns === RDNS_MOBILE ? "in-app" : o.id === "bwalletx-xdm" ? "xdm" : "extension";
|
|
819
|
+
if (o.kind === "handcash" || o.kind === "custom") return "adapter";
|
|
820
|
+
if (o.rdns === RDNS_EXTENSION) return "extension";
|
|
821
|
+
if (o.rdns === RDNS_MOBILE) return "in-app";
|
|
822
|
+
if (o.id === "window.CWI") return "window.CWI";
|
|
823
|
+
if (o.id === "localhost:3321" || o.id === DESKTOP_OPTION_ID) return "json-api";
|
|
824
|
+
if (o.id === "localhost:3301") return "cicada";
|
|
825
|
+
return "injected";
|
|
826
|
+
}
|
|
827
|
+
function walletTypeOf(o) {
|
|
828
|
+
return o.isBwalletX ? BWALLETX_TYPE : o.rdns ?? o.id;
|
|
829
|
+
}
|
|
830
|
+
var OWN_MARKERS = ["extension", "in-app", "xdm"];
|
|
831
|
+
function isPresentBwalletX(o) {
|
|
832
|
+
return o.isBwalletX && o.kind === "brc100" && !!o.wallet && OWN_MARKERS.includes(substrateOf(o));
|
|
833
|
+
}
|
|
834
|
+
function hasBwalletX(list) {
|
|
835
|
+
return list.some(isPresentBwalletX);
|
|
836
|
+
}
|
|
837
|
+
function otherDetected(list) {
|
|
838
|
+
return list.filter((o) => !o.isBwalletX && o.kind === "brc100" && !!o.wallet);
|
|
839
|
+
}
|
|
840
|
+
function chooserOrder(list) {
|
|
841
|
+
const rank = (o) => isPresentBwalletX(o) ? 0 : o.isBwalletX ? 1 : o.kind === "brc100" ? 2 : o.kind === "custom" ? 3 : 4;
|
|
842
|
+
return list.map((o, i) => ({ o, i })).sort((a, b) => rank(a.o) - rank(b.o) || a.i - b.i).map((x) => x.o);
|
|
843
|
+
}
|
|
844
|
+
function decideWallet(list, o = {}) {
|
|
845
|
+
if (o.walletId) {
|
|
846
|
+
const w = list.find((x) => x.id === o.walletId);
|
|
847
|
+
if (w) return { option: w, reason: "requested" };
|
|
848
|
+
}
|
|
849
|
+
if (o.chooser) return { option: null, reason: list.length || o.picker !== false ? "chooser" : "none" };
|
|
850
|
+
if (o.fastPath === false) {
|
|
851
|
+
const a = autoChoice([...list], o.picker === false);
|
|
852
|
+
return a === void 0 ? { option: null, reason: "chooser" } : a === null ? { option: null, reason: "none" } : { option: a, reason: "legacy" };
|
|
853
|
+
}
|
|
854
|
+
const b = list.find(isPresentBwalletX);
|
|
855
|
+
if (b) return { option: b, reason: "bwalletx" };
|
|
856
|
+
if (o.picker === false) {
|
|
857
|
+
const phone = list.find((x) => x.isBwalletX);
|
|
858
|
+
return phone ? { option: phone, reason: "bwalletx-phone" } : { option: null, reason: "none" };
|
|
859
|
+
}
|
|
860
|
+
return list.length ? { option: null, reason: "chooser" } : { option: null, reason: "none" };
|
|
861
|
+
}
|
|
862
|
+
function outcomeOf(e) {
|
|
863
|
+
const err = e;
|
|
864
|
+
if (err?.code === "NOT_FOUND") return "not-found";
|
|
865
|
+
if (err?.name === "AbortError" || err?.message === "cancelled") return "cancelled";
|
|
866
|
+
if (isUserRejection(e)) return "rejected";
|
|
867
|
+
return "error";
|
|
868
|
+
}
|
|
869
|
+
function attemptFor(option, outcome, startedAt, now = Date.now()) {
|
|
870
|
+
return {
|
|
871
|
+
walletType: option ? walletTypeOf(option) : null,
|
|
872
|
+
substrate: option ? substrateOf(option) : null,
|
|
873
|
+
outcome,
|
|
874
|
+
ms: Math.max(0, Math.round(now - startedAt))
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
function reportAttempt(hook, a) {
|
|
878
|
+
if (!hook) return;
|
|
879
|
+
try {
|
|
880
|
+
hook(Object.freeze({ walletType: a.walletType, substrate: a.substrate, outcome: a.outcome, ms: a.ms }));
|
|
881
|
+
} catch {
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
// src/modal.ts
|
|
886
|
+
import qrcode from "qrcode-generator";
|
|
887
|
+
|
|
888
|
+
// src/access-client.ts
|
|
889
|
+
import { Hash, PublicKey as PublicKey2, Utils } from "@bsv/sdk";
|
|
890
|
+
var ONESAT = [0, "onesat"];
|
|
891
|
+
var LEGACY_ONESAT = [0, "p 1sat"];
|
|
892
|
+
function toAccessError(e) {
|
|
893
|
+
if (e instanceof AccessError) return e;
|
|
894
|
+
const err = e;
|
|
895
|
+
const msg = String(err?.message ?? e ?? "");
|
|
896
|
+
if (err?.code === "NOT_FOUND") return new AccessError("not_installed", msg);
|
|
897
|
+
if (/reject|denied|declin|cancel|user/i.test(msg) || err?.code === "ERR_USER_REJECTED" || err?.status === 4001) {
|
|
898
|
+
return new AccessError("user_rejected", msg);
|
|
899
|
+
}
|
|
900
|
+
return new AccessError("bad_request", msg || "access failed");
|
|
901
|
+
}
|
|
902
|
+
function throwIfAccessError(body) {
|
|
903
|
+
const b = body;
|
|
904
|
+
if (b && typeof b.error === "string") throw new AccessError(b.error, b.message, { have: b.have, need: b.need });
|
|
905
|
+
}
|
|
906
|
+
function hash160FromP2pkh(script) {
|
|
907
|
+
const m = /^76a914([0-9a-f]{40})88ac$/i.exec(script.slice(-50));
|
|
908
|
+
return m ? m[1].toLowerCase() : null;
|
|
909
|
+
}
|
|
910
|
+
async function proveHoldings(wallet, challenge, tokenId, basket = "bsv21") {
|
|
911
|
+
const bare = tokenId.replace(/^bsv21:/, "");
|
|
912
|
+
const list = await wallet.listOutputs({ basket, include: "locking scripts", includeCustomInstructions: true, limit: 1e4 });
|
|
913
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
914
|
+
const data = challengeBytes(challenge);
|
|
915
|
+
for (const out of list.outputs) {
|
|
916
|
+
if (byKey.size >= MAX_HOLDING_PROOFS) break;
|
|
917
|
+
let ci = {};
|
|
918
|
+
try {
|
|
919
|
+
ci = JSON.parse(out.customInstructions ?? "{}");
|
|
920
|
+
} catch {
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
923
|
+
if (ci.id !== bare || !ci.keyID) continue;
|
|
924
|
+
const want = out.lockingScript ? hash160FromP2pkh(out.lockingScript) : null;
|
|
925
|
+
const counterparty = ci.counterparty ?? "self";
|
|
926
|
+
for (const protocolID of ci.protocolID ? [ci.protocolID] : [ONESAT, LEGACY_ONESAT]) {
|
|
927
|
+
const { publicKey } = await wallet.getPublicKey({ protocolID, keyID: ci.keyID, counterparty });
|
|
928
|
+
const h = Utils.toHex(Hash.hash160(PublicKey2.fromString(publicKey).encode(true)));
|
|
929
|
+
if (want && h !== want) continue;
|
|
930
|
+
const existing = byKey.get(publicKey);
|
|
931
|
+
if (existing) {
|
|
932
|
+
existing.outpoints.push(out.outpoint.replace(".", "_"));
|
|
933
|
+
break;
|
|
934
|
+
}
|
|
935
|
+
const { signature } = await wallet.createSignature({ protocolID, keyID: ci.keyID, counterparty, data });
|
|
936
|
+
byKey.set(publicKey, { pubkey: publicKey, signature: Array.from(signature), outpoints: [out.outpoint.replace(".", "_")] });
|
|
937
|
+
break;
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
return [...byKey.values()];
|
|
941
|
+
}
|
|
942
|
+
async function requestAccess(o) {
|
|
943
|
+
let wallet;
|
|
944
|
+
try {
|
|
945
|
+
({ wallet } = await connectBwalletX({ wallet: o.wallet, pairing: o.pairing, discoverMs: o.discoverMs }));
|
|
946
|
+
} catch (e) {
|
|
947
|
+
throw e?.code === "NOT_FOUND" ? new AccessError("not_installed") : toAccessError(e);
|
|
948
|
+
}
|
|
949
|
+
try {
|
|
950
|
+
const { publicKey: identityKey } = await wallet.getPublicKey({ identityKey: true });
|
|
951
|
+
const { challenge } = await o.challenge(identityKey);
|
|
952
|
+
const c = parseAccessChallenge(challenge);
|
|
953
|
+
if (c.sub && c.sub !== identityKey.toLowerCase()) throw new AccessError("wrong_account");
|
|
954
|
+
if (Date.now() / 1e3 >= c.exp) throw new AccessError("expired");
|
|
955
|
+
const { signature } = await wallet.createSignature({
|
|
956
|
+
protocolID: ACCESS_PROTOCOL,
|
|
957
|
+
keyID: c.nonce,
|
|
958
|
+
counterparty: "anyone",
|
|
959
|
+
data: challengeBytes(challenge)
|
|
960
|
+
});
|
|
961
|
+
const response = { v: 1, challenge, identityKey, signature: Array.from(signature) };
|
|
962
|
+
if (c.require.token) response.holdings = await proveHoldings(wallet, challenge, c.require.token.id, o.tokenBasket);
|
|
963
|
+
let verified;
|
|
964
|
+
if (o.verify) {
|
|
965
|
+
verified = await o.verify(response);
|
|
966
|
+
throwIfAccessError(verified);
|
|
967
|
+
}
|
|
968
|
+
return { response, verified, wallet };
|
|
969
|
+
} catch (e) {
|
|
970
|
+
throw toAccessError(e);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
// src/element.ts
|
|
975
|
+
var SIGNIN_TAG = "bwalletx-signin";
|
|
976
|
+
var GET_URL = "https://bwalletx.com/get";
|
|
977
|
+
var WEB_WALLET_URL = "https://web.bwalletx.com";
|
|
978
|
+
var VERBS = { signin: "Sign in with", continue: "Continue with", connect: "Connect", get: "Get", play: "Play with" };
|
|
979
|
+
function signinText(label = "signin", brand = "bWalletX") {
|
|
980
|
+
return `${VERBS[label] ?? VERBS.signin} ${brand}`;
|
|
981
|
+
}
|
|
982
|
+
var uid = 0;
|
|
983
|
+
function markSvg(px = 20) {
|
|
984
|
+
const n = ++uid;
|
|
985
|
+
return `<svg viewBox="23 8 74 100" width="${Math.round(px * 0.74)}" height="${px}" aria-hidden="true" focusable="false"><defs><linearGradient id="bwxg${n}" gradientUnits="userSpaceOnUse" x1="0" y1="12" x2="0" y2="105"><stop offset="0" stop-color="#FFE58A"/><stop offset=".55" stop-color="#FFD24D"/><stop offset="1" stop-color="#C98F00"/></linearGradient><mask id="bwxh${n}"><rect width="140" height="140" fill="#fff"/><circle cx="60" cy="72" r="15" fill="#000"/></mask></defs><g fill="url(#bwxg${n})" mask="url(#bwxh${n})"><polygon points="45,12 45,76 27,76 27,30"/><circle cx="60" cy="72" r="33"/></g></svg>`;
|
|
986
|
+
}
|
|
987
|
+
var SIGNIN_CSS = `
|
|
988
|
+
:host{display:inline-block;vertical-align:middle;font-family:Inter,ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif}
|
|
989
|
+
:host([full-width]){display:block;width:100%}
|
|
990
|
+
:host([hidden]){display:none}
|
|
991
|
+
button{all:unset;box-sizing:border-box;display:inline-flex;align-items:center;justify-content:center;gap:12px;width:100%;cursor:pointer;
|
|
992
|
+
padding:16px 20px;border-radius:12px;font-size:16px;font-weight:700;line-height:1.2;text-align:left;
|
|
993
|
+
background:linear-gradient(180deg,#fcd34d,#f59e0b);color:#000;
|
|
994
|
+
box-shadow:0 10px 15px -3px rgba(245,158,11,.2),0 4px 6px -4px rgba(245,158,11,.2);
|
|
995
|
+
transition:background .15s,transform .1s,box-shadow .15s,border-color .15s;-webkit-tap-highlight-color:transparent}
|
|
996
|
+
button:hover{background:linear-gradient(180deg,#fde68a,#fbbf24)}
|
|
997
|
+
button:active{transform:translateY(1px);background:linear-gradient(180deg,#fbbf24,#d97706)}
|
|
998
|
+
button:focus-visible{outline:2px solid #b45309;outline-offset:3px}
|
|
999
|
+
button:disabled{opacity:.6;cursor:not-allowed;transform:none}
|
|
1000
|
+
button[aria-busy=true]{cursor:wait}
|
|
1001
|
+
.mark{display:inline-flex;align-items:center;justify-content:center;flex:none;width:28px;height:28px;border-radius:50%;background:#000}
|
|
1002
|
+
.txt{display:flex;flex-direction:column;align-items:flex-start;min-width:0}
|
|
1003
|
+
.txt b{font-weight:700;white-space:nowrap}
|
|
1004
|
+
.txt small{font-size:11px;font-weight:500;color:rgba(0,0,0,.65);white-space:nowrap}
|
|
1005
|
+
.spin{width:16px;height:16px;border-radius:50%;border:2px solid rgba(255,210,77,.35);border-top-color:#ffd24d;animation:r .8s linear infinite}
|
|
1006
|
+
@keyframes r{to{transform:rotate(360deg)}}
|
|
1007
|
+
@media (prefers-reduced-motion:reduce){button{transition:none}button:active{transform:none}.spin{animation:none;border-color:#ffd24d;border-right-color:transparent}}
|
|
1008
|
+
/* sizes */
|
|
1009
|
+
:host([size=md]) button{padding:10px 16px;border-radius:10px;font-size:15px;gap:10px}
|
|
1010
|
+
:host([size=md]) .mark{width:24px;height:24px}
|
|
1011
|
+
:host([size=md]) .txt small{font-size:10.5px}
|
|
1012
|
+
:host([size=sm]) button{padding:6px 12px 6px 6px;border-radius:8px;font-size:13px;gap:8px;min-height:36px}
|
|
1013
|
+
:host([size=sm]) .mark{width:22px;height:22px}
|
|
1014
|
+
:host([size=icon]) button{width:48px;height:48px;padding:0;border-radius:50%;gap:0}
|
|
1015
|
+
:host([size=icon]) .mark{width:34px;height:34px}
|
|
1016
|
+
/* themes */
|
|
1017
|
+
:host([theme=dark]) button{background:#0a0a0a;color:#fafafa;border:1px solid #3f3f46;box-shadow:none}
|
|
1018
|
+
:host([theme=dark]) button:hover{background:#18181b;border-color:rgba(251,191,36,.6)}
|
|
1019
|
+
:host([theme=dark]) button:active{background:#000}
|
|
1020
|
+
:host([theme=dark]) button:focus-visible{outline-color:#fcd34d}
|
|
1021
|
+
:host([theme=dark]) .mark{box-shadow:inset 0 0 0 1px #3f3f46}
|
|
1022
|
+
:host([theme=dark]) .txt small{color:rgba(255,255,255,.65)}
|
|
1023
|
+
:host([theme=light]) button{background:#fff;color:#09090b;border:1px solid #d4d4d8;box-shadow:0 1px 2px rgba(0,0,0,.06)}
|
|
1024
|
+
:host([theme=light]) button:hover{background:#fafafa;border-color:#f59e0b}
|
|
1025
|
+
:host([theme=light]) button:active{background:#f4f4f5}
|
|
1026
|
+
:host([theme=light]) .txt small{color:#52525b}
|
|
1027
|
+
/* not found */
|
|
1028
|
+
.nf{margin-top:8px;font-size:12px;line-height:1.4;color:#71717a;text-align:center}
|
|
1029
|
+
.nf a{color:inherit;font-weight:600;text-decoration:underline;text-underline-offset:2px}
|
|
1030
|
+
.nf a:focus-visible{outline:2px solid #b45309;outline-offset:2px;border-radius:2px}
|
|
1031
|
+
:host([theme=dark]) .nf,:host-context(.dark) .nf{color:#a1a1aa}
|
|
1032
|
+
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
|
|
1033
|
+
/* pending: one plain line under the button */
|
|
1034
|
+
.note{margin:8px 0 0;width:0;min-width:100%;font-size:12px;line-height:1.4;color:#71717a;text-align:center;overflow-wrap:break-word}
|
|
1035
|
+
:host([theme=dark]) .note,:host-context(.dark) .note{color:#a1a1aa}
|
|
1036
|
+
/* "Other wallets" (0.5.6): one small link under the button */
|
|
1037
|
+
.ow{margin:8px 0 0;width:0;min-width:100%;font-size:12px;line-height:1.4;color:#71717a;text-align:center}
|
|
1038
|
+
:host([theme=dark]) .ow,:host-context(.dark) .ow{color:#a1a1aa}
|
|
1039
|
+
.lnk{all:unset;cursor:pointer;color:inherit;font-weight:600;text-decoration:underline;text-underline-offset:2px}
|
|
1040
|
+
.lnk:focus-visible{outline:2px solid #b45309;outline-offset:2px;border-radius:2px}
|
|
1041
|
+
/* error: one line plus Retry */
|
|
1042
|
+
.err{margin:0 0 8px;width:0;min-width:100%;font-size:13px;font-weight:600;line-height:1.4;color:#b91c1c;text-align:center;overflow-wrap:anywhere}
|
|
1043
|
+
:host([theme=dark]) .err,:host-context(.dark) .err{color:#fca5a5}
|
|
1044
|
+
/* signed in: the badge */
|
|
1045
|
+
button.chip{display:inline-flex;align-items:center;justify-content:flex-start;gap:10px;width:auto;max-width:100%;padding:5px 14px 5px 5px;border-radius:999px;
|
|
1046
|
+
background:#0b0b0c;color:#ffd24d;border:1px solid rgba(255,210,77,.55);box-shadow:none;font-size:15px}
|
|
1047
|
+
button.chip:hover{background:#151517;border-color:#ffd24d}
|
|
1048
|
+
button.chip:active{background:#000;transform:none}
|
|
1049
|
+
button.chip:focus-visible{outline:2px solid #ffd24d;outline-offset:3px}
|
|
1050
|
+
:host([theme=light]) button.chip{background:#fff;color:#09090b;border-color:#f59e0b}
|
|
1051
|
+
:host([theme=light]) button.chip:hover{background:#fffbeb}
|
|
1052
|
+
:host([full-width]) button.chip{width:100%}
|
|
1053
|
+
.av{position:relative;flex:none;display:inline-grid;place-items:center;width:30px;height:30px;border-radius:50%;overflow:hidden;
|
|
1054
|
+
background:radial-gradient(circle at 30% 30%,#ffe27a,#f5b800 55%,#b88600);color:#000;font-weight:800;font-size:14px}
|
|
1055
|
+
.av img{position:absolute;inset:0;width:100%;height:100%;object-fit:cover}
|
|
1056
|
+
.name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;min-width:0;max-width:min(52vw,260px)}
|
|
1057
|
+
.dot{flex:none;width:8px;height:8px;border-radius:50%;background:#ffd24d;box-shadow:0 0 0 3px rgba(255,210,77,.22)}
|
|
1058
|
+
:host([size=sm]) button.chip{font-size:13px;padding:3px 12px 3px 3px;gap:8px}
|
|
1059
|
+
:host([size=sm]) .av{width:26px;height:26px}
|
|
1060
|
+
:host([size=icon]) button.chip{width:48px;height:48px;padding:0;justify-content:center}
|
|
1061
|
+
:host([size=icon]) .name,:host([size=icon]) .dot{display:none}
|
|
1062
|
+
:host([size=icon]) .av{width:40px;height:40px}
|
|
1063
|
+
`;
|
|
1064
|
+
var MENU_CSS = `
|
|
1065
|
+
:host{all:initial}
|
|
1066
|
+
.menu{position:fixed;z-index:2147483600;box-sizing:border-box;min-width:200px;padding:6px;border-radius:14px;background:#0b0b0c;color:#ffd24d;
|
|
1067
|
+
border:1px solid rgba(255,210,77,.55);box-shadow:0 12px 32px rgba(0,0,0,.45);font:600 14px/1.3 Inter,ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif}
|
|
1068
|
+
.who{padding:8px 10px 10px;font-size:12px;font-weight:500;color:#e7d9a8;overflow-wrap:anywhere;border-bottom:1px solid rgba(255,210,77,.3);margin-bottom:4px}
|
|
1069
|
+
.item{all:unset;box-sizing:border-box;display:block;width:100%;padding:11px 10px;border-radius:10px;cursor:pointer;color:#ffd24d}
|
|
1070
|
+
.item:hover,.item:focus-visible{background:#ffd24d;color:#000;outline:none}
|
|
1071
|
+
.tip{position:fixed;z-index:2147483600;box-sizing:border-box;padding:8px 12px;border-radius:10px;background:#0b0b0c;color:#e7d9a8;
|
|
1072
|
+
border:1px solid rgba(255,210,77,.4);box-shadow:0 8px 24px rgba(0,0,0,.35);font:500 12px/1.4 Inter,ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif}
|
|
1073
|
+
`;
|
|
1074
|
+
var PENDING_NOTE = "Sign in: shares only your public identity key, moves no money";
|
|
1075
|
+
var PENDING_LABEL = "Approve in bWalletX\u2026";
|
|
1076
|
+
var PENDING_LABEL_OTHER = "Approve in your wallet\u2026";
|
|
1077
|
+
function b64url2(bytes) {
|
|
1078
|
+
let s = "";
|
|
1079
|
+
for (const b of bytes) s += String.fromCharCode(b);
|
|
1080
|
+
return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
1081
|
+
}
|
|
1082
|
+
async function buildAuthorizeUrl(p) {
|
|
1083
|
+
const rnd = (n) => b64url2(crypto.getRandomValues(new Uint8Array(n)));
|
|
1084
|
+
const verifier = rnd(32);
|
|
1085
|
+
const state = rnd(16);
|
|
1086
|
+
const nonce = rnd(16);
|
|
1087
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier)));
|
|
1088
|
+
const u = new URL(p.authorizeUrl, typeof location !== "undefined" ? location.href : void 0);
|
|
1089
|
+
const set = (k, v) => {
|
|
1090
|
+
if (v) u.searchParams.set(k, v);
|
|
1091
|
+
};
|
|
1092
|
+
set("response_type", "code");
|
|
1093
|
+
set("client_id", p.clientId);
|
|
1094
|
+
set("redirect_uri", p.redirectUri);
|
|
1095
|
+
set("scope", p.scope || "openid profile");
|
|
1096
|
+
set("state", state);
|
|
1097
|
+
set("nonce", nonce);
|
|
1098
|
+
set("code_challenge", b64url2(digest));
|
|
1099
|
+
set("code_challenge_method", "S256");
|
|
1100
|
+
try {
|
|
1101
|
+
sessionStorage.setItem("bwalletx:oauth", JSON.stringify({ verifier, state, nonce }));
|
|
1102
|
+
} catch {
|
|
1103
|
+
}
|
|
1104
|
+
return u.toString();
|
|
1105
|
+
}
|
|
1106
|
+
async function postJson(url, body) {
|
|
1107
|
+
const res = await fetch(url, { method: "POST", credentials: "include", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
1108
|
+
const data = await res.json().catch(() => ({}));
|
|
1109
|
+
if (!res.ok) throw Object.assign(new Error(data.error ?? `HTTP ${res.status}`), { status: res.status });
|
|
1110
|
+
return data;
|
|
1111
|
+
}
|
|
1112
|
+
async function postAccess(url, body) {
|
|
1113
|
+
const res = await fetch(url, { method: "POST", credentials: "include", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
1114
|
+
return res.json().catch(() => ({ error: "bad_request", message: `HTTP ${res.status}` }));
|
|
1115
|
+
}
|
|
1116
|
+
var OBSERVED = ["size", "theme", "label", "brand", "subtitle", "loading", "disabled", "full-width", "aria-label", "get-url", "web-url", "state", "identity-key", "paymail", "handle", "avatar-url", "error", "other-wallets", "challenge-url"];
|
|
1117
|
+
var STATES = ["signed-out", "restoring", "pending", "signed-in", "error"];
|
|
1118
|
+
function createClass() {
|
|
1119
|
+
return class BwalletxSigninElement extends HTMLElement {
|
|
1120
|
+
static get observedAttributes() {
|
|
1121
|
+
return OBSERVED;
|
|
1122
|
+
}
|
|
1123
|
+
#root;
|
|
1124
|
+
#notFound = false;
|
|
1125
|
+
#busy = false;
|
|
1126
|
+
#phase = "signed-out";
|
|
1127
|
+
#account = null;
|
|
1128
|
+
#error = "";
|
|
1129
|
+
#restored = false;
|
|
1130
|
+
#pendingLabel = PENDING_LABEL;
|
|
1131
|
+
#menu = null;
|
|
1132
|
+
constructor() {
|
|
1133
|
+
super();
|
|
1134
|
+
this.#root = this.attachShadow({ mode: "open" });
|
|
1135
|
+
}
|
|
1136
|
+
connectedCallback() {
|
|
1137
|
+
this.#render();
|
|
1138
|
+
void this.#restore();
|
|
1139
|
+
}
|
|
1140
|
+
disconnectedCallback() {
|
|
1141
|
+
this.#menu?.close(false);
|
|
1142
|
+
this.#hideTip();
|
|
1143
|
+
}
|
|
1144
|
+
attributeChangedCallback() {
|
|
1145
|
+
if (this.isConnected) this.#render();
|
|
1146
|
+
}
|
|
1147
|
+
/** The inner <button>, for focus() and tests. */
|
|
1148
|
+
get button() {
|
|
1149
|
+
return this.#root.querySelector("button");
|
|
1150
|
+
}
|
|
1151
|
+
get notFound() {
|
|
1152
|
+
return this.#notFound;
|
|
1153
|
+
}
|
|
1154
|
+
/** The state shown now (the `state` attribute when set, else the element's own). */
|
|
1155
|
+
get state() {
|
|
1156
|
+
const a = this.#attr("state");
|
|
1157
|
+
return a && STATES.includes(a) ? a : this.#phase;
|
|
1158
|
+
}
|
|
1159
|
+
/** The signed-in account, or null. */
|
|
1160
|
+
get account() {
|
|
1161
|
+
if (!this.#controlled) return this.#account;
|
|
1162
|
+
const identityKey = this.#attr("identity-key");
|
|
1163
|
+
return identityKey ? { identityKey, paymail: this.#attr("paymail"), handle: this.#attr("handle"), avatarUrl: this.#attr("avatar-url") } : null;
|
|
1164
|
+
}
|
|
1165
|
+
/** True while the menu is open (tests). */
|
|
1166
|
+
get menuOpen() {
|
|
1167
|
+
return !!this.#menu;
|
|
1168
|
+
}
|
|
1169
|
+
focus(opts) {
|
|
1170
|
+
this.button?.focus(opts);
|
|
1171
|
+
}
|
|
1172
|
+
get #controlled() {
|
|
1173
|
+
return this.hasAttribute("state");
|
|
1174
|
+
}
|
|
1175
|
+
get #stateful() {
|
|
1176
|
+
return !this.hasAttribute("stateless");
|
|
1177
|
+
}
|
|
1178
|
+
#attr(name) {
|
|
1179
|
+
return this.getAttribute(name);
|
|
1180
|
+
}
|
|
1181
|
+
/** Restore the badge from session-url once, with no wallet call. */
|
|
1182
|
+
async #restore() {
|
|
1183
|
+
const url = this.#attr("session-url");
|
|
1184
|
+
if (this.#restored || !url || this.#controlled || !this.#stateful) return;
|
|
1185
|
+
this.#restored = true;
|
|
1186
|
+
this.#set("restoring");
|
|
1187
|
+
try {
|
|
1188
|
+
const r = await fetch(url, { credentials: "include", headers: { accept: "application/json" } });
|
|
1189
|
+
const j = await r.json().catch(() => ({}));
|
|
1190
|
+
if (this.#phase !== "restoring") return;
|
|
1191
|
+
if (j.identityKey) this.#signedIn({ identityKey: j.identityKey, handle: j.handle ?? null, paymail: j.paymail ?? null });
|
|
1192
|
+
else this.#set("signed-out");
|
|
1193
|
+
} catch {
|
|
1194
|
+
if (this.#phase === "restoring") this.#set("signed-out");
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
#set(p) {
|
|
1198
|
+
this.#phase = p;
|
|
1199
|
+
if (p !== "signed-in") this.#menu?.close(false);
|
|
1200
|
+
this.#render();
|
|
1201
|
+
}
|
|
1202
|
+
#signedIn(a) {
|
|
1203
|
+
this.#account = a;
|
|
1204
|
+
this.#error = "";
|
|
1205
|
+
this.#set("signed-in");
|
|
1206
|
+
const lookup = this.#attr("lookup-url");
|
|
1207
|
+
if (!a.paymail && lookup !== "off") {
|
|
1208
|
+
void lookupPaymail(a.identityKey, lookup ?? void 0).then((pm) => {
|
|
1209
|
+
if (pm && this.#account?.identityKey === a.identityKey && !this.#account.paymail) {
|
|
1210
|
+
this.#account = { ...this.#account, paymail: pm };
|
|
1211
|
+
this.#render();
|
|
1212
|
+
}
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
#render() {
|
|
1217
|
+
const size = this.#attr("size") ?? "lg";
|
|
1218
|
+
const brand = this.#attr("brand") === "bWallet" ? "bWallet" : "bWalletX";
|
|
1219
|
+
const esc = (s) => s.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
|
1220
|
+
const state = this.state;
|
|
1221
|
+
const acct = this.account;
|
|
1222
|
+
if (state === "signed-in" && acct) {
|
|
1223
|
+
this.#hideTip();
|
|
1224
|
+
const name = accountLabel(acct);
|
|
1225
|
+
const avatar = acct.avatarUrl ?? avatarUrlFor(acct.identityKey);
|
|
1226
|
+
const initial = (name.replace(/^\$/, "").charAt(0) || "b").toUpperCase();
|
|
1227
|
+
this.#root.innerHTML = `<style>${SIGNIN_CSS}</style><button type="button" class="chip" part="button chip" aria-haspopup="menu" aria-expanded="${this.#menu ? "true" : "false"}" aria-label="Signed in with bWalletX as ${esc(name)}. Account menu" title="Signed in as ${esc(name)}"><span class="av" part="avatar" aria-hidden="true">${esc(initial)}${avatar ? `<img alt="" src="${esc(avatar)}" referrerpolicy="no-referrer">` : ""}</span><span class="name" part="label">${esc(name)}</span><span class="dot" part="dot" aria-hidden="true"></span></button>`;
|
|
1228
|
+
const img = this.#root.querySelector("img");
|
|
1229
|
+
if (img) img.addEventListener("error", () => img.remove(), { once: true });
|
|
1230
|
+
const chip = this.button;
|
|
1231
|
+
chip.addEventListener("click", () => this.#menu ? this.#menu.close(true) : this.#openMenu());
|
|
1232
|
+
chip.addEventListener("keydown", (e) => {
|
|
1233
|
+
if ((e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") && !this.#menu) {
|
|
1234
|
+
e.preventDefault();
|
|
1235
|
+
this.#openMenu();
|
|
1236
|
+
}
|
|
1237
|
+
});
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
const pending = state === "pending";
|
|
1241
|
+
const restoring = state === "restoring";
|
|
1242
|
+
const error = state === "error";
|
|
1243
|
+
const pendingLabel = this.#controlled ? PENDING_LABEL : this.#pendingLabel;
|
|
1244
|
+
const text = pending ? pendingLabel : error ? "Retry" : signinText(this.#attr("label") ?? "signin", brand);
|
|
1245
|
+
const subAttr = this.#attr("subtitle");
|
|
1246
|
+
const subDefault = brand === "bWalletX" && (size === "lg" || size === "md") ? "also works with bWallet" : null;
|
|
1247
|
+
const subtitle = pending || error ? null : subAttr === null || subAttr === "" || subAttr === "on" ? subAttr === "on" ? "also works with bWallet" : subDefault : subAttr === "off" ? null : subAttr;
|
|
1248
|
+
const loading = this.hasAttribute("loading") || this.#busy || pending || restoring;
|
|
1249
|
+
const disabled = this.hasAttribute("disabled");
|
|
1250
|
+
const markPx = size === "icon" ? 24 : size === "sm" ? 15 : size === "md" ? 17 : 20;
|
|
1251
|
+
const aria = pending ? pendingLabel : this.#attr("aria-label") ?? (subtitle && size !== "icon" ? `${text}, ${subtitle}` : text);
|
|
1252
|
+
const getUrl = this.#attr("get-url") ?? GET_URL;
|
|
1253
|
+
const webUrl = this.#attr("web-url") ?? WEB_WALLET_URL;
|
|
1254
|
+
const errText = error ? (this.#controlled ? this.#attr("error") : this.#error) || "Sign-in failed" : "";
|
|
1255
|
+
const label = size === "icon" ? "" : `<span class="txt" part="text"><b part="label">${esc(text)}</b>${subtitle ? `<small part="subtitle">${esc(subtitle)}</small>` : ""}</span>`;
|
|
1256
|
+
const mark = loading ? '<span class="spin" aria-hidden="true"></span>' : markSvg(markPx);
|
|
1257
|
+
const otherOff = this.#attr("other-wallets") === "off";
|
|
1258
|
+
const otherLink = '<button type="button" class="lnk" data-act="other" part="other-wallets-link">Other wallets</button>';
|
|
1259
|
+
const nf = this.#notFound ? `<p class="nf" part="not-found" role="status">bWalletX wasn't found in this browser. <a href="${esc(getUrl)}" target="_blank" rel="noopener">Get ${brand}</a> or <a href="${esc(webUrl)}" target="_blank" rel="noopener">use the web wallet</a>.${otherOff ? "" : ` ${otherLink}`}</p>` : "";
|
|
1260
|
+
const ow = !this.#notFound && !pending && !restoring && this.#otherWalletsShown(size) ? `<p class="ow" part="other-wallets">${otherLink}</p>` : "";
|
|
1261
|
+
const err = error ? `<p class="err" part="error" role="alert">${esc(errText)}</p>` : "";
|
|
1262
|
+
const compact = size === "sm" || size === "icon";
|
|
1263
|
+
const note = pending && !compact ? `<p class="note" part="note">${esc(PENDING_NOTE)}</p>` : "";
|
|
1264
|
+
const status = pending ? `<span class="sr" role="status">${esc(pendingLabel)}</span>` : loading ? '<span class="sr" role="status">Connecting to bWalletX\u2026</span>' : "";
|
|
1265
|
+
this.#root.innerHTML = `<style>${SIGNIN_CSS}</style>${err}<button type="button" part="button" aria-label="${esc(aria)}" title="${esc(subtitle ? `${text} (${subtitle})` : text)}"${disabled ? " disabled" : ""}${loading ? ' aria-busy="true"' : ""}><span class="mark" part="mark">${mark}</span>${label}</button>${status}${note}${ow}${nf}`;
|
|
1266
|
+
this.button.addEventListener("click", () => void this.#click());
|
|
1267
|
+
for (const l of Array.from(this.#root.querySelectorAll("[data-act=other]"))) l.addEventListener("click", () => void this.otherWallets());
|
|
1268
|
+
bindPrewarm(this.button);
|
|
1269
|
+
if (pending && compact) this.#showTip(PENDING_NOTE);
|
|
1270
|
+
else this.#hideTip();
|
|
1271
|
+
}
|
|
1272
|
+
#tip = null;
|
|
1273
|
+
#showTip(text) {
|
|
1274
|
+
const b = this.button;
|
|
1275
|
+
if (!b || typeof document === "undefined") return;
|
|
1276
|
+
if (!this.#tip) {
|
|
1277
|
+
const host = document.createElement("div");
|
|
1278
|
+
host.setAttribute("data-bwalletx-tip", "");
|
|
1279
|
+
const root = host.attachShadow({ mode: "open" });
|
|
1280
|
+
root.innerHTML = `<style>${MENU_CSS}</style><div class="tip" role="note"></div>`;
|
|
1281
|
+
document.body.appendChild(host);
|
|
1282
|
+
this.#tip = host;
|
|
1283
|
+
}
|
|
1284
|
+
const tip = this.#tip.shadowRoot.querySelector(".tip");
|
|
1285
|
+
tip.textContent = text;
|
|
1286
|
+
const r = b.getBoundingClientRect();
|
|
1287
|
+
const vw = window.innerWidth || document.documentElement.clientWidth || 360;
|
|
1288
|
+
const width = Math.min(280, vw - 32);
|
|
1289
|
+
tip.style.width = `${width}px`;
|
|
1290
|
+
tip.style.left = `${Math.max(16, Math.min(r.right - width, vw - width - 16))}px`;
|
|
1291
|
+
tip.style.top = `${r.bottom + 8}px`;
|
|
1292
|
+
}
|
|
1293
|
+
#hideTip() {
|
|
1294
|
+
this.#tip?.remove();
|
|
1295
|
+
this.#tip = null;
|
|
1296
|
+
}
|
|
1297
|
+
#openMenu() {
|
|
1298
|
+
const acct = this.account;
|
|
1299
|
+
const chip = this.button;
|
|
1300
|
+
if (!acct || !chip || typeof document === "undefined") return;
|
|
1301
|
+
const name = accountLabel(acct);
|
|
1302
|
+
const host = document.createElement("div");
|
|
1303
|
+
host.setAttribute("data-bwalletx-menu", "");
|
|
1304
|
+
const root = host.attachShadow({ mode: "open" });
|
|
1305
|
+
const style = document.createElement("style");
|
|
1306
|
+
style.textContent = MENU_CSS;
|
|
1307
|
+
const menu = document.createElement("div");
|
|
1308
|
+
menu.className = "menu";
|
|
1309
|
+
menu.setAttribute("role", "menu");
|
|
1310
|
+
menu.setAttribute("aria-label", "bWalletX account");
|
|
1311
|
+
const who = document.createElement("div");
|
|
1312
|
+
who.className = "who";
|
|
1313
|
+
who.textContent = `Signed in as ${acct.paymail || name}`;
|
|
1314
|
+
const item = (text, run) => {
|
|
1315
|
+
const b = document.createElement("button");
|
|
1316
|
+
b.type = "button";
|
|
1317
|
+
b.className = "item";
|
|
1318
|
+
b.setAttribute("role", "menuitem");
|
|
1319
|
+
b.tabIndex = -1;
|
|
1320
|
+
b.textContent = text;
|
|
1321
|
+
b.addEventListener("click", () => run(b));
|
|
1322
|
+
return b;
|
|
1323
|
+
};
|
|
1324
|
+
const copyWhat = acct.paymail ? "paymail" : "key";
|
|
1325
|
+
const copy = item(`Copy ${copyWhat}`, (b) => {
|
|
1326
|
+
void copyText(acct.paymail || acct.identityKey).then((ok) => {
|
|
1327
|
+
b.textContent = ok ? "Copied" : `Copy ${copyWhat}`;
|
|
1328
|
+
setTimeout(() => close(true), ok ? 600 : 0);
|
|
1329
|
+
});
|
|
1330
|
+
});
|
|
1331
|
+
const out = item("Disconnect", () => {
|
|
1332
|
+
close(false);
|
|
1333
|
+
void this.signOut();
|
|
1334
|
+
});
|
|
1335
|
+
menu.append(who, copy, out);
|
|
1336
|
+
root.append(style, menu);
|
|
1337
|
+
document.body.appendChild(host);
|
|
1338
|
+
const place = () => {
|
|
1339
|
+
const r = chip.getBoundingClientRect();
|
|
1340
|
+
const vw = window.innerWidth || document.documentElement.clientWidth || 360;
|
|
1341
|
+
const vh = window.innerHeight || document.documentElement.clientHeight || 640;
|
|
1342
|
+
const width = Math.min(260, vw - 32);
|
|
1343
|
+
menu.style.width = `${width}px`;
|
|
1344
|
+
const left = Math.max(16, Math.min(r.right - width, vw - width - 16));
|
|
1345
|
+
menu.style.left = `${left}px`;
|
|
1346
|
+
const h = menu.offsetHeight || 150;
|
|
1347
|
+
const below = r.bottom + 8;
|
|
1348
|
+
menu.style.top = `${below + h > vh && r.top - 8 - h > 0 ? r.top - 8 - h : below}px`;
|
|
1349
|
+
};
|
|
1350
|
+
place();
|
|
1351
|
+
const items = [copy, out];
|
|
1352
|
+
const focusAt = (i) => items[(i + items.length) % items.length].focus();
|
|
1353
|
+
const onKey = (e) => {
|
|
1354
|
+
const i = items.indexOf(root.activeElement);
|
|
1355
|
+
if (e.key === "Escape") {
|
|
1356
|
+
e.preventDefault();
|
|
1357
|
+
close(true);
|
|
1358
|
+
} else if (e.key === "ArrowDown") {
|
|
1359
|
+
e.preventDefault();
|
|
1360
|
+
focusAt(i + 1);
|
|
1361
|
+
} else if (e.key === "ArrowUp") {
|
|
1362
|
+
e.preventDefault();
|
|
1363
|
+
focusAt(i - 1);
|
|
1364
|
+
} else if (e.key === "Home") {
|
|
1365
|
+
e.preventDefault();
|
|
1366
|
+
focusAt(0);
|
|
1367
|
+
} else if (e.key === "End") {
|
|
1368
|
+
e.preventDefault();
|
|
1369
|
+
focusAt(items.length - 1);
|
|
1370
|
+
} else if (e.key === "Tab") close(false);
|
|
1371
|
+
};
|
|
1372
|
+
const onDown = (e) => {
|
|
1373
|
+
const path = e.composedPath?.() ?? [];
|
|
1374
|
+
if (!path.includes(host) && !path.includes(this)) close(false);
|
|
1375
|
+
};
|
|
1376
|
+
const onMove = () => close(false);
|
|
1377
|
+
menu.addEventListener("keydown", onKey);
|
|
1378
|
+
document.addEventListener("pointerdown", onDown, true);
|
|
1379
|
+
window.addEventListener("resize", onMove);
|
|
1380
|
+
window.addEventListener("scroll", place, true);
|
|
1381
|
+
const close = (focusChip = false) => {
|
|
1382
|
+
if (this.#menu?.host !== host) return;
|
|
1383
|
+
this.#menu = null;
|
|
1384
|
+
menu.removeEventListener("keydown", onKey);
|
|
1385
|
+
document.removeEventListener("pointerdown", onDown, true);
|
|
1386
|
+
window.removeEventListener("resize", onMove);
|
|
1387
|
+
window.removeEventListener("scroll", place, true);
|
|
1388
|
+
host.remove();
|
|
1389
|
+
this.button?.setAttribute("aria-expanded", "false");
|
|
1390
|
+
if (focusChip) this.button?.focus();
|
|
1391
|
+
};
|
|
1392
|
+
this.#menu = { host, close };
|
|
1393
|
+
chip.setAttribute("aria-expanded", "true");
|
|
1394
|
+
focusAt(0);
|
|
1395
|
+
}
|
|
1396
|
+
/**
|
|
1397
|
+
* Disconnect: emits bwalletx-disconnect (cancelable). Unless a listener cancels it (BwalletXProvider signs out
|
|
1398
|
+
* itself) or the element is driven by `state`, posts logout-url and shows signed out.
|
|
1399
|
+
*/
|
|
1400
|
+
async signOut() {
|
|
1401
|
+
this.#menu?.close(false);
|
|
1402
|
+
if (!this.#emit("bwalletx-disconnect", { element: this, account: this.account }, true)) return;
|
|
1403
|
+
if (this.#controlled) return;
|
|
1404
|
+
const url = this.#attr("logout-url");
|
|
1405
|
+
if (url) await fetch(url, { method: "POST", credentials: "include", headers: { "content-type": "application/json" }, body: "{}" }).catch(() => void 0);
|
|
1406
|
+
this.#account = null;
|
|
1407
|
+
this.#set("signed-out");
|
|
1408
|
+
this.#emit("bwalletx-signed-out");
|
|
1409
|
+
this.button?.focus();
|
|
1410
|
+
}
|
|
1411
|
+
#emit(type, detail, cancelable = false) {
|
|
1412
|
+
return this.dispatchEvent(new CustomEvent(type, { detail, bubbles: true, composed: true, cancelable }));
|
|
1413
|
+
}
|
|
1414
|
+
/** "Other wallets" is offered under the button: other-wallets on, or auto at lg/md when this element signs in. */
|
|
1415
|
+
#otherWalletsShown(size) {
|
|
1416
|
+
const a = this.#attr("other-wallets");
|
|
1417
|
+
if (a === "off") return false;
|
|
1418
|
+
if (a === "on") return true;
|
|
1419
|
+
return (size === "lg" || size === "md") && (this.#controlled || !!this.#attr("challenge-url"));
|
|
1420
|
+
}
|
|
1421
|
+
/** Open the wallet chooser (the "Other wallets" link): phone QR, other browser wallets, desktop wallets. */
|
|
1422
|
+
otherWallets() {
|
|
1423
|
+
return this.#click("chooser");
|
|
1424
|
+
}
|
|
1425
|
+
#attempt(option, outcome, started) {
|
|
1426
|
+
this.#emit("bwalletx-attempt", attemptFor(option, outcome, started));
|
|
1427
|
+
}
|
|
1428
|
+
/**
|
|
1429
|
+
* Which wallet: bWalletX by its own marker goes straight to its sheet; otherwise the chooser ("Get bWalletX" first)
|
|
1430
|
+
* when another wallet is here or the user asked; otherwise 'not-found'. null: the chooser was closed.
|
|
1431
|
+
*/
|
|
1432
|
+
async #choose(mode, ms, pairing) {
|
|
1433
|
+
if (mode !== "chooser") {
|
|
1434
|
+
const f = await discoverBwalletX(ms);
|
|
1435
|
+
if (f) {
|
|
1436
|
+
const substrate = f.rdns === RDNS_EXTENSION ? "extension" : f.rdns === RDNS_MOBILE ? "in-app" : "xdm";
|
|
1437
|
+
return brc100Option(f.wallet, { id: f.rdns ?? "bwalletx-xdm", rdns: f.rdns ?? void 0, name: "bWalletX", isBwalletX: true, substrate });
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
const list = await discoverWallets({ ms: 50, pairing: () => pairing });
|
|
1441
|
+
if (mode !== "chooser" && !hasBwalletX(list) && !otherDetected(list).length) return "not-found";
|
|
1442
|
+
return chooseWallet(list, { getUrl: this.#attr("get-url") ?? void 0 });
|
|
1443
|
+
}
|
|
1444
|
+
async #click(mode = "signin") {
|
|
1445
|
+
if (this.hasAttribute("disabled") || this.hasAttribute("loading") || this.#busy || this.state === "pending") return;
|
|
1446
|
+
if (!this.#emit("bwalletx-signin", { element: this, mode }, true)) return;
|
|
1447
|
+
if (mode === "signin") {
|
|
1448
|
+
const href = this.#attr("href");
|
|
1449
|
+
const authorizeUrl = this.#attr("authorize-url");
|
|
1450
|
+
if (authorizeUrl) {
|
|
1451
|
+
location.assign(await buildAuthorizeUrl({ authorizeUrl, clientId: this.#attr("client-id"), redirectUri: this.#attr("redirect-uri"), scope: this.#attr("scope") }));
|
|
1452
|
+
return;
|
|
1453
|
+
}
|
|
1454
|
+
if (href) {
|
|
1455
|
+
location.assign(href);
|
|
1456
|
+
return;
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
const challengeUrl = this.#attr("challenge-url");
|
|
1460
|
+
if (!challengeUrl) return;
|
|
1461
|
+
const own = this.#stateful && !this.#controlled;
|
|
1462
|
+
const prev = this.#phase === "signed-in" ? "signed-in" : "signed-out";
|
|
1463
|
+
const started = Date.now();
|
|
1464
|
+
const pairing = pairingModal();
|
|
1465
|
+
let option = null;
|
|
1466
|
+
this.#notFound = false;
|
|
1467
|
+
this.#busy = true;
|
|
1468
|
+
this.#pendingLabel = PENDING_LABEL;
|
|
1469
|
+
if (own) this.#set("pending");
|
|
1470
|
+
else this.#render();
|
|
1471
|
+
try {
|
|
1472
|
+
const ms = Number(this.#attr("discover-ms")) || DEFAULT_DISCOVER_MS;
|
|
1473
|
+
const picked = await this.#choose(mode, ms, pairing);
|
|
1474
|
+
if (picked === "not-found") {
|
|
1475
|
+
this.#notFound = true;
|
|
1476
|
+
if (own) this.#phase = "signed-out";
|
|
1477
|
+
this.#attempt(null, "not-found", started);
|
|
1478
|
+
this.#emit("bwalletx-not-found");
|
|
1479
|
+
if (this.#attr("require-token")) this.#emit("bwalletx-access-denied", { code: "not_installed", message: "No wallet found" });
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
if (!picked) {
|
|
1483
|
+
if (own) this.#phase = prev;
|
|
1484
|
+
this.#attempt(null, "cancelled", started);
|
|
1485
|
+
return;
|
|
1486
|
+
}
|
|
1487
|
+
option = picked;
|
|
1488
|
+
if (!option.isBwalletX) {
|
|
1489
|
+
this.#pendingLabel = PENDING_LABEL_OTHER;
|
|
1490
|
+
if (own) this.#render();
|
|
1491
|
+
}
|
|
1492
|
+
const cw = await option.connect({ identity: false });
|
|
1493
|
+
const wallet = cw.wallet;
|
|
1494
|
+
if (!wallet) throw Object.assign(new Error("This wallet cannot sign in here"), { code: "NOT_SUPPORTED" });
|
|
1495
|
+
const sub = substrateOf(option);
|
|
1496
|
+
const method = sub === "extension" ? "extension" : sub === "in-app" || sub === "xdm" ? "in-app" : sub === "pairing" ? "pairing" : "wallet";
|
|
1497
|
+
const requireToken = this.#attr("require-token");
|
|
1498
|
+
if (requireToken) {
|
|
1499
|
+
if (own) this.#phase = "signed-out";
|
|
1500
|
+
const verifyUrl2 = this.#attr("verify-url");
|
|
1501
|
+
const min = this.#attr("min");
|
|
1502
|
+
const minBps = this.#attr("min-bps");
|
|
1503
|
+
const hint = { token: { id: requireToken, ...minBps ? { minBps: Number(minBps) } : { min: min ?? "1" } } };
|
|
1504
|
+
try {
|
|
1505
|
+
const a = await requestAccess({
|
|
1506
|
+
wallet,
|
|
1507
|
+
challenge: (identityKey) => postJson(challengeUrl, { identityKey, require: hint }),
|
|
1508
|
+
verify: verifyUrl2 ? (response2) => postAccess(verifyUrl2, response2) : void 0
|
|
1509
|
+
});
|
|
1510
|
+
this.#attempt(option, "signed-in", started);
|
|
1511
|
+
this.#emit("bwalletx-access-granted", { identityKey: a.response.identityKey, response: a.verified });
|
|
1512
|
+
} catch (e) {
|
|
1513
|
+
const err = e instanceof AccessError ? e : new AccessError("bad_request", String(e));
|
|
1514
|
+
this.#attempt(option, outcomeOf(e), started);
|
|
1515
|
+
this.#emit("bwalletx-access-denied", { code: err.code, message: err.message, have: err.have, need: err.need });
|
|
1516
|
+
}
|
|
1517
|
+
return;
|
|
1518
|
+
}
|
|
1519
|
+
const verifyUrl = this.#attr("verify-url");
|
|
1520
|
+
const { result: r, verified } = await signInAndVerify({
|
|
1521
|
+
wallet,
|
|
1522
|
+
identityKeyHint: verifyUrl ? rememberedIdentity() : void 0,
|
|
1523
|
+
challenge: (identityKey) => postJson(challengeUrl, identityKey ? { identityKey } : {}),
|
|
1524
|
+
verify: async (res) => verifyUrl ? await postJson(verifyUrl, { identityKey: res.identityKey, nonce: res.nonce, signature: res.signature }) : {}
|
|
1525
|
+
});
|
|
1526
|
+
if (verifyUrl) rememberIdentity(r.identityKey);
|
|
1527
|
+
const response = verifyUrl ? verified : null;
|
|
1528
|
+
if (own) this.#signedIn({ identityKey: r.identityKey, handle: response?.handle ?? null, paymail: response?.paymail ?? null });
|
|
1529
|
+
this.#attempt(option, "signed-in", started);
|
|
1530
|
+
this.#emit("bwalletx-signed-in", { identityKey: r.identityKey, method, nonce: r.nonce, signature: r.signature, response });
|
|
1531
|
+
} catch (error) {
|
|
1532
|
+
const outcome = outcomeOf(error);
|
|
1533
|
+
this.#attempt(option, outcome, started);
|
|
1534
|
+
const closedQr = outcome === "cancelled" && option?.kind === "pairing";
|
|
1535
|
+
if (own) {
|
|
1536
|
+
if (closedQr) this.#phase = prev;
|
|
1537
|
+
else {
|
|
1538
|
+
this.#error = signInErrorText(error);
|
|
1539
|
+
this.#phase = "error";
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
if (!closedQr) this.#emit("bwalletx-error", { error });
|
|
1543
|
+
} finally {
|
|
1544
|
+
this.#busy = false;
|
|
1545
|
+
try {
|
|
1546
|
+
pairing.close();
|
|
1547
|
+
} catch {
|
|
1548
|
+
}
|
|
1549
|
+
this.#render();
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
};
|
|
1553
|
+
}
|
|
1554
|
+
function defineBwalletxSignin(tag = SIGNIN_TAG) {
|
|
1555
|
+
if (typeof window === "undefined" || typeof customElements === "undefined") return void 0;
|
|
1556
|
+
const existing = customElements.get(tag);
|
|
1557
|
+
if (existing) return existing;
|
|
1558
|
+
const C = createClass();
|
|
1559
|
+
customElements.define(tag, C);
|
|
1560
|
+
return C;
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
// src/modal.ts
|
|
1564
|
+
var CSS = `
|
|
1565
|
+
.bwx-ov{position:fixed;inset:0;z-index:2147483000;background:rgba(0,0,0,.6);display:flex;align-items:center;justify-content:center;padding:16px;font:15px/1.4 system-ui,-apple-system,Segoe UI,Roboto,sans-serif}
|
|
1566
|
+
.bwx-box{background:#0b0b0b;color:#f4f1e8;border:1px solid #3a3220;border-radius:16px;width:min(360px,100%);padding:20px;box-shadow:0 20px 60px rgba(0,0,0,.5)}
|
|
1567
|
+
.bwx-box h2{margin:0 0 6px;font-size:18px;color:#e8c766}
|
|
1568
|
+
.bwx-box p{margin:0 0 14px;color:#bdb6a3;font-size:14px}
|
|
1569
|
+
.bwx-qr{background:#fff;border-radius:12px;padding:12px;display:flex;justify-content:center}
|
|
1570
|
+
.bwx-qr svg{width:100%;height:auto;max-width:260px}
|
|
1571
|
+
.bwx-code{margin:14px 0 0;text-align:center;font-size:28px;letter-spacing:.3em;color:#e8c766;font-variant-numeric:tabular-nums}
|
|
1572
|
+
.bwx-row{display:flex;gap:8px;margin-top:14px}
|
|
1573
|
+
.bwx-btn{flex:1;border:1px solid #3a3220;background:#15130e;color:#f4f1e8;border-radius:10px;padding:10px 12px;font:inherit;cursor:pointer}
|
|
1574
|
+
.bwx-btn:hover{border-color:#e8c766}
|
|
1575
|
+
a.bwx-btn{display:block;text-align:center;text-decoration:none;box-sizing:border-box}
|
|
1576
|
+
.bwx-primary{background:linear-gradient(180deg,#fcd34d,#f59e0b);color:#000;border:0;font-weight:700;min-height:48px;padding:13px 12px}
|
|
1577
|
+
.bwx-stack{display:flex;flex-direction:column;gap:8px}
|
|
1578
|
+
.bwx-list{display:flex;flex-direction:column;gap:8px}
|
|
1579
|
+
.bwx-opt{display:flex;align-items:center;gap:10px;text-align:left}
|
|
1580
|
+
.bwx-opt img{width:24px;height:24px;border-radius:6px}
|
|
1581
|
+
.bwx-tag{margin-left:auto;font-size:11px;color:#8d866f;white-space:nowrap}
|
|
1582
|
+
.bwx-opt{min-height:48px;box-sizing:border-box}
|
|
1583
|
+
a.bwx-btn.bwx-opt{display:flex;text-align:left}
|
|
1584
|
+
.bwx-btn:focus{outline:none}
|
|
1585
|
+
.bwx-btn:focus-visible{outline:2px solid #e8c766;outline-offset:2px}
|
|
1586
|
+
.bwx-mk{display:inline-flex;align-items:center;justify-content:center;flex:none;width:28px;height:28px;border-radius:50%;background:#000}
|
|
1587
|
+
.bwx-ico{display:inline-grid;place-items:center;flex:none;width:28px;height:28px;border-radius:8px;background:#2a2518;color:#e8c766;font-weight:700;font-size:13px}
|
|
1588
|
+
.bwx-opt img{width:28px;height:28px;border-radius:8px;flex:none}
|
|
1589
|
+
.bwx-txt{display:flex;flex-direction:column;min-width:0;gap:1px}
|
|
1590
|
+
.bwx-txt b{font-weight:600}
|
|
1591
|
+
.bwx-txt small{font-size:12px;color:#8d866f;font-weight:400}
|
|
1592
|
+
.bwx-primary .bwx-txt small{color:rgba(0,0,0,.68)}
|
|
1593
|
+
.bwx-primary .bwx-tag{color:rgba(0,0,0,.6)}
|
|
1594
|
+
.bwx-sep{font-size:12px;color:#8d866f;margin:6px 2px 0}
|
|
1595
|
+
.bwx-foot{font-size:12px;color:#8d866f;margin:12px 0 0}
|
|
1596
|
+
`;
|
|
1597
|
+
function mount() {
|
|
1598
|
+
const root = document.createElement("div");
|
|
1599
|
+
root.className = "bwx-ov";
|
|
1600
|
+
root.setAttribute("role", "dialog");
|
|
1601
|
+
root.setAttribute("aria-modal", "true");
|
|
1602
|
+
const style = document.createElement("style");
|
|
1603
|
+
style.textContent = CSS;
|
|
1604
|
+
const box = document.createElement("div");
|
|
1605
|
+
box.className = "bwx-box";
|
|
1606
|
+
root.append(style, box);
|
|
1607
|
+
document.body.appendChild(root);
|
|
1608
|
+
return { root, box, close: () => root.remove() };
|
|
1609
|
+
}
|
|
1610
|
+
function el(tag, cls, text) {
|
|
1611
|
+
const e = document.createElement(tag);
|
|
1612
|
+
if (cls) e.className = cls;
|
|
1613
|
+
if (text != null) e.textContent = text;
|
|
1614
|
+
return e;
|
|
1615
|
+
}
|
|
1616
|
+
function qrSvg(text) {
|
|
1617
|
+
const q = qrcode(0, "M");
|
|
1618
|
+
q.addData(text);
|
|
1619
|
+
q.make();
|
|
1620
|
+
return q.createSvgTag({ cellSize: 4, margin: 2, scalable: true });
|
|
1621
|
+
}
|
|
1622
|
+
function isTouchDevice(env = globalThis) {
|
|
1623
|
+
const nav = env.navigator;
|
|
1624
|
+
if (!nav) return false;
|
|
1625
|
+
const ua = nav.userAgent ?? "";
|
|
1626
|
+
if (/Android|iPhone|iPad|iPod|Mobile|Silk|Kindle/i.test(ua)) return true;
|
|
1627
|
+
if (/Macintosh/.test(ua) && (nav.maxTouchPoints ?? 0) > 1) return true;
|
|
1628
|
+
try {
|
|
1629
|
+
return !!env.matchMedia?.("(pointer: coarse)").matches && !env.matchMedia?.("(hover: hover)").matches;
|
|
1630
|
+
} catch {
|
|
1631
|
+
return false;
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
function pairingModal(o = {}) {
|
|
1635
|
+
const ac = new AbortController();
|
|
1636
|
+
let ui = null;
|
|
1637
|
+
let codeEl = null;
|
|
1638
|
+
const close = () => {
|
|
1639
|
+
ui?.close();
|
|
1640
|
+
ui = null;
|
|
1641
|
+
};
|
|
1642
|
+
ac.signal.addEventListener("abort", close);
|
|
1643
|
+
return {
|
|
1644
|
+
signal: ac.signal,
|
|
1645
|
+
close,
|
|
1646
|
+
onLink(link) {
|
|
1647
|
+
if (typeof document === "undefined") return;
|
|
1648
|
+
close();
|
|
1649
|
+
ui = mount();
|
|
1650
|
+
const app = o.mode === "app" || (o.mode ?? "auto") === "auto" && isTouchDevice();
|
|
1651
|
+
const h = el("h2", "", o.title ?? "Connect bWalletX");
|
|
1652
|
+
const cancel = el("button", "bwx-btn", "Cancel");
|
|
1653
|
+
cancel.onclick = () => {
|
|
1654
|
+
o.onCancel?.();
|
|
1655
|
+
ac.abort();
|
|
1656
|
+
};
|
|
1657
|
+
codeEl = el("div", "bwx-code", "");
|
|
1658
|
+
codeEl.setAttribute("aria-live", "polite");
|
|
1659
|
+
if (app) {
|
|
1660
|
+
const p2 = el("p", "", "Open bWalletX, approve, then come back to this page.");
|
|
1661
|
+
const stack = el("div", "bwx-stack");
|
|
1662
|
+
const open = el("a", "bwx-btn bwx-primary", "Open bWalletX");
|
|
1663
|
+
open.href = link;
|
|
1664
|
+
open.rel = "noopener";
|
|
1665
|
+
const web = el("button", "bwx-btn", "Use web.bwalletx.com instead");
|
|
1666
|
+
web.onclick = () => {
|
|
1667
|
+
void navigator.clipboard?.writeText(link).catch(() => void 0);
|
|
1668
|
+
window.open(WEB_WALLET_URL, "_blank", "noopener");
|
|
1669
|
+
p2.textContent = "Link copied. In web.bwalletx.com tap Connect and paste it, then come back here.";
|
|
1670
|
+
};
|
|
1671
|
+
stack.append(open, web, cancel);
|
|
1672
|
+
ui.box.append(h, p2, codeEl, stack);
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
const p = el("p", "", "Scan with bWalletX on your phone, or paste the link into web.bwalletx.com.");
|
|
1676
|
+
const qr = el("div", "bwx-qr");
|
|
1677
|
+
qr.innerHTML = qrSvg(link);
|
|
1678
|
+
const row = el("div", "bwx-row");
|
|
1679
|
+
const copy = el("button", "bwx-btn", "Copy link");
|
|
1680
|
+
copy.onclick = () => void navigator.clipboard?.writeText(link).then(() => copy.textContent = "Copied");
|
|
1681
|
+
row.append(copy, cancel);
|
|
1682
|
+
ui.box.append(h, p, qr, codeEl, row);
|
|
1683
|
+
},
|
|
1684
|
+
onCode(code) {
|
|
1685
|
+
if (codeEl) {
|
|
1686
|
+
codeEl.textContent = code;
|
|
1687
|
+
const p = ui?.box.querySelector("p");
|
|
1688
|
+
if (p) p.textContent = "Check bWalletX shows the same code, then tap Connect.";
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
};
|
|
1692
|
+
}
|
|
1693
|
+
function pickWallet(options, o = {}) {
|
|
1694
|
+
if (typeof document === "undefined") return Promise.resolve(null);
|
|
1695
|
+
return new Promise((resolve) => {
|
|
1696
|
+
const ui = mount();
|
|
1697
|
+
const done = (w) => {
|
|
1698
|
+
ui.close();
|
|
1699
|
+
resolve(w);
|
|
1700
|
+
};
|
|
1701
|
+
ui.box.append(el("h2", "", o.title ?? "Choose a wallet"));
|
|
1702
|
+
if (!options.length) ui.box.append(el("p", "", "No wallet found. Get bWalletX at bwalletx.com."));
|
|
1703
|
+
const list = el("div", "bwx-list");
|
|
1704
|
+
for (const w of options) {
|
|
1705
|
+
const b = el("button", "bwx-btn bwx-opt");
|
|
1706
|
+
if (w.icon) {
|
|
1707
|
+
const img = el("img");
|
|
1708
|
+
img.src = w.icon;
|
|
1709
|
+
img.alt = "";
|
|
1710
|
+
b.append(img);
|
|
1711
|
+
}
|
|
1712
|
+
b.append(el("span", "", w.name));
|
|
1713
|
+
if (w.kind === "handcash") b.append(el("span", "bwx-tag", "until BRC-100"));
|
|
1714
|
+
b.onclick = () => done(w);
|
|
1715
|
+
list.append(b);
|
|
1716
|
+
}
|
|
1717
|
+
const cancel = el("button", "bwx-btn", "Cancel");
|
|
1718
|
+
cancel.onclick = () => done(null);
|
|
1719
|
+
ui.root.onclick = (e) => e.target === ui.root && done(null);
|
|
1720
|
+
ui.box.append(list, el("div", "bwx-row"));
|
|
1721
|
+
ui.box.lastChild.append(cancel);
|
|
1722
|
+
});
|
|
1723
|
+
}
|
|
1724
|
+
function optionRow(w, primary, sub, tag) {
|
|
1725
|
+
const b = el("button", `bwx-btn bwx-opt${primary ? " bwx-primary" : ""}`);
|
|
1726
|
+
b.type = "button";
|
|
1727
|
+
b.dataset.wallet = w.id;
|
|
1728
|
+
if (w.isBwalletX) {
|
|
1729
|
+
const mk = el("span", "bwx-mk");
|
|
1730
|
+
mk.innerHTML = markSvg(18);
|
|
1731
|
+
b.append(mk);
|
|
1732
|
+
} else if (w.icon) {
|
|
1733
|
+
const img = el("img");
|
|
1734
|
+
img.src = w.icon;
|
|
1735
|
+
img.alt = "";
|
|
1736
|
+
b.append(img);
|
|
1737
|
+
} else {
|
|
1738
|
+
b.append(el("span", "bwx-ico", (w.name.trim().charAt(0) || "W").toUpperCase()));
|
|
1739
|
+
}
|
|
1740
|
+
const t = el("span", "bwx-txt");
|
|
1741
|
+
t.append(el("b", "", w.name));
|
|
1742
|
+
if (sub) t.append(el("small", "", sub));
|
|
1743
|
+
b.append(t);
|
|
1744
|
+
if (tag) b.append(el("span", "bwx-tag", tag));
|
|
1745
|
+
return b;
|
|
1746
|
+
}
|
|
1747
|
+
function chooseWallet(options, o = {}) {
|
|
1748
|
+
if (typeof document === "undefined") return Promise.resolve(null);
|
|
1749
|
+
return new Promise((resolve) => {
|
|
1750
|
+
const ui = mount();
|
|
1751
|
+
ui.root.dataset.bwxChooser = "";
|
|
1752
|
+
const done = (w) => {
|
|
1753
|
+
document.removeEventListener("keydown", onKey, true);
|
|
1754
|
+
ui.close();
|
|
1755
|
+
resolve(w);
|
|
1756
|
+
};
|
|
1757
|
+
const onKey = (e) => {
|
|
1758
|
+
if (e.key === "Escape") {
|
|
1759
|
+
e.preventDefault();
|
|
1760
|
+
done(null);
|
|
1761
|
+
}
|
|
1762
|
+
};
|
|
1763
|
+
document.addEventListener("keydown", onKey, true);
|
|
1764
|
+
ui.box.append(el("h2", "", o.title ?? "Choose a wallet"));
|
|
1765
|
+
const list = el("div", "bwx-list");
|
|
1766
|
+
const here = hasBwalletX(options);
|
|
1767
|
+
if (!here) {
|
|
1768
|
+
const get = el("a", "bwx-btn bwx-opt bwx-primary");
|
|
1769
|
+
get.href = o.getUrl ?? GET_URL;
|
|
1770
|
+
get.target = "_blank";
|
|
1771
|
+
get.rel = "noopener";
|
|
1772
|
+
get.dataset.wallet = "get-bwalletx";
|
|
1773
|
+
const mk = el("span", "bwx-mk");
|
|
1774
|
+
mk.innerHTML = markSvg(18);
|
|
1775
|
+
const t = el("span", "bwx-txt");
|
|
1776
|
+
t.append(el("b", "", "Get bWalletX"), el("small", "", "Free. Every feature on this site"));
|
|
1777
|
+
get.append(mk, t);
|
|
1778
|
+
list.append(get);
|
|
1779
|
+
}
|
|
1780
|
+
const ordered = chooserOrder(options);
|
|
1781
|
+
const touch = isTouchDevice();
|
|
1782
|
+
let sep = false;
|
|
1783
|
+
for (const w of ordered) {
|
|
1784
|
+
if (!here && !sep) {
|
|
1785
|
+
list.append(el("p", "bwx-sep", "Or use"));
|
|
1786
|
+
sep = true;
|
|
1787
|
+
}
|
|
1788
|
+
const sub = isPresentBwalletX(w) ? "Recommended" : w.kind === "pairing" ? touch ? "Open the bWalletX app" : "Scan a QR with your phone" : void 0;
|
|
1789
|
+
const tag = w.kind === "handcash" ? "until BRC-100" : !w.isBwalletX && w.kind === "brc100" ? "Fewer features" : void 0;
|
|
1790
|
+
const row2 = optionRow(w, isPresentBwalletX(w), sub, tag);
|
|
1791
|
+
row2.onclick = () => done(w);
|
|
1792
|
+
list.append(row2);
|
|
1793
|
+
}
|
|
1794
|
+
if (o.desktop !== false && !options.some((w) => w.substrate === "json-api" || w.substrate === "cicada")) {
|
|
1795
|
+
if (!here && !sep) list.append(el("p", "bwx-sep", "Or use"));
|
|
1796
|
+
const d = desktopOption();
|
|
1797
|
+
const row2 = optionRow(d, false, "Metanet or BSV Desktop on this computer", "Fewer features");
|
|
1798
|
+
row2.onclick = () => done(d);
|
|
1799
|
+
list.append(row2);
|
|
1800
|
+
}
|
|
1801
|
+
ui.box.append(list);
|
|
1802
|
+
if (otherDetected(options).length || !here) ui.box.append(el("p", "bwx-foot", "Some features need bWalletX."));
|
|
1803
|
+
const row = el("div", "bwx-row");
|
|
1804
|
+
const cancel = el("button", "bwx-btn", "Cancel");
|
|
1805
|
+
cancel.type = "button";
|
|
1806
|
+
cancel.onclick = () => done(null);
|
|
1807
|
+
row.append(cancel);
|
|
1808
|
+
ui.box.append(row);
|
|
1809
|
+
ui.root.onclick = (e) => e.target === ui.root && done(null);
|
|
1810
|
+
list.querySelector("a,button")?.focus();
|
|
1811
|
+
});
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
export {
|
|
1815
|
+
RDNS_EXTENSION,
|
|
1816
|
+
RDNS_MOBILE,
|
|
1817
|
+
BWALLETX_RDNS,
|
|
1818
|
+
DEFAULT_DISCOVER_MS,
|
|
1819
|
+
discoverBwalletX,
|
|
1820
|
+
findBwalletX,
|
|
1821
|
+
isFramed,
|
|
1822
|
+
prewarmBwalletX,
|
|
1823
|
+
bindPrewarm,
|
|
1824
|
+
inOrder,
|
|
1825
|
+
pairBwalletX,
|
|
1826
|
+
restorePairing,
|
|
1827
|
+
clearPairing,
|
|
1828
|
+
LOGIN_PROTOCOL,
|
|
1829
|
+
connectBwalletX,
|
|
1830
|
+
waitIfLocked,
|
|
1831
|
+
challengeWantsIdentityKey,
|
|
1832
|
+
signInWithBwalletX,
|
|
1833
|
+
signInAndVerify,
|
|
1834
|
+
brc100Option,
|
|
1835
|
+
pairingOption,
|
|
1836
|
+
handcashAdapter,
|
|
1837
|
+
cwiIsAnnounced,
|
|
1838
|
+
discoverWallets,
|
|
1839
|
+
DESKTOP_OPTION_ID,
|
|
1840
|
+
desktopOption,
|
|
1841
|
+
autoChoice,
|
|
1842
|
+
PAYMAIL_LOOKUP_URL,
|
|
1843
|
+
BAVATAR_URL,
|
|
1844
|
+
shortKey,
|
|
1845
|
+
accountLabel,
|
|
1846
|
+
avatarUrlFor,
|
|
1847
|
+
lookupPaymail,
|
|
1848
|
+
isUserRejection,
|
|
1849
|
+
signInErrorText,
|
|
1850
|
+
IDENTITY_HINT_KEY,
|
|
1851
|
+
rememberedIdentity,
|
|
1852
|
+
rememberIdentity,
|
|
1853
|
+
BWALLETX_TYPE,
|
|
1854
|
+
substrateOf,
|
|
1855
|
+
walletTypeOf,
|
|
1856
|
+
isPresentBwalletX,
|
|
1857
|
+
hasBwalletX,
|
|
1858
|
+
otherDetected,
|
|
1859
|
+
chooserOrder,
|
|
1860
|
+
decideWallet,
|
|
1861
|
+
outcomeOf,
|
|
1862
|
+
attemptFor,
|
|
1863
|
+
reportAttempt,
|
|
1864
|
+
qrSvg,
|
|
1865
|
+
isTouchDevice,
|
|
1866
|
+
pairingModal,
|
|
1867
|
+
pickWallet,
|
|
1868
|
+
chooseWallet,
|
|
1869
|
+
toAccessError,
|
|
1870
|
+
throwIfAccessError,
|
|
1871
|
+
proveHoldings,
|
|
1872
|
+
requestAccess,
|
|
1873
|
+
SIGNIN_TAG,
|
|
1874
|
+
GET_URL,
|
|
1875
|
+
WEB_WALLET_URL,
|
|
1876
|
+
signinText,
|
|
1877
|
+
markSvg,
|
|
1878
|
+
SIGNIN_CSS,
|
|
1879
|
+
MENU_CSS,
|
|
1880
|
+
PENDING_NOTE,
|
|
1881
|
+
PENDING_LABEL,
|
|
1882
|
+
PENDING_LABEL_OTHER,
|
|
1883
|
+
buildAuthorizeUrl,
|
|
1884
|
+
defineBwalletxSignin
|
|
1885
|
+
};
|
|
1886
|
+
//# sourceMappingURL=chunk-LS6AYEBO.js.map
|