Skip to content

Commit 98d7e04

Browse files
author
zuozhuo
committed
init
0 parents  commit 98d7e04

16 files changed

Lines changed: 3901 additions & 0 deletions

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.eslintrc.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports = {
2+
parserOptions: {
3+
ecmaVersion: 2018,
4+
},
5+
extends: [
6+
],
7+
plugins: [
8+
'json',
9+
],
10+
globals: {
11+
window: true,
12+
},
13+
ignorePatterns: [
14+
'!.eslintrc.js',
15+
'node_modules/',
16+
'dist/',
17+
],
18+
overrides: [
19+
{
20+
files: ['*.ts'],
21+
extends: [
22+
],
23+
},
24+
{
25+
files: [
26+
'.eslintrc.js',
27+
],
28+
parserOptions: {
29+
sourceType: 'script',
30+
},
31+
},
32+
],
33+
}

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* text=auto
2+
3+
yarn.lock linguist-generated=false

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Lines starting with '#' are comments.
2+
# Each line is a file pattern followed by one or more owners.
3+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Build, Lint, and Test
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
build-lint-test:
10+
name: Build, Lint, and Test
11+
runs-on: ubuntu-20.04
12+
strategy:
13+
matrix:
14+
node-version: [12.x, 14.x, 16.x]
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Use Node.js ${{ matrix.node-version }}
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node-version }}
21+
- run: yarn --frozen-lockfile
22+
- run: yarn build
23+
- run: yarn lint
24+
- run: yarn test
25+
all-jobs-pass:
26+
name: All jobs pass
27+
runs-on: ubuntu-20.04
28+
needs:
29+
- build-lint-test
30+
steps:
31+
- run: echo "Great success!"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
dist/
3+
*.log

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v12

CHANGELOG.md

Whitespace-only changes.

LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ISC License
2+
3+
Copyright (c) 2020 OneKey
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)