javascriptreactjsblockchainweb3jstrustwallet

TrustWallet not injecting Ethereum provider with deeplinking into mobile IOS TrustWallet app


const connectTrustWallet = () => {
    // alert(`is window.trustwallet.ethereum ${window.trustwallet?.ethereum}`);
    // alert(`is window.ethereum ${window.ethereum}`);
    if (isOnMobile() && linkRef.current && (!window.ethereum || !window.trustwallet.ethereum)) {
      linkRef.current.href = `https://link.trustwallet.com/open_url?url=${document.URL}`;
      linkRef.current.click();
      return;
    }
    connect('trust');
  }

When deeplinking into TrustWallet ios mobile app, both window.ethereum and window.trustwallet?.ethereum are undefined.

Does TrustWallet inject the provider in a different place then?

Or should I enable it somehow so that it injects it?

I was not able to find it on their Developer docs.


Please help me figure that out!


Here's a Codesandbox with an example app:

https://codesandbox.io/s/falling-sea-qnq46l?file=/src/index.html

My first suspect was ViteJS that we use in our production app (because I thought the service workers might be the culprit). (References: https://github.com/trustwallet/trust-web3-provider/issues/139, https://github.com/MetaMask/detect-provider/issues/20#issuecomment-1353333471)


But it doesn't seem to work even in JS/HTML apps either (no service workers are to doubt for sure then!).


Solution

  • I had the same problem, here's how I solved it.

    The problem is that you don't specify coin_id in the query parameters and the dapp browser opens like a normal browser. If you specify the coin_id parameter, trust will open the dapp browser correctly and inject window.ethereum there.

    just replace your link with this one https://link.trustwallet.com/open_url?coin_id=60&url=${document.URL}