*Use validateChain

This commit is contained in:
Derrick Hammer 2022-08-05 02:39:40 -04:00
parent d3e234c7b7
commit f22c4dd847
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 11 additions and 14 deletions

View File

@ -1,4 +1,4 @@
import { rpcError, RpcMethodList } from "./index.js"; import { rpcError, RpcMethodList, validateChain } from "./index.js";
import type { IPFS } from "ipfs-core"; import type { IPFS } from "ipfs-core";
import type { UnixFSEntry } from "ipfs-core/dist/src/utils"; import type { UnixFSEntry } from "ipfs-core/dist/src/utils";
import { dynImport } from "../util.js"; import { dynImport } from "../util.js";
@ -211,20 +211,17 @@ async function resolveIpns(hash: string, path: string): Promise<string> {
); );
} }
export default { const CHAIN = "ipfs";
stat_ipfs: async (args: any, context: object) => {
// @ts-ignore
if ("ipfs" !== context.chain) {
return rpcError(ERR_INVALID_CHAIN);
}
export default {
stat_ipfs: validateChain(CHAIN, async (args: any, context: object) => {
try { try {
return await statFile(args?.hash, args?.path); return await statFile(args?.hash, args?.path);
} catch (e: any) { } catch (e: any) {
return rpcError((e as Error).message); return rpcError((e as Error).message);
} }
}, }),
stat_ipns: async (args: any, context: object) => { stat_ipns: validateChain(CHAIN, async (args: any, context: object) => {
// @ts-ignore // @ts-ignore
if ("ipfs" !== context.chain) { if ("ipfs" !== context.chain) {
return rpcError(ERR_INVALID_CHAIN); return rpcError(ERR_INVALID_CHAIN);
@ -239,9 +236,9 @@ export default {
} catch (e: any) { } catch (e: any) {
return rpcError((e as Error).message); return rpcError((e as Error).message);
} }
}, }),
fetch_ipfs: async (args: any, context: object) => { fetch_ipfs: validateChain(CHAIN, async (args: any, context: object) => {
// @ts-ignore // @ts-ignore
if ("ipfs" !== context.chain) { if ("ipfs" !== context.chain) {
return rpcError(ERR_INVALID_CHAIN); return rpcError(ERR_INVALID_CHAIN);
@ -256,8 +253,8 @@ export default {
} catch (e: any) { } catch (e: any) {
return rpcError((e as Error).message); return rpcError((e as Error).message);
} }
}, }),
fetch_ipns: async (args: any, context: object) => { fetch_ipns: validateChain(CHAIN, async (args: any, context: object) => {
// @ts-ignore // @ts-ignore
if ("ipfs" !== context.chain) { if ("ipfs" !== context.chain) {
return rpcError(ERR_INVALID_CHAIN); return rpcError(ERR_INVALID_CHAIN);
@ -274,5 +271,5 @@ export default {
} catch (e: any) { } catch (e: any) {
return rpcError((e as Error).message); return rpcError((e as Error).message);
} }
}, }),
} as RpcMethodList; } as RpcMethodList;