diff --git a/ln/connect.js b/ln/connect.ts similarity index 89% rename from ln/connect.js rename to ln/connect.ts index 167d5445..f82b5c04 100644 --- a/ln/connect.js +++ b/ln/connect.ts @@ -1,7 +1,7 @@ -const fs = require('fs'); -const path = require('path'); -const lightning = require('lightning'); -const logger = require('../logger'); +import * as fs from 'fs'; +import * as path from 'path'; +import * as lightning from "lightning"; +import logger from "../logger"; const { authenticatedLndGrpc } = lightning; @@ -47,4 +47,4 @@ const { lnd } = authenticatedLndGrpc({ socket, }); -module.exports = lnd; +export { lnd }; diff --git a/ln/hold_invoice.js b/ln/hold_invoice.ts similarity index 61% rename from ln/hold_invoice.js rename to ln/hold_invoice.ts index 1a36c39c..201d644c 100644 --- a/ln/hold_invoice.js +++ b/ln/hold_invoice.ts @@ -1,26 +1,26 @@ -const { createHash, randomBytes } = require('crypto'); -const lightning = require('lightning'); -const lnd = require('./connect'); -const logger = require('../logger'); +import { randomBytes, createHash } from 'crypto'; +import * as lightning from "lightning"; +import { lnd } from './connect' +import logger from "../logger"; -const createHoldInvoice = async ({ description, amount }) => { +const createHoldInvoice = async (description: string, amount: number ) => { try { const randomSecret = () => randomBytes(32); - const sha256 = buffer => createHash('sha256').update(buffer).digest('hex'); + const sha256 = (buffer: Buffer): string => createHash('sha256').update(buffer).digest('hex'); // We create a random secret const secret = randomSecret(); const expiresAt = new Date(); expiresAt.setSeconds(expiresAt.getSeconds() + 3600); const hash = sha256(secret); - const cltv_delta = parseInt(process.env.HOLD_INVOICE_CLTV_DELTA); + const cltv_delta = Number(process.env.HOLD_INVOICE_CLTV_DELTA); const { request, id } = await lightning.createHodlInvoice({ cltv_delta, lnd, description, id: hash, tokens: amount, - expires_at: expiresAt, + expires_at: expiresAt.toISOString(), }); // We sent back the response hash (id) to be used on testing @@ -30,7 +30,7 @@ const createHoldInvoice = async ({ description, amount }) => { } }; -const settleHoldInvoice = async ({ secret }) => { +const settleHoldInvoice = async ( { secret }: { secret: string } ) => { try { await lightning.settleHodlInvoice({ lnd, secret }); } catch (error) { @@ -38,7 +38,7 @@ const settleHoldInvoice = async ({ secret }) => { } }; -const cancelHoldInvoice = async ({ hash }) => { +const cancelHoldInvoice = async ( { hash }: { hash: string } ) => { try { await lightning.cancelHodlInvoice({ lnd, id: hash }); } catch (error) { @@ -46,7 +46,7 @@ const cancelHoldInvoice = async ({ hash }) => { } }; -const getInvoice = async ({ hash }) => { +const getInvoice = async ( { hash }: { hash: string } ) => { try { return await lightning.getInvoice({ lnd, id: hash }); } catch (error) { @@ -54,7 +54,7 @@ const getInvoice = async ({ hash }) => { } }; -module.exports = { +export { createHoldInvoice, settleHoldInvoice, cancelHoldInvoice, diff --git a/ln/index.js b/ln/index.ts similarity index 64% rename from ln/index.js rename to ln/index.ts index 109300f8..57372418 100644 --- a/ln/index.js +++ b/ln/index.ts @@ -1,16 +1,16 @@ -const { +import { createHoldInvoice, settleHoldInvoice, cancelHoldInvoice, getInvoice, -} = require('./hold_invoice'); -const subscribeInvoice = require('./subscribe_invoice'); +} from './hold_invoice'; +import subscribeInvoice from './subscribe_invoice'; const subscribeProbe = require('./subscribe_probe'); -const resubscribeInvoices = require('./resubscribe_invoices'); +import resubscribeInvoices from './resubscribe_invoices'; const { payRequest, payToBuyer, isPendingPayment } = require('./pay_request'); -const { getInfo } = require('./info'); +import { getInfo } from './info'; -module.exports = { +export { createHoldInvoice, subscribeInvoice, resubscribeInvoices, @@ -22,4 +22,4 @@ module.exports = { isPendingPayment, subscribeProbe, getInvoice, -}; +} diff --git a/ln/info.js b/ln/info.js deleted file mode 100644 index da731135..00000000 --- a/ln/info.js +++ /dev/null @@ -1,13 +0,0 @@ -const lightning = require('lightning'); -const lnd = require('./connect'); -const logger = require('../logger'); - -const getInfo = async () => { - try { - return await lightning.getWalletInfo({ lnd }); - } catch (error) { - logger.error(error); - } -}; - -module.exports = { getInfo }; diff --git a/ln/info.ts b/ln/info.ts new file mode 100644 index 00000000..179fb82e --- /dev/null +++ b/ln/info.ts @@ -0,0 +1,11 @@ +import * as lightning from "lightning"; +import { lnd } from './connect' +import logger from "../logger"; + +export const getInfo = async () => { + try { + return await lightning.getWalletInfo({ lnd }); + } catch (error) { + logger.error(error); + } +}; diff --git a/ln/resubscribe_invoices.js b/ln/resubscribe_invoices.ts similarity index 65% rename from ln/resubscribe_invoices.js rename to ln/resubscribe_invoices.ts index 644dbbba..33ae6e90 100644 --- a/ln/resubscribe_invoices.js +++ b/ln/resubscribe_invoices.ts @@ -1,13 +1,15 @@ -const { getInvoices } = require('lightning'); -const lnd = require('./connect'); +import { Telegraf } from 'telegraf'; +import { MainContext } from "../bot/start"; +import { getInvoices, GetInvoicesResult } from 'lightning'; +import { lnd } from './connect'; const subscribeInvoice = require('./subscribe_invoice'); -const { Order } = require('../models'); -const logger = require('../logger'); +import { Order } from '../models'; +import logger from "../logger"; -const resubscribeInvoices = async bot => { +const resubscribeInvoices = async (bot: Telegraf) => { try { let invoicesReSubscribed = 0; - const isHeld = invoice => !!invoice.is_held; + const isHeld = (invoice: any) => !!invoice.is_held; const unconfirmedInvoices = ( await getInvoices({ lnd, @@ -29,9 +31,9 @@ const resubscribeInvoices = async bot => { } logger.info(`Invoices resubscribed: ${invoicesReSubscribed}`); } catch (error) { - logger.error(`ResuscribeInvoice catch: ${error.toString()}`); + logger.error(`ResuscribeInvoice catch: ${String(error)}`); return false; } }; -module.exports = resubscribeInvoices; +export default resubscribeInvoices diff --git a/ln/subscribe_invoice.js b/ln/subscribe_invoice.ts similarity index 82% rename from ln/subscribe_invoice.js rename to ln/subscribe_invoice.ts index 2f96e36a..fe5a5142 100644 --- a/ln/subscribe_invoice.js +++ b/ln/subscribe_invoice.ts @@ -1,23 +1,27 @@ -const { subscribeToInvoice } = require('lightning'); -const { Order, User } = require('../models'); +import { Telegraf } from "telegraf"; +import { MainContext } from "../bot/start"; +import {subscribeToInvoice} from 'lightning' +import { Order, User } from '../models'; const { payToBuyer } = require('./pay_request'); -const lnd = require('./connect'); -const messages = require('../bot/messages'); +import { lnd } from "./connect"; +import * as messages from '../bot/messages'; const ordersActions = require('../bot/ordersActions'); const { getUserI18nContext, getEmojiRate, decimalRound } = require('../util'); -const logger = require('../logger'); +import logger from "../logger"; -const subscribeInvoice = async (bot, id, resub) => { +const subscribeInvoice = async (bot: Telegraf, id: string, resub: boolean) => { try { const sub = subscribeToInvoice({ id, lnd }); sub.on('invoice_updated', async invoice => { if (invoice.is_held && !resub) { const order = await Order.findOne({ hash: invoice.id }); + if (order === null) throw Error("Order was not found in DB"); logger.info( `Order ${order._id} Invoice with hash: ${id} is being held!` ); const buyerUser = await User.findOne({ _id: order.buyer_id }); const sellerUser = await User.findOne({ _id: order.seller_id }); + if (buyerUser === null || sellerUser === null) throw Error("buyer or seller was not found in DB"); order.status = 'ACTIVE'; // This is the i18n context we need to pass to the message const i18nCtxBuyer = await getUserI18nContext(buyerUser); @@ -47,11 +51,12 @@ const subscribeInvoice = async (bot, id, resub) => { rate ); } - order.invoice_held_at = Date.now(); + order.invoice_held_at = new Date(); order.save(); } if (invoice.is_confirmed) { const order = await Order.findOne({ hash: id }); + if (order === null) throw Error("Order was not found in DB"); logger.info( `Order ${order._id} - Invoice with hash: ${id} was settled!` ); @@ -59,6 +64,7 @@ const subscribeInvoice = async (bot, id, resub) => { await order.save(); const buyerUser = await User.findOne({ _id: order.buyer_id }); const sellerUser = await User.findOne({ _id: order.seller_id }); + if (buyerUser === null || sellerUser === null) throw Error("buyer and/or seller was not found in DB"); // We need two i18n contexts to send messages to each user const i18nCtxBuyer = await getUserI18nContext(buyerUser); const i18nCtxSeller = await getUserI18nContext(sellerUser); @@ -121,4 +127,4 @@ const subscribeInvoice = async (bot, id, resub) => { } }; -module.exports = subscribeInvoice; +export default subscribeInvoice;