mirror of https://github.com/status-im/EIPs.git
Automatically merged updates to draft EIP(s) 1193 (#2092)
Hi, I'm a bot! This change was automatically merged because: - It only modifies existing Draft or Last Call EIP(s) - The PR was approved or written by at least one author of each modified EIP - The build is passing
This commit is contained in:
parent
5b60eb679b
commit
36f02dffec
|
@ -27,36 +27,19 @@ Promise resolves with `result` or rejects with `Error`.
|
|||
|
||||
See the [available methods](https://github.com/ethereum/wiki/wiki/JSON-RPC#json-rpc-methods).
|
||||
|
||||
#### eth_requestAccounts
|
||||
|
||||
By default, the provider supplied to a new dapp has is a "read-only" provider with no accounts authenticated. See [EIP 1102: Opt-in account exposure](https://eips.ethereum.org/EIPS/eip-1102).
|
||||
|
||||
To request accounts, call `ethereum.send('eth_requestAccounts')`. This will ask the user which account(s) they would like to authenticate to the dapp.
|
||||
|
||||
Promise resolves with an array of the enabled account(s) addresses.
|
||||
|
||||
### Events
|
||||
|
||||
Events are emitted using [EventEmitter](https://nodejs.org/api/events.html).
|
||||
|
||||
#### notification
|
||||
|
||||
All subscriptions from the node emit on "subscription type" (e.g. `eth_subscription`, or `ssh_subscription`). Attach listeners with:
|
||||
All subscriptions from the node emit on notification. Attach listeners with:
|
||||
|
||||
```js
|
||||
ethereum.on('eth_subscription', listener: (result: any) => void): this;
|
||||
ethereum.on('notification', listener: (result: any) => void): this;
|
||||
```
|
||||
|
||||
To create a subscription, call `ethereum.send('eth_subscribe')` or `ethereum.send('shh_subscribe')`. The subscription object will emit through the specific subscription type.
|
||||
|
||||
The result object will look as follows:
|
||||
|
||||
```js
|
||||
{
|
||||
"subscription":"0xc3b33aa549fb9a60e95d21862596617c",
|
||||
"result": {...}
|
||||
}
|
||||
```
|
||||
To create a subscription, call `ethereum.send('eth_subscribe', [])` or `ethereum.send('shh_subscribe', [])`.
|
||||
|
||||
See the [eth subscription methods](https://github.com/ethereum/go-ethereum/wiki/RPC-PUB-SUB#supported-subscriptions) and [shh subscription methods](https://github.com/ethereum/go-ethereum/wiki/Whisper-v6-RPC-API#shh_subscribe).
|
||||
|
||||
|
@ -112,6 +95,7 @@ const ethereum = window.ethereum;
|
|||
|
||||
// A) Set provider in web3.js
|
||||
var web3 = new Web3(ethereum);
|
||||
// web3.eth.getBlock('latest', true).then(...)
|
||||
|
||||
|
||||
// B) Use provider object directly
|
||||
|
@ -128,26 +112,7 @@ ethereum
|
|||
);
|
||||
});
|
||||
|
||||
|
||||
// Example 2: Request accounts
|
||||
ethereum
|
||||
.send('eth_requestAccounts')
|
||||
.then(accounts => {
|
||||
if (accounts.length > 0) {
|
||||
console.log(`Accounts enabled:\n${accounts.join('\n')}`);
|
||||
} else {
|
||||
console.error(`No accounts enabled.`);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(
|
||||
`Error requesting accounts: ${error.message}.
|
||||
Code: ${error.code}. Data: ${error.data}`
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
// Example 3: Log available accounts
|
||||
// Example 2: Log available accounts
|
||||
ethereum
|
||||
.send('eth_accounts')
|
||||
.then(accounts => {
|
||||
|
@ -161,13 +126,13 @@ ethereum
|
|||
});
|
||||
|
||||
|
||||
// Example 4: Log new blocks
|
||||
// Example 3: Log new blocks
|
||||
let subId;
|
||||
ethereum
|
||||
.send('eth_subscribe', ['newHeads'])
|
||||
.then(subscriptionId => {
|
||||
subId = subscriptionId;
|
||||
ethereum.on('eth_subscription', result => {
|
||||
ethereum.on('notification', result => {
|
||||
if (result.subscription === subscriptionId) {
|
||||
if (result.result instanceof Error) {
|
||||
const error = result.result;
|
||||
|
@ -188,22 +153,9 @@ ethereum
|
|||
Code: ${error.code}. Data: ${error.data}`
|
||||
);
|
||||
});
|
||||
|
||||
// to unsubscribe
|
||||
ethereum
|
||||
.send('eth_unsubscribe', [subId])
|
||||
.then(result => {
|
||||
console.log(`Unsubscribed newHeads subscription ${subId}`);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(
|
||||
`Error unsubscribing newHeads subscription: ${error.message}.
|
||||
Code: ${error.code}. Data: ${error.data}`
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
// Example 5: Log when accounts change
|
||||
// Example 4: Log when accounts change
|
||||
const logAccounts = accounts => {
|
||||
console.log(`Accounts:\n${accounts.join('\n')}`);
|
||||
};
|
||||
|
@ -211,7 +163,7 @@ ethereum.on('accountsChanged', logAccounts);
|
|||
// to unsubscribe
|
||||
ethereum.removeListener('accountsChanged', logAccounts);
|
||||
|
||||
// Example 6: Log if connection ends
|
||||
// Example 5: Log if connection ends
|
||||
ethereum.on('close', (code, reason) => {
|
||||
console.log(`Ethereum provider connection closed: ${reason}. Code: ${code}`);
|
||||
});
|
||||
|
@ -227,18 +179,6 @@ If an error occurs during processing, such as an HTTP error or internal parsing
|
|||
|
||||
If the request requires an account that is not yet authenticated, the Promise **MUST** reject with Error code 4100.
|
||||
|
||||
#### eth_requestAccounts
|
||||
|
||||
The provider supplied to a new dapp **MUST** be a "read-only" provider: authenticating no accounts by default, returning a blank array for `eth_accounts`, and rejecting any methods that require an account with Error code `4100`.
|
||||
|
||||
If the dapp has been previously authenticated and remembered by the user, then the provider supplied on load **MAY** automatically be enabled with the previously authenticated accounts.
|
||||
|
||||
If no accounts are authenticated, the `eth_requestAccounts` method **MUST** ask the user which account(s) they would like to authenticate to the dapp. If the request has been previously granted and remembered, the `eth_requestAccounts` method **MAY** immediately return.
|
||||
|
||||
The `eth_requestAccounts` method **MUST** resolve with an array of the account(s) addresses or reject with an `Error`. If the account(s) enabled by the provider change, the `accountsChanged` event **MUST** also emit.
|
||||
|
||||
For full specification of the `eth_requestAccounts` RPC method, see [EIP 1102: Opt-in account exposure](https://eips.ethereum.org/EIPS/eip-1102).
|
||||
|
||||
### Events
|
||||
|
||||
The provider **SHOULD** extend from `EventEmitter` to provide dapps flexibility in listening to events. In place of full `EventEmitter` functionality, the provider **MAY** provide as many methods as it can reasonably provide, but **MUST** provide at least `on`, `emit`, and `removeListener`.
|
||||
|
|
Loading…
Reference in New Issue