Skip to content

Commit

Permalink
:deprecate - injected connector (#2223)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvoskamp authored May 8, 2024
1 parent 1066194 commit cf64cc0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 335 deletions.
1 change: 0 additions & 1 deletion examples/next-wagmi/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const config = defaultWagmiConfig({
url: 'https://myapp.com',
icons: ['https://myapp.com/favicon.ico']
},
enableInjected: true,
enableWalletConnect: true,
enableEIP6963: true,
enableCoinbase: true,
Expand Down
131 changes: 2 additions & 129 deletions packages/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,7 @@ export class Web3Modal extends Web3ModalScaffold {
info: Info
provider: Provider
}) => {
if (id === ConstantsUtil.INJECTED_CONNECTOR_ID) {
const InjectedProvider = ethersConfig.injected
if (!InjectedProvider) {
throw new Error('connectionControllerClient:connectInjected - provider is undefined')
}
try {
EthersStoreUtil.setError(undefined)
await InjectedProvider.request({ method: 'eth_requestAccounts' })
this.setInjectedProvider(ethersConfig)
} catch (error) {
EthersStoreUtil.setError(error)
}
} else if (id === ConstantsUtil.EIP6963_CONNECTOR_ID && info && provider) {
if (id === ConstantsUtil.EIP6963_CONNECTOR_ID && info && provider) {
try {
EthersStoreUtil.setError(undefined)
await provider.request({ method: 'eth_requestAccounts' })
Expand Down Expand Up @@ -288,12 +276,6 @@ export class Web3Modal extends Web3ModalScaffold {
return Boolean(window.ethereum)
}

if (ethersConfig.injected) {
if (!window?.ethereum) {
return false
}
}

return ids.some(id => Boolean(window.ethereum?.[String(id)]))
},

Expand Down Expand Up @@ -494,9 +476,6 @@ export class Web3Modal extends Web3ModalScaffold {
this.syncEmailConnector(w3mOptions.projectId)
}

if (ethersConfig.injected) {
this.checkActiveInjectedProvider(ethersConfig)
}
if (ethersConfig.coinbase) {
this.checkActiveCoinbaseProvider(ethersConfig)
}
Expand Down Expand Up @@ -565,7 +544,7 @@ export class Web3Modal extends Web3ModalScaffold {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()

if (providerType === 'injected' || providerType === 'eip6963') {
if (providerType === 'eip6963') {
provider?.emit('disconnect')
} else {
const walletConnectProvider = provider as unknown as EthereumProvider
Expand Down Expand Up @@ -654,18 +633,6 @@ export class Web3Modal extends Web3ModalScaffold {
}
}

private checkActiveInjectedProvider(config: ProviderType) {
const InjectedProvider = config.injected
const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID)

if (InjectedProvider) {
if (walletId === ConstantsUtil.INJECTED_CONNECTOR_ID) {
this.setInjectedProvider(config)
this.watchInjected(config)
}
}
}

private checkActiveCoinbaseProvider(config: ProviderType) {
const CoinbaseProvider = config.coinbase as unknown as ExternalProvider
const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID)
Expand Down Expand Up @@ -711,23 +678,6 @@ export class Web3Modal extends Web3ModalScaffold {
}
}

private async setInjectedProvider(config: ProviderType) {
window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.INJECTED_CONNECTOR_ID)
const InjectedProvider = config.injected

if (InjectedProvider) {
const { address, chainId } = await EthersHelpersUtil.getUserInfo(InjectedProvider)
if (address && chainId) {
EthersStoreUtil.setChainId(chainId)
EthersStoreUtil.setProviderType('injected')
EthersStoreUtil.setProvider(config.injected)
EthersStoreUtil.setIsConnected(true)
this.setAddress(address)
this.watchCoinbase(config)
}
}
}

private async setEIP6963Provider(provider: Provider, name: string) {
window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, name)

Expand Down Expand Up @@ -820,45 +770,6 @@ export class Web3Modal extends Web3ModalScaffold {
}
}

private watchInjected(config: ProviderType) {
const provider = config.injected

function disconnectHandler() {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()

provider?.removeListener('disconnect', disconnectHandler)
provider?.removeListener('accountsChanged', accountsChangedHandler)
provider?.removeListener('chainChanged', chainChangedHandler)
}

function accountsChangedHandler(accounts: string[]) {
const currentAccount = accounts?.[0]
if (currentAccount) {
EthersStoreUtil.setAddress(getOriginalAddress(currentAccount) as Address)
} else {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
}
}

function chainChangedHandler(chainId: string) {
if (chainId) {
const chain =
typeof chainId === 'string'
? EthersHelpersUtil.hexStringToNumber(chainId)
: Number(chainId)
EthersStoreUtil.setChainId(chain)
}
}

if (provider) {
provider.on('disconnect', disconnectHandler)
provider.on('accountsChanged', accountsChangedHandler)
provider.on('chainChanged', chainChangedHandler)
}
}

private watchEIP6963(provider: Provider) {
function disconnectHandler() {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
Expand Down Expand Up @@ -1199,29 +1110,6 @@ export class Web3Modal extends Web3ModalScaffold {
}
}
}
} else if (providerType === ConstantsUtil.INJECTED_CONNECTOR_ID && chain) {
const InjectedProvider = provider
if (InjectedProvider) {
try {
await InjectedProvider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: EthersHelpersUtil.numberToHexString(chain.chainId) }]
})
EthersStoreUtil.setChainId(chain.chainId)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (switchError: any) {
if (
switchError.code === EthersConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID ||
switchError.code === EthersConstantsUtil.ERROR_CODE_DEFAULT ||
switchError?.data?.originalError?.code ===
EthersConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID
) {
await EthersHelpersUtil.addEthereumChain(InjectedProvider, chain)
} else {
throw new Error('Chain is not supported')
}
}
}
} else if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID && chain) {
const EIP6963Provider = provider

Expand Down Expand Up @@ -1305,21 +1193,6 @@ export class Web3Modal extends Web3ModalScaffold {
})
}

