|
| 1 | +# @onekeyhq/detect-provider |
| 2 | + |
| 3 | +A tiny utility for detecting the OneKey Ethereum provider, or any provider injected at `window.ethereum`. |
| 4 | + |
| 5 | +It has 0 dependencies and works out of the box in any modern browser, for synchronously and asynchronously injected providers. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +Keep in mind that the providers detected by this package may or may not support [the Ethereum JavaScript Provider API](https://eips.ethereum.org/EIPS/eip-1193). |
| 10 | +Please consult [the OneKey documentation](https://docs.onekey.so/guide/ethereum-provider.html) to learn how to use our provider. |
| 11 | + |
| 12 | +### Node.js |
| 13 | + |
| 14 | +```javascript |
| 15 | +import detectEthereumProvider from '@onekeyhq/detect-provider' |
| 16 | + |
| 17 | +const provider = await detectEthereumProvider() |
| 18 | + |
| 19 | +if (provider) { |
| 20 | + |
| 21 | + console.log('Ethereum successfully detected!') |
| 22 | + |
| 23 | + // From now on, this should always be true: |
| 24 | + // provider === window.ethereum |
| 25 | + |
| 26 | + // Access the decentralized web! |
| 27 | + |
| 28 | + // Legacy providers may only have ethereum.sendAsync |
| 29 | + const chainId = await provider.request({ |
| 30 | + method: 'eth_chainId' |
| 31 | + }) |
| 32 | +} else { |
| 33 | + |
| 34 | + // if the provider is not detected, detectEthereumProvider resolves to null |
| 35 | + console.error('Please install OneKey!', error) |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### HTML |
| 40 | + |
| 41 | +```html |
| 42 | +<script src="https://unpkg.com/@onekeyhq/detect-provider/dist/detect-provider.min.js"></script> |
| 43 | +<script type="text/javascript"> |
| 44 | + const provider = await detectEthereumProvider() |
| 45 | +
|
| 46 | + if (provider) { |
| 47 | + // handle provider |
| 48 | + } else { |
| 49 | + // handle no provider |
| 50 | + } |
| 51 | +</script> |
| 52 | +``` |
| 53 | + |
| 54 | +### Options |
| 55 | + |
| 56 | +The exported function takes an optional `options` object. |
| 57 | +If invalid options are provided, an error will be thrown. |
| 58 | +All options have default values. |
| 59 | + |
| 60 | +#### `options.mustBeOneKey` |
| 61 | + |
| 62 | +Type: `boolean` |
| 63 | + |
| 64 | +Default: `false` |
| 65 | + |
| 66 | +Whether `window.ethereum.isOneKey === true` is required for the returned Promise to resolve. |
| 67 | + |
| 68 | +#### `options.silent` |
| 69 | + |
| 70 | +Type: `boolean` |
| 71 | + |
| 72 | +Default: `false` |
| 73 | + |
| 74 | +Whether error messages should be logged to the console. |
| 75 | +Does not affect errors thrown due to invalid options. |
| 76 | + |
| 77 | +#### `options.timeout` |
| 78 | + |
| 79 | +Type: `number` |
| 80 | + |
| 81 | +Default: `3000` |
| 82 | + |
| 83 | +How many milliseconds to wait for asynchronously injected providers. |
| 84 | + |
| 85 | +## Advanced Topics |
| 86 | + |
| 87 | +### Synchronous and Asynchronous Injection |
| 88 | + |
| 89 | +Providers can be either synchronously or asynchronously injected: |
| 90 | + |
| 91 | +- _Synchronously_ injected providers will be available by the time website code starts executing. |
| 92 | +- _Asynchronously_ injected providers may not become available until later in the page lifecycle. |
| 93 | + |
| 94 | +The OneKey _extension_ provider is synchronously injected, while the OneKey _mobile_ provider is asynchronously injected. |
| 95 | + |
| 96 | +To notify sites of asynchronous injection, OneKey dispatches the `ethereum#initialized` event on `window` immediately after the provider has been set as `window.ethereum`. |
| 97 | +This package relies on that event to detect asynchronous injection. |
| 98 | + |
| 99 | +### Overwriting or Modifying `window.ethereum` |
| 100 | + |
| 101 | +The detected provider object returned by this package will strictly equal (`===`) `window.ethereum` for the entire page lifecycle, unless `window.ethereum` is overwritten. |
| 102 | +In general, consumers should never overwrite `window.ethereum` or attempt to modify the provider object. |
| 103 | + |
| 104 | +If, as a dapp developer, you notice that the provider returned by this package does not strictly equal `window.ethereum`, something is wrong. |
| 105 | +This may happen, for example, if the user has multiple wallets installed. |
| 106 | +After confirming that your code and dependencies are not modifying or overwriting `window.ethereum`, you should ask the user to ensure that they only have a single provider-injecting wallet enabled at any one time. |
0 commit comments