@lukso/up-connector 0.5.0 → 0.5.1-dev.e515cef

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 (34) hide show
  1. package/dist/auto-setup.cjs +3 -3
  2. package/dist/auto-setup.js +2 -2
  3. package/dist/{chunk-PB7QP3SJ.cjs → chunk-GCYDV7FB.cjs} +159 -91
  4. package/dist/chunk-GCYDV7FB.cjs.map +1 -0
  5. package/dist/{chunk-LCF5SJOE.js → chunk-KMCCANMJ.js} +4 -4
  6. package/dist/{chunk-LCF5SJOE.js.map → chunk-KMCCANMJ.js.map} +1 -1
  7. package/dist/{chunk-4PC7ZTI7.js → chunk-SAQWNAQ6.js} +138 -70
  8. package/dist/chunk-SAQWNAQ6.js.map +1 -0
  9. package/dist/{chunk-QRZDP5RR.cjs → chunk-W7QI6BTA.cjs} +3 -3
  10. package/dist/{chunk-QRZDP5RR.cjs.map → chunk-W7QI6BTA.cjs.map} +1 -1
  11. package/dist/connect-modal/index.cjs +4 -2
  12. package/dist/connect-modal/index.cjs.map +1 -1
  13. package/dist/connect-modal/index.d.cts +1 -1
  14. package/dist/connect-modal/index.d.ts +1 -1
  15. package/dist/connect-modal/index.js +11 -9
  16. package/dist/index.cjs +24 -16
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +2 -2
  19. package/dist/index.d.ts +2 -2
  20. package/dist/index.js +31 -23
  21. package/dist/index.js.map +1 -1
  22. package/dist/{wagmi-BwAJkYMM.d.cts → wagmi-DgjkdmGk.d.cts} +77 -21
  23. package/dist/{wagmi-BwAJkYMM.d.ts → wagmi-DgjkdmGk.d.ts} +77 -21
  24. package/package.json +5 -5
  25. package/src/connect-modal/components/connection-view.ts +4 -1
  26. package/src/connect-modal/components/eoa-connection-view.ts +4 -0
  27. package/src/connect-modal/connect-modal.types.ts +4 -0
  28. package/src/connect-modal/index.ts +6 -4
  29. package/src/connect-modal/services/wagmi.ts +176 -65
  30. package/src/connect-modal/utils/chainParams.ts +32 -0
  31. package/src/connector.ts +18 -12
  32. package/src/index.ts +6 -4
  33. package/dist/chunk-4PC7ZTI7.js.map +0 -1
  34. package/dist/chunk-PB7QP3SJ.cjs.map +0 -1
@@ -1,20 +1,111 @@
1
1
  /**
2
- * Wagmi Connector Adapter
3
- * Converts wagmi/viem connectors to our WalletConnector format
2
+ * Wagmi Service
3
+ * Centralized service for wagmi interactions with proxy pattern
4
4
  */
5
5
 
6
+ import { getChainById } from '@lukso/core/chains'
6
7
  import { slug } from '@lukso/core/utils'
8
+ import debug from 'debug'
7
9
  import type {
8
10
  ConnectModalSetup,
9
11
  WagmiConnector,
10
12
  } from '../connect-modal.types.js'
11
13
  import type { WalletConnector } from '../index.js'
14
+ import { constructAddEthereumChainParameter } from '../utils/chainParams'
15
+
16
+ const logInfo = debug('connect-modal:info')
12
17
 
13
18
  // UP Extension ID
14
19
  const UP_EXTENSION_ID = 'cloud.universalprofile'
15
20
 
