2018-05-17 05:46:34 +02:00
|
|
|
const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware')
|
2018-05-17 07:46:11 +02:00
|
|
|
const createScaffoldMiddleware = require('json-rpc-engine/src/createScaffoldMiddleware')
|
2018-05-17 05:46:34 +02:00
|
|
|
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware')
|
|
|
|
const createWalletSubprovider = require('eth-json-rpc-middleware/wallet')
|
|
|
|
|
|
|
|
module.exports = createMetamaskMiddleware
|
|
|
|
|
2018-05-17 06:08:19 +02:00
|
|
|
function createMetamaskMiddleware ({
|
2018-05-17 05:46:34 +02:00
|
|
|
version,
|
|
|
|
getAccounts,
|
|
|
|
processTransaction,
|
|
|
|
processEthSignMessage,
|
|
|
|
processTypedMessage,
|
|
|
|
processPersonalMessage,
|
2018-05-17 06:08:19 +02:00
|
|
|
getPendingNonce,
|
2018-05-17 05:46:34 +02:00
|
|
|
}) {
|
|
|
|
const metamaskMiddleware = mergeMiddleware([
|
|
|
|
createScaffoldMiddleware({
|
|
|
|
// staticSubprovider
|
|
|
|
eth_syncing: false,
|
|
|
|
web3_clientVersion: `MetaMask/v${version}`,
|
|
|
|
}),
|
|
|
|
createWalletSubprovider({
|
|
|
|
getAccounts,
|
|
|
|
processTransaction,
|
|
|
|
processEthSignMessage,
|
|
|
|
processTypedMessage,
|
|
|
|
processPersonalMessage,
|
|
|
|
}),
|
|
|
|
createPendingNonceMiddleware({ getPendingNonce }),
|
2018-05-17 06:08:19 +02:00
|
|
|
])
|
2018-05-17 05:46:34 +02:00
|
|
|
return metamaskMiddleware
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPendingNonceMiddleware ({ getPendingNonce }) {
|
|
|
|
return createAsyncMiddleware(async (req, res, next) => {
|
|
|
|
if (req.method !== 'eth_getTransactionCount') return next()
|
|
|
|
const address = req.params[0]
|
|
|
|
const blockRef = req.params[1]
|
|
|
|
if (blockRef !== 'pending') return next()
|
2018-09-21 19:34:21 +02:00
|
|
|
res.result = await getPendingNonce(address)
|
2018-05-17 05:46:34 +02:00
|
|
|
})
|
|
|
|
}
|