Ethereum Provider
Request data and signatures from Ethereum wallets
Ethereum Providers are an EIP-1193 compliant Provider object used by popular libraries like Web3.js, Wagmi, and Ethers.js. The Provider object exposes methods for fetching accounts and balances and sending transactions, and also provides listeners for events like account or connection changes.
The EIP-1193 Appendix has a complete definition of the methods available on the object, but in summary their APIs are:
provider.request(message)
- Send JSON RPC methods to the wallet and/or node gateway.provider.on(event, callback)
- Subscribe to events like account or connection changes.provider.removeListener(event, callback)
- Remove subscriptions added byprovider.on
Here are some examples of using the provider
:
1// Getting the connected accounts2const accounts: string[] = await provider.request({ method: 'eth_accounts' });34// Request a signed message5const account = accounts[0];6const message = Web3.utils.toHex('Hello!');7const signature: string = await provider.request({8 method: 'personal_sign',9 params: [message, accounts],10});11// The signature can be used to verify the signer using another library12// For example, using Web3.js:13const recoveredAccount: string = web3.eth.accounts.recover(message, signature);14const validSignature = recoveredAccount === account;1516// Send network currency (e.g. ETH)17const toAddress: string = '0x...';18const fromAddress: string = account;19// value is denominated in e.g. wei for ETH20// Usually a library should be used to convert21// between the denominations22const value: string = Web3.utils.toWei('1', 'ether');23const txHash: string = await provider.request({24 method: 'eth_sendTransaction',25 params: [26 {27 toAddress,28 fromAddress,29 value,30 },31 ],32});
For more documentation on the available JSON RPC methods, see the Ethereum.org docs and Plaid's provider
TypeScript definitions.
As shown above, many usages of the provider
are simplified by libraries such as Web3.js and Ethers.js. These libraries will use EIP-1193 providers to build other
abstractions and implement more complicated numberic or cryptographic operations:
1// Example: Web3.js https://web3js.readthedocs.io/en/v1.7.3/2const web3 = new Web3(provider);3const address = (await web3.eth.getAccounts())[0];4const balance = await web3.eth.getBalance(address);56// Example: Ethers.js https://docs.ethers.io/v5/7const web3Provider = new providers.Web3Provider(provider);8const signer = web3Provider.getSigner();9const address = await signer.getAddress();10const balance = await web3Provider.getBalance(address);
Your app should listen for important provider
events and respond appropriately:
1const handleChainChanged = (chainId) => {2 if (isCompatibleChain(chainId)) {3 // update UI4 } else {5 // alert the user6 }7};8provider.on('chainChanged', handleChainChanged);910const handleAccountsChanged = (accounts) => {11 if (accounts.length > 0) {12 // update UI13 } else {14 // alert the user15 }16};17provider.on('accountsChanged', accountsChanged);
Polygon
Polygon, like other EVM-compatible chains, utilizes the same EIP-1193 provider standard as Ethereum.