@aurum-sdk/core 0.2.8 → 0.3.0

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 (35) hide show
  1. package/dist/{Aurum-B0Okb4Qv.d.mts → Aurum-BoIhHFKm.d.mts} +25 -2
  2. package/dist/{Aurum-B0Okb4Qv.d.ts → Aurum-BoIhHFKm.d.ts} +25 -2
  3. package/dist/BraveAdapter-ALBZR2RG.js +153 -0
  4. package/dist/BraveAdapter-BPD5PDJH.mjs +153 -0
  5. package/dist/CoinbaseWalletAdapter-BLUJR23D.js +144 -0
  6. package/dist/CoinbaseWalletAdapter-IDB2DFH4.mjs +144 -0
  7. package/dist/EmailAdapter-RUV6JG6Z.mjs +212 -0
  8. package/dist/EmailAdapter-SY65VHGK.js +212 -0
  9. package/dist/MetaMaskAdapter-4V3XM6J2.js +152 -0
  10. package/dist/MetaMaskAdapter-KPNRCMOF.mjs +152 -0
  11. package/dist/PhantomAdapter-7MB32F5M.mjs +151 -0
  12. package/dist/PhantomAdapter-YTN5PUDI.js +151 -0
  13. package/dist/RabbyAdapter-QVNCSWYC.mjs +146 -0
  14. package/dist/RabbyAdapter-SOLYF74M.js +146 -0
  15. package/dist/WalletConnectAdapter-5YL5KFGI.mjs +540 -0
  16. package/dist/WalletConnectAdapter-VMN6R5LO.js +540 -0
  17. package/dist/{chunk-GMFEFDGS.mjs → chunk-222UJ3OP.mjs} +348 -6778
  18. package/dist/chunk-4S5E6KOY.mjs +21 -0
  19. package/dist/{chunk-K64T6WW3.js → chunk-MRORL6L3.js} +420 -6850
  20. package/dist/chunk-PFK6YHOX.mjs +15 -0
  21. package/dist/chunk-QOCN4F47.js +21 -0
  22. package/dist/chunk-RRUPCUSZ.mjs +6558 -0
  23. package/dist/chunk-XUD6VNF7.js +6558 -0
  24. package/dist/chunk-XVRTCAR4.js +15 -0
  25. package/dist/index.d.mts +7 -2
  26. package/dist/index.d.ts +7 -2
  27. package/dist/index.js +304 -1573
  28. package/dist/index.mjs +243 -1512
  29. package/dist/{index.web-Q2L3EMC3.mjs → index.web-NX7JZBIY.mjs} +267 -267
  30. package/dist/{index.web-V7DLWC7H.js → index.web-OPNRZHYZ.js} +267 -267
  31. package/dist/widgets.d.mts +1 -1
  32. package/dist/widgets.d.ts +1 -1
  33. package/dist/widgets.js +14 -12
  34. package/dist/widgets.mjs +3 -1
  35. package/package.json +3 -7
