Skip to content

Commit

Permalink
fix: validation & remove exceptionMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
shresthagrawal committed Oct 31, 2022
1 parent f7564d9 commit 2e1f18d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
19 changes: 6 additions & 13 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 1,8 @@
import express from 'express';
import bodyParser from 'body-parser';
import { JSONRPCErrorException, JSONRPCServer, JSONRPCServerMiddleware } from 'json-rpc-2.0';
import { JSONRPCServer, JSONRPCServerMiddleware } from 'json-rpc-2.0';
import { RPCTx } from './types';
import { InternalError } from './errors';
// import { InternalError } from './errors';
import { VerifyingProvider } from './provider';
import { validators } from './validation';

Expand Down Expand Up @@ -100,19 100,12 @@ export function getJSONRPCServer(provider: VerifyingProvider) {
return BigInt(provider.chainId()).toString();
});

const exceptionMiddleware: JSONRPCServerMiddleware<void> = async (next, request, serverParams) => {
try {
console.log(`RPC Request ${request.method}`);
return await next(request, serverParams);
} catch (error) {
if (error instanceof JSONRPCErrorException) {
throw error;
}
throw new InternalError(error.message);
}
const logMiddleware: JSONRPCServerMiddleware<void> = async (next, request, serverParams) => {
console.log(`RPC Request ${request.method}`);
return await next(request, serverParams);
};

server.applyMiddleware(exceptionMiddleware);
server.applyMiddleware(logMiddleware);
return server;
}

Expand Down
14 changes: 6 additions & 8 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 63,6 @@ export const validators = {

const blockOption = params[index];

console.log(blockOption);
if (['latest', 'earliest', 'pending'].includes(blockOption)) {
return;
}
Expand Down Expand Up @@ -112,19 111,18 @@ export const validators = {

const tx = params[index];

const validate = (field: any, validator: Function) => {
if (field === undefined) return;
return validator([field], index);
};

// validate addresses
for (const field of [tx.to, tx.from]) {
validate(field, this.address);
// TODO: the below will create an error with incorrect index if the tx is not at index 0
if (field !== undefined)
this.address([field], 0);
}

// validate hex
for (const field of [tx.gas, tx.gasPrice, tx.value, tx.data]) {
validate(field, this.hex);
// TODO: the below will create an error with incorrect index if the tx is not at index 0
if (field !== undefined)
this.hex([field], 0);
}
},
};

0 comments on commit 2e1f18d

Please sign in to comment.