16
- // Global wagmi setup storage
17
- let globalWagmiSetup: ConnectModalSetup | null = null
21
+ /**
22
+ * Wagmi connection state from getConnection/watchConnection
23
+ */
24
+ export type WagmiConnection = {
25
+ address?: string
26
+ addresses?: readonly string[]
27
+ chainId?: number
28
+ chain?: any
29
+ connector?: any
30
+ status: 'connecting' | 'reconnecting' | 'connected' | 'disconnected'
31
+ isConnecting?: boolean
32
+ isReconnecting?: boolean
33
+ isConnected?: boolean
34
+ isDisconnected?: boolean
35
+ }
36
+
37
+ /**
38
+ * Wagmi Service Class
39
+ * Provides centralized access to wagmi core functions
40
+ */
41
+ class WagmiService {
42
+ private setup: ConnectModalSetup | null = null
43
+ private wagmiCore: any = null
44
+
45
+ configure(setup: ConnectModalSetup): void {
46
+ this.setup = setup
47
+ }
48
+
49
+ getSetup(): ConnectModalSetup | null {
50
+ return this.setup
51
+ }
52
+
53
+ /**
54
+ * Ensure wagmi/core is loaded and return initialized state
55
+ * Returns { core, config } if ready, null otherwise
56
+ */
57
+ private async init() {
58
+ if (!this.setup) return null
59
+ if (!this.wagmiCore) {
60
+ try {
61
+ this.wagmiCore = await import('@wagmi/core')
62
+ } catch {
63
+ return null
64
+ }
65
+ }
66
+ return { core: this.wagmiCore, config: this.setup.wagmiConfig }
67
+ }
68
+
69
+ async getConnection() {
70
+ const wagmi = await this.init()
71
+ if (!wagmi) return null
72
+
73
+ try {
74
+ return wagmi.core.getConnection(wagmi.config)
75
+ } catch {
76
+ return null
77
+ }
78
+ }
79
+
80
+ async watchConnection(
81
+ callback: (connection: WagmiConnection) => void
82
+ ): Promise<(() => void) | null> {
83
+ const wagmi = await this.init()
84
+ if (!wagmi) return null
85
+
86
+ try {
87
+ return wagmi.core.watchConnection(wagmi.config, { onChange: callback })
88
+ } catch {
89
+ return null
90
+ }
91
+ }
92
+
93
+ async disconnect(): Promise<boolean> {
94
+ const wagmi = await this.init()
95
+ if (!wagmi) return false
96
+
97
+ try {
98
+ await wagmi.core.disconnect(wagmi.config)
99
+ return true
100
+ } catch (error) {
101
+ console.warn('Failed to disconnect from wagmi', error)
102
+ return false
103
+ }
104
+ }
105
+ }
106
+
107
+ // Singleton instance
108
+ const wagmiService = new WagmiService()
18
109
 
19
110
  /**
20
111
  * Convert a wagmi connector to our WalletConnector format
@@ -63,6 +154,39 @@ export function fromWagmiConnector(
63
154
  getProvider: connector.getProvider
64
155
  ? () => connector.getProvider()
65
156
  : undefined,
157
+ switchChain: connector.switchChain
158
+ ? async (params: {
159
+ chainId?: number
160
+ addEthereumChainParameter?: any
161
+ }) => {
162
+ if (!params.chainId) {
163
+ console.warn('No chainId provided for switchChain, skipping')
164
+ return
165
+ }
166
+
167
+ // Get the chain configuration based on chainId
168
+ // If addEthereumChainParameter is not provided, construct it from chain definition
169
+ let addEthereumChainParameter = params.addEthereumChainParameter
170
+
171
+ if (!addEthereumChainParameter) {
172
+ const chain = getChainById(params.chainId)
173
+
174
+ if (chain) {
175
+ addEthereumChainParameter =
176
+ constructAddEthereumChainParameter(chain)
177
+ }
178
+ }
179
+
180
+ await connector.switchChain({
181
+ chainId: params.chainId,
182
+ addEthereumChainParameter,
183
+ })
184
+
185
+ logInfo(
186
+ `Switched to chainId ${params.chainId} via connector "${connector.name}"`
187
+ )
188
+ }
189
+ : undefined,
66
190
  }
67
191
  }
68
192
 
@@ -169,10 +293,10 @@ export function fromWagmiConnectors(
169
293
  *
170
294
  * @example
171
295
  * ```typescript
172
- * import { setupConnectModal } from '@lukso/up-connector'
296
+ * import { setupWagmi } from '@lukso/up-connector'
173
297
  * import { wagmiConfig } from './wagmi-config'
174
298
  *
175
- * setupConnectModal({
299
+ * setupWagmi({
176
300
  * wagmiConfig,
177
301
  * chainId: 42, // Optional: LUKSO mainnet
178
302
  * embeddedWalletConnect: async () => {
@@ -181,86 +305,73 @@ export function fromWagmiConnectors(
181
305
  * })
182
306
  * ```
183
307
  */
