mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-03 14:44:27 +01:00
34 lines
776 B
JavaScript
34 lines
776 B
JavaScript
|
import { ethErrors, serializeError } from 'eth-rpc-errors';
|
||
|
|
||
|
const createMetaRPCHandler = (api, outStream) => {
|
||
|
return (data) => {
|
||
|
if (!api[data.method]) {
|
||
|
outStream.write({
|
||
|
jsonrpc: '2.0',
|
||
|
error: ethErrors.rpc.methodNotFound({
|
||
|
message: `${data.method} not found`,
|
||
|
}),
|
||
|
id: data.id,
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
api[data.method](...data.params, (err, result) => {
|
||
|
if (err) {
|
||
|
outStream.write({
|
||
|
jsonrpc: '2.0',
|
||
|
error: serializeError(err, { shouldIncludeStack: true }),
|
||
|
id: data.id,
|
||
|
});
|
||
|
} else {
|
||
|
outStream.write({
|
||
|
jsonrpc: '2.0',
|
||
|
result,
|
||
|
id: data.id,
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
};
|
||
|
|
||
|
export default createMetaRPCHandler;
|