WalletConnect Web3 Provider
Web3 Provider for WalletConnect
For more details, read the documentation
Setup
import Web3 from "web3";
import WalletConnectProvider from "@walletconnect/web3-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",
]
}
});