mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-12 12:47:14 +01:00
87ce653c86
The "MetaMask middleware" is the set of middleware for handling methods that we don't send to the network. This includes signing, encryption, `getAccounts`, and methods that rely on pending transaction state. Previously this middleware was setup as part of the network client, despite having nothing to do with the network. They have been moved into the main RPC pipeline established in `metamask-controller.js` instead. This is a pure refactor, and should have no functional changes. The middleware are still run in exactly the same order with the same arguments.
36 lines
965 B
JavaScript
36 lines
965 B
JavaScript
import { createAsyncMiddleware } from 'json-rpc-engine';
|
|
import { formatTxMetaForRpcResult } from '../util';
|
|
|
|
export function createPendingNonceMiddleware({ getPendingNonce }) {
|
|
return createAsyncMiddleware(async (req, res, next) => {
|
|
const { method, params } = req;
|
|
if (method !== 'eth_getTransactionCount') {
|
|
next();
|
|
return;
|
|
}
|
|
const [param, blockRef] = params;
|
|
if (blockRef !== 'pending') {
|
|
next();
|
|
return;
|
|
}
|
|
res.result = await getPendingNonce(param);
|
|
});
|
|
}
|
|
|
|
export function createPendingTxMiddleware({ getPendingTransactionByHash }) {
|
|
return createAsyncMiddleware(async (req, res, next) => {
|
|
const { method, params } = req;
|
|
if (method !== 'eth_getTransactionByHash') {
|
|
next();
|
|
return;
|
|
}
|
|
const [hash] = params;
|
|
const txMeta = getPendingTransactionByHash(hash);
|
|
if (!txMeta) {
|
|
next();
|
|
return;
|
|
}
|
|
res.result = formatTxMetaForRpcResult(txMeta);
|
|
});
|
|
}
|