Ethereum & EVM chains

Provider discovery with EIP-6963

Modern browsers often have multiple wallet extensions installed. EIP-6963arrow-up-right standardizes provider discovery through a well-defined event mechanism, allowing each wallet to announce itself without conflicts. This replaces the legacy window.ethereum pattern where wallets would overwrite each other.

Trust Wallet supports EIP-6963 to announce itself. You should always use this discovery mechanism to locate the Trust Wallet EIP-1193 provider.

/**
 * Represents the assets needed to display a wallet
 */
interface EIP6963ProviderInfo {
  uuid: string;
  name: string;
  icon: string;
  rdns: string;
}

// EIP-6963 Provider Detail containing wallet info and provider instance
interface EIP6963ProviderDetail {
  info: EIP6963ProviderInfo;
  provider: EIP1193Provider;
}

// Announce Event dispatched by a Wallet
interface EIP6963AnnounceProviderEvent extends CustomEvent {
  type: "eip6963:announceProvider";
  detail: EIP6963ProviderDetail;
}

// Request Event dispatched by a DApp
interface EIP6963RequestProviderEvent extends Event {
  type: "eip6963:requestProvider";
}

// Store all announced providers by their UUID identifier
const announcedProviders = new Map<string, EIP6963ProviderDetail>();

function initializeEIP6963() {
  const onAnnounce = (event: EIP6963AnnounceProviderEvent) => {
    const { info, provider } = event.detail;
    const key = info.uuid
    // Avoid duplicates
    if(announcedProviders.has(key)) return

    announcedProviders.set(key, { info, provider });
  };

  // Listen for wallet announcements
  window.addEventListener("eip6963:announceProvider", onAnnounce);

  // Request all wallets to announce themselves
  window.dispatchEvent(new Event("eip6963:requestProvider"));

  // Return cleanup function
  return () => {
    window.removeEventListener("eip6963:announceProvider", onAnnounce);
  };
}

// Start listening for wallet announcements
initializeEIP6963()

Initialize discovery by calling initializeEIP6963() early in your application lifecycle. This allows you to detect which wallet extensions are installed in the user's browser.

Detecting Trust Wallet

Use the getTrustWalletProvider() function to check if Trust Wallet is installed by matching its reverse DNS identifier (com.trustwallet.app):

If Trust Wallet is not installed, prompt users to download it:

Connecting to Trust Wallet

Once you have the Trust Wallet provider, use its provider.request() method (EIP-1193arrow-up-right) to make wallet RPC calls.

Request user accounts (connect)

The eth_requestAccounts method (EIP-1102arrow-up-right) prompts the user to authorize your dApp and share their account addresses.

Check connected accounts

The eth_accounts method (EIP-1474arrow-up-right) returns currently connected accounts without triggering a user prompt.

Listening for account changes

The accountsChanged event (EIP-1193arrow-up-right) fires when the user switches accounts or disconnects from your dApp.

Working with networks

Listen for network changes

The chainChanged event (EIP-1193arrow-up-right) notifies your dApp when the user switches to a different network.

Get current network

The eth_chainId method (EIP-695arrow-up-right) returns the currently active network's chain ID as a hexadecimal string.

Request network switch

The wallet_switchEthereumChain method (EIP-3326arrow-up-right) prompts the user to switch to a different network.

Add a new network

The wallet_addEthereumChain method (EIP-3085arrow-up-right) requests that the user add a custom network to Trust Wallet.

Signing messages

Sign arbitrary messages

The personal_sign method (EIP-1474arrow-up-right) requests a signature for an arbitrary message. This is commonly used for authentication and proof of ownership.

Sign typed data (EIP-712)

The eth_signTypedData_v4 method (EIP-712arrow-up-right) signs structured data, providing better UX and security than raw message signing.

Managing assets

Add token to wallet

The wallet_watchAsset method (EIP-747arrow-up-right) requests that the user track a token in Trust Wallet.

Interacting with smart contracts

Send transactions to contracts

The eth_sendTransaction method (EIP-1474arrow-up-right) creates a transaction that modifies blockchain state. This requires user approval and gas fees.

When interacting with smart contracts, you need to encode function calls into the data field of transactions. This encoding follows the Contract ABI Specificationarrow-up-right.

Understanding the data field

The data field contains:

  1. Function selector: First 4 bytes (8 hex characters) - the keccak256 hash of the function signature

  2. Encoded parameters: The function arguments encoded according to ABI rules

For example, calling transfer(address recipient, uint256 amount):

  • Function signature: transfer(address,uint256)

  • Function selector: 0xa9059cbb (first 4 bytes of keccak256 hash)

  • Encoded parameters: recipient address (32 bytes) + amount (32 bytes)

To simplify this process you can use libraries like Viemarrow-up-right, Voltairearrow-up-right or Ethers.jsarrow-up-right to handle ABI encoding automatically. These libraries take care of the complex encoding process for you.

High level abstractions

While the manual provider approach shown in this guide is useful for understanding the underlying protocol, production applications can use higher-level abstractions that handle the complexity for you.

Wagmi (React/Vue) and Wagmi Core (framework-agnostic)

We recommend Wagmiarrow-up-right, which provide a higher-level, TypeScript-first API on top of EIP-1193 providers:

  • Automatic EIP-6963 discovery: Detects injected wallets (including Trust Wallet) without manual event wiring

  • Type-safe APIs:

    • Wagmi (React/Vue): hooks like useAccount, useConnect, useWriteContract, useReadContract, and more

    • Wagmi Core (any framework / vanilla JS): action-based functions like getAccount, connect, writeContract, readContract

  • Built-in state management (Wagmi): handles connection state, caching, and automatic reconnection

Need help?

If you have questions or need assistance integrating Trust Wallet into your application, feel free to open a discussion on our GitHub Discussionsarrow-up-right.

Last updated