feat: get and verify backup

This commit is contained in:
Anthony Potdevin 2019-12-26 12:46:39 +05:30
parent cef7a65ea5
commit 123dd904bc
5 changed files with 81 additions and 0 deletions

View file

@ -0,0 +1,25 @@
import { getBackups as getLnBackups } from 'ln-service';
import { logger } from '../../../helpers/logger';
import { requestLimiter } from '../../../helpers/rateLimiter';
import { GraphQLNonNull, GraphQLString } from 'graphql';
import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers';
export const getBackups = {
type: GraphQLString,
args: { auth: { type: new GraphQLNonNull(GraphQLString) } },
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, 'getBackups');
const lnd = getAuthLnd(params.auth);
try {
const backups = await getLnBackups({
lnd: lnd,
});
return JSON.stringify(backups);
} catch (error) {
logger.error('Error getting backups: %o', error);
throw new Error(getErrorMsg(error));
}
},
};

View file

@ -0,0 +1,7 @@
import { getBackups } from './getBackups';
import { verifyBackups } from './verifyBackups';
export const backupQueries = {
getBackups,
verifyBackups,
};

View file

@ -0,0 +1,45 @@
import { verifyBackups as verifyLnBackups } from 'ln-service';
import { logger } from '../../../helpers/logger';
import { requestLimiter } from '../../../helpers/rateLimiter';
import { GraphQLNonNull, GraphQLString, GraphQLBoolean } from 'graphql';
import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers';
interface BackupProps {
backup: string;
channels: {}[];
}
export const verifyBackups = {
type: GraphQLBoolean,
args: {
auth: { type: new GraphQLNonNull(GraphQLString) },
backup: { type: new GraphQLNonNull(GraphQLString) },
},
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, 'verifyBackups');
const lnd = getAuthLnd(params.auth);
let backupObj: BackupProps = { backup: '', channels: [] };
try {
backupObj = JSON.parse(params.backup);
} catch (error) {
logger.error('Corrupt backup file: %o', error);
throw new Error('Corrupt backup file');
}
const { backup, channels } = backupObj;
try {
const { is_valid } = await verifyLnBackups({
lnd: lnd,
backup,
channels,
});
return is_valid;
} catch (error) {
logger.error('Error verifying backups: %o', error);
throw new Error(getErrorMsg(error));
}
},
};

View file

@ -4,6 +4,7 @@ import { invoiceQueries } from './invoices';
import { dataQueries } from './data';
import { reportQueries } from './report';
import { flowQueries } from './flow';
import { backupQueries } from './backup';
export const query = {
...channelQueries,
@ -12,4 +13,5 @@ export const query = {
...dataQueries,
...reportQueries,
...flowQueries,
...backupQueries,
};

View file

@ -33,4 +33,6 @@ export const RateConfig: RateConfigProps = {
pay: { max: 3, window: '1s' },
getAddress: { max: 3, window: '1s' },
sendToAddress: { max: 3, window: '1s' },
getBackups: { max: 3, window: '1s' },
verifyBackups: { max: 3, window: '1s' },
};