thunderhub/server/schema/invoice/resolvers.ts
Anthony Potdevin 581185e6b0
chore: 🔧 remove client (#111)
* chore: 🔧 remove client

* chore: 🔧 change cookie name

* chore: 🔧 remove auth param

* chore: 🔧 remove auth components

* chore: 🔧 add getaccount query

* fix: 🐛 tests

* chore: 🔧 get account

* chore: 🔧 status check

* chore: 🔧 remove log

* chore: 🔧 update apollo client

* refactor: ♻️ server side props

* chore: 🔧 ssr queries

* chore: 🔧 more ssr queries

* chore: 🔧 type check

* chore: 🔧 increase nodeinfo limit

Co-authored-by: apotdevin <apotdevincab@gmail.com>
2020-08-05 08:37:02 +02:00

133 lines
3.5 KiB
TypeScript

import { randomBytes, createHash } from 'crypto';
import {
payViaRoutes,
createInvoice,
decodePaymentRequest,
payViaPaymentDetails,
createInvoice as createInvoiceRequest,
} from 'ln-service';
import { ContextType } from 'server/types/apiTypes';
import { logger } from 'server/helpers/logger';
import { requestLimiter } from 'server/helpers/rateLimiter';
import { getErrorMsg } from 'server/helpers/helpers';
import { to } from 'server/helpers/async';
import { DecodedType } from 'server/types/ln-service.types';
const KEYSEND_TYPE = '5482373484';
export const invoiceResolvers = {
Query: {
decodeRequest: async (_: undefined, params: any, context: ContextType) => {
await requestLimiter(context.ip, 'decode');
const { lnd } = context;
const decoded = await to<DecodedType>(
decodePaymentRequest({
lnd,
request: params.request,
})
);
return {
...decoded,
destination_node: { lnd, publicKey: decoded.destination },
probe_route: {
lnd,
destination: decoded.destination,
tokens: decoded.tokens,
},
};
},
},
Mutation: {
createInvoice: async (_: undefined, params: any, context: ContextType) => {
await requestLimiter(context.ip, 'createInvoice');
const { lnd } = context;
return await to(
createInvoiceRequest({
lnd,
tokens: params.amount,
})
);
},
keysend: async (_: undefined, params: any, context: ContextType) => {
await requestLimiter(context.ip, 'keysend');
const { destination, tokens } = params;
const { lnd } = context;
const preimage = randomBytes(32);
const secret = preimage.toString('hex');
const id = createHash('sha256').update(preimage).digest().toString('hex');
return await to(
payViaPaymentDetails({
id,
lnd,
tokens,
destination,
messages: [
{
type: KEYSEND_TYPE,
value: secret,
},
],
})
);
},
circularRebalance: async (
_: undefined,
params: any,
context: ContextType
) => {
await requestLimiter(context.ip, 'circularRebalance');
const { lnd } = context;
let route;
try {
route = JSON.parse(params.route);
} catch (error) {
logger.error('Corrupt route json: %o', error);
throw new Error('Corrupt Route JSON');
}
const { id } = await createInvoice({
lnd,
tokens: params.tokens,
description: 'Rebalance',
}).catch((error: any) => {
logger.error('Error getting invoice: %o', error);
throw new Error(getErrorMsg(error));
});
await payViaRoutes({ lnd, routes: [route], id }).catch((error: any) => {
logger.error('Error making payment: %o', error);
throw new Error(getErrorMsg(error));
});
return true;
},
payViaRoute: async (_: undefined, params: any, context: ContextType) => {
await requestLimiter(context.ip, 'payViaRoute');
const { route: routeJSON, id } = params;
const { lnd } = context;
let route;
try {
route = JSON.parse(routeJSON);
} catch (error) {
logger.error('Corrupt route json: %o', error);
throw new Error('Corrupt Route JSON');
}
await to(payViaRoutes({ lnd, routes: [route], id }));
return true;
},
},
};