184
- export function setupConnectModal(setup: ConnectModalSetup): void {
185
- globalWagmiSetup = setup
308
+ export function setupWagmi(setup: ConnectModalSetup): void {
309
+ wagmiService.configure(setup)
186
310
  }
187
311
 
188
312
  /**
189
313
  * Get the current wagmi setup (for internal use by connector)
190
314
  */
191
315
  export function getWagmiSetup(): ConnectModalSetup | null {
192
- return globalWagmiSetup
316
+ return wagmiService.getSetup()
193
317
  }
194
318
 
195
319
  /**
196
- * Get current wagmi account state
320
+ * Get current wagmi connection state
197
321
  * Returns null if wagmi is not set up
322
+ *
323
+ * @example
324
+ * ```typescript
325
+ * import { getConnection } from '@lukso/up-connector'
326
+ *
327
+ * const connection = await getConnection()
328
+ * if (connection?.status === 'connected') {
329
+ * console.log(connection.address, connection.chainId)
330
+ * }
331
+ * ```
198
332
  */
199
- export async function getWagmiAccount(): Promise<{
200
- isConnected: boolean
201
- address?: string
202
- chainId?: number
203
- connector?: any
204
- } | null> {
205
- if (!globalWagmiSetup) {
206
- return null
207
- }
208
-
209
- try {
210
- const { getAccount } = await import('@wagmi/core')
211
- const account = getAccount(globalWagmiSetup.wagmiConfig)
212
- return account
213
- } catch (_error) {
214
- return null
215
- }
333
+ export async function getConnection() {
334
+ return wagmiService.getConnection()
216
335
  }
217
336
 
218
337
  /**
219
- * Subscribe to wagmi account changes
338
+ * Watch for wagmi connection changes
220
339
  * Returns unsubscribe function
340
+ *
341
+ * @example
342
+ * ```typescript
343
+ * import { watchConnection } from '@lukso/up-connector'
344
+ *
345
+ * const unwatch = await watchConnection((connection) => {
346
+ * console.log('Connection changed:', connection.status, connection.address)
347
+ * })
348
+ *
349
+ * // Later: unwatch()
350
+ * ```
221
351
  */
222
- export async function watchWagmiAccount(
223
- callback: (account: {
224
- isConnected: boolean
225
- address?: string
226
- chainId?: number
227
- connector?: any
228
- }) => void
352
+ export async function watchConnection(
353
+ callback: (connection: WagmiConnection) => void
229
354
  ): Promise<(() => void) | null> {
230
- if (!globalWagmiSetup) {
231
- return null
232
- }
233
-
234
- try {
235
- const { watchAccount } = await import('@wagmi/core')
236
-
237
- const unsubscribe = watchAccount(globalWagmiSetup.wagmiConfig, {
238
- onChange: (account) => {
239
- callback(account)
240
- },
241
- })
242
-
243
- return unsubscribe
244
- } catch (_error) {
245
- return null
246
- }
355
+ return wagmiService.watchConnection(callback)
247
356
  }
248
357
 
249
358
  /**
250
359
  * Disconnect from wagmi
251
360
  * Returns true if disconnect was successful
361
+ *
362
+ * @example
363
+ * ```typescript
364
+ * import { disconnect } from '@lukso/up-connector'
365
+ *
366
+ * await disconnect()
367
+ * ```
252
368
  */
253
- export async function disconnectWagmi(): Promise<boolean> {
254
- if (!globalWagmiSetup) {
255
- return false
256
- }
257
-
258
- try {
259
- const { disconnect } = await import('@wagmi/core')
260
- await disconnect(globalWagmiSetup.wagmiConfig)
261
- return true
262
- } catch (error) {
263
- console.warn('Failed to disconnect from wagmi', error)
264
- return false
265
- }
369
+ export async function disconnect(): Promise<boolean> {
370
+ return wagmiService.disconnect()
266
371
  }
372
+
373
+ /**
374
+ * Export the wagmi service for advanced usage
375
+ * @deprecated Use named exports (getConnection, watchConnection, disconnect) instead
376
+ */
377
+ export { wagmiService as wagmi }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Chain Parameter Utilities
3
+ *
4
+ * Utilities to construct EIP-3085 addEthereumChain parameters from chain definitions
5
+ */
6
+
7
+ import type { ChainExtended } from '@lukso/core/chains'
8
+ import type { AddEthereumChainParameter } from 'viem'
9
+
10
+ /**
11
+ * Construct addEthereumChainParameter from a ChainExtended definition
12
+ * Following EIP-3085 format for wallet_addEthereumChain
13
+ *
14
+ * @param chain - Extended chain configuration
15
+ * @returns Parameters for wallet_addEthereumChain (excluding chainId which is passed separately)
16
+ */
17
+ export function constructAddEthereumChainParameter(
18
+ chain: ChainExtended
19
+ ): Omit<AddEthereumChainParameter, 'chainId'> {
20
+ return {
21
+ chainName: chain.name,
22
+ nativeCurrency: {
23
+ name: chain.nativeCurrency.name,
24
+ symbol: chain.nativeCurrency.symbol,
25
+ decimals: chain.nativeCurrency.decimals,
26
+ },
27
+ rpcUrls: [...chain.rpcUrls.default.http],
28
+ blockExplorerUrls: chain.blockExplorers?.default.url
29
+ ? [chain.blockExplorers.default.url]
30
+ : undefined,
31
+ }
32
+ }
package/src/connector.ts CHANGED
@@ -27,9 +27,9 @@ import { setLuksoConfig } from '@lukso/transaction-view-headless'
27
27
  import type { AccountModal } from './account-modal.js'
28
28
  import type { ConnectModal, WalletConnector } from './connect-modal/index.js'
29
29
  import {
30
- disconnectWagmi,
31
- getWagmiAccount,
32
- watchWagmiAccount,
30
+ disconnect,
31
+ getConnection,
32
+ watchConnection,
33
33
  } from './connect-modal/index.js'
34
34
  import type {
35
35
  ConnectionState,
@@ -116,22 +116,28 @@ export class UPConnector {
116
116
  private async startWagmiSync(): Promise<void> {
117
117
  try {
118
118
  // Check initial state first
119
- const account = await getWagmiAccount()
119
+ const connection = await getConnection()
120
120
 
121
- if (!account) {
121
+ if (!connection) {
122
122
  return
123
123
  }
124
124
 
125
- if (account.isConnected && account.address) {
125
+ if (connection.status === 'connected' && connection.address) {
126
126
  this.handleConnection(
127
- account.address,
128
- account.connector,
129
- account.chainId
127
+ connection.address,
128
+ connection.connector,
129
+ connection.chainId
130
130
  )
131
131
  }
132
132
 
133
- // Subscribe to wagmi account changes
134
- this.wagmiUnsubscribe = await watchWagmiAccount((account) => {
133
+ // Subscribe to wagmi connection changes
134
+ this.wagmiUnsubscribe = await watchConnection((connection) => {
135
+ const account = {
136
+ isConnected: connection.status === 'connected',
137
+ address: connection.address,
138
+ chainId: connection.chainId,
139
+ connector: connection.connector,
140
+ }
135
141
  if (account.isConnected && account.address) {
136
142
  // Connected
137
143
  if (
@@ -295,7 +301,7 @@ export class UPConnector {
295
301
 
296
302
  private async handleDisconnection(): Promise<void> {
297
303
  // Disconnect from wagmi first to ensure state is clean
298
- await disconnectWagmi()
304
+ await disconnect()
299
305
 
300
306
  this.connectedAddress = undefined
301
307
  this.connectedProvider = undefined
package/src/index.ts CHANGED
@@ -21,15 +21,17 @@ export type {
21
21
  ConnectionModalView,
22
22
  ConnectModalSetup,
23
23
  ConnectModalTheme,
24
+ WagmiConnection,
24
25
  WalletConnector,
25
26
  } from './connect-modal/index.js'
26
27
  export {
27
28
  ConnectModal,
28
- disconnectWagmi,
29
- getWagmiAccount,
29
+ disconnect,
30
+ getConnection,
30
31
  getWagmiSetup,
31
32
  setupConnectModal,
32
- watchWagmiAccount,
33
+ wagmi,
34
+ watchConnection,
33
35
  } from './connect-modal/index.js'
34
36
  // Wagmi Adapter
35
37
  export {
@@ -61,4 +63,4 @@ export type {
61
63
  WalletProvider,
62
64
  } from './types.js'
63
65
  // Release update
64
- // build 2
66
+ // build 1