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

Commit

Permalink
feat: add ledger to switchChainAllowedRegex (#150)
Browse files Browse the repository at this point in the history
* Add ledger to switchChainAllowedRegex

* Add chain switching to the Ledger connector

* Update hot-peas-kiss.md

---------

Co-authored-by: jxom <[email protected]>
  • Loading branch information
hlopes-ledger and jxom authored Mar 15, 2023
1 parent 033486b commit 6a4af48
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-peas-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@wagmi/connectors': patch
---

Enabled support for programmatic chain switching on `LedgerConnector` & added `"ledger"` to the switch chain regex on `WalletConnectLegacyConnector`.
45 changes: 44 additions & 1 deletion packages/connectors/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
Chain,
ProviderRpcError,
RpcError,
SwitchChainError,
UserRejectedRequestError,
normalizeChainId,
} from '@wagmi/core'
import { providers } from 'ethers'
import { getAddress } from 'ethers/lib/utils.js'
import { getAddress, hexValue } from 'ethers/lib/utils.js'

import type { ConnectorData } from './base'
import { Connector } from './base'
Expand Down Expand Up @@ -65,6 +66,9 @@ export class LedgerConnector extends Connector<
const id = await this.getChainId()
const unsupported = this.isChainUnsupported(id)

// Enable support for programmatic chain switching
this.switchChain = this.#switchChain

return {
account,
chain: { id, unsupported },
Expand Down Expand Up @@ -172,6 +176,45 @@ export class LedgerConnector extends Connector<
}
}

async #switchChain(chainId: number) {
const provider = await this.getProvider()
const id = hexValue(chainId)

try {
// Set up a race between `wallet_switchEthereumChain` & the `chainChanged` event
// to ensure the chain has been switched. This is because there could be a case
// where a wallet may not resolve the `wallet_switchEthereumChain` method, or
// resolves slower than `chainChanged`.
await Promise.race([
provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: id }],
}),
new Promise((res) =>
this.on('change', ({ chain }) => {
if (chain?.id === chainId) res(chainId)
}),
),
])
return (
this.chains.find((x) => x.id === chainId) ??
({
id: chainId,
name: `Chain ${id}`,
network: `${id}`,
nativeCurrency: { name: 'Ether', decimals: 18, symbol: 'ETH' },
rpcUrls: { default: { http: [''] }, public: { http: [''] } },
} as Chain)
)
} catch (error) {
const message =
typeof error === 'string' ? error : (error as ProviderRpcError)?.message
if (/user rejected request/i.test(message))
throw new UserRejectedRequestError(error)
throw new SwitchChainError(error)
}
}

protected onAccountsChanged = (accounts: string[]) => {
if (accounts.length === 0) this.emit('disconnect')
else this.emit('change', { account: getAddress(accounts[0] as string) })
Expand Down
3 changes: 2 additions & 1 deletion packages/connectors/src/walletConnectLegacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import { Connector } from './base'
/**
* Wallets that support chain switching through WalletConnect
* - imToken (token.im)
* - Ledger Live (ledger.com)
* - MetaMask (metamask.io)
* - Rainbow (rainbow.me)
* - Trust Wallet (trustwallet.com)
* - Uniswap Wallet (uniswap.org)
*/
const switchChainAllowedRegex =
/(imtoken|metamask|rainbow|trust wallet|uniswap wallet)/i
/(imtoken|metamask|rainbow|trust wallet|uniswap wallet|ledger)/i

type WalletConnectOptions = ConstructorParameters<
typeof WalletConnectProvider
Expand Down

0 comments on commit 6a4af48

Please sign in to comment.