Skip to content

Commit

Permalink
Revert "ln: convert js to ts (#541)"
Browse files Browse the repository at this point in the history
This reverts commit 3166fe5.
  • Loading branch information
grunch authored Jul 25, 2024
1 parent e2b461d commit 7081540
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 61 deletions.
10 changes: 5 additions & 5 deletions ln/connect.ts → ln/connect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import * as lightning from 'lightning';
import { logger } from '../logger';
const fs = require('fs');
const path = require('path');
const lightning = require('lightning');
const { logger } = require('../logger');

const { authenticatedLndGrpc } = lightning;

Expand Down Expand Up @@ -47,4 +47,4 @@ const { lnd } = authenticatedLndGrpc({
socket,
});

export { lnd };
module.exports = lnd;
24 changes: 12 additions & 12 deletions ln/hold_invoice.ts → ln/hold_invoice.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { randomBytes, createHash } from 'crypto';
import * as lightning from 'lightning';
import { lnd } from './connect'
import { logger } from '../logger';
const { createHash, randomBytes } = require('crypto');
const lightning = require('lightning');
const lnd = require('./connect');
const { logger } = require('../logger');

const createHoldInvoice = async ({ description, amount } : { description: string, amount: number }) => {
const createHoldInvoice = async ({ description, amount }) => {
try {
const randomSecret = () => randomBytes(32);
const sha256 = (buffer: Buffer): string => createHash('sha256').update(buffer).digest('hex');
const sha256 = buffer => 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 = Number(process.env.HOLD_INVOICE_CLTV_DELTA);
const cltv_delta = parseInt(process.env.HOLD_INVOICE_CLTV_DELTA);
const { request, id } = await lightning.createHodlInvoice({
cltv_delta,
lnd,
description,
id: hash,
tokens: amount,
expires_at: expiresAt.toISOString(),
expires_at: expiresAt,
});

// We sent back the response hash (id) to be used on testing
Expand All @@ -30,31 +30,31 @@ const createHoldInvoice = async ({ description, amount } : { description: string
}
};

const settleHoldInvoice = async ( { secret }: { secret: string } ) => {
const settleHoldInvoice = async ({ secret }) => {
try {
await lightning.settleHodlInvoice({ lnd, secret });
} catch (error) {
logger.error(error);
}
};

const cancelHoldInvoice = async ( { hash }: { hash: string } ) => {
const cancelHoldInvoice = async ({ hash }) => {
try {
await lightning.cancelHodlInvoice({ lnd, id: hash });
} catch (error) {
logger.error(error);
}
};

const getInvoice = async ( { hash }: { hash: string } ) => {
const getInvoice = async ({ hash }) => {
try {
return await lightning.getInvoice({ lnd, id: hash });
} catch (error) {
logger.error(error);
}
};

export {
module.exports = {
createHoldInvoice,
settleHoldInvoice,
cancelHoldInvoice,
Expand Down
12 changes: 6 additions & 6 deletions ln/index.ts → ln/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
const {
createHoldInvoice,
settleHoldInvoice,
cancelHoldInvoice,
getInvoice,
} from './hold_invoice';
import { subscribeInvoice, payHoldInvoice } from './subscribe_invoice';
} = require('./hold_invoice');
const { subscribeInvoice, payHoldInvoice } = require('./subscribe_invoice');
const subscribeProbe = require('./subscribe_probe');
import { resubscribeInvoices } from './resubscribe_invoices';
const resubscribeInvoices = require('./resubscribe_invoices');
const { payRequest, payToBuyer, isPendingPayment } = require('./pay_request');
import { getInfo } from './info';
const { getInfo } = require('./info');

export {
module.exports = {
createHoldInvoice,
subscribeInvoice,
resubscribeInvoices,
Expand Down
13 changes: 13 additions & 0 deletions ln/info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 };
11 changes: 0 additions & 11 deletions ln/info.ts

This file was deleted.

20 changes: 9 additions & 11 deletions ln/resubscribe_invoices.ts → ln/resubscribe_invoices.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Telegraf } from 'telegraf';
import { MainContext } from "../bot/start";
import { getInvoices, GetInvoicesResult } from 'lightning';
import { lnd } from './connect';
import { subscribeInvoice } from './subscribe_invoice';
import { Order } from '../models';
import { logger } from "../logger";
const { getInvoices } = require('lightning');
const lnd = require('./connect');
const { subscribeInvoice } = require('./subscribe_invoice');
const { Order } = require('../models');
const { logger } = require('../logger');

const resubscribeInvoices = async (bot: Telegraf<MainContext>) => {
const resubscribeInvoices = async bot => {
try {
let invoicesReSubscribed = 0;
const isHeld = (invoice: any) => !!invoice.is_held;
const isHeld = invoice => !!invoice.is_held;
const unconfirmedInvoices = (
await getInvoices({
lnd,
Expand All @@ -31,9 +29,9 @@ const resubscribeInvoices = async (bot: Telegraf<MainContext>) => {
}
logger.info(`Invoices resubscribed: ${invoicesReSubscribed}`);
} catch (error) {
logger.error(`ResubscribeInvoices catch: ${String(error)}`);
logger.error(`ResubcribeInvoice catch: ${error.toString()}`);
return false;
}
};

export { resubscribeInvoices };
module.exports = resubscribeInvoices;
25 changes: 9 additions & 16 deletions ln/subscribe_invoice.ts → ln/subscribe_invoice.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import { Telegraf } from "telegraf";
import { MainContext } from "../bot/start";
import { subscribeToInvoice } from 'lightning'
import { Order, User } from '../models';
const { subscribeToInvoice } = require('lightning');
const { Order, User } = require('../models');
const { payToBuyer } = require('./pay_request');
import { lnd } from "./connect";
import * as messages from '../bot/messages';
const lnd = require('./connect');
const messages = require('../bot/messages');
const ordersActions = require('../bot/ordersActions');
const { getUserI18nContext, getEmojiRate, decimalRound } = require('../util');
import { logger } from "../logger";
import { IOrder } from "../models/order";
const { logger } = require('../logger');
const OrderEvents = require('../bot/modules/events/orders');

const subscribeInvoice = async (bot: Telegraf<MainContext>, id: string, resub: boolean) => {
const subscribeInvoice = async (bot, id, resub) => {
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);
Expand Down Expand Up @@ -53,13 +48,12 @@ const subscribeInvoice = async (bot: Telegraf<MainContext>, id: string, resub: b
rate
);
}
order.invoice_held_at = new Date();
order.invoice_held_at = Date.now();
order.save();
OrderEvents.orderUpdated(order);
}
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!`
);
Expand All @@ -78,14 +72,13 @@ const subscribeInvoice = async (bot: Telegraf<MainContext>, id: string, resub: b
}
};

const payHoldInvoice = async (bot: Telegraf<MainContext>, order: IOrder) => {
const payHoldInvoice = async (bot, order) => {
try {
order.status = 'PAID_HOLD_INVOICE';
await order.save();
OrderEvents.orderUpdated(order);
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");
// We need two i18n contexts to send messages to each user
const i18nCtxBuyer = await getUserI18nContext(buyerUser);
const i18nCtxSeller = await getUserI18nContext(sellerUser);
Expand Down Expand Up @@ -145,4 +138,4 @@ const payHoldInvoice = async (bot: Telegraf<MainContext>, order: IOrder) => {
}
};

export { subscribeInvoice, payHoldInvoice };
module.exports = { subscribeInvoice, payHoldInvoice };

0 comments on commit 7081540

Please sign in to comment.