if (config.injected) {
const injectedConnectorType =
PresetsUtil.ConnectorTypesMap[ConstantsUtil.INJECTED_CONNECTOR_ID]
if (injectedConnectorType) {
w3mConnectors.push({
id: ConstantsUtil.INJECTED_CONNECTOR_ID,
explorerId: PresetsUtil.ConnectorExplorerIds[ConstantsUtil.INJECTED_CONNECTOR_ID],
imageId: PresetsUtil.ConnectorImageIds[ConstantsUtil.INJECTED_CONNECTOR_ID],
imageUrl: this.options?.connectorImages?.[ConstantsUtil.INJECTED_CONNECTOR_ID],
name: PresetsUtil.ConnectorNamesMap[ConstantsUtil.INJECTED_CONNECTOR_ID],
type: injectedConnectorType
})
}
}

if (config.coinbase) {
w3mConnectors.push({
id: ConstantsUtil.COINBASE_CONNECTOR_ID,
Expand Down
33 changes: 6 additions & 27 deletions packages/ethers/src/utils/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { CoinbaseWalletSDK } from '@coinbase/wallet-sdk'

export interface ConfigOptions {
enableEIP6963?: boolean
enableInjected?: boolean
enableCoinbase?: boolean
enableEmail?: boolean
/**
* Use enableEIP6963 to show all injected wallets
* @deprecated
*/
enableInjected?: boolean
rpcUrl?: string
defaultChainId?: number
metadata: Metadata
Expand All @@ -15,40 +19,19 @@ export interface ConfigOptions {
export function defaultConfig(options: ConfigOptions) {
const {
enableEIP6963 = true,
enableInjected = true,
enableCoinbase = true,
enableEmail = false,

metadata,
rpcUrl,
defaultChainId
} = options

// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
let injectedProvider: Provider | undefined = undefined
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
let coinbaseProvider: Provider | undefined = undefined

const providers: ProviderType = { metadata }

function getInjectedProvider() {
if (injectedProvider) {
return injectedProvider
}

if (typeof window === 'undefined') {
return undefined
}

if (!window.ethereum) {
return undefined
}

// @ts-expect-error window.ethereum satisfies Provider
injectedProvider = window.ethereum

return injectedProvider
}

function getCoinbaseProvider() {
if (coinbaseProvider) {
return coinbaseProvider
Expand All @@ -70,10 +53,6 @@ export function defaultConfig(options: ConfigOptions) {
return coinbaseProvider
}

if (enableInjected) {
providers.injected = getInjectedProvider()
}

if (enableCoinbase && rpcUrl && defaultChainId) {
providers.coinbase = getCoinbaseProvider()
}
Expand Down
Loading

0 comments on commit cf64cc0

Please sign in to comment.