*add validateChain higher order function for RPC validation

This commit is contained in:
Derrick Hammer 2022-08-04 23:56:11 -04:00
parent 383ec64692
commit ec649d2d93
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 23 additions and 14 deletions

View File

@ -8,6 +8,10 @@ import { ERR_INVALID_CHAIN } from "../error.js";
type RpcProviderMethod = (method: string, params: Array<any>) => Promise<any>; type RpcProviderMethod = (method: string, params: Array<any>) => Promise<any>;
interface RpcContext {
chain?: string;
}
const gatewayProviders: { [name: string]: RpcProviderMethod } = {}; const gatewayProviders: { [name: string]: RpcProviderMethod } = {};
const gatewayMethods: { const gatewayMethods: {
@ -126,3 +130,13 @@ class RpcError extends Error {
export function rpcError(message: string): Promise<RpcError> { export function rpcError(message: string): Promise<RpcError> {
return Promise.reject(new RpcError(message)); return Promise.reject(new RpcError(message));
} }
export function validateChain(chain: string, handler: any) {
return async (args: any, context: RpcContext) => {
if (!context?.chain || "hns" !== context?.chain) {
return rpcError(ERR_INVALID_CHAIN);
}
return handler(args, context);
};
}

View File

@ -2,7 +2,7 @@
//const require = createRequire(import.meta.url); //const require = createRequire(import.meta.url);
import { isIp } from "../util.js"; import { isIp } from "../util.js";
import { RpcMethodList } from "./index.js"; import { RpcMethodList, validateChain } from "./index.js";
// @ts-ignore // @ts-ignore
import bns from "bns"; import bns from "bns";
const { StubResolver, RecursiveResolver } = bns; const { StubResolver, RecursiveResolver } = bns;
@ -59,7 +59,7 @@ async function getDnsRecords(
} }
export default { export default {
dnslookup: async function (args: any) { dnslookup: validateChain("icann", async function (args: any) {
let dnsResults: string[] = []; let dnsResults: string[] = [];
let domain = args.domain; let domain = args.domain;
let ns = args.nameserver; let ns = args.nameserver;
@ -104,5 +104,5 @@ export default {
} }
return false; return false;
}, }),
} as RpcMethodList; } as RpcMethodList;

View File

@ -1,7 +1,7 @@
//const require = createRequire(import.meta.url); //const require = createRequire(import.meta.url);
//import { createRequire } from "module"; //import { createRequire } from "module";
import { rpcError, RpcMethodList } from "./index.js"; import { rpcError, RpcMethodList, validateChain } from "./index.js";
// @ts-ignore // @ts-ignore
import rand from "random-key"; import rand from "random-key";
// @ts-ignore // @ts-ignore
@ -78,12 +78,7 @@ if (!config.bool("hsd-use-external-node")) {
const hnsClient = new NodeClient(clientArgs); const hnsClient = new NodeClient(clientArgs);
export default { export default {
getnameresource: async (args: any, context: object) => { getnameresource: validateChain("hns", async (args: any) => {
// @ts-ignore
if ("hns" !== context.chain) {
throw rpcError(ERR_INVALID_CHAIN);
}
let resp; let resp;
try { try {
resp = await hnsClient.execute("getnameresource", args); resp = await hnsClient.execute("getnameresource", args);
@ -100,5 +95,5 @@ export default {
} }
return resp; return resp;
}, }),
} as RpcMethodList; } as RpcMethodList;

View File

@ -1,7 +1,7 @@
import { RpcMethodList } from "./index"; import { RpcMethodList, validateChain } from "./index.js";
export default { export default {
ping: async () => { ping: validateChain("misc", async () => {
return { pong: true }; return { pong: true };
}, }),
} as RpcMethodList; } as RpcMethodList;