Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filter connectors based on rdns #2039

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
tomiir marked this conversation as resolved.
Show resolved Hide resolved
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