Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EIP-1193 improvements #2092

Merged
merged 3 commits into from
Jun 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 9 additions & 69 deletions EIPS/eip-1193.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this example below is super important so one can see how the notification object looks like.

And as not the full object is returned, the reference to the wiki might not be enough.


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).

Expand Down Expand Up @@ -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
Expand All @@ -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 => {
Expand All @@ -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;
Expand All @@ -188,30 +153,17 @@ 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')}`);
};
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}`);
});
Expand All @@ -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`.
Expand Down