@@ -0,0 +1,146 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+
4
+
5
+
6
+ var _chunkXUD6VNF7js = require('./chunk-XUD6VNF7.js');
7
+
8
+
9
+ var _chunkQIPVNM7Tjs = require('./chunk-QIPVNM7T.js');
10
+
11
+ // src/wallet-adapters/RabbyAdapter.ts
12
+ _chunkQIPVNM7Tjs.init_polyfills.call(void 0, );
13
+ var RABBY_RDNS = "io.rabby";
14
+ var RabbyAdapter = class {
15
+ constructor() {
16
+ this.id = _chunkXUD6VNF7js.WalletId.Rabby;
17
+ this.name = _chunkXUD6VNF7js.WalletName.Rabby;
18
+ this.icon = _nullishCoalesce(_chunkXUD6VNF7js.getLogoDataUri.call(void 0, _chunkXUD6VNF7js.WalletId.Rabby, "brand"), () => ( ""));
19
+ this.hide = false;
20
+ this.downloadUrl = "https://rabby.io";
21
+ this.wcDeepLinkUrl = null;
22
+ this.provider = null;
23
+ this.accountsChangedCallback = null;
24
+ this.providerPromise = null;
25
+ this.providerPromise = this.discoverProvider();
26
+ }
27
+ /**
28
+ * Uses EIP-6963 to discover the Rabby provider by its RDNS identifier.
29
+ * Falls back to window.ethereum for legacy detection.
30
+ */
31
+ discoverProvider() {
32
+ if (typeof window === "undefined") return Promise.resolve(null);
33
+ return new Promise((resolve) => {
34
+ let resolved = false;
35
+ const onAnnouncement = (event) => {
36
+ const { detail } = event;
37
+ if (detail.info.rdns === RABBY_RDNS) {
38
+ resolved = true;
39
+ this.provider = detail.provider;
40
+ window.removeEventListener("eip6963:announceProvider", onAnnouncement);
41
+ resolve(detail.provider);
42
+ }
43
+ };
44
+ window.addEventListener("eip6963:announceProvider", onAnnouncement);
45
+ window.dispatchEvent(new Event("eip6963:requestProvider"));
46
+ setTimeout(() => {
47
+ if (!resolved) {
48
+ window.removeEventListener("eip6963:announceProvider", onAnnouncement);
49
+ const legacyProvider = this.detectLegacyProvider();
50
+ if (legacyProvider) {
51
+ this.provider = legacyProvider;
52
+ }
53
+ resolve(legacyProvider);
54
+ }
55
+ }, 100);
56
+ });
57
+ }
58
+ /**
59
+ * Fallback detection for legacy Rabby installations.
60
+ * Checks window.ethereum for Rabby-specific flags.
61
+ */
62
+ detectLegacyProvider() {
63
+ const ethereum = window.ethereum;
64
+ if (_optionalChain([ethereum, 'optionalAccess', _ => _.isRabby])) {
65
+ return ethereum;
66
+ }
67
+ return null;
68
+ }
69
+ isInstalled() {
70
+ return Boolean(_nullishCoalesce(this.provider, () => ( this.detectLegacyProvider())));
71
+ }
72
+ async connect() {
73
+ if (!this.provider && this.providerPromise) {
74
+ await this.providerPromise;
75
+ }
76
+ if (!this.provider) {
77
+ _chunkXUD6VNF7js.sentryLogger.error("Rabby is not available");
78
+ throw new Error("Rabby is not available");
79
+ }
80
+ await this.provider.request({
81
+ method: "wallet_requestPermissions",
82
+ params: [{ eth_accounts: {} }]
83
+ });
84
+ const accounts = await this.provider.request({
85
+ method: "eth_requestAccounts",
86
+ params: []
87
+ });
88
+ if (!accounts || accounts.length === 0 || !accounts[0]) {
89
+ _chunkXUD6VNF7js.sentryLogger.error("No accounts returned from Rabby");
90
+ throw new Error("No accounts returned from Rabby");
91
+ }
92
+ return {
93
+ address: accounts[0],
94
+ provider: this.provider,
95
+ walletId: this.id
96
+ };
97
+ }
98
+ async tryRestoreConnection() {
99
+ if (!this.provider && this.providerPromise) {
100
+ await this.providerPromise;
101
+ }
102
+ if (!this.provider) {
103
+ return null;
104
+ }
105
+ try {
106
+ const accounts = await this.provider.request({
107
+ method: "eth_accounts",
108
+ params: []
109
+ });
110
+ if (!accounts || accounts.length === 0 || !accounts[0]) {
111
+ return null;
112
+ }
113
+ return {
114
+ address: accounts[0],
115
+ provider: this.provider,
116
+ walletId: this.id
117
+ };
118
+ } catch (e) {
119
+ return null;
120
+ }
121
+ }
122
+ async disconnect() {
123
+ }
124
+ getProvider() {
125
+ return this.provider;
126
+ }
127
+ // Called by Aurum when user connects wallet
128
+ // Passes Aurum.ts --> syncStateFromAccountsChanged() to handle the provider accounts changed event
129
+ onAccountsChanged(callback) {
130
+ if (!_optionalChain([this, 'access', _2 => _2.provider, 'optionalAccess', _3 => _3.on])) return;
131
+ if (this.accountsChangedCallback) {
132
+ _optionalChain([this, 'access', _4 => _4.provider, 'access', _5 => _5.removeListener, 'optionalCall', _6 => _6("accountsChanged", this.accountsChangedCallback)]);
133
+ }
134
+ this.accountsChangedCallback = callback;
135
+ this.provider.on("accountsChanged", this.accountsChangedCallback);
136
+ }
137
+ removeListeners() {
138
+ if (!_optionalChain([this, 'access', _7 => _7.provider, 'optionalAccess', _8 => _8.removeListener]) || !this.accountsChangedCallback) return;
139
+ this.provider.removeListener("accountsChanged", this.accountsChangedCallback);
140
+ this.accountsChangedCallback = null;
141
+ }
142
+ };
143
+
144
+
145
+ exports.RabbyAdapter = RabbyAdapter;
146
+ //# sourceMappingURL=RabbyAdapter-SOLYF74M.js.map
@@ -0,0 +1,540 @@
1
+ import {
2
+ createConfigError
3
+ } from "./chunk-4S5E6KOY.mjs";
4
+ import {
5
+ WalletId,
6
+ WalletName,
7
+ getLogoDataUri,
8
+ sentryLogger
9
+ } from "./chunk-RRUPCUSZ.mjs";
10
+ import {
11
+ init_polyfills
12
+ } from "./chunk-J6XFKNJN.mjs";
13
+
14
+ // src/wallet-adapters/WalletConnectAdapter.ts
15
+ init_polyfills();
16
+
17
+ // src/constants/adapters.ts
18
+ init_polyfills();
19
+ var WALLETCONNECT_NAMESPACE = {
20
+ eip155: {
21
+ methods: [
22
+ // Transaction methods
23
+ "eth_sendTransaction",
24
+ "eth_signTransaction",
25
+ "eth_sendRawTransaction",
26
+ // Signing methods
27
+ "eth_sign",
28
+ "personal_sign",
29
+ "eth_signTypedData",
30
+ "eth_signTypedData_v3",
31
+ "eth_signTypedData_v4",
32
+ // Account methods
33
+ "eth_accounts",
34
+ "eth_requestAccounts",
35
+ // Chain management
36
+ "wallet_switchEthereumChain",
37
+ "wallet_addEthereumChain",
38
+ // Permissions (EIP-2255)
39
+ "wallet_requestPermissions",
40
+ "wallet_getPermissions"
41
+ ],
42
+ chains: [
43
+ // Ethereum
44
+ "eip155:1",
45
+ // Mainnet
46
+ "eip155:11155111",
47
+ // Sepolia
48
+ // Base
49
+ "eip155:8453",
50
+ // Mainnet
51
+ "eip155:84532",
52
+ // Sepolia
53
+ // Optimism
54
+ "eip155:10",
55
+ // Mainnet
56
+ "eip155:11155420",
57
+ // Sepolia
58
+ // Arbitrum
59
+ "eip155:42161",
60
+ // Mainnet
61
+ "eip155:421614",
62
+ // Sepolia
63
+ // BNB Chain
64
+ "eip155:56",
65
+ // Mainnet
66
+ "eip155:97",
67
+ // Testnet
68
+ // Polygon
69
+ "eip155:137",
70
+ // Mainnet
71
+ "eip155:80002",
72
+ // Amoy
73
+ // Fantom
74
+ "eip155:250",
75
+ // Mainnet
76
+ "eip155:4002",
77
+ // Testnet
78
+ // Linea
79
+ "eip155:59144",
80
+ // Mainnet
81
+ "eip155:59141",
82
+ // Sepolia
83
+ // Gnosis
84
+ "eip155:100",
85
+ // Mainnet
86
+ "eip155:10200",
87
+ // Chiado
88
+ // Polygon zkEVM
89
+ "eip155:1101",
90
+ // Mainnet
91
+ "eip155:2442",
92
+ // Cardona
93
+ // Avalanche C-Chain
94
+ "eip155:43114",
95
+ // Mainnet
96
+ "eip155:43113"
97
+ // Fuji
98
+ ],
99
+ events: ["chainChanged", "accountsChanged"]
100
+ }
101
+ };
102
+
103
+ // src/wallet-adapters/WalletConnectAdapter.ts
104
+ function extractAddressFromSession(namespaces) {
105
+ const accounts = namespaces?.eip155?.accounts || [];
106
+ const firstAccount = accounts[0];
107
+ return firstAccount?.split(":")[2] ?? null;
108
+ }
109
+ var WalletConnectAdapter = class {
110
+ constructor(config) {
111
+ this.id = WalletId.WalletConnect;
112
+ this.name = WalletName.WalletConnect;
113
+ this.icon = getLogoDataUri(WalletId.WalletConnect, "brand") ?? "";
114
+ this.hide = false;
115
+ this.downloadUrl = null;
116
+ this.wcDeepLinkUrl = null;
117
+ this.modal = null;
118
+ this.wagmiAdapter = null;
119
+ this.universalProvider = null;
120
+ this.provider = null;
121
+ this.address = null;
122
+ this.connectionUri = null;
123
+ this.accountsChangedCallback = null;
124
+ this.chainChangedCallback = null;
125
+ this.disconnectCallback = null;
126
+ this.sessionUpdateHandler = null;
127
+ this.unsubscribeFunctions = [];
128
+ this.initPromise = null;
129
+ this.lastKnownAccounts = [];
130
+ this.lastKnownChainId = null;
131
+ this.config = config;
132
+ }
133
+ async ensureInitialized() {
134
+ if (this.modal) return;
135
+ if (!this.initPromise) {
136
+ this.initPromise = this.initializeAppKit();
137
+ }
138
+ await this.initPromise;
139
+ }
140
+ async initializeAppKit() {
141
+ if (typeof window === "undefined") return;
142
+ const [{ createAppKit }, { WagmiAdapter }, { mainnet }] = await Promise.all([
143
+ import("@reown/appkit"),
144
+ import("@reown/appkit-adapter-wagmi"),
145
+ import("@reown/appkit/networks")
146
+ ]);
147
+ const networks = [mainnet];
148
+ this.wagmiAdapter = new WagmiAdapter({
149
+ projectId: this.config.projectId,
150
+ networks,
151
+ ssr: true
152
+ });
153
+ this.modal = createAppKit({
154
+ adapters: [this.wagmiAdapter],
155
+ networks,
156
+ projectId: this.config.projectId,
157
+ metadata: {
158
+ name: this.config.appName,
159
+ description: this.config.appName,
160
+ url: window.location.origin,
161
+ icons: []
162
+ },
163
+ allowUnsupportedChain: true,
164
+ themeMode: this.config.theme,
165
+ themeVariables: {
166
+ "--apkt-z-index": this.config.modalZIndex + 1
167
+ },
168
+ features: {
169
+ analytics: this.config.telemetry ?? false
170
+ }
171
+ });
172
+ this.universalProvider = await this.modal.getUniversalProvider() ?? null;
173
+ this.setupEventListeners();
174
+ }
175
+ setupEventListeners() {
176
+ if (!this.modal) return;
177
+ const unsubscribeProviders = this.modal.subscribeProviders((state) => {
178
+ const eip155Provider = state["eip155"];
179
+ this.provider = eip155Provider || null;
180
+ if (!eip155Provider) {
181
+ this.address = null;
182
+ }
183
+ });
184
+ this.unsubscribeFunctions.push(unsubscribeProviders);
185
+ if (this.universalProvider) {
186
+ this.universalProvider.on("display_uri", (uri) => {
187
+ this.connectionUri = uri;
188
+ if (typeof window !== "undefined") {
189
+ window.dispatchEvent(new CustomEvent("walletconnect:uri", { detail: { uri } }));
190
+ }
191
+ });
192
+ this.universalProvider.on("session_delete", () => {
193
+ this.handleRemoteDisconnect();
194
+ });
195
+ this.universalProvider.on("session_expire", () => {
196
+ this.handleRemoteDisconnect();
197
+ });
198
+ }
199
+ }
200
+ /** Called when user disconnects from the wallet side (mobile app) */
201
+ handleRemoteDisconnect() {
202
+ this.connectionUri = null;
203
+ this.address = null;
204
+ this.provider = null;
205
+ this.lastKnownAccounts = [];
206
+ this.lastKnownChainId = null;
207
+ if (this.accountsChangedCallback) {
208
+ this.accountsChangedCallback([]);
209
+ }
210
+ if (this.disconnectCallback) {
211
+ this.disconnectCallback();
212
+ }
213
+ if (typeof window !== "undefined") {
214
+ window.dispatchEvent(new CustomEvent("walletconnect:disconnect"));
215
+ }
216
+ }
217
+ syncAddressFromWagmi() {
218
+ if (!this.wagmiAdapter?.wagmiConfig) return;
219
+ const { state } = this.wagmiAdapter.wagmiConfig;
220
+ if (state.current && state.connections) {
221
+ const connection = state.connections.get(state.current);
222
+ if (connection?.accounts?.[0]) {
223
+ this.address = connection.accounts[0];
224
+ }
225
+ }
226
+ }
227
+ async syncProviderFromModal() {
228
+ if (!this.modal) return;
229
+ try {
230
+ const getProvidersFn = this.modal.getProviders;
231
+ if (typeof getProvidersFn === "function") {
232
+ const providers = getProvidersFn.call(this.modal);
233
+ const eip155Provider = providers?.["eip155"];
234
+ if (eip155Provider) {
235
+ this.provider = eip155Provider;
236
+ return;
237
+ }
238
+ }
239
+ if (this.wagmiAdapter?.wagmiConfig) {
240
+ const { state } = this.wagmiAdapter.wagmiConfig;
241
+ if (state.current && state.connections) {
242
+ const connection = state.connections.get(state.current);
243
+ const connector = connection?.connector;
244
+ if (connector && typeof connector.getProvider === "function") {
245
+ try {
246
+ const provider = await connector.getProvider();
247
+ if (provider) {
248
+ this.provider = provider;
249
+ }
250
+ } catch (error) {
251
+ sentryLogger.warn("Failed to get provider from wagmi connector", { error });
252
+ }
253
+ }
254
+ }
255
+ }
256
+ } catch (error) {
257
+ sentryLogger.warn("Failed to get provider from WalletConnect", { error });
258
+ }
259
+ }
260
+ isInstalled() {
261
+ return true;
262
+ }
263
+ getConnectionUri() {
264
+ return this.connectionUri;
265
+ }
266
+ /** Resets connection state for a fresh connection flow */
267
+ async resetConnectionState() {
268
+ this.connectionUri = null;
269
+ this.address = null;
270
+ this.lastKnownAccounts = [];
271
+ this.lastKnownChainId = null;
272
+ try {
273
+ const { state } = this.wagmiAdapter?.wagmiConfig || {};
274
+ const connection = state?.current && state.connections?.get(state.current);
275
+ if (connection && typeof connection !== "string" && connection.connector?.disconnect) {
276
+ await connection.connector.disconnect();
277
+ }
278
+ } catch {
279
+ }
280
+ if (this.modal?.getIsConnectedState()) {
281
+ try {
282
+ await this.modal.disconnect("eip155");
283
+ } catch {
284
+ }
285
+ }
286
+ if (this.universalProvider?.session) {
287
+ try {
288
+ await this.universalProvider.disconnect();
289
+ } catch {
290
+ }
291
+ }
292
+ await new Promise((r) => setTimeout(r, 200));
293
+ this.provider = null;
294
+ }
295
+ /**
296
+ * Connects via WalletConnect QR code flow.
297
+ * Emits walletconnect:uri event for QR code display.
298
+ */
299
+ async connect() {
300
+ if (!this.config.projectId) throw createConfigError("WalletConnect");
301
+ await this.ensureInitialized();
302
+ if (!this.universalProvider) throw new Error("WalletConnect is not available");
303
+ await this.resetConnectionState();
304
+ try {
305
+ const session = await this.universalProvider.connect({ namespaces: WALLETCONNECT_NAMESPACE });
306
+ if (!session) throw new Error("Failed to establish WalletConnect session");
307
+ const address = extractAddressFromSession(session.namespaces);
308
+ if (!address) throw new Error("No accounts returned from WalletConnect");
309
+ this.provider = this.universalProvider;
310
+ this.address = address;
311
+ this.lastKnownAccounts = [address];
312
+ return { address, provider: this.provider, walletId: this.id };
313
+ } catch (error) {
314
+ this.connectionUri = null;
315
+ throw error;
316
+ }
317
+ }
318
+ /**
319
+ * Starts a WalletConnect session for headless/custom QR code flows.
320
+ * Returns the URI immediately and a function to wait for the connection.
321
+ */
322
+ async startSession(timeout = 1e4) {
323
+ if (!this.config.projectId) throw new Error("WalletConnect projectId is required");
324
+ await this.ensureInitialized();
325
+ if (!this.universalProvider) throw new Error("WalletConnect is not available");
326
+ await this.resetConnectionState();
327
+ const uriPromise = new Promise((resolve, reject) => {
328
+ const timeoutId = setTimeout(() => reject(new Error("Timeout waiting for WalletConnect URI")), timeout);
329
+ this.universalProvider.once("display_uri", (uri2) => {
330
+ clearTimeout(timeoutId);
331
+ this.connectionUri = uri2;
332
+ resolve(uri2);
333
+ });
334
+ });
335
+ const connectionPromise = (async () => {
336
+ const session = await this.universalProvider.connect({ namespaces: WALLETCONNECT_NAMESPACE });
337
+ if (!session) throw new Error("Failed to establish WalletConnect session");
338
+ const address = extractAddressFromSession(session.namespaces);
339
+ if (!address) throw new Error("No accounts returned from WalletConnect");
340
+ this.provider = this.universalProvider;
341
+ this.address = address;
342
+ this.lastKnownAccounts = [address];
343
+ return { address, provider: this.provider, walletId: this.id };
344
+ })();
345
+ const uri = await uriPromise;
346
+ return {
347
+ uri,
348
+ waitForConnection: async () => {
349
+ try {
350
+ return await connectionPromise;
351
+ } catch (error) {
352
+ this.connectionUri = null;
353
+ throw error;
354
+ }
355
+ }
356
+ };
357
+ }
358
+ /**
359
+ * Opens the AppKit modal for wallet selection.
360
+ * Used on mobile and when user clicks "Open Modal" button.
361
+ */
362
+ async openModal() {
363
+ if (!this.config.projectId) throw createConfigError("WalletConnect");
364
+ await this.ensureInitialized();
365
+ if (!this.modal) throw new Error("AppKit is not available");
366
+ await this.resetConnectionState();
367
+ this.modal.open({ view: "AllWallets" });
368
+ return this.waitForModalConnection();
369
+ }
370
+ waitForModalConnection(timeout = 6e4) {
371
+ return new Promise((resolve, reject) => {
372
+ const startTime = Date.now();
373
+ let unsubscribe = null;
374
+ let resolved = false;
375
+ const cleanup = () => unsubscribe?.();
376
+ const checkConnection = async () => {
377
+ if (resolved) return true;
378
+ this.syncAddressFromWagmi();
379
+ if (this.address && !this.provider) await this.syncProviderFromModal();
380
+ if (this.provider && this.address) {
381
+ try {
382
+ const accounts = await this.provider.request({ method: "eth_accounts" });
383
+ if (accounts?.length) {
384
+ resolved = true;
385
+ cleanup();
386
+ this.modal?.close();
387
+ this.lastKnownAccounts = accounts;
388
+ resolve({ address: this.address, provider: this.provider, walletId: this.id });
389
+ return true;
390
+ }
391
+ } catch {
392
+ }
393
+ }
394
+ return false;
395
+ };
396
+ unsubscribe = this.modal.subscribeState(async (state) => {
397
+ if (await checkConnection()) return;
398
+ if (!state.open && !this.address && !resolved) {
399
+ cleanup();
400
+ reject(new Error("Connection rejected by user"));
401
+ }
402
+ });
403
+ const poll = async () => {
404
+ if (await checkConnection()) return;
405
+ if (Date.now() - startTime > timeout) {
406
+ cleanup();
407
+ reject(new Error("Connection timeout"));
408
+ return;
409
+ }
410
+ setTimeout(poll, 500);
411
+ };
412
+ poll();
413
+ });
414
+ }
415
+ async tryRestoreConnection() {
416
+ await this.ensureInitialized();
417
+ if (!this.wagmiAdapter) return null;
418
+ try {
419
+ await new Promise((r) => setTimeout(r, 1e3));
420
+ const { state } = this.wagmiAdapter.wagmiConfig || {};
421
+ const connection = state?.current && state.connections?.get(state.current);
422
+ if (!connection || typeof connection === "string") return null;
423
+ const address = connection.accounts?.[0];
424
+ if (address && this.provider) {
425
+ this.address = address;
426
+ this.lastKnownAccounts = [...connection.accounts || []];
427
+ return { address, provider: this.provider, walletId: this.id };
428
+ }
429
+ return null;
430
+ } catch {
431
+ return null;
432
+ }
433
+ }
434
+ async disconnect() {
435
+ if (this.modal) {
436
+ await this.modal.disconnect("eip155");
437
+ const deadline = Date.now() + 2e3;
438
+ while (Date.now() < deadline && (this.modal.getIsConnectedState() || this.modal.getAddress())) {
439
+ await new Promise((r) => setTimeout(r, 100));
440
+ }
441
+ }
442
+ this.address = null;
443
+ this.provider = null;
444
+ this.connectionUri = null;
445
+ this.lastKnownAccounts = [];
446
+ this.lastKnownChainId = null;
447
+ }
448
+ getProvider() {
449
+ return this.provider;
450
+ }
451
+ onAccountsChanged(callback) {
452
+ if (this.accountsChangedCallback && this.provider?.removeListener) {
453
+ this.provider.removeListener("accountsChanged", this.accountsChangedCallback);
454
+ }
455
+ if (this.sessionUpdateHandler && this.universalProvider?.removeListener) {
456
+ this.universalProvider.removeListener("session_update", this.sessionUpdateHandler);
457
+ }
458
+ if (!this.lastKnownAccounts.length && this.address) this.lastKnownAccounts = [this.address];
459
+ this.accountsChangedCallback = (accounts) => {
460
+ this.address = accounts[0] || null;
461
+ this.lastKnownAccounts = accounts;
462
+ callback(accounts);
463
+ };
464
+ if (this.provider?.on) {
465
+ this.provider.on("accountsChanged", this.accountsChangedCallback);
466
+ }
467
+ if (this.universalProvider?.on) {
468
+ this.sessionUpdateHandler = (args) => {
469
+ const accounts = this.extractAccountsFromNamespaces(args?.params?.namespaces);
470
+ if (accounts.length && this.hasAccountsChanged(accounts)) {
471
+ this.lastKnownAccounts = accounts;
472
+ this.address = accounts[0];
473
+ callback(accounts);
474
+ }
475
+ };
476
+ this.universalProvider.on("session_update", this.sessionUpdateHandler);
477
+ }
478
+ }
479
+ onChainChanged(callback) {
480
+ if (!this.provider?.on) return;
481
+ if (this.chainChangedCallback) this.provider.removeListener?.("chainChanged", this.chainChangedCallback);
482
+ this.chainChangedCallback = (chainId) => {
483
+ const normalized = typeof chainId === "string" ? chainId : `0x${Number(chainId).toString(16)}`;
484
+ if (normalized !== this.lastKnownChainId) {
485
+ this.lastKnownChainId = normalized;
486
+ callback(normalized);
487
+ }
488
+ };
489
+ this.provider.on("chainChanged", this.chainChangedCallback);
490
+ }
491
+ /** Called when remote wallet disconnects (user disconnects from mobile app) */
492
+ onDisconnect(callback) {
493
+ this.disconnectCallback = callback;
494
+ }
495
+ /** Updates the AppKit modal theme */
496
+ updateTheme(theme) {
497
+ this.config.theme = theme;
498
+ if (this.modal && typeof this.modal.setThemeMode === "function") {
499
+ this.modal.setThemeMode(theme);
500
+ }
501
+ }
502
+ extractAccountsFromNamespaces(namespaces) {
503
+ if (!namespaces) return [];
504
+ const accounts = [];
505
+ Object.values(namespaces).forEach((ns) => {
506
+ ns?.accounts?.forEach((account) => {
507
+ const address = account.split(":")[2];
508
+ if (address) accounts.push(address);
509
+ });
510
+ });
511
+ return [...new Set(accounts)];
512
+ }
513
+ hasAccountsChanged(newAccounts) {
514
+ if (newAccounts.length !== this.lastKnownAccounts.length) return true;
515
+ return newAccounts.some((acc, i) => acc.toLowerCase() !== this.lastKnownAccounts[i]?.toLowerCase());
516
+ }
517
+ removeListeners() {
518
+ if (this.accountsChangedCallback) {
519
+ this.provider?.removeListener?.("accountsChanged", this.accountsChangedCallback);
520
+ this.accountsChangedCallback = null;
521
+ }
522
+ if (this.chainChangedCallback) {
523
+ this.provider?.removeListener?.("chainChanged", this.chainChangedCallback);
524
+ this.chainChangedCallback = null;
525
+ }
526
+ if (this.sessionUpdateHandler) {
527
+ this.universalProvider?.removeListener?.("session_update", this.sessionUpdateHandler);
528
+ this.sessionUpdateHandler = null;
529
+ }
530
+ this.disconnectCallback = null;
531
+ this.unsubscribeFunctions.forEach((unsub) => unsub());
532
+ this.unsubscribeFunctions = [];
533
+ this.lastKnownAccounts = [];
534
+ this.lastKnownChainId = null;
535
+ }
536
+ };
537
+ export {
538
+ WalletConnectAdapter
539
+ };
540
+ //# sourceMappingURL=WalletConnectAdapter-5YL5KFGI.mjs.map