EIP-6963: Multi Injected Provider Discovery
Introduction
EIP-6963 proposes the "Multi Injected Provider Discovery" standard, which aims to enhance the discoverability and interaction with multiple injected Ethereum providers in a browser environment. Injected providers refer to browser extensions or other injected scripts that provide access to an Ethereum provider within the context of a web application.
Web3.js library has utility function for discovery of injected providers using requestEIP6963Providers()
function. When requestEIP6963Providers()
is called it returns Promise object that resolves to Map<string, EIP6963ProviderDetail>
object containing list of providers. For updated providers eip6963:providersMapUpdated
event is emitted and it has updated Map object.
eip6963ProvidersMap
object has provider's UUID
as keys and EIP6963ProviderDetail
as values. EIP6963ProviderDetail
is:
export interface EIP6963ProviderDetail {
info: EIP6963ProviderInfo;
provider: EIP1193Provider;
}
where info
has details of provider containing UUID, name, Icon and RDNS as defined in EIP-6963:
export interface EIP6963ProviderInfo {
uuid: string;
name: string;
icon: string;
rdns: string;
}
provider
in EIP6963ProviderDetail
is EIP1193Provider
and it contains actual provider that can be injected in web3 instance.
Following code snippet demonstrates usage of requestEIP6963Providers()
function for providers discovery.
//Assuming multiple providers are installed in browser.
import { Web3 } from 'web3';
window.addEventListener('web3:providersMapUpdated', (event) => {
console.log(event.detail); // This will log the populated providers map object
// add logic here for updating UI of your DApp
});
// Call the function and wait for the promise to resolve
let providers = await Web3.requestEIP6963Providers();
for (const [key, value] of providers) {
console.log(value);
/* Based on your DApp's logic show use list of providers and get selected provider's UUID from user for injecting its EIP6963ProviderDetail.provider EIP1193 object into web3 object */
if (value.info.name === 'MetaMask') {
const web3 = new Web3(value.provider);
// now you can use web3 object with injected provider
console.log(await web3.eth.getTransaction('0x82512812c11f56aa2474a16d5cc8916b73cd6ed96bf9b8defb3499ec2d9070cb'));
}
}