-
Notifications
You must be signed in to change notification settings - Fork 5.5k
EIP-1193: standard provider API #6170
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e7f2aa
EIP-1193: Implement new provider API
bitpshr 845eb8d
EIP-1193: Updated implementation
bitpshr ac2ab6c
Remove test file
bitpshr 252f238
Fix tests
bitpshr 4a393e2
Update ping check
bitpshr 5fc1d66
Update logic
bitpshr 9f1d7ef
PR feedback
bitpshr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| const BlockTracker = require('eth-block-tracker') | ||
|
|
||
| /** | ||
| * Creates a block tracker that sends platform events on success and failure | ||
| */ | ||
| module.exports = function createBlockTracker (args, platform) { | ||
| const blockTracker = new BlockTracker(args) | ||
| blockTracker.on('latest', () => { | ||
| if (platform && platform.sendMessage) { | ||
| platform.sendMessage({ action: 'ethereum-ping-success' }) | ||
| } | ||
| }) | ||
| blockTracker.on('error', () => { | ||
| if (platform && platform.sendMessage) { | ||
| platform.sendMessage({ action: 'ethereum-ping-error' }) | ||
| } | ||
| }) | ||
| return blockTracker | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| class StandardProvider { | ||
| _isConnected | ||
| _provider | ||
|
|
||
| constructor (provider) { | ||
| this._provider = provider | ||
| this._onMessage('ethereumpingerror', this._onClose.bind(this)) | ||
| this._onMessage('ethereumpingsuccess', this._onConnect.bind(this)) | ||
| window.addEventListener('load', () => { | ||
| this._subscribe() | ||
| this._ping() | ||
| }) | ||
| } | ||
|
|
||
| _onMessage (type, handler) { | ||
| window.addEventListener('message', function ({ data }) { | ||
| if (!data || data.type !== type) return | ||
| handler.apply(this, arguments) | ||
| }) | ||
| } | ||
|
|
||
| _onClose () { | ||
| if (this._isConnected === undefined || this._isConnected) { | ||
| this._provider.emit('close', { | ||
| code: 1011, | ||
| reason: 'Network connection error', | ||
| }) | ||
| } | ||
| this._isConnected = false | ||
| } | ||
|
|
||
| _onConnect () { | ||
| !this._isConnected && this._provider.emit('connect') | ||
| this._isConnected = true | ||
| } | ||
|
|
||
| async _ping () { | ||
| try { | ||
| await this.send('net_version') | ||
| window.postMessage({ type: 'ethereumpingsuccess' }, '*') | ||
| } catch (error) { | ||
| window.postMessage({ type: 'ethereumpingerror' }, '*') | ||
| } | ||
| } | ||
|
|
||
| _subscribe () { | ||
| this._provider.on('data', (error, { method, params }) => { | ||
| if (!error && method === 'eth_subscription') { | ||
| this._provider.emit('notification', params.result) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Initiate an RPC method call | ||
| * | ||
| * @param {string} method - RPC method name to call | ||
| * @param {string[]} params - Array of RPC method parameters | ||
| * @returns {Promise<*>} Promise resolving to the result if successful | ||
| */ | ||
| send (method, params = []) { | ||
| if (method === 'eth_requestAccounts') return this._provider.enable() | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| try { | ||
| this._provider.sendAsync({ method, params, beta: true }, (error, response) => { | ||
| error = error || response.error | ||
| error ? reject(error) : resolve(response) | ||
| }) | ||
| } catch (error) { | ||
| reject(error) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a legacy provider into an EIP-1193-compliant standard provider | ||
| * @param {Object} provider - Legacy provider to convert | ||
| * @returns {Object} Standard provider | ||
| */ | ||
| export default function createStandardProvider (provider) { | ||
| const standardProvider = new StandardProvider(provider) | ||
| const sendLegacy = provider.send | ||
| provider.send = (methodOrPayload, callbackOrArgs) => { | ||
| if (typeof methodOrPayload === 'string' && !callbackOrArgs || Array.isArray(callbackOrArgs)) { | ||
| return standardProvider.send(methodOrPayload, callbackOrArgs) | ||
| } | ||
| return sendLegacy.call(provider, methodOrPayload, callbackOrArgs) | ||
| } | ||
| return provider | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ const log = require('loglevel') | |
| const LocalMessageDuplexStream = require('post-message-stream') | ||
| const setupDappAutoReload = require('./lib/auto-reload.js') | ||
| const MetamaskInpageProvider = require('metamask-inpage-provider') | ||
| const createStandardProvider = require('./createStandardProvider').default | ||
|
|
||
| let isEnabled = false | ||
| let warned = false | ||
|
|
@@ -16,12 +17,6 @@ restoreContextAfterImports() | |
|
|
||
| log.setDefaultLevel(process.env.METAMASK_DEBUG ? 'debug' : 'warn') | ||
|
|
||
| console.warn('ATTENTION: In an effort to improve user privacy, MetaMask ' + | ||
| 'stopped exposing user accounts to dapps if "privacy mode" is enabled on ' + | ||
| 'November 2nd, 2018. Dapps should now call provider.enable() in order to view and use ' + | ||
| 'accounts. Please see https://bit.ly/2QQHXvF for complete information and up-to-date ' + | ||
| 'example code.') | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will make certain twitter users happy ;) |
||
| /** | ||
| * Adds a postMessage listener for a specific message type | ||
| * | ||
|
|
@@ -69,7 +64,10 @@ inpageProvider.enable = function ({ force } = {}) { | |
| return new Promise((resolve, reject) => { | ||
| providerHandle = ({ data: { error, selectedAddress } }) => { | ||
| if (typeof error !== 'undefined') { | ||
| reject(error) | ||
| reject({ | ||
| message: error, | ||
| code: 4001, | ||
| }) | ||
| } else { | ||
| window.removeEventListener('message', providerHandle) | ||
| setTimeout(() => { | ||
|
|
@@ -154,7 +152,7 @@ const proxiedInpageProvider = new Proxy(inpageProvider, { | |
| deleteProperty: () => true, | ||
| }) | ||
|
|
||
| window.ethereum = proxiedInpageProvider | ||
| window.ethereum = createStandardProvider(proxiedInpageProvider) | ||
|
|
||
| // detect eth_requestAccounts and pipe to enable for now | ||
| function detectAccountRequest (method) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If may, why use
beta: trueand not the classicjsonrpc: '2.0'?I think this the cause of this issue : MM 6.2.1 sends invalid JSONRPC requests to private network