Skip to content

Commit

Permalink
fix: filter connectors based on rdns (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir committed Mar 13, 2024
1 parent 0d0f234 commit 23e4eca
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/controllers/ConnectorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@ export const ConnectorController = {

getConnectors() {
return state.connectors
},

getConnector(id: string, rdns?: string | null) {
return state.connectors.find(c => c.explorerId === id || c.info?.rdns === rdns)
}
}
18 changes: 18 additions & 0 deletions packages/core/tests/controllers/ConnectorController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { ConnectorController } from '../../index.js'
// -- Setup --------------------------------------------------------------------
const walletConnectConnector = { id: 'walletConnect', type: 'WALLET_CONNECT' } as const
const externalConnector = { id: 'external', type: 'EXTERNAL' } as const
const metamaskConnector = {
id: 'metamask',
type: 'INJECTED',
info: { rdns: 'io.metamask.com' }
} as const

// -- Tests --------------------------------------------------------------------
describe('ConnectorController', () => {
Expand All @@ -22,5 +27,18 @@ describe('ConnectorController', () => {
walletConnectConnector,
externalConnector
])

ConnectorController.addConnector(metamaskConnector)
expect(ConnectorController.state.connectors).toEqual([
walletConnectConnector,
externalConnector,
metamaskConnector
])
})

it('should return the correct connector on getConnector', () => {
expect(ConnectorController.getConnector('walletConnect', '')).toBe(walletConnectConnector)
expect(ConnectorController.getConnector('', 'io.metamask.com')).toBe(metamaskConnector)
expect(ConnectorController.getConnector('unknown', '')).toBeUndefined()
})
})
3 changes: 1 addition & 2 deletions packages/scaffold/src/partials/w3m-all-wallets-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ export class W3mAllWalletsList extends LitElement {
}

private onConnectWallet(wallet: WcWallet) {
const { connectors } = ConnectorController.state
const connector = connectors.find(({ explorerId }) => explorerId === wallet.id)
const connector = ConnectorController.getConnector(wallet.id, wallet.rdns)
if (connector) {
RouterController.push('ConnectingExternal', { connector })
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ export class W3mAllWalletsSearch extends LitElement {
}

private onConnectWallet(wallet: WcWallet) {
const { connectors } = ConnectorController.state
const connector = connectors.find(({ explorerId }) => explorerId === wallet.id)
const connector = ConnectorController.getConnector(wallet.id, wallet.rdns)
if (connector) {
RouterController.push('ConnectingExternal', { connector })
} else {
Expand Down
12 changes: 10 additions & 2 deletions packages/scaffold/src/views/w3m-connect-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ export class W3mConnectView extends LitElement {
if (!connector) {
return null
}

const { featured } = ApiController.state
if (!featured.length) {
return null
}

const wallets = this.filterOutDuplicateWallets(featured)

return wallets.map(
Expand Down Expand Up @@ -283,8 +285,14 @@ export class W3mConnectView extends LitElement {

private filterOutDuplicateWallets(wallets: WcWallet[]) {
const recent = StorageUtil.getRecentWallets()
const recentIds = recent.map(wallet => wallet.id)
const filtered = wallets.filter(wallet => !recentIds.includes(wallet.id))

const connectorRDNSs = this.connectors
.map(connector => connector.info?.rdns)
.filter(Boolean) as string[]
const recentRDNSs = recent.map(wallet => wallet.rdns).filter(Boolean) as string[]
const allRDNSs = connectorRDNSs.concat(recentRDNSs)

const filtered = wallets.filter(wallet => !allRDNSs.includes(String(wallet?.rdns)))

return filtered
}
Expand Down
11 changes: 7 additions & 4 deletions packages/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,24 @@ export class Web3Modal extends Web3ModalScaffold {

// Check if coinbase injected connector is present
const coinbaseConnector = filteredConnectors.find(
c => c.id === CoreConstants.CONNECTOR_RDNS_MAP[coinbaseSDKId]
c => c.id === CoreConstants.CONNECTOR_RDNS_MAP[ConstantsUtil.COINBASE_CONNECTOR_ID]
)

filteredConnectors.forEach(({ id, name, type, icon }) => {
// If coinbase injected connector is present, skip coinbase sdk connector.
const shouldSkip =
(coinbaseConnector && id === coinbaseSDKId) || ConstantsUtil.EMAIL_CONNECTOR_ID === id
const isCoinbaseRepeated = coinbaseConnector && id === coinbaseSDKId
const shouldSkip = isCoinbaseRepeated || ConstantsUtil.EMAIL_CONNECTOR_ID === id
if (!shouldSkip) {
w3mConnectors.push({
id,
explorerId: PresetsUtil.ConnectorExplorerIds[id],
imageUrl: this.options?.connectorImages?.[id] ?? icon,
name: PresetsUtil.ConnectorNamesMap[id] ?? name,
imageId: PresetsUtil.ConnectorImageIds[id],
type: PresetsUtil.ConnectorTypesMap[type] ?? 'EXTERNAL'
type: PresetsUtil.ConnectorTypesMap[type] ?? 'EXTERNAL',
info: {
rdns: id
}
})
}
})
Expand Down

0 comments on commit 23e4eca

Please sign in to comment.