EIP-2700: JavaScript Provider Event Emitter Source

作者 Micah Zoltu, Erik Marks
讨论-To https://github.com/ethereum/EIPs/issues/2701
状态 Final
类型 Standards Track
分类 Interface
创建日期 2020-06-05
英文版 https://eips.ethereum.org/EIPS/eip-2700

简述

A standard mechanism for JavaScript Ethereum Providers to notify clients about chain state changes when both are able to interface with each other via a shared JavaScript object.

摘要

This standard provides the description of an object that is made available to JavaScript applications which they can use to receive notifications from an Ethereum Provider. This standard only describes the notification mechanism, it does not specify the payloads that are valid nor does it specify how the client or the provider will discover or agree on payload content.

How/where this Ethereum Provider object is exposed is left to future standards.

动机

When working within a JavaScript runtime (such as NodeJS, Electron, Browser, etc.) it may be possible for the runtime or a runtime plugin to inject objects into the runtime. Someone authoring a runtime or a runtime plugin may choose to expose an Ethereum Provider to any JavaScript apps or scripts running within that runtime in order to provide notifications of blockchain state changes. In order to achieve maximum compatibility between the provider and the client, a standard is necessary for what the shape of that object is.

规范

RFC-2119

The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC-2119.

Interface

interface EthereumProvider {
    on(eventName: string, listener: (...params: unknown[]) => void): void
    removeListener(eventName: string, listener: (...params: unknown[]) => void): void
}

The specific events that can be listened to and the shape of their listener callback functions is left to be defined in separate standards.

If on is called with an eventName that the provider is familiar with then the provider MUST call the provided listener when the named event occurs. If the same listener is added multiple times to the same event via on, the provider MAY choose to either callback the listener one time or one time per call to on.

If removeListener is called with an eventName and listener that was previously added via on then the provider MUST decrease the number of times it calls the listener per event by one.

基本原理

This EIP is mostly a retrospective EIP meaning it codifies an already existing specification so there isn’t a lot of room for improving things such as by using a discriminated union object for listener parameters or having a tighter definition of on. The specific events are intentionally left out of this specification as that set will be an ever-evolving collection and having the first few listed here doesn’t add value to this specification (especially if, over time, the first few end up deprecated or unused).

Security Considerations

The relationship between Ethereum Provider and client is a trusted one, where it is assumed that the user implicitly trusts the Ethereum Provider which is how it managed to get injected into the client, or the client expressly pulled in a connection to it.

版权声明

Copyright and related rights waived via CC0.

参考文献

Please cite this document as:

Micah Zoltu, Erik Marks, "EIP-2700: JavaScript Provider Event Emitter," Ethereum Improvement Proposals, no. 2700, June 2020. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-2700.