From 51e346e797f9950540a393c7cf80e15be7fc358e Mon Sep 17 00:00:00 2001 From: 0xAsimetriq <3154053+0xAsimetriq@users.noreply.github.com> Date: Wed, 5 Jul 2023 09:33:47 +0300 Subject: [PATCH] fix: make wc connector more caip compliant (#401) * Add logic to handle individual namespaces * changeset * Utalise @walletconnect/utils package * fix optionalChains * stash * stash * Update six-icons-bow.md --------- Co-authored-by: jxom --- .changeset/six-icons-bow.md | 5 +++++ packages/connectors/package.json | 1 + packages/connectors/src/walletConnect.ts | 22 ++++++++++++++++------ pnpm-lock.yaml | 3 +++ 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 .changeset/six-icons-bow.md diff --git a/.changeset/six-icons-bow.md b/.changeset/six-icons-bow.md new file mode 100644 index 00000000..70018ff1 --- /dev/null +++ b/.changeset/six-icons-bow.md @@ -0,0 +1,5 @@ +--- +"@wagmi/connectors": patch +--- + +Updated WalletConnectConnector logic to handle individual namespaces like eip155:\* diff --git a/packages/connectors/package.json b/packages/connectors/package.json index 3d8fdf87..e96a3a61 100644 --- a/packages/connectors/package.json +++ b/packages/connectors/package.json @@ -26,6 +26,7 @@ "@safe-global/safe-apps-provider": "^0.17.1", "@safe-global/safe-apps-sdk": "^8.0.0", "@walletconnect/ethereum-provider": "2.8.6", + "@walletconnect/utils": "2.8.6", "@walletconnect/legacy-provider": "^2.0.0", "@walletconnect/modal": "2.5.9", "abitype": "0.8.7", diff --git a/packages/connectors/src/walletConnect.ts b/packages/connectors/src/walletConnect.ts index 28669fdb..1466545a 100644 --- a/packages/connectors/src/walletConnect.ts +++ b/packages/connectors/src/walletConnect.ts @@ -1,7 +1,7 @@ import type { Chain } from '@wagmi/chains' import type WalletConnectProvider from '@walletconnect/ethereum-provider' -// eslint-disable-next-line import/no-unresolved import { EthereumProviderOptions } from '@walletconnect/ethereum-provider/dist/types/EthereumProvider' +import { normalizeNamespaces } from '@walletconnect/utils' import { ProviderRpcError, SwitchChainError, @@ -139,7 +139,7 @@ export class WalletConnectConnector extends Connector< await provider.connect({ pairingTopic, chains: [targetChainId], - optionalChains, + optionalChains: optionalChains.length ? optionalChains : undefined, }) this.#setRequestedChainsIds(this.chains.map(({ id }) => id)) @@ -302,7 +302,7 @@ export class WalletConnectConnector extends Connector< optionalMethods: OPTIONAL_METHODS, optionalEvents: OPTIONAL_EVENTS, chains: [defaultChain], - optionalChains: optionalChains.length > 0 ? optionalChains : undefined, + optionalChains: optionalChains.length ? optionalChains : undefined, rpcMap: Object.fromEntries( this.chains.map((chain) => [ chain.id, @@ -386,15 +386,25 @@ export class WalletConnectConnector extends Connector< #getNamespaceChainsIds() { if (!this.#provider) return [] - const chainIds = this.#provider.session?.namespaces[NAMESPACE]?.chains?.map( - (chain) => parseInt(chain.split(':')[1] || ''), + const namespaces = this.#provider.session?.namespaces + if (!namespaces) return [] + + const normalizedNamespaces = normalizeNamespaces(namespaces) + const chainIds = normalizedNamespaces[NAMESPACE]?.chains?.map((chain) => + parseInt(chain.split(':')[1] || ''), ) + return chainIds ?? [] } #getNamespaceMethods() { if (!this.#provider) return [] - const methods = this.#provider.session?.namespaces[NAMESPACE]?.methods + const namespaces = this.#provider.session?.namespaces + if (!namespaces) return [] + + const normalizedNamespaces = normalizeNamespaces(namespaces) + const methods = normalizedNamespaces[NAMESPACE]?.methods + return methods ?? [] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 71ccb70e..81e2bb64 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -104,6 +104,9 @@ importers: '@walletconnect/modal': specifier: 2.5.9 version: 2.5.9(react@18.2.0) + '@walletconnect/utils': + specifier: 2.8.6 + version: 2.8.6(lokijs@1.5.12) abitype: specifier: 0.8.7 version: 0.8.7(typescript@5.0.4)