diff --git a/index.js b/index.js index 4bb8ce81..53ba394a 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ module.exports = MetamaskInpageProvider inherits(MetamaskInpageProvider, SafeEventEmitter) -function MetamaskInpageProvider (connectionStream) { +function MetamaskInpageProvider (connectionStream, shouldSendMetadata = true) { // super constructor SafeEventEmitter.call(this) @@ -151,11 +151,13 @@ function MetamaskInpageProvider (connectionStream) { }) // send website metadata - const domContentLoadedHandler = () => { - sendSiteMetadata(this._rpcEngine) - window.removeEventListener('DOMContentLoaded', domContentLoadedHandler) + if (shouldSendMetadata) { + const domContentLoadedHandler = () => { + sendSiteMetadata(this._rpcEngine) + window.removeEventListener('DOMContentLoaded', domContentLoadedHandler) + } + window.addEventListener('DOMContentLoaded', domContentLoadedHandler) } - window.addEventListener('DOMContentLoaded', domContentLoadedHandler) // indicate that we've connected, for EIP-1193 compliance setTimeout(() => this.emit('connect')) @@ -239,6 +241,8 @@ MetamaskInpageProvider.prototype.send = function (methodOrPayload, params) { ) { // wrap params in array out of kindness + // params have to be an array per EIP 1193, even though JSON RPC + // allows objects if (params === undefined) { params = [] } else if (!Array.isArray(params)) { @@ -251,12 +255,11 @@ MetamaskInpageProvider.prototype.send = function (methodOrPayload, params) { } } - // typecheck payload and payload.method + // typecheck payload and payload.params if ( - Array.isArray(payload) || - typeof params === 'function' || typeof payload !== 'object' || - typeof payload.method !== 'string' + Array.isArray(payload) || + !Array.isArray(params) ) { throw ethErrors.rpc.invalidRequest({ message: messages.errors.invalidParams(), @@ -529,7 +532,6 @@ function getExperimentalApi (instance) { }, }, { - get: (obj, prop) => { if (!instance._state.sentWarnings.experimentalMethods) { diff --git a/package.json b/package.json index 52a1c0ca..bcb5ca21 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "metamask-inpage-provider", - "version": "4.0.3", + "version": "4.0.4", "description": "An ethereum provider that connects over a WebExtension port.", "main": "index.js", "scripts": { diff --git a/src/utils.js b/src/utils.js index 80242da6..dce5c003 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,6 @@ const log = require('loglevel') -const { serializeError } = require('eth-json-rpc-errors') +const { ethErrors, serializeError } = require('eth-json-rpc-errors') const EventEmitter = require('events') const SafeEventEmitter = require('safe-event-emitter') @@ -17,7 +17,16 @@ const SafeEventEmitter = require('safe-event-emitter') * @returns {Function} json-rpc-engine middleware function */ function createErrorMiddleware () { - return (_req, res, next) => { + return (req, res, next) => { + + // json-rpc-engine will terminate the request when it notices this error + if (!req.method || typeof req.method !== 'string') { + res.error = ethErrors.rpc.invalidRequest({ + message: `The request 'method' must be a non-empty string.`, + data: req, + }) + } + next(done => { const { error } = res if (!error) {