mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
metamask controller - move middleware into seperate files
This commit is contained in:
parent
96d1175834
commit
245c0f0c27
15
app/scripts/lib/createLoggerMiddleware.js
Normal file
15
app/scripts/lib/createLoggerMiddleware.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// log rpc activity
|
||||||
|
module.exports = createLoggerMiddleware
|
||||||
|
|
||||||
|
function createLoggerMiddleware({ origin }) {
|
||||||
|
return function loggerMiddleware (req, res, next, end) {
|
||||||
|
next((cb) => {
|
||||||
|
if (res.error) {
|
||||||
|
log.error('Error in RPC response:\n', res)
|
||||||
|
}
|
||||||
|
if (req.isMetamaskInternal) return
|
||||||
|
log.info(`RPC (${origin}):`, req, '->', res)
|
||||||
|
cb()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
9
app/scripts/lib/createOriginMiddleware.js
Normal file
9
app/scripts/lib/createOriginMiddleware.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// append dapp origin domain to request
|
||||||
|
module.exports = createOriginMiddleware
|
||||||
|
|
||||||
|
function createOriginMiddleware({ origin }) {
|
||||||
|
return function originMiddleware (req, res, next, end) {
|
||||||
|
req.origin = originDomain
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
}
|
13
app/scripts/lib/createProviderMiddleware.js
Normal file
13
app/scripts/lib/createProviderMiddleware.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
module.exports = createProviderMiddleware
|
||||||
|
|
||||||
|
// forward requests to provider
|
||||||
|
function createProviderMiddleware({ provider }) {
|
||||||
|
return (req, res, next, end) => {
|
||||||
|
provider.sendAsync(req, (err, _res) => {
|
||||||
|
if (err) return end(err)
|
||||||
|
res.result = _res.result
|
||||||
|
end()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -7,8 +7,12 @@ const ObservableStore = require('obs-store')
|
|||||||
const EthStore = require('./lib/eth-store')
|
const EthStore = require('./lib/eth-store')
|
||||||
const EthQuery = require('eth-query')
|
const EthQuery = require('eth-query')
|
||||||
const RpcEngine = require('json-rpc-engine')
|
const RpcEngine = require('json-rpc-engine')
|
||||||
|
const debounce = require('debounce')
|
||||||
const createEngineStream = require('json-rpc-middleware-stream/engineStream')
|
const createEngineStream = require('json-rpc-middleware-stream/engineStream')
|
||||||
const createFilterMiddleware = require('eth-json-rpc-filters')
|
const createFilterMiddleware = require('eth-json-rpc-filters')
|
||||||
|
const createOriginMiddleware = require('./lib/createOriginMiddleware')
|
||||||
|
const createLoggerMiddleware = require('./lib/createLoggerMiddleware')
|
||||||
|
const createProviderMiddleware = require('./lib/createProviderMiddleware')
|
||||||
const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex
|
const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex
|
||||||
const KeyringController = require('./keyring-controller')
|
const KeyringController = require('./keyring-controller')
|
||||||
const NetworkController = require('./controllers/network')
|
const NetworkController = require('./controllers/network')
|
||||||
@ -26,8 +30,6 @@ const ConfigManager = require('./lib/config-manager')
|
|||||||
const nodeify = require('./lib/nodeify')
|
const nodeify = require('./lib/nodeify')
|
||||||
const accountImporter = require('./account-import-strategies')
|
const accountImporter = require('./account-import-strategies')
|
||||||
const getBuyEthUrl = require('./lib/buy-eth-url')
|
const getBuyEthUrl = require('./lib/buy-eth-url')
|
||||||
const debounce = require('debounce')
|
|
||||||
|
|
||||||
const version = require('../manifest.json').version
|
const version = require('../manifest.json').version
|
||||||
|
|
||||||
module.exports = class MetamaskController extends EventEmitter {
|
module.exports = class MetamaskController extends EventEmitter {
|
||||||
@ -384,11 +386,11 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
setupProviderConnection (outStream, originDomain) {
|
setupProviderConnection (outStream, origin) {
|
||||||
// setup json rpc engine stack
|
// setup json rpc engine stack
|
||||||
const engine = new RpcEngine()
|
const engine = new RpcEngine()
|
||||||
engine.push(originMiddleware)
|
engine.push(createOriginMiddleware({ origin }))
|
||||||
engine.push(loggerMiddleware)
|
engine.push(createLoggerMiddleware({ origin }))
|
||||||
engine.push(createFilterMiddleware({
|
engine.push(createFilterMiddleware({
|
||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
blockTracker: this.blockTracker,
|
blockTracker: this.blockTracker,
|
||||||
@ -405,35 +407,6 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
if (err) log.error(err)
|
if (err) log.error(err)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// append dapp origin domain to request
|
|
||||||
function originMiddleware (req, res, next, end) {
|
|
||||||
req.origin = originDomain
|
|
||||||
next()
|
|
||||||
}
|
|
||||||
|
|
||||||
// log rpc activity
|
|
||||||
function loggerMiddleware (req, res, next, end) {
|
|
||||||
next((cb) => {
|
|
||||||
if (res.error) {
|
|
||||||
log.error('Error in RPC response:\n', res)
|
|
||||||
}
|
|
||||||
if (req.isMetamaskInternal) return
|
|
||||||
log.info(`RPC (${originDomain}):`, req, '->', res)
|
|
||||||
cb()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// forward requests to provider
|
|
||||||
function createProviderMiddleware({ provider }) {
|
|
||||||
return (req, res, next, end) => {
|
|
||||||
provider.sendAsync(req, (err, _res) => {
|
|
||||||
if (err) return end(err)
|
|
||||||
res.result = _res.result
|
|
||||||
end()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setupPublicConfig (outStream) {
|
setupPublicConfig (outStream) {
|
||||||
|
@ -68,7 +68,7 @@
|
|||||||
"eth-bin-to-ops": "^1.0.1",
|
"eth-bin-to-ops": "^1.0.1",
|
||||||
"eth-contract-metadata": "^1.1.4",
|
"eth-contract-metadata": "^1.1.4",
|
||||||
"eth-hd-keyring": "^1.1.1",
|
"eth-hd-keyring": "^1.1.1",
|
||||||
"eth-json-rpc-filters": "^1.0.2",
|
"eth-json-rpc-filters": "^1.1.0",
|
||||||
"eth-phishing-detect": "^1.1.4",
|
"eth-phishing-detect": "^1.1.4",
|
||||||
"eth-query": "^2.1.2",
|
"eth-query": "^2.1.2",
|
||||||
"eth-sig-util": "^1.2.2",
|
"eth-sig-util": "^1.2.2",
|
||||||
|
Loading…
Reference in New Issue
Block a user