Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
fix: make wc connector more caip compliant (#401)
Browse files Browse the repository at this point in the history
* Add logic to handle individual namespaces

* changeset

* Utalise @walletconnect/utils package

* fix optionalChains

* stash

* stash

* Update six-icons-bow.md

---------

Co-authored-by: jxom <[email protected]>
  • Loading branch information
xzilja and jxom authored Jul 5, 2023
1 parent 8fd81f6 commit 51e346e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-icons-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/connectors": patch
---

Updated WalletConnectConnector logic to handle individual namespaces like eip155:\*
1 change: 1 addition & 0 deletions packages/connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 16 additions & 6 deletions packages/connectors/src/walletConnect.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 ?? []
}

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 51e346e

Please sign in to comment.