hd-wallet-ui 2.0.21 → 2.0.27

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.
Files changed (39) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +101 -123
  3. package/data/common-passwords-sdn-v1.source.json +12 -0
  4. package/data/common-passwords-sdn-v1.txt +10000 -0
  5. package/dist/browser/sdn-wallet-callback.js +1 -0
  6. package/dist/browser/sdn-wallet-public-client.js +1 -0
  7. package/dist/browser/wallet-callback.html +6 -0
  8. package/dist/client/asset-review.d.ts +8 -0
  9. package/dist/client/asset-review.js +1570 -0
  10. package/dist/client/callback.d.ts +13 -0
  11. package/dist/client/callback.js +29 -0
  12. package/dist/client/index.d.ts +11 -0
  13. package/dist/client/index.js +1379 -0
  14. package/dist/client/sdn.d.ts +8 -0
  15. package/dist/client/sdn.js +1542 -0
  16. package/dist/client/style.css +32 -0
  17. package/dist/client/types.d.ts +278 -0
  18. package/dist/compat/index.d.ts +26 -0
  19. package/dist/compat/index.js +62 -0
  20. package/dist/wallet-origin/index.d.ts +21 -0
  21. package/dist/wallet-origin/index.js +4607 -0
  22. package/dist/wallet-origin-host/assets/wallet-origin.43cf84e0eba17742309357ec6bc2c544736e155ffd5d76d5024408e998500a57.wasm +0 -0
  23. package/dist/wallet-origin-host/assets/wallet-origin.4c9455f1a2618948e203f720485add9d8ebf77b5542b41f933594afcfbb34361.css +1 -0
  24. package/dist/wallet-origin-host/assets/wallet-origin.aedc355452e665e16c0f72a6597602873378dbc39d4cbf6104621c07579347e0.js +12463 -0
  25. package/dist/wallet-origin-host/index.html +10 -0
  26. package/dist/wallet-origin-host/integrity.json +1 -0
  27. package/package.json +46 -18
  28. package/src/address-derivation.js +0 -514
  29. package/src/app.js +0 -6008
  30. package/src/blockchain-trust.js +0 -752
  31. package/src/clipboard.js +0 -44
  32. package/src/constants.js +0 -66
  33. package/src/hash.js +0 -9
  34. package/src/lib.js +0 -66
  35. package/src/template.js +0 -626
  36. package/src/trust-ui.js +0 -745
  37. package/src/wallet-storage.js +0 -773
  38. package/styles/main.css +0 -6220
  39. package/styles/widget.css +0 -6212
