diff --git a/.changeset/brave-countries-tie.md b/.changeset/brave-countries-tie.md new file mode 100644 index 0000000000..082536f7c3 --- /dev/null +++ b/.changeset/brave-countries-tie.md @@ -0,0 +1,24 @@ +--- +'@reown/appkit-adapter-solana': patch +'@reown/appkit-scaffold-ui': patch +'@reown/appkit': patch +'@reown/appkit-core': patch +'@reown/appkit-siwx': patch +'@apps/demo': patch +'@apps/gallery': patch +'@apps/laboratory': patch +'@reown/appkit-adapter-ethers': patch +'@reown/appkit-adapter-ethers5': patch +'@reown/appkit-adapter-polkadot': patch +'@reown/appkit-adapter-wagmi': patch +'@reown/appkit-utils': patch +'@reown/appkit-cdn': patch +'@reown/appkit-common': patch +'@reown/appkit-experimental': patch +'@reown/appkit-polyfills': patch +'@reown/appkit-siwe': patch +'@reown/appkit-ui': patch +'@reown/appkit-wallet': patch +--- + +Fixes for improving SIWX initialization and flows diff --git a/packages/appkit/package.json b/packages/appkit/package.json index 9c40bcd5a7..c9c8bc56b8 100644 --- a/packages/appkit/package.json +++ b/packages/appkit/package.json @@ -98,8 +98,9 @@ "@reown/appkit-utils": "workspace:*", "@reown/appkit-wallet": "workspace:*", "@walletconnect/types": "2.17.0", - "@walletconnect/utils": "2.17.0", "@walletconnect/universal-provider": "2.17.0", + "@walletconnect/utils": "2.17.0", + "bs58": "6.0.0", "valtio": "1.11.2", "viem": "2.x" }, diff --git a/packages/appkit/src/universal-adapter/client.ts b/packages/appkit/src/universal-adapter/client.ts index ccff9be1a0..af7eb83d04 100644 --- a/packages/appkit/src/universal-adapter/client.ts +++ b/packages/appkit/src/universal-adapter/client.ts @@ -25,6 +25,7 @@ import { } from '@reown/appkit-common' import { ProviderUtil } from '../store/index.js' import type { AppKitOptions, AppKitOptionsWithCaipNetworks } from '../utils/TypesUtil.js' +import bs58 from 'bs58' type Metadata = { name: string @@ -246,12 +247,35 @@ export class UniversalAdapterClient { throw new Error('connectionControllerClient:signMessage - provider is undefined') } - const signature = await provider.request({ - method: 'personal_sign', - params: [message, address] - }) + let signature = '' + + if ( + ChainController.state.activeCaipNetwork?.chainNamespace === + CommonConstantsUtil.CHAIN.SOLANA + ) { + const response = await provider.request( + { + method: 'solana_signMessage', + params: { + message: bs58.encode(new TextEncoder().encode(message)), + pubkey: address + } + }, + ChainController.state.activeCaipNetwork?.caipNetworkId + ) + + signature = (response as { signature: string }).signature + } else { + signature = await provider.request( + { + method: 'personal_sign', + params: [message, address] + }, + ChainController.state.activeCaipNetwork?.caipNetworkId + ) + } - return signature as string + return signature }, estimateGas: async () => await Promise.resolve(BigInt(0)), diff --git a/packages/core/src/controllers/ConnectionController.ts b/packages/core/src/controllers/ConnectionController.ts index 0eecde9c8d..a4ebb545a5 100644 --- a/packages/core/src/controllers/ConnectionController.ts +++ b/packages/core/src/controllers/ConnectionController.ts @@ -136,18 +136,15 @@ export const ConnectionController = { } ) } - - await this.initializeSWIXIfAvailable() }, async connectExternal(options: ConnectExternalOptions, chain: ChainNamespace, setChain = true) { await this._getClient(chain).connectExternal?.(options) + if (setChain) { ChainController.setActiveNamespace(chain) StorageUtil.setConnectedConnector(options.type) } - - await this.initializeSWIXIfAvailable() }, async reconnectExternal(options: ConnectExternalOptions) { @@ -250,19 +247,19 @@ export const ConnectionController = { }, async disconnect() { - const connectionControllerClient = this._getClient() + try { + const connectionControllerClient = this._getClient() - const siwx = OptionsController.state.siwx - if (siwx) { - const activeCaipNetwork = ChainController.getActiveCaipNetwork() - const address = ChainController.getActiveCaipAddress()?.split(':')[2] || '' + const siwx = OptionsController.state.siwx + if (siwx) { + const activeCaipNetwork = ChainController.getActiveCaipNetwork() + const address = CoreHelperUtil.getPlainAddress(ChainController.getActiveCaipAddress()) - if (activeCaipNetwork && address) { - siwx.revokeSession(activeCaipNetwork.caipNetworkId, address) + if (activeCaipNetwork && address) { + await siwx.revokeSession(activeCaipNetwork.caipNetworkId, address) + } } - } - try { await connectionControllerClient?.disconnect() this.resetWcConnection() } catch (error) { @@ -277,7 +274,10 @@ export const ConnectionController = { */ async initializeSWIXIfAvailable() { const siwx = OptionsController.state.siwx - if (!siwx) { + const address = CoreHelperUtil.getPlainAddress(ChainController.getActiveCaipAddress()) + const network = ChainController.getActiveCaipNetwork() + + if (!(siwx && address && network)) { return } @@ -287,25 +287,21 @@ export const ConnectionController = { return } - const activeCaipNetwork = ChainController.getActiveCaipNetwork() - const client = this._getClient(activeCaipNetwork?.chainNamespace) + const client = this._getClient(network?.chainNamespace) try { - if (!activeCaipNetwork) { - throw new Error('No active chain') - } - - const address = ChainController.getActiveCaipAddress()?.split(':')[2] || '' - - const sessions = await siwx.getSessions(activeCaipNetwork.caipNetworkId, address) + const sessions = await siwx.getSessions(network.caipNetworkId, address) if (sessions.length) { return } - ModalController.open({ view: 'SIWXSignMessage' }) + await ModalController.open({ + view: + StorageUtil.getConnectedConnector() === 'AUTH' ? 'ApproveTransaction' : 'SIWXSignMessage' + }) const siwxMessage = await siwx.createMessage({ - chainId: activeCaipNetwork.caipNetworkId, + chainId: network.caipNetworkId, accountAddress: address }) @@ -323,10 +319,10 @@ export const ConnectionController = { } catch (error) { // eslint-disable-next-line no-console console.error('Failed to initialize SIWX', error) - - await client.disconnect() - - throw error + ModalController.setLoading(true) + await client.disconnect().finally(() => { + ModalController.setLoading(false) + }) } } } diff --git a/packages/scaffold-ui/src/modal/w3m-modal/index.ts b/packages/scaffold-ui/src/modal/w3m-modal/index.ts index b0b281ef66..d5f6d3604b 100644 --- a/packages/scaffold-ui/src/modal/w3m-modal/index.ts +++ b/packages/scaffold-ui/src/modal/w3m-modal/index.ts @@ -2,6 +2,7 @@ import { AccountController, ApiController, ChainController, + ConnectionController, CoreHelperUtil, EventsController, ModalController, @@ -198,6 +199,8 @@ export class W3mModal extends LitElement { this.caipAddress = caipAddress + await ConnectionController.initializeSWIXIfAvailable() + if (nextConnected && !isSameAddress && this.isSiweEnabled) { try { const { SIWEController } = await import('@reown/appkit-siwe') diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2dd98f94a3..4419fa7e30 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1238,6 +1238,9 @@ importers: '@walletconnect/utils': specifier: 2.17.0 version: 2.17.0 + bs58: + specifier: 6.0.0 + version: 6.0.0 valtio: specifier: 1.11.2 version: 1.11.2(@types/react@18.3.1)(react@18.3.1)