From 61053394f57f67ac949c9eba274e68acc8e76aaa Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Thu, 31 Oct 2024 18:06:29 -0300 Subject: [PATCH 01/10] feat: add siwx handler for account switch --- .../src/controllers/ConnectionController.ts | 34 +++++++++++++++---- .../scaffold-ui/src/modal/w3m-modal/index.ts | 3 ++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/core/src/controllers/ConnectionController.ts b/packages/core/src/controllers/ConnectionController.ts index a7316cfb05..af1f99d810 100644 --- a/packages/core/src/controllers/ConnectionController.ts +++ b/packages/core/src/controllers/ConnectionController.ts @@ -123,11 +123,17 @@ export const ConnectionController = { .catch(reject) resolve() }) - this.state.status = 'connecting' - await wcConnectionPromise - wcConnectionPromise = undefined - state.wcPairingExpiry = undefined - this.state.status = 'connected' + + try { + this.state.status = 'connecting' + await wcConnectionPromise + wcConnectionPromise = undefined + state.wcPairingExpiry = undefined + this.state.status = 'connected' + } catch (error) { + this.state.status = 'disconnected' + throw error + } } else { await ChainController.state?.universalAdapter?.connectionControllerClient?.connectWalletConnect?.( uri => { @@ -141,7 +147,15 @@ export const ConnectionController = { }, async connectExternal(options: ConnectExternalOptions, chain: ChainNamespace, setChain = true) { - await this._getClient(chain).connectExternal?.(options) + try { + this.state.status = 'connecting' + await this._getClient(chain).connectExternal?.(options) + this.state.status = 'connected' + } catch (error) { + this.state.status = 'disconnected' + throw error + } + if (setChain) { ChainController.setActiveNamespace(chain) StorageUtil.setConnectedConnector(options.type) @@ -277,7 +291,13 @@ export const ConnectionController = { */ async initializeSWIXIfAvailable() { const siwx = OptionsController.state.siwx - if (!siwx) { + if ( + !( + siwx && + ConnectionController.state.status === 'connected' && + ChainController.getActiveCaipAddress() + ) + ) { return } 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') From 0ac65a8494d30dfccd308488c980ec75739f0684 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Fri, 1 Nov 2024 12:07:38 -0300 Subject: [PATCH 02/10] fix: upa sign message for solana --- packages/adapters/solana/src/client.ts | 6 ++-- packages/appkit/package.json | 3 +- .../appkit/src/universal-adapter/client.ts | 34 ++++++++++++++++--- .../src/controllers/ConnectionController.ts | 12 ++----- pnpm-lock.yaml | 3 ++ 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/packages/adapters/solana/src/client.ts b/packages/adapters/solana/src/client.ts index e438180024..a3af900bcf 100644 --- a/packages/adapters/solana/src/client.ts +++ b/packages/adapters/solana/src/client.ts @@ -477,14 +477,14 @@ export class SolanaAdapter implements ChainAdapter { provider.chains.find(chain => chain.caipNetworkId === caipNetworkId) || provider.chains[0] if (connectionChain) { + ProviderUtil.setProvider(this.chainNamespace, provider) + this.provider = provider + const caipAddress = `${connectionChain.caipNetworkId}:${address}` as const this.appKit?.setCaipAddress(caipAddress, this.chainNamespace) await this.switchNetwork(connectionChain) - ProviderUtil.setProvider(this.chainNamespace, provider) - this.provider = provider - switch (provider.type) { case 'WALLET_CONNECT': ProviderUtil.setProviderId(this.chainNamespace, 'walletConnect') diff --git a/packages/appkit/package.json b/packages/appkit/package.json index db889c6c41..8c3c806b76 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 e7432082f3..d992692f77 100644 --- a/packages/appkit/src/universal-adapter/client.ts +++ b/packages/appkit/src/universal-adapter/client.ts @@ -24,6 +24,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 @@ -242,12 +243,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 af1f99d810..39304b28b0 100644 --- a/packages/core/src/controllers/ConnectionController.ts +++ b/packages/core/src/controllers/ConnectionController.ts @@ -291,13 +291,9 @@ export const ConnectionController = { */ async initializeSWIXIfAvailable() { const siwx = OptionsController.state.siwx - if ( - !( - siwx && - ConnectionController.state.status === 'connected' && - ChainController.getActiveCaipAddress() - ) - ) { + const address = CoreHelperUtil.getPlainAddress(ChainController.getActiveCaipAddress()) + + if (!(siwx && address)) { return } @@ -315,8 +311,6 @@ export const ConnectionController = { throw new Error('No active chain') } - const address = ChainController.getActiveCaipAddress()?.split(':')[2] || '' - const sessions = await siwx.getSessions(activeCaipNetwork.caipNetworkId, address) if (sessions.length) { return diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f1867b94e1..02be3c55d3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1146,6 +1146,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) From 30ebf33aea79fe0080995cc2e2df9b11f270bcb7 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Fri, 1 Nov 2024 12:11:04 -0300 Subject: [PATCH 03/10] chore: revert connection status changes --- .../src/controllers/ConnectionController.ts | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/packages/core/src/controllers/ConnectionController.ts b/packages/core/src/controllers/ConnectionController.ts index 39304b28b0..69fa59bd3e 100644 --- a/packages/core/src/controllers/ConnectionController.ts +++ b/packages/core/src/controllers/ConnectionController.ts @@ -123,17 +123,11 @@ export const ConnectionController = { .catch(reject) resolve() }) - - try { - this.state.status = 'connecting' - await wcConnectionPromise - wcConnectionPromise = undefined - state.wcPairingExpiry = undefined - this.state.status = 'connected' - } catch (error) { - this.state.status = 'disconnected' - throw error - } + this.state.status = 'connecting' + await wcConnectionPromise + wcConnectionPromise = undefined + state.wcPairingExpiry = undefined + this.state.status = 'connected' } else { await ChainController.state?.universalAdapter?.connectionControllerClient?.connectWalletConnect?.( uri => { @@ -147,14 +141,7 @@ export const ConnectionController = { }, async connectExternal(options: ConnectExternalOptions, chain: ChainNamespace, setChain = true) { - try { - this.state.status = 'connecting' - await this._getClient(chain).connectExternal?.(options) - this.state.status = 'connected' - } catch (error) { - this.state.status = 'disconnected' - throw error - } + await this._getClient(chain).connectExternal?.(options) if (setChain) { ChainController.setActiveNamespace(chain) From da1a731c18e6a841e629b7c7932f0df5419e0c37 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Fri, 1 Nov 2024 13:17:01 -0300 Subject: [PATCH 04/10] refactor: clean up siwx initialization logic --- .../src/controllers/ConnectionController.ts | 18 +++++------------- .../scaffold-ui/src/modal/w3m-modal/index.ts | 4 +++- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/packages/core/src/controllers/ConnectionController.ts b/packages/core/src/controllers/ConnectionController.ts index 69fa59bd3e..8e30471d15 100644 --- a/packages/core/src/controllers/ConnectionController.ts +++ b/packages/core/src/controllers/ConnectionController.ts @@ -136,8 +136,6 @@ export const ConnectionController = { } ) } - - await this.initializeSWIXIfAvailable() }, async connectExternal(options: ConnectExternalOptions, chain: ChainNamespace, setChain = true) { @@ -147,8 +145,6 @@ export const ConnectionController = { ChainController.setActiveNamespace(chain) StorageUtil.setConnectedConnector(options.type) } - - await this.initializeSWIXIfAvailable() }, async reconnectExternal(options: ConnectExternalOptions) { @@ -279,8 +275,9 @@ export const ConnectionController = { async initializeSWIXIfAvailable() { const siwx = OptionsController.state.siwx const address = CoreHelperUtil.getPlainAddress(ChainController.getActiveCaipAddress()) + const network = ChainController.getActiveCaipNetwork() - if (!(siwx && address)) { + if (!(siwx && address && network)) { return } @@ -290,15 +287,10 @@ 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 sessions = await siwx.getSessions(activeCaipNetwork.caipNetworkId, address) + const sessions = await siwx.getSessions(network.caipNetworkId, address) if (sessions.length) { return } @@ -306,7 +298,7 @@ export const ConnectionController = { ModalController.open({ view: 'SIWXSignMessage' }) const message = await siwx.createMessage({ - chainId: activeCaipNetwork.caipNetworkId, + chainId: network.caipNetworkId, accountAddress: address }) diff --git a/packages/scaffold-ui/src/modal/w3m-modal/index.ts b/packages/scaffold-ui/src/modal/w3m-modal/index.ts index d5f6d3604b..5c6c998096 100644 --- a/packages/scaffold-ui/src/modal/w3m-modal/index.ts +++ b/packages/scaffold-ui/src/modal/w3m-modal/index.ts @@ -199,7 +199,9 @@ export class W3mModal extends LitElement { this.caipAddress = caipAddress - await ConnectionController.initializeSWIXIfAvailable() + if (!prevConnected && nextConnected) { + await ConnectionController.initializeSWIXIfAvailable() + } if (nextConnected && !isSameAddress && this.isSiweEnabled) { try { From 53732f65d3d4a0adf0e80cd23e2ac19ec1a08f49 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Fri, 1 Nov 2024 14:45:31 -0300 Subject: [PATCH 05/10] fix: disconnection and user rejection issues --- .../src/controllers/ConnectionController.ts | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/core/src/controllers/ConnectionController.ts b/packages/core/src/controllers/ConnectionController.ts index 8e30471d15..f5e02c1b80 100644 --- a/packages/core/src/controllers/ConnectionController.ts +++ b/packages/core/src/controllers/ConnectionController.ts @@ -247,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) { @@ -315,8 +315,6 @@ export const ConnectionController = { console.error('Failed to initialize SIWX', error) await client.disconnect() - - throw error } } } From 0eda80d5ba290d45a29cfba2d2037a8ce61993d6 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Fri, 1 Nov 2024 14:50:42 -0300 Subject: [PATCH 06/10] chore: add changeset --- .changeset/brave-countries-tie.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .changeset/brave-countries-tie.md 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 From 7fde060403aef72254e9094ee0c3ebfee39ef996 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Mon, 4 Nov 2024 15:38:36 -0300 Subject: [PATCH 07/10] fix: issue with switch from evm to solana --- packages/appkit/src/client.ts | 9 ++------- packages/scaffold-ui/src/modal/w3m-modal/index.ts | 4 +--- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index 947ff36613..4136c45668 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -291,13 +291,8 @@ export class AppKit { return ChainController.getAccountProp('caipAddress', chainNamespace) } - public getAddress = (chainNamespace?: ChainNamespace) => { - if (ChainController.state.activeChain === chainNamespace || !chainNamespace) { - return AccountController.state.address - } - - return ChainController.getAccountProp('address', chainNamespace) - } + public getAddress = (chainNamespace?: ChainNamespace) => + ChainController.getAccountProp('address', chainNamespace) public getProvider = () => AccountController.state.provider diff --git a/packages/scaffold-ui/src/modal/w3m-modal/index.ts b/packages/scaffold-ui/src/modal/w3m-modal/index.ts index 5c6c998096..d5f6d3604b 100644 --- a/packages/scaffold-ui/src/modal/w3m-modal/index.ts +++ b/packages/scaffold-ui/src/modal/w3m-modal/index.ts @@ -199,9 +199,7 @@ export class W3mModal extends LitElement { this.caipAddress = caipAddress - if (!prevConnected && nextConnected) { - await ConnectionController.initializeSWIXIfAvailable() - } + await ConnectionController.initializeSWIXIfAvailable() if (nextConnected && !isSameAddress && this.isSiweEnabled) { try { From 0848b21fd5526358cf0e1432450bb169a16dc7e2 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Mon, 4 Nov 2024 17:59:46 -0300 Subject: [PATCH 08/10] fix: sign message view for auth connector --- packages/core/src/controllers/ConnectionController.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/core/src/controllers/ConnectionController.ts b/packages/core/src/controllers/ConnectionController.ts index f5e02c1b80..9e5f37a6fb 100644 --- a/packages/core/src/controllers/ConnectionController.ts +++ b/packages/core/src/controllers/ConnectionController.ts @@ -295,7 +295,10 @@ export const ConnectionController = { return } - ModalController.open({ view: 'SIWXSignMessage' }) + await ModalController.open({ + view: + StorageUtil.getConnectedConnector() === 'AUTH' ? 'ApproveTransaction' : 'SIWXSignMessage' + }) const message = await siwx.createMessage({ chainId: network.caipNetworkId, @@ -313,8 +316,10 @@ export const ConnectionController = { } catch (error) { // eslint-disable-next-line no-console console.error('Failed to initialize SIWX', error) - - await client.disconnect() + ModalController.setLoading(true) + await client.disconnect().finally(() => { + ModalController.setLoading(false) + }) } } } From 48a278a0614c3f51611fe18258bc79b9d75befac Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Wed, 6 Nov 2024 11:56:08 -0300 Subject: [PATCH 09/10] refactor: revert change on appkit client --- packages/appkit/src/client.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index 4136c45668..947ff36613 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -291,8 +291,13 @@ export class AppKit { return ChainController.getAccountProp('caipAddress', chainNamespace) } - public getAddress = (chainNamespace?: ChainNamespace) => - ChainController.getAccountProp('address', chainNamespace) + public getAddress = (chainNamespace?: ChainNamespace) => { + if (ChainController.state.activeChain === chainNamespace || !chainNamespace) { + return AccountController.state.address + } + + return ChainController.getAccountProp('address', chainNamespace) + } public getProvider = () => AccountController.state.provider From c87f362eec78ffb5289ff1bd55cfc7d0e53a54d0 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Wed, 6 Nov 2024 12:55:02 -0300 Subject: [PATCH 10/10] refactor: revert change in solana client --- packages/adapters/solana/src/client.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/adapters/solana/src/client.ts b/packages/adapters/solana/src/client.ts index e000846a89..5f4c05b344 100644 --- a/packages/adapters/solana/src/client.ts +++ b/packages/adapters/solana/src/client.ts @@ -477,14 +477,14 @@ export class SolanaAdapter implements ChainAdapter { provider.chains.find(chain => chain.caipNetworkId === caipNetworkId) || provider.chains[0] if (connectionChain) { - ProviderUtil.setProvider(this.chainNamespace, provider) - this.provider = provider - const caipAddress = `${connectionChain.caipNetworkId}:${address}` as const this.appKit?.setCaipAddress(caipAddress, this.chainNamespace) await this.switchNetwork(connectionChain) + ProviderUtil.setProvider(this.chainNamespace, provider) + this.provider = provider + switch (provider.type) { case 'WALLET_CONNECT': ProviderUtil.setProviderId(this.chainNamespace, 'walletConnect')