Skip to content

Commit

Permalink
feat: support multiple rdns to filter 6963 providers (#4406)
Browse files Browse the repository at this point in the history
* feat: support multiple rdns values to prevent duplicate connectors

* chore: tweaks

---------

Co-authored-by: Saurabh Chauhan <[email protected]>
  • Loading branch information
tmm and starc007 authored Nov 13, 2024
1 parent e9ca469 commit a13aa8d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-pans-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": patch
---

Added support for multiple connector `rdns` entries.
5 changes: 5 additions & 0 deletions .changeset/wild-guests-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/connectors": patch
---

Added additional RDNS to MetaMask Connector.
2 changes: 1 addition & 1 deletion packages/connectors/src/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function metaMask(parameters: MetaMaskParameters = {}) {
return createConnector<Provider, Properties>((config) => ({
id: 'metaMaskSDK',
name: 'MetaMask',
rdns: 'io.metamask',
rdns: ['io.metamask', 'io.metamask.mobile'],
type: metaMask.type,
async setup() {
const provider = await this.getProvider()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/connectors/createConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type CreateConnectorFn<
readonly icon?: string | undefined
readonly id: string
readonly name: string
readonly rdns?: string | undefined
readonly rdns?: string | readonly string[] | undefined
readonly supportsSimulation?: boolean | undefined
readonly type: string

Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/createConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ test('behavior: eip 6963 providers', async () => {
createConnector((c) => {
return {
...mock({ accounts })(c),
rdns: 'com.mock',
rdns: ['com.mock', 'com.baz'],
}
}),
],
Expand All @@ -413,9 +413,12 @@ test('behavior: eip 6963 providers', async () => {
announceProvider(detail_3)()
await wait(100)

expect(config.connectors.map((x) => x.rdns ?? x.id)).toMatchInlineSnapshot(`
expect(
config.connectors.flatMap((x) => x.rdns ?? x.id),
).toMatchInlineSnapshot(`
[
"com.mock",
"com.baz",
"com.example",
"com.foo",
"com.bar",
Expand Down
20 changes: 16 additions & 4 deletions packages/core/src/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ export function createConfig<
for (const connectorFns of rest.connectors ?? []) {
const connector = setup(connectorFns)
collection.push(connector)
if (!ssr && connector.rdns) rdnsSet.add(connector.rdns)
if (!ssr && connector.rdns) {
const rdnsValues =
typeof connector.rdns === 'string' ? [connector.rdns] : connector.rdns
for (const rdns of rdnsValues) {
rdnsSet.add(rdns)
}
}
}
if (!ssr && mipd) {
const providers = mipd.getProviders()
Expand Down Expand Up @@ -323,11 +329,17 @@ export function createConfig<

// EIP-6963 subscribe for new wallet providers
mipd?.subscribe((providerDetails) => {
const connectorIdSet = new Set()
const connectorRdnsSet = new Set()
const connectorIdSet = new Set<string>()
const connectorRdnsSet = new Set<string>()
for (const connector of connectors.getState()) {
connectorIdSet.add(connector.id)
if (connector.rdns) connectorRdnsSet.add(connector.rdns)
if (connector.rdns) {
const rdnsValues =
typeof connector.rdns === 'string' ? [connector.rdns] : connector.rdns
for (const rdns of rdnsValues) {
connectorRdnsSet.add(rdns)
}
}
}

const newConnectors: Connector[] = []
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ export function hydrate(config: Config, parameters: HydrateParameters) {
config._internal.connectors.setState((connectors) => {
const rdnsSet = new Set<string>()
for (const connector of connectors ?? []) {
if (connector.rdns) rdnsSet.add(connector.rdns)
if (connector.rdns) {
const rdnsValues = Array.isArray(connector.rdns)
? connector.rdns
: [connector.rdns]
for (const rdns of rdnsValues) {
rdnsSet.add(rdns)
}
}
}
const mipdConnectors = []
const providers = config._internal.mipd?.getProviders() ?? []
Expand Down

0 comments on commit a13aa8d

Please sign in to comment.