What is @walletconnect/ethereum-provider?
@walletconnect/ethereum-provider is an npm package that allows developers to integrate WalletConnect functionality into their Ethereum-based applications. It provides a bridge between decentralized applications (dApps) and mobile wallets, enabling users to interact with dApps using their mobile wallets.
What are @walletconnect/ethereum-provider's main functionalities?
Initialize WalletConnect Provider
This feature allows you to initialize the WalletConnect provider with your Infura ID. The provider can then be enabled to start connecting with mobile wallets.
const WalletConnectProvider = require('@walletconnect/ethereum-provider');
const provider = new WalletConnectProvider({
infuraId: 'YOUR_INFURA_ID' // Required
});
await provider.enable();
Send Transaction
This feature allows you to send a transaction using the WalletConnect provider. You need to specify the transaction parameters such as from, to, value, gas, and gasPrice.
const tx = await provider.request({
method: 'eth_sendTransaction',
params: [{
from: '0xYourAddress',
to: '0xRecipientAddress',
value: '0xAmountInWei',
gas: '0xGasLimit',
gasPrice: '0xGasPrice'
}]
});
Sign Message
This feature allows you to sign a message using the WalletConnect provider. You need to provide the message and the address of the signer.
const message = 'Hello, WalletConnect!';
const signedMessage = await provider.request({
method: 'personal_sign',
params: [message, '0xYourAddress']
});
Switch Ethereum Chain
This feature allows you to switch the Ethereum chain using the WalletConnect provider. You need to specify the chain ID of the desired network.
await provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x1' }] // Chain ID for Ethereum Mainnet
});
Other packages similar to @walletconnect/ethereum-provider
web3modal
Web3Modal is a library that allows developers to easily integrate multiple wallet providers into their dApps. It supports WalletConnect, MetaMask, and other popular wallet providers. Compared to @walletconnect/ethereum-provider, Web3Modal offers a more comprehensive solution for integrating various wallet providers.
ethers
Ethers.js is a library for interacting with the Ethereum blockchain and its ecosystem. It provides utilities for connecting to Ethereum nodes, signing transactions, and interacting with smart contracts. While it does not specifically focus on WalletConnect, it can be used in conjunction with WalletConnect to achieve similar functionality.
web3
Web3.js is a collection of libraries that allow you to interact with a local or remote Ethereum node using HTTP, IPC, or WebSocket. It provides functionalities for sending transactions, interacting with smart contracts, and more. Like ethers.js, it can be used alongside WalletConnect for similar purposes.
WalletConnect Ethereum Provider
Ethereum Provider for WalletConnect
For more details, read the documentation
Setup
import Web3 from "web3";
import WalletConnectProvider from "@walletconnect/ethereum-provider";
const provider = new WalletConnectProvider({
infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
});
await provider.enable();
const web3 = new Web3(provider);
Events (EIP-1193)
provider.on("accountsChanged", (accounts: string[]) => {
console.log(accounts);
});
provider.on("chainChanged", (chainId: number) => {
console.log(chainId);
});
provider.on("connect", () => {
console.log("connect");
});
provider.on("disconnect", (code: number, reason: string) => {
console.log(code, reason);
});
Provider Methods
interface RequestArguments {
method: string;
params?: unknown[] | object;
}
const result = await provider.request(payload: RequestArguments);
await provider.disconnect()
Web3 Methods
const accounts = await web3.eth.getAccounts();
const chainId = await web3.eth.chainId();
const networkId = await web3.eth.net.getId();
const txHash = await web3.eth.sendTransaction(tx);
const signedTx = await web3.eth.signTransaction(tx);
const signedMessage = await web3.eth.sign(msg);
const signedTypedData = await web3.eth.signTypedData(msg);
Provider Options
Required
In order to resolve non-signing requests you need to provide one of the following:
Infura ID
The infuraId will support the following chainId's: Mainnet (1), Ropsten (3), Rinkeby(4), Goerli (5) and Kovan (42)
const provider = new WalletConnectProvider({
infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
});
RPC URL Mapping
The RPC URL mapping should indexed by chainId and it requires at least one value.
const provider = new WalletConnectProvider({
rpc: {
1: "https://mainnet.mycustomnode.com",
3: "https://ropsten.mycustomnode.com",
100: "https://dai.poa.network",
},
});
Optional
You can also customize the connector through the provider using the following options
Bridge URL
Use your own hosted bridge by providing the url
const provider = new WalletConnectProvider({
infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
bridge: "https://bridge.myhostedserver.com",
});
Disable QR Code Modal
Use your own custom qrcode modal and disable the built-in one
const provider = new WalletConnectProvider({
infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
qrcode: false,
});
provider.connector.on("display_uri", (err, payload) => {
const uri = payload.params[0];
CustomQRCodeModal.display(uri);
});
Filter Linking Options
If you would like to reduce the number of linking options or customize its order, you can provide an array of wallet names.
Providing empty whitelist disables linking.
const provider = new WalletConnectProvider({
infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
qrcodeModalOptions: {
mobileLinks: [
"rainbow",
"metamask",
"argent",
"trust",
"imtoken",
"pillar",
],
desktopLinks: [
"encrypted ink",
]
}
});