Skip to content

Commit

Permalink
fix: trustWallet detection mobile in-app browser (#1583)
Browse files Browse the repository at this point in the history
* fix: trust wallet detection on mobile, adopt shared provider util

* chore: changeset
  • Loading branch information
DanielSinclair authored Oct 25, 2023
1 parent e2075b3 commit 9f68c30
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-elephants-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rainbow-me/rainbowkit": patch
---

Fixed an issue with Trust Wallet detection in Trust's in-app mobile browser
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { InjectedConnectorOptions } from '@wagmi/core/connectors/injected';
import { InjectedConnector } from 'wagmi/connectors/injected';
import { Chain } from '../../../components/RainbowKitProvider/RainbowKitChainContext';
import { getWalletConnectUri } from '../../../utils/getWalletConnectUri';
import { isMobile } from '../../../utils/isMobile';
import { InstructionStepName, Wallet } from '../../Wallet';
import {
getInjectedConnector,
hasInjectedProvider,
} from '../../getInjectedConnector';
import { getWalletConnectConnector } from '../../getWalletConnectConnector';
import type {
WalletConnectConnectorOptions,
Expand All @@ -29,46 +33,6 @@ export interface TrustWalletOptions {
walletConnectOptions?: WalletConnectConnectorOptions;
}

function getTrustWalletInjectedProvider(): Window['ethereum'] {
const isTrustWallet = (ethereum: NonNullable<Window['ethereum']>) => {
// Identify if Trust Wallet injected provider is present.
const trustWallet = !!ethereum.isTrust;

return trustWallet;
};

const injectedProviderExist =
typeof window !== 'undefined' && typeof window.ethereum !== 'undefined';

// No injected providers exist.
if (!injectedProviderExist) {
return;
}

// Trust Wallet injected provider is available in the global scope.
// There are cases that some cases injected providers can replace window.ethereum
// without updating the ethereum.providers array. To prevent issues where
// the TW connector does not recognize the provider when TW extension is installed,
// we begin our checks by relying on TW's global object.
if (window['trustwallet']) {
return window['trustwallet'];
}

// Trust Wallet was injected into window.ethereum.
if (isTrustWallet(window.ethereum!)) {
return window.ethereum;
}

// Trust Wallet provider might be replaced by another
// injected provider, check the providers array.
if (window.ethereum?.providers) {
// ethereum.providers array is a non-standard way to
// preserve multiple injected providers. Eventually, EIP-5749
// will become a living standard and we will have to update this.
return window.ethereum.providers.find(isTrustWallet);
}
}

export const trustWallet = ({
chains,
projectId,
Expand All @@ -77,7 +41,9 @@ export const trustWallet = ({
...options
}: (TrustWalletLegacyOptions | TrustWalletOptions) &
InjectedConnectorOptions): Wallet => {
const isTrustWalletInjected = Boolean(getTrustWalletInjectedProvider());
const isTrustWalletInjected = isMobile()
? hasInjectedProvider('isTrust')
: hasInjectedProvider('isTrustWallet');
const shouldUseWalletConnect = !isTrustWalletInjected;

return {
Expand Down Expand Up @@ -120,13 +86,9 @@ export const trustWallet = ({
version: walletConnectVersion,
options: walletConnectOptions,
})
: new InjectedConnector({
chains,
options: {
getProvider: getTrustWalletInjectedProvider,
...options,
},
});
: isMobile()
? getInjectedConnector({ flag: 'isTrust', chains, options })
: getInjectedConnector({ flag: 'isTrustWallet', chains, options });

const mobileConnector = {
getUri: shouldUseWalletConnect ? getUriMobile : undefined,
Expand Down

2 comments on commit 9f68c30

@vercel
Copy link

@vercel vercel bot commented on 9f68c30 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 9f68c30 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.