From 098f541a337ace1c436f091d6e8922d647a03d4e Mon Sep 17 00:00:00 2001 From: AP Date: Sat, 22 Feb 2020 15:33:06 +0100 Subject: [PATCH] feat: add adminCheck query --- package.json | 2 +- src/helpers/helpers.ts | 14 ++++++++---- src/schemas/query/general/adminCheck.ts | 29 +++++++++++++++++++++++++ src/schemas/query/general/index.ts | 16 ++++++++------ src/utils/rateLimitConfig.ts | 1 + 5 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 src/schemas/query/general/adminCheck.ts diff --git a/package.json b/package.json index cf427916..72b30738 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "thunderhub", - "version": "0.1.1", + "version": "0.1.2", "description": "", "main": "index.js", "scripts": { diff --git a/src/helpers/helpers.ts b/src/helpers/helpers.ts index 2ac76cf5..f1428ff4 100644 --- a/src/helpers/helpers.ts +++ b/src/helpers/helpers.ts @@ -39,10 +39,7 @@ export const getAuthLnd = (auth: { return lnd; }; -export const getErrorMsg = (error: any[]): string => { - const code = error[0]; - const msg = error[1]; - +export const getErrorDetails = (error: any[]): string => { let details = ''; if (error.length > 2) { if (error[2].err) { @@ -52,5 +49,14 @@ export const getErrorMsg = (error: any[]): string => { } } + return details; +}; + +export const getErrorMsg = (error: any[]): string => { + const code = error[0]; + const msg = error[1]; + + let details = getErrorDetails(error); + return JSON.stringify({ code, msg, details }); }; diff --git a/src/schemas/query/general/adminCheck.ts b/src/schemas/query/general/adminCheck.ts new file mode 100644 index 00000000..d3c85435 --- /dev/null +++ b/src/schemas/query/general/adminCheck.ts @@ -0,0 +1,29 @@ +import { pay as payRequest } from 'ln-service'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { GraphQLBoolean } from 'graphql'; +import { getAuthLnd, getErrorDetails } from '../../../helpers/helpers'; +import { defaultParams } from '../../../helpers/defaultProps'; + +export const adminCheck = { + type: GraphQLBoolean, + args: { + ...defaultParams, + }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, 'adminCheck'); + + const lnd = getAuthLnd(params.auth); + + try { + await payRequest({ + lnd, + request: 'admin check', + }); + } catch (error) { + const details = getErrorDetails(error); + if (details.includes('invalid character in string')) return true; + + throw new Error(); + } + }, +}; diff --git a/src/schemas/query/general/index.ts b/src/schemas/query/general/index.ts index be8465a5..d8e2bf4e 100644 --- a/src/schemas/query/general/index.ts +++ b/src/schemas/query/general/index.ts @@ -1,10 +1,12 @@ -import { getChainBalance, getPendingChainBalance } from "./chainBalance"; -import { getNetworkInfo } from "./networkInfo"; -import { getNodeInfo } from "./nodeInfo"; +import { getChainBalance, getPendingChainBalance } from './chainBalance'; +import { getNetworkInfo } from './networkInfo'; +import { getNodeInfo } from './nodeInfo'; +import { adminCheck } from './adminCheck'; export const generalQueries = { - getChainBalance, - getPendingChainBalance, - getNetworkInfo, - getNodeInfo + getChainBalance, + getPendingChainBalance, + getNetworkInfo, + getNodeInfo, + adminCheck, }; diff --git a/src/utils/rateLimitConfig.ts b/src/utils/rateLimitConfig.ts index 911686f7..2f57f3db 100644 --- a/src/utils/rateLimitConfig.ts +++ b/src/utils/rateLimitConfig.ts @@ -40,4 +40,5 @@ export const RateConfig: RateConfigProps = { chainTransactions: { max: 3, window: '1s' }, getRoutes: { max: 3, window: '1s' }, payViaRoute: { max: 3, window: '1s' }, + adminCheck: { max: 3, window: '1s' }, };