@@ -0,0 +1,1379 @@
1
+ const lt = Object.freeze({
2
+ cm: 0.01,
3
+ km: 1e3,
4
+ m: 1,
5
+ mm: 1e-3
6
+ }), ut = Object.freeze({
7
+ metersPerSourceUnit: lt,
8
+ quaternionNormTolerance: 1e-6,
9
+ scaleComponentExclusiveMin: 0,
10
+ scaleComponentInclusiveMax: 1e6,
11
+ translationComponentAbsMax: 1e6,
12
+ upAxes: Object.freeze(["X_UP", "Y_UP", "Z_UP"])
13
+ }), ht = Object.freeze({ reviewedTransform: ut, schemaVersion: 1 }), $ = new TextEncoder(), tt = Object.getPrototypeOf(Uint8Array.prototype);
14
+ Object.getOwnPropertyDescriptor(tt, "buffer").get;
15
+ Object.getOwnPropertyDescriptor(tt, "length").get;
16
+ Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get;
17
+ typeof SharedArrayBuffer > "u" || Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, "byteLength").get;
18
+ const k = "sdn-bip32-slip10-purpose-v1", M = "password-scrypt-v2", W = "ed25519-over-sha256-jcs-v1", ft = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u, dt = /^[0-9a-f]{64}$/u, yt = /^sha256:[0-9a-f]{64}$/u, mt = /^[1-9A-HJ-NP-Za-km-z]+$/u, Rt = 131072, pt = [
19
+ "accountFingerprint",
20
+ "accountIndex",
21
+ "accountLabel",
22
+ "accountPeerId",
23
+ "accountXpub",
24
+ "identityScheme",
25
+ "keys",
26
+ "schemaVersion",
27
+ "seedProfile"
28
+ ], Et = [
29
+ "bip32Fingerprint",
30
+ "curve",
31
+ "derivation",
32
+ "encoding",
33
+ "identityScheme",
34
+ "keyId",
35
+ "path",
36
+ "publicKeyHex",
37
+ "purpose",
38
+ "seedProfile",
39
+ "signatureProfile"
40
+ ], bt = [
41
+ "connectionExpiresAt",
42
+ "event",
43
+ "identity",
44
+ "schemaVersion"
45
+ ];
46
+ function u(r) {
47
+ throw new TypeError(r);
48
+ }
49
+ function At(r) {
50
+ if (r === null || typeof r != "object" || Array.isArray(r)) return !1;
51
+ const t = Object.getPrototypeOf(r);
52
+ return t === Object.prototype || t === null;
53
+ }
54
+ function S(r, t) {
55
+ typeof r != "string" && u(`${t} must be a string`);
56
+ for (let e = 0; e < r.length; e += 1) {
57
+ const i = r.charCodeAt(e);
58
+ let s = i;
59
+ if (i >= 55296 && i <= 56319) {
60
+ const o = r.charCodeAt(e + 1);
61
+ o >= 56320 && o <= 57343 || u(`${t} contains an unpaired surrogate`), s = 65536 + (i - 55296 << 10) + (o - 56320), e += 1;
62
+ } else i >= 56320 && i <= 57343 && u(`${t} contains an unpaired surrogate`);
63
+ ((s & 65535) === 65534 || (s & 65535) === 65535 || s >= 64976 && s <= 65007) && u(`${t} contains a Unicode noncharacter`);
64
+ }
65
+ return r;
66
+ }
67
+ function Ot(r) {
68
+ typeof r != "string" && u("wire JSON must be a string"), $.encode(r).byteLength > Rt && u("wire JSON is too large"), r.charCodeAt(0) === 65279 && u("wire JSON must not contain a BOM");
69
+ let t = 0, e = 0;
70
+ const i = () => {
71
+ for (; t < r.length && /[\u0009\u000a\u000d\u0020]/u.test(r[t]); ) t += 1;
72
+ }, s = () => {
73
+ r[t] !== '"' && u("invalid JSON string");
74
+ const l = t;
75
+ for (t += 1; t < r.length; ) {
76
+ const a = r.charCodeAt(t);
77
+ if (a === 34) {
78
+ t += 1;
79
+ let h;
80
+ try {
81
+ h = JSON.parse(r.slice(l, t));
82
+ } catch {
83
+ u("invalid JSON string");
84
+ }
85
+ return S(h, "JSON string");
86
+ }
87
+ a < 32 && u("invalid JSON control character"), a === 92 && (t += 1, t >= r.length && u("unterminated JSON escape")), t += 1;
88
+ }
89
+ u("unterminated JSON string");
90
+ }, o = (l = 0) => {
91
+ l > 32 && u("wire JSON nesting is too deep"), e += 1, e > 4096 && u("wire JSON contains too many values"), i();
92
+ const a = r[t];
93
+ if (a === '"') return s();
94
+ if (a === "{") {
95
+ t += 1;
96
+ const d = /* @__PURE__ */ Object.create(null), y = /* @__PURE__ */ new Set();
97
+ if (i(), r[t] === "}")
98
+ return t += 1, d;
99
+ for (; t < r.length; ) {
100
+ i();
101
+ const m = s();
102
+ if (y.has(m) && u(`duplicate JSON field: ${m}`), y.add(m), i(), r[t] !== ":" && u("missing JSON object colon"), t += 1, d[m] = o(l + 1), i(), r[t] === "}")
103
+ return t += 1, d;
104
+ r[t] !== "," && u("invalid JSON object separator"), t += 1;
105
+ }
106
+ u("unterminated JSON object");
107
+ }
108
+ if (a === "[") {
109
+ t += 1;
110
+ const d = [];
111
+ if (i(), r[t] === "]")
112
+ return t += 1, d;
113
+ for (; t < r.length; ) {
114
+ if (d.push(o(l + 1)), i(), r[t] === "]")
115
+ return t += 1, d;
116
+ r[t] !== "," && u("invalid JSON array separator"), t += 1;
117
+ }
118
+ u("unterminated JSON array");
119
+ }
120
+ for (const [d, y] of [["true", !0], ["false", !1], ["null", null]])
121
+ if (r.startsWith(d, t))
122
+ return t += d.length, y;
123
+ const h = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/u.exec(r.slice(t));
124
+ h || u("invalid JSON value"), t += h[0].length;
125
+ const f = Number(h[0]);
126
+ return Number.isFinite(f) || u("wire JSON number must be finite"), f;
127
+ };
128
+ i();
129
+ const n = o();
130
+ return i(), t !== r.length && u("wire JSON contains trailing bytes"), n;
131
+ }
132
+ function wt(r) {
133
+ return typeof r == "string" ? Ot(r) : r;
134
+ }
135
+ function vt(r, t) {
136
+ if (r.byteLength !== t.byteLength) return !1;
137
+ let e = 0;
138
+ for (let i = 0; i < r.byteLength; i += 1) e |= r[i] ^ t[i];
139
+ return e === 0;
140
+ }
141
+ function O(r, t, e) {
142
+ const i = wt(r);
143
+ At(i) || u(`${e} must be a JSON object`), Object.getOwnPropertySymbols(i).length !== 0 && u(`${e} has an unknown symbol field`);
144
+ const s = Object.getOwnPropertyNames(i).sort(), o = [...t].sort(), n = $.encode(s.join("\0")), l = $.encode(o.join("\0"));
145
+ vt(n, l) || u(`${e} has missing or unknown fields`);
146
+ for (const a of s) {
147
+ const h = Object.getOwnPropertyDescriptor(i, a);
148
+ (!h || !h.enumerable || !("value" in h)) && u(`${e}.${a} must be an enumerable data field`), h.value === void 0 && u(`${e}.${a} must not be undefined`);
149
+ }
150
+ return i;
151
+ }
152
+ function Y(r) {
153
+ if (!r || typeof r != "object" || Object.isFrozen(r)) return r;
154
+ for (const t of Object.values(r)) Y(t);
155
+ return Object.freeze(r);
156
+ }
157
+ function C(r) {
158
+ return Y(Object.fromEntries([...r].sort(([t], [e]) => t < e ? -1 : t > e ? 1 : 0)));
159
+ }
160
+ function et(r, t, e) {
161
+ (!Array.isArray(r) || Object.getPrototypeOf(r) !== Array.prototype || r.length !== t) && u(`${e} must be a plain array with exactly ${t} values`);
162
+ const i = [...Array.from({ length: t }, (o, n) => String(n)), "length"], s = Reflect.ownKeys(r);
163
+ (s.length !== i.length || s.some((o, n) => o !== i[n])) && u(`${e} must be a dense plain array`);
164
+ for (let o = 0; o < t; o += 1) {
165
+ const n = Object.getOwnPropertyDescriptor(r, String(o));
166
+ (!n || !n.enumerable || !("value" in n) || n.value === void 0) && u(`${e} must contain enumerable data values`);
167
+ }
168
+ }
169
+ function Lt(r) {
170
+ const t = O(
171
+ r,
172
+ ["reviewedTransform", "schemaVersion"],
173
+ "asset review protocol policy"
174
+ );
175
+ R(t.schemaVersion, 1, "asset review protocol schemaVersion");
176
+ const e = O(t.reviewedTransform, [
177
+ "metersPerSourceUnit",
178
+ "quaternionNormTolerance",
179
+ "scaleComponentExclusiveMin",
180
+ "scaleComponentInclusiveMax",
181
+ "translationComponentAbsMax",
182
+ "upAxes"
183
+ ], "asset review transform policy"), i = O(
184
+ e.metersPerSourceUnit,
185
+ ["cm", "km", "m", "mm"],
186
+ "asset review unit policy"
187
+ );
188
+ for (const [s, o] of Object.entries(i))
189
+ (typeof o != "number" || !Number.isFinite(o) || o <= 0) && u(`asset review unit policy ${s} must be positive and finite`);
190
+ for (const [s, o] of [
191
+ ["quaternionNormTolerance", e.quaternionNormTolerance],
192
+ ["scaleComponentExclusiveMin", e.scaleComponentExclusiveMin],
193
+ ["scaleComponentInclusiveMax", e.scaleComponentInclusiveMax],
194
+ ["translationComponentAbsMax", e.translationComponentAbsMax]
195
+ ])
196
+ (typeof o != "number" || !Number.isFinite(o)) && u(`asset review transform policy ${s} must be finite`);
197
+ return (e.quaternionNormTolerance <= 0 || e.scaleComponentInclusiveMax <= e.scaleComponentExclusiveMin || e.translationComponentAbsMax <= 0) && u("asset review transform policy bounds are inconsistent"), et(e.upAxes, 3, "asset review up-axis policy"), e.upAxes.join("\0") !== "X_UP\0Y_UP\0Z_UP" && u("asset review up-axis policy is invalid"), C([
198
+ ["metersPerSourceUnit", C(Object.entries(i))],
199
+ ["quaternionNormTolerance", e.quaternionNormTolerance],
200
+ ["scaleComponentExclusiveMin", e.scaleComponentExclusiveMin],
201
+ ["scaleComponentInclusiveMax", e.scaleComponentInclusiveMax],
202
+ ["translationComponentAbsMax", e.translationComponentAbsMax],
203
+ ["upAxes", Object.freeze(Array.from({ length: 3 }, (s, o) => e.upAxes[o]))]
204
+ ]);
205
+ }
206
+ Lt(ht);
207
+ function R(r, t, e) {
208
+ return r !== t && u(`${e} must equal ${JSON.stringify(t)}`), r;
209
+ }
210
+ function _t(r, t, e) {
211
+ return t.includes(r) || u(`${e} is not an allowed value`), r;
212
+ }
213
+ function U(r, t, e) {
214
+ return S(r, e), t.test(r) || u(`${e} has an invalid encoding`), r;
215
+ }
216
+ function It(r, t) {
217
+ return S(r, t), (!ft.test(r) || new Date(r).toISOString() !== r) && u(`${t} must be exact RFC3339 milliseconds UTC`), r;
218
+ }
219
+ function Tt(r, t) {
220
+ const e = O(r, Et, `identity key ${t.purpose}`);
221
+ return R(e.purpose, t.purpose, "key purpose"), R(e.identityScheme, k, "key identityScheme"), R(e.seedProfile, M, "key seedProfile"), R(e.signatureProfile, t.signatureProfile, "key signatureProfile"), R(e.curve, t.curve, "key curve"), R(e.derivation, "slip10", "key derivation"), R(e.path, t.path, "key path"), R(e.encoding, "raw", "key encoding"), U(e.publicKeyHex, dt, "key publicKeyHex"), R(e.bip32Fingerprint, null, "key bip32Fingerprint"), U(e.keyId, yt, "key keyId"), C([
222
+ ["bip32Fingerprint", null],
223
+ ["curve", e.curve],
224
+ ["derivation", "slip10"],
225
+ ["encoding", "raw"],
226
+ ["identityScheme", k],
227
+ ["keyId", e.keyId],
228
+ ["path", e.path],
229
+ ["publicKeyHex", e.publicKeyHex],
230
+ ["purpose", e.purpose],
231
+ ["seedProfile", M],
232
+ ["signatureProfile", e.signatureProfile]
233
+ ]);
234
+ }
235
+ function St(r) {
236
+ const t = O(r, pt, "WalletPublicIdentity");
237
+ R(t.schemaVersion, 1, "identity schemaVersion"), R(t.identityScheme, k, "identity identityScheme"), R(t.seedProfile, M, "identity seedProfile"), t.accountIndex !== 0 && u("public wallet identity must use account 0"), R(t.accountLabel, null, "identity accountLabel"), S(t.accountXpub, "identity accountXpub"), /^xpub[1-9A-HJ-NP-Za-km-z]{107}$/u.test(t.accountXpub) || u("identity accountXpub is invalid"), S(t.accountPeerId, "identity accountPeerId"), (!t.accountPeerId.startsWith("16Uiu2H") || t.accountPeerId.length < 40 || t.accountPeerId.length > 64 || !mt.test(t.accountPeerId)) && u("identity accountPeerId is invalid"), U(t.accountFingerprint, /^[0-9a-f]{8}$/u, "identity accountFingerprint"), et(t.keys, 3, "public identity keys array");
238
+ const e = [
239
+ {
240
+ purpose: "asset-review-approval",
241
+ curve: "ed25519",
242
+ signatureProfile: W,
243
+ path: "m/44'/0'/0'/2'/0'"
244
+ },
245
+ {
246
+ purpose: "contact-encryption",
247
+ curve: "x25519",
248
+ signatureProfile: null,
249
+ path: "m/44'/0'/0'/1'/0'"
250
+ },
251
+ {
252
+ purpose: "sdn-authentication",
253
+ curve: "ed25519",
254
+ signatureProfile: W,
255
+ path: "m/44'/0'/0'/0'/0'"
256
+ }
257
+ ], i = Array.from(
258
+ { length: 3 },
259
+ (s, o) => Tt(t.keys[o], e[o])
260
+ );
261
+ return C([
262
+ ["accountFingerprint", t.accountFingerprint],
263
+ ["accountIndex", 0],
264
+ ["accountLabel", null],
265
+ ["accountPeerId", t.accountPeerId],
266
+ ["accountXpub", t.accountXpub],
267
+ ["identityScheme", k],
268
+ ["keys", Y(i)],
269
+ ["schemaVersion", 1],
270
+ ["seedProfile", M]
271
+ ]);
272
+ }
273
+ function rt(r, t) {
274
+ const e = O(r, bt, `${t} result`);
275
+ R(e.schemaVersion, 1, "connection result schemaVersion"), _t(e.event, ["connected", "disconnected"], "connection event"), t === "connect" && e.event !== "connected" && u("connect result must be connected");
276
+ let i = null, s = null;
277
+ return e.event === "connected" ? (i = St(e.identity), s = It(e.connectionExpiresAt, "connectionExpiresAt")) : (e.identity !== null || e.connectionExpiresAt !== null) && u("disconnected result must clear identity and expiry"), C([
278
+ ["connectionExpiresAt", s],
279
+ ["event", e.event],
280
+ ["identity", i],
281
+ ["schemaVersion", 1]
282
+ ]);
283
+ }
284
+ function it(r, t) {
285
+ return O(r, [], t), Y({});
286
+ }
287
+ function Ct(r = {}) {
288
+ return it(r, "wallet connect request");
289
+ }
290
+ function Pt(r) {
291
+ return rt(r, "connect");
292
+ }
293
+ function xt(r = {}) {
294
+ return it(r, "wallet account request");
295
+ }
296
+ function Nt(r) {
297
+ return rt(r, "account");
298
+ }
299
+ const v = "https://wallet.spacedatanetwork.org", P = "sdn.wallet.callback.v1:", _ = /^[0-9a-f]{64}$/u, q = /^[A-Za-z0-9_-]{43}$/u, jt = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u, gt = 250, Dt = 3e4, kt = 3e5, Mt = 9e5, Yt = 12e4, Vt = 8, K = 4096, z = 70 * 1024, $t = 64, st = Object.freeze({
300
+ CALLBACK_ERROR: "The wallet return could not be verified. Try again.",
301
+ CRYPTO_UNAVAILABLE: "Secure browser cryptography is unavailable.",
302
+ DESTROYED: "This wallet client has been destroyed.",
303
+ DISCONNECTED: "The wallet was disconnected.",
304
+ EXPIRED: "The wallet connection expired.",
305
+ INVALID_CLIENT: "This wallet client is not registered.",
306
+ INVALID_REQUEST: "The wallet request is invalid.",
307
+ RELAY_ERROR: "The wallet service could not complete the request. Try again.",
308
+ REPLACED: "A newer wallet request replaced this one.",
309
+ WALLET_NOT_COMPLETED: "The wallet did not complete. Check popup settings and try again."
310
+ }), nt = new TextEncoder(), Ut = new TextDecoder("utf-8", { fatal: !0 });
311
+ class T extends Error {
312
+ constructor(t) {
313
+ super(st[t]), this.name = "WalletClientError", Object.defineProperty(this, "code", {
314
+ configurable: !1,
315
+ enumerable: !0,
316
+ value: t,
317
+ writable: !1
318
+ });
319
+ }
320
+ }
321
+ function c(r) {
322
+ return Object.hasOwn(st, r) ? new T(r) : new T("RELAY_ERROR");
323
+ }
324
+ function Ft(r) {
325
+ if (r === null || typeof r != "object" || Array.isArray(r)) return !1;
326
+ const t = Object.getPrototypeOf(r);
327
+ return t === Object.prototype || t === null;
328
+ }
329
+ function N(r, t) {
330
+ if (typeof r != "string" || r.length > t || nt.encode(r).byteLength > t || r.charCodeAt(0) === 65279)
331
+ throw c("RELAY_ERROR");
332
+ let e = 0, i = 0;
333
+ const s = () => {
334
+ for (; e < r.length && /[\u0009\u000a\u000d\u0020]/u.test(r[e]); ) e += 1;
335
+ }, o = () => {
336
+ if (r[e] !== '"') throw c("RELAY_ERROR");
337
+ const a = e;
338
+ for (e += 1; e < r.length; ) {
339
+ const h = r.charCodeAt(e);
340
+ if (h === 34) {
341
+ e += 1;
342
+ let f;
343
+ try {
344
+ f = JSON.parse(r.slice(a, e));
345
+ } catch {
346
+ throw c("RELAY_ERROR");
347
+ }
348
+ for (let d = 0; d < f.length; d += 1) {
349
+ const y = f.charCodeAt(d);
350
+ if (y >= 55296 && y <= 56319) {
351
+ const m = f.charCodeAt(d + 1);
352
+ if (!(m >= 56320 && m <= 57343)) throw c("RELAY_ERROR");
353
+ d += 1;
354
+ } else if (y >= 56320 && y <= 57343)
355
+ throw c("RELAY_ERROR");
356
+ }
357
+ return f;
358
+ }
359
+ if (h < 32 || h === 92 && (e += 1, e >= r.length))
360
+ throw c("RELAY_ERROR");
361
+ e += 1;
362
+ }
363
+ throw c("RELAY_ERROR");
364
+ }, n = (a = 0) => {
365
+ if (a > 32 || ++i > 4096) throw c("RELAY_ERROR");
366
+ s();
367
+ const h = r[e];
368
+ if (h === '"') return o();
369
+ if (h === "{") {
370
+ e += 1;
371
+ const y = /* @__PURE__ */ Object.create(null), m = /* @__PURE__ */ new Set();
372
+ if (s(), r[e] === "}")
373
+ return e += 1, y;
374
+ for (; e < r.length; ) {
375
+ s();
376
+ const b = o();
377
+ if (m.has(b) || (m.add(b), s(), r[e] !== ":")) throw c("RELAY_ERROR");
378
+ if (e += 1, y[b] = n(a + 1), s(), r[e] === "}")
379
+ return e += 1, y;
380
+ if (r[e] !== ",") throw c("RELAY_ERROR");
381
+ e += 1;
382
+ }
383
+ throw c("RELAY_ERROR");
384
+ }
385
+ if (h === "[") {
386
+ e += 1;
387
+ const y = [];
388
+ if (s(), r[e] === "]")
389
+ return e += 1, y;
390
+ for (; e < r.length; ) {
391
+ if (y.push(n(a + 1)), s(), r[e] === "]")
392
+ return e += 1, y;
393
+ if (r[e] !== ",") throw c("RELAY_ERROR");
394
+ e += 1;
395
+ }
396
+ throw c("RELAY_ERROR");
397
+ }
398
+ for (const [y, m] of [["true", !0], ["false", !1], ["null", null]])
399
+ if (r.startsWith(y, e))
400
+ return e += y.length, m;
401
+ const f = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/u.exec(r.slice(e));
402
+ if (!f) throw c("RELAY_ERROR");
403
+ e += f[0].length;
404
+ const d = Number(f[0]);
405
+ if (!Number.isFinite(d)) throw c("RELAY_ERROR");
406
+ return d;
407
+ };
408
+ s();
409
+ const l = n();
410
+ if (s(), e !== r.length) throw c("RELAY_ERROR");
411
+ return l;
412
+ }
413
+ function j(r, t, e = "RELAY_ERROR") {
414
+ if (!Ft(r) || Object.getOwnPropertySymbols(r).length !== 0)
415
+ throw c(e);
416
+ const i = Object.getOwnPropertyNames(r).sort(), s = [...t].sort();
417
+ if (i.length !== s.length || i.some((o, n) => o !== s[n]))
418
+ throw c(e);
419
+ for (const o of i) {
420
+ const n = Object.getOwnPropertyDescriptor(r, o);
421
+ if (!n?.enumerable || !("value" in n) || n.value === void 0)
422
+ throw c(e);
423
+ }
424
+ return r;
425
+ }
426
+ function g(r, t = "RELAY_ERROR") {
427
+ if (typeof r != "string" || !jt.test(r))
428
+ throw c(t);
429
+ const e = Date.parse(r);
430
+ if (!Number.isFinite(e) || new Date(e).toISOString() !== r)
431
+ throw c(t);
432
+ return e;
433
+ }
434
+ function A(r) {
435
+ return Array.isArray(r) ? Object.freeze(r.map(A)) : r && typeof r == "object" ? Object.freeze(Object.fromEntries(
436
+ Object.entries(r).map(([t, e]) => [t, A(e)])
437
+ )) : r;
438
+ }
439
+ function X(r) {
440
+ const t = {
441
+ identity: r.identity === null ? null : A(r.identity),
442
+ status: r.status
443
+ };
444
+ return r.connectionExpiresAt !== void 0 && (t.connectionExpiresAt = r.connectionExpiresAt), r.error !== void 0 && (t.error = A(r.error)), A(t);
445
+ }
446
+ function L(r) {
447
+ return Object.freeze({ code: r.code, message: r.message });
448
+ }
449
+ function H(r) {
450
+ const t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
451
+ let e = 0, i = 0, s = "";
452
+ for (const o of r)
453
+ for (e = e << 8 | o, i += 8; i >= 6; )
454
+ i -= 6, s += t[e >>> i & 63], e &= (1 << i) - 1;
455
+ return i !== 0 && (s += t[e << 6 - i & 63]), s;
456
+ }
457
+ function Q(r) {
458
+ let t = "";
459
+ for (const e of r) t += e.toString(16).padStart(2, "0");
460
+ return t;
461
+ }
462
+ function V(r, t) {
463
+ const e = new Uint8Array(32);
464
+ return Reflect.apply(t, r, [e]), e;
465
+ }
466
+ function Bt(r) {
467
+ const t = r?.headers?.get?.("content-type");
468
+ return typeof t == "string" && /^application\/json(?:\s*;\s*charset=utf-8)?$/iu.test(t.trim());
469
+ }
470
+ function F(r) {
471
+ try {
472
+ Promise.resolve(r).catch(() => {
473
+ });
474
+ } catch {
475
+ }
476
+ }
477
+ function I(r) {
478
+ try {
479
+ const t = r?.body, e = t?.cancel;
480
+ typeof e == "function" && F(Reflect.apply(e, t, []));
481
+ } catch {
482
+ }
483
+ }
484
+ function ot(r) {
485
+ try {
486
+ const t = r?.cancel;
487
+ typeof t == "function" && F(Reflect.apply(t, r, []));
488
+ } catch {
489
+ }
490
+ }
491
+ function Jt(r) {
492
+ try {
493
+ const t = r?.releaseLock;
494
+ typeof t == "function" && Reflect.apply(t, r, []);
495
+ } catch {
496
+ }
497
+ }
498
+ function w(r, t, e = null) {
499
+ try {
500
+ r.abort();
501
+ } catch {
502
+ }
503
+ e && ot(e), I(t);
504
+ }
505
+ function D(r, t, e = null) {
506
+ let i;
507
+ try {
508
+ i = Promise.resolve(r);
509
+ } catch (s) {
510
+ return Promise.reject(s);
511
+ }
512
+ return new Promise((s, o) => {
513
+ let n = !1;
514
+ const l = () => {
515
+ try {
516
+ t.removeEventListener("abort", h);
517
+ } catch {
518
+ }
519
+ }, a = (f, d) => n ? !1 : (n = !0, l(), f(d), !0), h = () => {
520
+ a(o, c("RELAY_ERROR"));
521
+ };
522
+ try {
523
+ t.addEventListener("abort", h, { once: !0 }), t.aborted && h();
524
+ } catch {
525
+ h();
526
+ }
527
+ i.then(
528
+ (f) => {
529
+ if (n) {
530
+ try {
531
+ e?.(f);
532
+ } catch {
533
+ }
534
+ return;
535
+ }
536
+ a(s, f);
537
+ },
538
+ (f) => {
539
+ n || a(o, f);
540
+ }
541
+ );
542
+ });
543
+ }
544
+ async function Z(r, t, e) {
545
+ let i = null;
546
+ const s = [];
547
+ let o = 0;
548
+ try {
549
+ if (!Bt(r) || typeof r?.body?.getReader != "function")
550
+ throw c("RELAY_ERROR");
551
+ for (i = r.body.getReader(); ; ) {
552
+ const a = i.read(), { done: h, value: f } = await D(a, e.signal);
553
+ if (h) break;
554
+ if (!(f instanceof Uint8Array) || (o += f.byteLength, o > t))
555
+ throw c("RELAY_ERROR");
556
+ s.push(f);
557
+ }
558
+ const n = new Uint8Array(o);
559
+ let l = 0;
560
+ for (const a of s)
561
+ n.set(a, l), l += a.byteLength;
562
+ return Ut.decode(n);
563
+ } catch {
564
+ try {
565
+ e.abort();
566
+ } catch {
567
+ }
568
+ throw i && ot(i), c("RELAY_ERROR");
569
+ } finally {
570
+ i && Jt(i);
571
+ }
572
+ }
573
+ function x(r, t) {
574
+ const e = {
575
+ body: JSON.stringify(r),
576
+ cache: "no-store",
577
+ credentials: "omit",
578
+ headers: { "Content-Type": "application/json" },
579
+ method: "POST",
580
+ mode: "cors",
581
+ redirect: "error",
582
+ referrerPolicy: "no-referrer"
583
+ };
584
+ return t !== void 0 && (e.signal = t), e;
585
+ }
586
+ function Wt(r, t, e) {
587
+ let i;
588
+ try {
589
+ i = N(r, 2048), j(i, ["code", "expiresAt", "schemaVersion", "state"], "CALLBACK_ERROR");
590
+ } catch {
591
+ throw c("CALLBACK_ERROR");
592
+ }
593
+ if (i.schemaVersion !== 1 || i.state !== t || !_.test(i.code))
594
+ throw c("CALLBACK_ERROR");
595
+ const s = g(i.expiresAt, "CALLBACK_ERROR");
596
+ if (s <= e || s - e > Yt)
597
+ throw c("CALLBACK_ERROR");
598
+ return { code: i.code, expiresAt: s, state: i.state };
599
+ }
600
+ function qt() {
601
+ let r;
602
+ return { promise: new Promise((e) => {
603
+ r = e;
604
+ }), resolve: r };
605
+ }
606
+ function G() {
607
+ let r, t;
608
+ return { promise: new Promise((i, s) => {
609
+ r = s, t = i;
610
+ }), reject: r, resolve: t };
611
+ }
612
+ function Kt() {
613
+ const r = globalThis.window;
614
+ return Object.freeze({
615
+ AbortController: globalThis.AbortController,
616
+ clearInterval: globalThis.clearInterval.bind(globalThis),
617
+ clearTimeout: globalThis.clearTimeout.bind(globalThis),
618
+ crypto: globalThis.crypto,
619
+ fetch: typeof globalThis.fetch == "function" ? globalThis.fetch.bind(globalThis) : void 0,
620
+ getStorage: () => {
621
+ try {
622
+ return r?.localStorage ?? null;
623
+ } catch {
624
+ return null;
625
+ }
626
+ },
627
+ now: () => Date.now(),
628
+ setInterval: globalThis.setInterval.bind(globalThis),
629
+ setTimeout: globalThis.setTimeout.bind(globalThis),
630
+ window: r
631
+ });
632
+ }
633
+ class zt {
634
+ #k;
635
+ #M;
636
+ #C = !1;
637
+ #l = !1;
638
+ #Y = !1;
639
+ #n = null;
640
+ #w = null;
641
+ #b = /* @__PURE__ */ new Set();
642
+ #t;
643
+ #v = null;
644
+ #r = !1;
645
+ #R = 0;
646
+ #L = 0;
647
+ #_ = /* @__PURE__ */ new Set();
648
+ #P = [];
649
+ #o = null;
650
+ #x = !1;
651
+ #y = /* @__PURE__ */ new Set();
652
+ #V = Object.freeze({ identity: null, status: "dormant" });
653
+ #u = null;
654
+ #I = null;
655
+ #A = null;
656
+ #p = /* @__PURE__ */ new Set();
657
+ #m;
658
+ constructor({ adapters: t, clientId: e, dependencies: i }) {
659
+ this.#k = t, this.#M = e, this.#t = i ?? Kt(), this.#m = this.#t.window;
660
+ }
661
+ getSnapshot() {
662
+ return this.#F(), X(this.#V);
663
+ }
664
+ subscribe(t) {
665
+ if (typeof t != "function") throw c("INVALID_REQUEST");
666
+ if (this.#r) throw c("DESTROYED");
667
+ this.#_.add(t);
668
+ try {
669
+ t(this.getSnapshot());
670
+ } catch {
671
+ }
672
+ let e = !0;
673
+ return () => {
674
+ e && (e = !1, this.#_.delete(t));
675
+ };
676
+ }
677
+ execute(t, e) {
678
+ if (this.#r) return Promise.reject(c("DESTROYED"));
679
+ const i = this.#R, s = this.#k[t];
680
+ if (!s) return Promise.reject(c("INVALID_REQUEST"));
681
+ let o;
682
+ try {
683
+ o = s.buildRequest(e);
684
+ } catch {
685
+ const p = this.#e(i);
686
+ return p ? Promise.reject(p) : Promise.reject(c("INVALID_REQUEST"));
687
+ }
688
+ let n = this.#e(i);
689
+ if (n) return Promise.reject(n);
690
+ if (this.#C)
691
+ return this.#l = !0, Promise.reject(c("REPLACED"));
692
+ const l = ++this.#R;
693
+ if (this.#Q(), n = this.#e(l), n) return Promise.reject(n);
694
+ if (this.#l)
695
+ return Promise.reject(c("CALLBACK_ERROR"));
696
+ if (this.#F(), n = this.#e(l), n) return Promise.reject(n);
697
+ const a = this.#s();
698
+ if (n = this.#e(l), n) return Promise.reject(n);
699
+ const h = this.#n && this.#n.expiresAtMs > a ? this.#n : null;
700
+ if (n = this.#e(l), n) return Promise.reject(n);
701
+ const f = this.#o;
702
+ let d, y, m;
703
+ try {
704
+ const p = this.#t.crypto, E = p?.getRandomValues, at = p?.subtle?.digest, J = this.#m?.open;
705
+ if (n = this.#e(l), n) throw n;
706
+ if (typeof E != "function" || typeof at != "function" || typeof J != "function")
707
+ throw c("CRYPTO_UNAVAILABLE");
708
+ if (d = Q(V(p, E)), n = this.#e(l), n || (y = Q(V(p, E)), n = this.#e(l), n) || (m = H(V(p, E)), n = this.#e(l), n)) throw n;
709
+ if (!_.test(d) || !_.test(y) || !q.test(m))
710
+ throw c("CRYPTO_UNAVAILABLE");
711
+ if (Reflect.apply(J, this.#m, [
712
+ `${v}/transaction/${d}`,
713
+ "_blank",
714
+ "noopener"
715
+ ]), n = this.#e(l), n) throw n;
716
+ } catch (p) {
717
+ if (n = this.#e(l), n) return Promise.reject(n);
718
+ let E;
719
+ try {
720
+ E = p instanceof T ? p : c("CRYPTO_UNAVAILABLE");
721
+ } catch {
722
+ E = c("CRYPTO_UNAVAILABLE");
723
+ }
724
+ return f && (this.#T(f, "REPLACED", !1), n = this.#e(l), n) ? Promise.reject(n) : (this.#$(null, E, h, s.kind), Promise.reject(E));
725
+ }
726
+ if (f && (this.#T(f, "REPLACED", !1), n = this.#e(l), n))
727
+ return Promise.reject(n);
728
+ const b = G(), ct = qt(), B = {
729
+ active: !0,
730
+ adapter: s,
731
+ callbackBusy: !1,
732
+ challenge: null,
733
+ cleanup: ct,
734
+ cleanupFinished: !1,
735
+ cleanupStarted: !1,
736
+ controllers: /* @__PURE__ */ new Set(),
737
+ expiryTimer: null,
738
+ generation: ++this.#L,
739
+ operationTimer: null,
740
+ phase: "hashing",
741
+ pollTimer: null,
742
+ priorConnection: h,
743
+ promise: b.promise,
744
+ registrationDeadlineMs: a + Dt,
745
+ registrationOutcome: "none",
746
+ reject: b.reject,
747
+ request: o,
748
+ resolve: b.resolve,
749
+ retryResolve: null,
750
+ retryTimer: null,
751
+ state: y,
752
+ transactionExpiresAtMs: null,
753
+ transactionId: d,
754
+ verifier: m
755
+ };
756
+ return this.#o = B, this.#i({
757
+ ...h ? {
758
+ connectionExpiresAt: h.expiresAt,
759
+ identity: h.identity
760
+ } : { identity: null },
761
+ status: "opening"
762
+ }), this.#W(B), b.promise;
763
+ }
764
+ async disconnect() {
765
+ if (this.#r) throw c("DESTROYED");
766
+ const t = ++this.#R;
767
+ this.#f();
768
+ const e = this.#o;
769
+ let i = Promise.resolve();
770
+ e && (i = this.#T(e, "DISCONNECTED", !1));
771
+ let s = this.#e(t);
772
+ if (s)
773
+ throw await i, s;
774
+ if (this.#i({ identity: null, status: "dormant" }), await i, s = this.#e(t), s) throw s;
775
+ }
776
+ destroy() {
777
+ if (this.#v) return this.#v;
778
+ const t = G();
779
+ this.#v = t.promise, this.#R += 1;
780
+ try {
781
+ this.#J(), t.resolve();
782
+ } catch {
783
+ t.reject(c("RELAY_ERROR"));
784
+ }
785
+ return this.#v;
786
+ }
787
+ #J() {
788
+ this.#r = !0, this.#L += 1, this.#f();
789
+ const t = [], e = this.#o;
790
+ e && (e.active = !1, this.#o = null, this.#S(e), e.reject(c("DESTROYED")), e.challenge && e.verifier && t.push(e));
791
+ for (const i of this.#y)
792
+ i.challenge && i.verifier && t.push(i);
793
+ for (const i of this.#b) i.abort();
794
+ this.#b.clear();
795
+ for (const i of this.#p)
796
+ this.#t.clearTimeout(i), this.#t.clearInterval(i);
797
+ if (this.#p.clear(), this.#A)
798
+ try {
799
+ this.#A();
800
+ } catch {
801
+ }
802
+ this.#I = null, this.#A = null, this.#u = null, this.#_.clear();
803
+ for (const i of t) this.#H(i);
804
+ e && this.#a(e);
805
+ for (const i of [...this.#y]) this.#a(i);
806
+ this.#i({
807
+ error: L(c("DESTROYED")),
808
+ identity: null,
809
+ status: "error"
810
+ });
811
+ }
812
+ async #W(t) {
813
+ if (!(!this.#c(t) || t.cleanupFinished))
814
+ try {
815
+ t.operationTimer = this.#O(() => {
816
+ if (!t.cleanupFinished) {
817
+ if (t.phase === "hashing") {
818
+ this.#c(t) && this.#h(t, "CRYPTO_UNAVAILABLE", !1);
819
+ return;
820
+ }
821
+ if (t.phase === "registering")
822
+ for (const l of t.controllers) l.abort();
823
+ }
824
+ }, Math.max(0, t.registrationDeadlineMs - this.#s()));
825
+ const e = await this.#t.crypto.subtle.digest(
826
+ "SHA-256",
827
+ nt.encode(t.verifier)
828
+ ), i = H(new Uint8Array(e));
829
+ if (!q.test(i)) throw c("CRYPTO_UNAVAILABLE");
830
+ if (!this.#c(t)) {
831
+ t.registrationOutcome = "not-started", this.#a(t);
832
+ return;
833
+ }
834
+ t.challenge = i, t.phase = "registering";
835
+ const s = this.#j(t);
836
+ let o, n = null;
837
+ try {
838
+ try {
839
+ const a = this.#t.fetch(
840
+ `${v}/relay/v1/transactions`,
841
+ x({
842
+ clientId: this.#M,
843
+ codeChallenge: t.challenge,
844
+ codeChallengeMethod: "S256",
845
+ operation: t.adapter.operation,
846
+ request: t.request,
847
+ schemaVersion: 1,
848
+ state: t.state,
849
+ transactionId: t.transactionId
850
+ }, s.signal)
851
+ );
852
+ n = await D(
853
+ a,
854
+ s.signal,
855
+ I
856
+ );
857
+ } catch {
858
+ t.registrationOutcome = "ambiguous", t.active ? this.#h(t, "RELAY_ERROR") : this.#E(t);
859
+ return;
860
+ }
861
+ t.registrationOutcome = "ambiguous";
862
+ let l;
863
+ try {
864
+ l = n.status;
865
+ } catch {
866
+ w(s, n), t.active ? this.#h(t, "RELAY_ERROR") : this.#E(t);
867
+ return;
868
+ }
869
+ if (l !== 201) {
870
+ t.registrationOutcome = "definitive-failure", w(s, n), t.active ? this.#h(t, "RELAY_ERROR", !1) : this.#a(t);
871
+ return;
872
+ }
873
+ try {
874
+ const a = await Z(
875
+ n,
876
+ K,
877
+ s
878
+ ), h = j(
879
+ N(a, K),
880
+ ["expiresAt", "schemaVersion", "transactionId"]
881
+ );
882
+ if (h.schemaVersion !== 1 || h.transactionId !== t.transactionId)
883
+ throw c("RELAY_ERROR");
884
+ o = g(h.expiresAt);
885
+ const f = this.#s();
886
+ if (o <= f || o - f > kt)
887
+ throw c("RELAY_ERROR");
888
+ } catch {
889
+ t.registrationOutcome = "ambiguous", w(s, n), t.active ? this.#h(t, "RELAY_ERROR") : this.#E(t);
890
+ return;
891
+ }
892
+ } finally {
893
+ try {
894
+ s.abort();
895
+ } catch {
896
+ }
897
+ this.#g(t, s), this.#d(t.operationTimer), t.operationTimer = null;
898
+ }
899
+ if (t.transactionExpiresAtMs = o, t.registrationOutcome = "registered", !this.#c(t)) {
900
+ this.#E(t);
901
+ return;
902
+ }
903
+ t.phase = "waiting-callback", t.expiryTimer = this.#O(() => {
904
+ this.#c(t) && this.#h(t, "WALLET_NOT_COMPLETED");
905
+ }, Math.max(0, t.transactionExpiresAtMs - this.#s())), t.pollTimer = this.#X(() => {
906
+ this.#N(t, !1);
907
+ }, gt), await this.#N(t, !1);
908
+ } catch (e) {
909
+ if (!t.active) {
910
+ t.registrationOutcome === "ambiguous" || t.registrationOutcome === "registered" ? this.#E(t) : this.#a(t);
911
+ return;
912
+ }
913
+ const i = e instanceof T ? e.code : t.phase === "hashing" ? "CRYPTO_UNAVAILABLE" : "RELAY_ERROR";
914
+ this.#h(t, i, t.registrationOutcome !== "none");
915
+ }
916
+ }
917
+ async #N(t, e) {
918
+ if (!this.#c(t) || t.phase !== "waiting-callback" || t.callbackBusy || !this.#u) return;
919
+ const i = `${P}${t.state}`;
920
+ let s;
921
+ try {
922
+ s = this.#u.getItem(i);
923
+ } catch {
924
+ e && this.#h(t, "CALLBACK_ERROR");
925
+ return;
926
+ }
927
+ if (s === null) return;
928
+ t.callbackBusy = !0;
929
+ let o;
930
+ try {
931
+ o = Wt(s, t.state, this.#s()), this.#u.removeItem(i);
932
+ } catch {
933
+ try {
934
+ this.#u.removeItem(i);
935
+ } catch {
936
+ }
937
+ this.#h(t, "CALLBACK_ERROR");
938
+ return;
939
+ }
940
+ if (!this.#c(t)) return;
941
+ t.phase = "redeeming", this.#d(t.pollTimer, !0), t.pollTimer = null;
942
+ const n = this.#j(t);
943
+ let l = null;
944
+ try {
945
+ const a = this.#t.fetch(
946
+ `${v}/relay/v1/codes/redeem`,
947
+ x({
948
+ code: o.code,
949
+ codeVerifier: t.verifier,
950
+ schemaVersion: 1,
951
+ state: t.state,
952
+ transactionId: t.transactionId
953
+ }, n.signal)
954
+ );
955
+ if (l = await D(
956
+ a,
957
+ n.signal,
958
+ I
959
+ ), l.status !== 200) throw c("RELAY_ERROR");
960
+ const h = await Z(
961
+ l,
962
+ z,
963
+ n
964
+ ), f = j(
965
+ N(h, z),
966
+ ["result", "schemaVersion", "transactionId"]
967
+ );
968
+ if (f.schemaVersion !== 1 || f.transactionId !== t.transactionId)
969
+ throw c("RELAY_ERROR");
970
+ let d;
971
+ try {
972
+ d = t.adapter.parseResult(f.result);
973
+ } catch {
974
+ throw c("RELAY_ERROR");
975
+ }
976
+ if (!this.#c(t)) return;
977
+ this.#q(t, d);
978
+ } catch (a) {
979
+ if (l && w(n, l), !this.#c(t)) return;
980
+ const h = a instanceof T ? a.code : "RELAY_ERROR";
981
+ this.#h(t, h);
982
+ } finally {
983
+ try {
984
+ n.abort();
985
+ } catch {
986
+ }
987
+ this.#g(t, n);
988
+ }
989
+ }
990
+ #q(t, e) {
991
+ if (!this.#c(t)) return;
992
+ let i = null;
993
+ (t.adapter.kind === "connect" || t.adapter.kind === "account" && e.event === "connected") && (i = this.#K(e)), t.active = !1, this.#o = null, this.#S(t);
994
+ let s = e;
995
+ t.adapter.kind === "connect" ? (this.#U(i), s = A(e.identity)) : t.adapter.kind === "account" ? (e.event === "connected" ? this.#U(i) : (this.#f(), this.#i({ identity: null, status: "dormant" })), s = void 0) : this.#z(t, void 0), t.resolve(s), this.#a(t);
996
+ }
997
+ #h(t, e, i = !0) {
998
+ this.#c(t) && this.#T(t, e, !0, i);
999
+ }
1000
+ #T(t, e, i, s = !0) {
1001
+ if (!t.active) return t.cleanup.promise;
1002
+ const o = this.#R;
1003
+ t.active = !1, this.#o === t && (this.#o = null), this.#L += 1, this.#S(t);
1004
+ for (const a of t.controllers)
1005
+ t.phase !== "registering" && a.abort();
1006
+ const n = this.#e(o), l = n ?? c(e);
1007
+ return t.reject(l), i && !n && this.#$(t, l, t.priorConnection, t.adapter.kind), this.#r ? (t.challenge && t.verifier && this.#B(t), this.#a(t), t.cleanup.promise) : (!s || t.registrationOutcome === "definitive-failure" || t.registrationOutcome === "not-started" ? this.#a(t) : t.phase === "hashing" ? (t.registrationOutcome = "not-started", this.#a(t)) : t.phase === "registering" && t.registrationOutcome === "none" ? this.#D(t) : (this.#D(t), this.#E(t)), t.cleanup.promise);
1008
+ }
1009
+ #$(t, e, i, s) {
1010
+ if (i?.expiresAtMs > this.#s()) {
1011
+ this.#n = i, this.#i({
1012
+ connectionExpiresAt: i.expiresAt,
1013
+ error: L(e),
1014
+ identity: i.identity,
1015
+ status: "connected"
1016
+ });
1017
+ return;
1018
+ }
1019
+ this.#f(), this.#i({
1020
+ error: L(e),
1021
+ identity: null,
1022
+ status: "error"
1023
+ });
1024
+ }
1025
+ #K(t) {
1026
+ const e = g(t.connectionExpiresAt), i = this.#s();
1027
+ if (e <= i || e - i > Mt)
1028
+ throw c("RELAY_ERROR");
1029
+ return {
1030
+ expiresAt: t.connectionExpiresAt,
1031
+ expiresAtMs: e,
1032
+ identity: A(t.identity)
1033
+ };
1034
+ }
1035
+ #U(t) {
1036
+ const e = this.#s();
1037
+ this.#f(), this.#n = t;
1038
+ const i = this.#n;
1039
+ this.#w = this.#O(() => {
1040
+ this.#n === i && (this.#n = null, this.#w = null, this.#o ? this.#i({ identity: null, status: "opening" }) : this.#i({ identity: null, status: "dormant" }));
1041
+ }, Math.max(0, t.expiresAtMs - e)), this.#i({
1042
+ connectionExpiresAt: t.expiresAt,
1043
+ identity: this.#n.identity,
1044
+ status: "connected"
1045
+ });
1046
+ }
1047
+ #z(t, e) {
1048
+ const i = t.priorConnection;
1049
+ i?.expiresAtMs > this.#s() ? (this.#n = i, this.#i({
1050
+ connectionExpiresAt: i.expiresAt,
1051
+ ...e ? { error: L(e) } : {},
1052
+ identity: i.identity,
1053
+ status: "connected"
1054
+ })) : e ? (this.#f(), this.#i({ error: L(e), identity: null, status: "error" })) : (this.#f(), this.#i({ identity: null, status: "dormant" }));
1055
+ }
1056
+ #F() {
1057
+ !this.#n || this.#n.expiresAtMs > this.#s() || (this.#f(), this.#o ? this.#i({ identity: null, status: "opening" }) : this.#i({ identity: null, status: "dormant" }));
1058
+ }
1059
+ #f() {
1060
+ this.#n = null, this.#d(this.#w), this.#w = null;
1061
+ }
1062
+ #i(t) {
1063
+ const e = A(t);
1064
+ if (this.#V = e, this.#P.push(e), !this.#x) {
1065
+ this.#x = !0;
1066
+ try {
1067
+ for (; this.#P.length !== 0; ) {
1068
+ const i = this.#P.shift();
1069
+ for (const s of [...this.#_])
1070
+ try {
1071
+ s(X(i));
1072
+ } catch {
1073
+ }
1074
+ }
1075
+ } finally {
1076
+ this.#x = !1;
1077
+ }
1078
+ }
1079
+ }
1080
+ #c(t) {
1081
+ return !this.#r && t.active && this.#o === t && t.generation === this.#L;
1082
+ }
1083
+ #e(t) {
1084
+ return this.#r ? c("DESTROYED") : t !== this.#R ? c("REPLACED") : null;
1085
+ }
1086
+ #s() {
1087
+ return this.#t.now();
1088
+ }
1089
+ #j(t) {
1090
+ const e = new this.#t.AbortController();
1091
+ return t.controllers.add(e), this.#b.add(e), e;
1092
+ }
1093
+ #g(t, e) {
1094
+ t.controllers.delete(e), this.#b.delete(e);
1095
+ }
1096
+ #O(t, e) {
1097
+ let i;
1098
+ return i = this.#t.setTimeout(() => {
1099
+ this.#p.delete(i), t();
1100
+ }, Math.max(0, e)), this.#p.add(i), i;
1101
+ }
1102
+ #X(t, e) {
1103
+ const i = this.#t.setInterval(t, e);
1104
+ return this.#p.add(i), i;
1105
+ }
1106
+ #d(t, e = !1) {
1107
+ t != null && (e ? this.#t.clearInterval(t) : this.#t.clearTimeout(t), this.#p.delete(t));
1108
+ }
1109
+ #S(t) {
1110
+ this.#d(t.expiryTimer), this.#d(t.pollTimer, !0), t.expiryTimer = null, t.pollTimer = null;
1111
+ }
1112
+ #D(t) {
1113
+ if (!(t.cleanupFinished || this.#y.has(t)))
1114
+ for (this.#y.add(t); this.#y.size > Vt; ) {
1115
+ const e = this.#y.values().next().value;
1116
+ for (const i of e.controllers) i.abort();
1117
+ this.#a(e);
1118
+ }
1119
+ }
1120
+ async #E(t) {
1121
+ if (t.cleanupFinished || t.cleanupStarted || this.#r)
1122
+ return t.cleanup.promise;
1123
+ t.cleanupStarted = !0, this.#D(t);
1124
+ let e = 100, i = !0;
1125
+ for (; !this.#r && !t.cleanupFinished && (i || this.#s() <= t.registrationDeadlineMs); ) {
1126
+ i = !1;
1127
+ const s = await this.#B(t);
1128
+ if (t.cleanupFinished) return t.cleanup.promise;
1129
+ if (s === "done")
1130
+ return this.#a(t), t.cleanup.promise;
1131
+ const o = t.registrationDeadlineMs - this.#s();
1132
+ if (o <= 0) break;
1133
+ await new Promise((n) => {
1134
+ t.retryResolve = n, t.retryTimer = this.#O(() => {
1135
+ t.retryResolve = null, n();
1136
+ }, Math.min(e, o));
1137
+ }), t.retryResolve = null, t.retryTimer = null, e = Math.min(e * 2, 4e3);
1138
+ }
1139
+ return this.#a(t), t.cleanup.promise;
1140
+ }
1141
+ async #B(t) {
1142
+ if (!t.verifier || !t.challenge) return "done";
1143
+ const e = this.#j(t), i = t.registrationDeadlineMs - this.#s(), s = i > 0 ? Math.min(4e3, i) : 1e3, o = this.#O(() => e.abort(), Math.max(1, s));
1144
+ let n = null;
1145
+ try {
1146
+ const l = this.#t.fetch(
1147
+ `${v}/relay/v1/transactions/${t.transactionId}/cancel`,
1148
+ x({
1149
+ codeVerifier: t.verifier,
1150
+ schemaVersion: 1,
1151
+ state: t.state,
1152
+ transactionId: t.transactionId
1153
+ }, e.signal)
1154
+ );
1155
+ n = await D(
1156
+ l,
1157
+ e.signal,
1158
+ I
1159
+ );
1160
+ const a = n.status;
1161
+ return w(e, n), n = null, a === 204 || a === 400 || a === 403 || a === 409 || a === 410 ? "done" : "retry";
1162
+ } catch {
1163
+ return n && w(e, n), "retry";
1164
+ } finally {
1165
+ try {
1166
+ e.abort();
1167
+ } catch {
1168
+ }
1169
+ this.#d(o), this.#g(t, e);
1170
+ }
1171
+ }
1172
+ #H(t) {
1173
+ let e;
1174
+ try {
1175
+ e = this.#t.fetch(
1176
+ `${v}/relay/v1/transactions/${t.transactionId}/cancel`,
1177
+ {
1178
+ ...x({
1179
+ codeVerifier: t.verifier,
1180
+ schemaVersion: 1,
1181
+ state: t.state,
1182
+ transactionId: t.transactionId
1183
+ }),
1184
+ // Let the browser finish the tiny revocation after local teardown or unload.
1185
+ keepalive: !0
1186
+ }
1187
+ );
1188
+ } catch {
1189
+ return;
1190
+ }
1191
+ F(Promise.resolve(e).then(I));
1192
+ }
1193
+ #a(t) {
1194
+ if (!t.cleanupFinished) {
1195
+ t.cleanupFinished = !0, this.#d(t.retryTimer), t.retryResolve?.(), t.retryResolve = null, t.retryTimer = null, this.#d(t.operationTimer), t.operationTimer = null, this.#S(t);
1196
+ for (const e of t.controllers)
1197
+ e.abort(), this.#b.delete(e);
1198
+ t.controllers.clear(), this.#y.delete(t), t.adapter = null, t.challenge = null, t.priorConnection = null, t.request = null, t.state = null, t.transactionExpiresAtMs = null, t.transactionId = null, t.verifier = null, t.cleanup.resolve();
1199
+ }
1200
+ }
1201
+ #Q() {
1202
+ if (!(this.#Y || this.#r)) {
1203
+ this.#Y = !0, this.#C = !0;
1204
+ try {
1205
+ let t = null;
1206
+ try {
1207
+ const o = this.#t.getStorage;
1208
+ t = typeof o == "function" ? Reflect.apply(o, void 0, []) : this.#t.storage ?? null;
1209
+ } catch {
1210
+ t = null, this.#l = !0;
1211
+ }
1212
+ try {
1213
+ (!t || typeof t.getItem != "function" || typeof t.removeItem != "function") && (this.#l = !0);
1214
+ } catch {
1215
+ this.#l = !0;
1216
+ }
1217
+ if (this.#r || this.#l) {
1218
+ this.#u = null;
1219
+ return;
1220
+ }
1221
+ if (this.#u = t, this.#Z(), this.#r || this.#l) {
1222
+ this.#u = null;
1223
+ return;
1224
+ }
1225
+ const e = (o) => {
1226
+ const n = this.#o;
1227
+ if (!n || !this.#c(n)) return;
1228
+ const l = `${P}${n.state}`;
1229
+ o?.key === l && this.#N(n, !0);
1230
+ };
1231
+ this.#I = e;
1232
+ let i = !1, s = null;
1233
+ try {
1234
+ const o = this.#m?.addEventListener, n = this.#m?.removeEventListener;
1235
+ if (typeof o != "function" || typeof n != "function")
1236
+ throw new TypeError("storage events unavailable");
1237
+ s = () => Reflect.apply(
1238
+ n,
1239
+ this.#m,
1240
+ ["storage", e]
1241
+ ), this.#A = s, Reflect.apply(o, this.#m, ["storage", this.#I]), i = !0;
1242
+ } catch {
1243
+ this.#l = !0;
1244
+ }
1245
+ if (!i || this.#r || this.#l) {
1246
+ try {
1247
+ s?.();
1248
+ } catch {
1249
+ }
1250
+ this.#I = null, this.#A = null, this.#u = null;
1251
+ }
1252
+ } finally {
1253
+ this.#C = !1;
1254
+ }
1255
+ }
1256
+ }
1257
+ #Z() {
1258
+ const t = this.#u;
1259
+ if (!t) return;
1260
+ let e;
1261
+ try {
1262
+ e = t.length;
1263
+ } catch {
1264
+ return;
1265
+ }
1266
+ if (!Number.isSafeInteger(e) || e < 0) return;
1267
+ const i = Math.min(e, $t), s = [];
1268
+ for (let o = 0; o < i && !this.#r && !this.#l; o += 1) {
1269
+ let n;
1270
+ try {
1271
+ n = t.key(o);
1272
+ } catch {
1273
+ continue;
1274
+ }
1275
+ s.push(n);
1276
+ }
1277
+ for (const o of s) {
1278
+ if (this.#r || this.#l) return;
1279
+ if (typeof o != "string" || !o.startsWith(P)) continue;
1280
+ const n = o.slice(P.length);
1281
+ if (_.test(n))
1282
+ try {
1283
+ const l = t.getItem(o);
1284
+ if (l === null) continue;
1285
+ const a = j(
1286
+ N(l, 2048),
1287
+ ["code", "expiresAt", "schemaVersion", "state"],
1288
+ "CALLBACK_ERROR"
1289
+ );
1290
+ if (a.schemaVersion !== 1 || a.state !== n || !_.test(a.code))
1291
+ continue;
1292
+ g(a.expiresAt, "CALLBACK_ERROR") <= this.#s() && t.removeItem(o);
1293
+ } catch {
1294
+ }
1295
+ }
1296
+ }
1297
+ }
1298
+ function Xt(r) {
1299
+ return new zt(r);
1300
+ }
1301
+ function Ht(r, t = {}) {
1302
+ const e = (s, o) => s !== 0 ? Promise.reject(c("INVALID_REQUEST")) : o(), i = {
1303
+ connect(...s) {
1304
+ return e(s.length, () => r.execute("connect", void 0));
1305
+ },
1306
+ destroy(...s) {
1307
+ return e(s.length, () => r.destroy());
1308
+ },
1309
+ disconnect(...s) {
1310
+ return e(s.length, () => r.disconnect());
1311
+ },
1312
+ getSnapshot(...s) {
1313
+ if (s.length !== 0) throw c("INVALID_REQUEST");
1314
+ return r.getSnapshot();
1315
+ },
1316
+ openAccount(...s) {
1317
+ return e(s.length, () => r.execute("account", void 0));
1318
+ },
1319
+ subscribe(s, ...o) {
1320
+ if (o.length !== 0) throw c("INVALID_REQUEST");
1321
+ return r.subscribe(s);
1322
+ }
1323
+ };
1324
+ for (const [s, o] of Object.entries(t))
1325
+ i[s] = function(l, ...a) {
1326
+ return a.length !== 0 ? Promise.reject(c("INVALID_REQUEST")) : r.execute(o, l);
1327
+ };
1328
+ return Object.freeze(i);
1329
+ }
1330
+ const Qt = Object.freeze([
1331
+ "orbpro-pages-v1",
1332
+ "sdn-asset-models-pages-v1",
1333
+ "sdn-asset-review-v1",
1334
+ "sdn-flatbuffers-pages-v1",
1335
+ "sdn-flatsql-pages-v1",
1336
+ "sdn-landing-web-v1",
1337
+ "sdn-module-sdk-pages-v1",
1338
+ "sdn-node-console-v1",
1339
+ "sdn-standards-web-v1",
1340
+ "spaceaware-web-v1"
1341
+ ]);
1342
+ function Zt(r) {
1343
+ let t;
1344
+ try {
1345
+ const e = Reflect.ownKeys(r);
1346
+ if (r === null || typeof r != "object" || Array.isArray(r) || Object.getPrototypeOf(r) !== Object.prototype || e.length !== 1 || e[0] !== "clientId")
1347
+ throw c("INVALID_REQUEST");
1348
+ t = Object.getOwnPropertyDescriptor(r, "clientId");
1349
+ } catch {
1350
+ throw c("INVALID_REQUEST");
1351
+ }
1352
+ if (!t?.enumerable || !("value" in t) || typeof t.value != "string")
1353
+ throw c("INVALID_REQUEST");
1354
+ if (!Qt.includes(t.value)) throw c("INVALID_CLIENT");
1355
+ return t.value;
1356
+ }
1357
+ const Gt = Object.freeze({
1358
+ account: Object.freeze({
1359
+ buildRequest: () => xt({}),
1360
+ kind: "account",
1361
+ operation: "sdn.wallet.account.v1",
1362
+ parseResult: Nt
1363
+ }),
1364
+ connect: Object.freeze({
1365
+ buildRequest: () => Ct({}),
1366
+ kind: "connect",
1367
+ operation: "sdn.wallet.connect.v1",
1368
+ parseResult: Pt
1369
+ })
1370
+ });
1371
+ function te(r) {
1372
+ if (arguments.length !== 1) throw c("INVALID_REQUEST");
1373
+ const t = Zt(r), e = Xt({ adapters: Gt, clientId: t });
1374
+ return Ht(e);
1375
+ }
1376
+ export {
1377
+ st as WALLET_CLIENT_ERRORS,
1378
+ te as createWalletClient
1379
+ };