|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +sidebar_label: 'EIP-6963: Multi Injected Provider Discovery' |
| 4 | +--- |
| 5 | + |
| 6 | +# EIP-6963: Multi Injected Provider Discovery |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +Web3.js library has utility function for discovery of injected providers using `requestEIP6963Providers()` function. When `requestEIP6963Providers()` is used it returns `eip6963Providers` Map object. This Map object is in global scope so every time `requestEIP6963Providers()` function is called it will update Map object and return it. |
| 13 | + |
| 14 | +`eip6963Providers` Map object has provider's `UUID` as keys and `EIP6963ProviderDetail` as values. `EIP6963ProviderDetail` is: |
| 15 | + |
| 16 | +```ts |
| 17 | +export interface EIP6963ProviderDetail { |
| 18 | + info: EIP6963ProviderInfo; |
| 19 | + provider: EIP1193Provider; |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +where `info` has details of provider containing UUID, name, Icon and RDNS as defined in EIP-6963: |
| 24 | + |
| 25 | +```ts |
| 26 | +export interface EIP6963ProviderInfo { |
| 27 | + uuid: string; |
| 28 | + name: string; |
| 29 | + icon: string; |
| 30 | + rdns: string; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +`provider` in `EIP6963ProviderDetail` is `EIP1193Provider` and it contains actual provider that can be injected in web3 instance. |
| 35 | + |
| 36 | +Following code snippet demonstrates usage of `requestEIP6963Providers()` function for providers discovery. |
| 37 | + |
| 38 | +```ts |
| 39 | +//Assuming multiple providers are installed in browser. |
| 40 | + |
| 41 | +import { Web3 } from 'web3'; |
| 42 | + |
| 43 | +const providers = Web3.requestEIP6963Providers(); |
| 44 | + |
| 45 | +for (const [key, value] of providers) { |
| 46 | + console.log(value); |
| 47 | + |
| 48 | +/* 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 */ |
| 49 | + |
| 50 | + if (value.info.name === 'MetaMask') { |
| 51 | + const web3 = new Web3(value.provider); |
| 52 | + |
| 53 | + // now you can use web3 object with injected provider |
| 54 | + console.log(await web3.eth.getTransaction('0x82512812c11f56aa2474a16d5cc8916b73cd6ed96bf9b8defb3499ec2d9070cb')); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +``` |
0 commit comments