-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Trung-Tin Pham <[email protected]>
- Loading branch information
1 parent
efb4f5e
commit 21aa4bf
Showing
16 changed files
with
427 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as MetaMaskConnector } from './metamask'; | ||
export { default as RainbowConnector } from './rainbow'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { InjectedConnectorOptions, WindowProvider } from '@wagmi/core'; | ||
import { InjectedConnector } from 'wagmi/connectors/injected'; | ||
import { Chain } from 'wagmi'; | ||
import { getExplicitInjectedProvider } from './utils'; | ||
|
||
// handle multi injected connectors problem with providers | ||
// Related: https://github.com/wevm/wagmi/discussions/742 | ||
export default class MetaMaskConnector extends InjectedConnector { | ||
readonly id = 'metamask'; | ||
readonly name = 'Metamask'; | ||
readonly ready = | ||
typeof window != 'undefined' && !!this.#findProvider(window.ethereum); | ||
|
||
#provider?: Window['ethereum']; | ||
|
||
constructor({ | ||
chains, | ||
options, | ||
}: { chains?: Chain[]; options?: InjectedConnectorOptions } = {}) { | ||
super({ chains, options: { name: 'MetaMask Wallet', ...options } }); | ||
} | ||
|
||
async getProvider() { | ||
if (typeof window !== 'undefined') { | ||
this.#provider = this.#findProvider(window.ethereum); | ||
// this.#provider = getExplicitInjectedProvider('isMetaMask'); | ||
} | ||
return this.#provider; | ||
} | ||
|
||
#getReady(provider?: WindowProvider) { | ||
if (!provider?.isMetaMask) return; | ||
return provider; | ||
} | ||
|
||
#findProvider(provider?: WindowProvider) { | ||
if (provider?.providers) | ||
return getExplicitInjectedProvider(provider.providers, 'isMetaMask'); | ||
return this.#getReady(provider); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { InjectedConnectorOptions, WindowProvider } from '@wagmi/core'; | ||
import { InjectedConnector } from 'wagmi/connectors/injected'; | ||
import { Chain } from 'wagmi'; | ||
import { getExplicitInjectedProvider } from './utils'; | ||
|
||
// handle multi injected connectors problem with providers | ||
// Related: https://github.com/wevm/wagmi/discussions/742 | ||
export default class RainbowConnector extends InjectedConnector { | ||
readonly id = 'rainbow'; | ||
readonly name = 'rainbow'; | ||
readonly ready = | ||
typeof window != 'undefined' && !!this.#findProvider(window.ethereum); | ||
|
||
#provider?: Window['ethereum']; | ||
|
||
constructor({ | ||
chains, | ||
options, | ||
}: { chains?: Chain[]; options?: InjectedConnectorOptions } = {}) { | ||
super({ chains, options: { name: 'Rainbow Wallet', ...options } }); | ||
} | ||
|
||
async getProvider() { | ||
if (typeof window !== 'undefined') { | ||
this.#provider = this.#findProvider(window.ethereum); | ||
} | ||
return this.#provider; | ||
} | ||
|
||
#getReady(provider?: WindowProvider) { | ||
if (!provider?.isRainbow) return; | ||
return provider; | ||
} | ||
|
||
#findProvider(provider?: WindowProvider) { | ||
if (provider?.providers) | ||
return getExplicitInjectedProvider(provider.providers, 'isRainbow'); | ||
return this.#getReady(provider); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { InjectedProviderFlags, WindowProvider } from 'wagmi/window'; | ||
|
||
/* | ||
* Returns the explicit window provider that matches the flag and the flag is true | ||
*/ | ||
export function getExplicitInjectedProvider( | ||
providers: WindowProvider[] | undefined, | ||
flag: keyof InjectedProviderFlags | ||
): WindowProvider | undefined { | ||
if (typeof window === 'undefined' || typeof window.ethereum === 'undefined') | ||
return; | ||
return providers | ||
? providers | ||
.filter((provider) => provider[flag]) | ||
// if not targeting rainbow wallet, prevent rainbow wallet from being used | ||
.find( | ||
(provider: any) => | ||
provider?.['rainbowIsDefaultProvider'] === | ||
(flag === 'isRainbow' ? true : undefined) | ||
) | ||
: window.ethereum[flag] | ||
? window.ethereum | ||
: undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.