Skip to content

Commit

Permalink
feat: export defaultConnectors
Browse files Browse the repository at this point in the history
  • Loading branch information
q20274982 committed Aug 23, 2023
1 parent eec572b commit 48ce4ac
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 100 deletions.
102 changes: 2 additions & 100 deletions packages/connectkit/src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ import {
} from 'wagmi';
import { Chain, mainnet, polygon, optimism, arbitrum } from 'wagmi/chains';

import { MetaMaskConnector } from 'wagmi/connectors/metaMask';
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect';
import { WalletConnectLegacyConnector } from 'wagmi/connectors/walletConnectLegacy';
import { CoinbaseWalletConnector } from 'wagmi/connectors/coinbaseWallet';
import { SafeConnector } from 'wagmi/connectors/safe';
import { InjectedConnector } from 'wagmi/connectors/injected';

import { alchemyProvider } from 'wagmi/providers/alchemy';
import { infuraProvider } from 'wagmi/providers/infura';
import { jsonRpcProvider } from 'wagmi/providers/jsonRpc';
import { publicProvider } from 'wagmi/providers/public';
import defaultConnectors from './defaultConnectors';

let globalAppName: string;
let globalAppIcon: string;
Expand All @@ -27,17 +21,6 @@ export const getAppIcon = () => globalAppIcon;

const defaultChains = [mainnet, polygon, optimism, arbitrum];

type DefaultConnectorsProps = {
chains?: Chain[];
app: {
name: string;
icon?: string;
description?: string;
url?: string;
};
walletConnectProjectId?: string;
};

type DefaultConfigProps = {
appName: string;
appIcon?: string;
Expand All @@ -63,87 +46,6 @@ type ConnectKitClientProps = {
webSocketPublicClient?: WebSocketPublicClient;
};

const getDefaultConnectors = ({
chains,
app,
walletConnectProjectId,
}: DefaultConnectorsProps) => {
const hasAllAppData = app.name && app.icon && app.description && app.url;
const shouldUseSafeConnector =
!(typeof window === 'undefined') && window?.parent !== window;

let connectors: Connector[] = [];

// If we're in an iframe, include the SafeConnector
if (shouldUseSafeConnector) {
connectors = [
...connectors,
new SafeConnector({
chains,
options: {
allowedDomains: [/gnosis-safe.io$/, /app.safe.global$/],
debug: false,
},
}),
];
}

// Add the rest of the connectors
connectors = [
...connectors,
new MetaMaskConnector({
chains,
options: {
shimDisconnect: true,
UNSTABLE_shimOnConnectSelectAccount: true,
},
}),
new CoinbaseWalletConnector({
chains,
options: {
appName: app.name,
headlessMode: true,
},
}),
walletConnectProjectId
? new WalletConnectConnector({
chains,
options: {
showQrModal: false,
projectId: walletConnectProjectId,
metadata: hasAllAppData
? {
name: app.name,
description: app.description!,
url: app.url!,
icons: [app.icon!],
}
: undefined,
},
})
: new WalletConnectLegacyConnector({
chains,
options: {
qrcode: false,
},
}),
new InjectedConnector({
chains,
options: {
shimDisconnect: true,
name: (detectedName) =>
`Injected (${
typeof detectedName === 'string'
? detectedName
: detectedName.join(', ')
})`,
},
}),
];

return connectors;
};

const defaultConfig = ({
autoConnect = true,
appName = 'ConnectKit',
Expand Down Expand Up @@ -189,7 +91,7 @@ const defaultConfig = ({
autoConnect,
connectors:
connectors ??
getDefaultConnectors({
defaultConnectors({
chains: configuredChains,
app: {
name: appName,
Expand Down
102 changes: 102 additions & 0 deletions packages/connectkit/src/defaultConnectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Connector } from 'wagmi';
import { Chain } from 'wagmi/chains';
import { MetaMaskConnector } from 'wagmi/connectors/metaMask';
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect';
import { WalletConnectLegacyConnector } from 'wagmi/connectors/walletConnectLegacy';
import { CoinbaseWalletConnector } from 'wagmi/connectors/coinbaseWallet';
import { SafeConnector } from 'wagmi/connectors/safe';
import { InjectedConnector } from 'wagmi/connectors/injected';

type DefaultConnectorsProps = {
chains?: Chain[];
app: {
name: string;
icon?: string;
description?: string;
url?: string;
};
walletConnectProjectId?: string;
};

const defaultConnectors = ({
chains,
app,
walletConnectProjectId,
}: DefaultConnectorsProps) => {
const hasAllAppData = app.name && app.icon && app.description && app.url;
const shouldUseSafeConnector =
!(typeof window === 'undefined') && window?.parent !== window;

let connectors: Connector[] = [];

// If we're in an iframe, include the SafeConnector
if (shouldUseSafeConnector) {
connectors = [
...connectors,
new SafeConnector({
chains,
options: {
allowedDomains: [/gnosis-safe.io$/, /app.safe.global$/],
debug: false,
},
}),
];
}

// Add the rest of the connectors
connectors = [
...connectors,
new MetaMaskConnector({
chains,
options: {
shimDisconnect: true,
UNSTABLE_shimOnConnectSelectAccount: true,
},
}),
new CoinbaseWalletConnector({
chains,
options: {
appName: app.name,
headlessMode: true,
},
}),
walletConnectProjectId
? new WalletConnectConnector({
chains,
options: {
showQrModal: false,
projectId: walletConnectProjectId,
metadata: hasAllAppData
? {
name: app.name,
description: app.description!,
url: app.url!,
icons: [app.icon!],
}
: undefined,
},
})
: new WalletConnectLegacyConnector({
chains,
options: {
qrcode: false,
},
}),
new InjectedConnector({
chains,
options: {
shimDisconnect: true,
name: (detectedName) =>
`Injected (${
typeof detectedName === 'string'
? detectedName
: detectedName.join(', ')
})`,
},
}),
];

return connectors;
};

export default defaultConnectors;
1 change: 1 addition & 0 deletions packages/connectkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const CONNECTKIT_VERSION = '1.5.1';

export * as Types from './types';
export { default as getDefaultConfig } from './defaultConfig';
export { default as getDefaultConnectors } from './defaultConnectors';

export { useModal } from './hooks/useModal';
export { SIWEProvider, useSIWE } from './siwe';
Expand Down

0 comments on commit 48ce4ac

Please sign in to comment.