Skip to content

Commit 970ae84

Browse files
authored
Revert "ln: convert js to ts (#541)" (#545)
This reverts commit 3166fe5.
1 parent e2b461d commit 970ae84

7 files changed

+54
-61
lines changed

Diff for: ln/connect.ts renamed to ln/connect.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as fs from 'fs';
2-
import * as path from 'path';
3-
import * as lightning from 'lightning';
4-
import { logger } from '../logger';
1+
const fs = require('fs');
2+
const path = require('path');
3+
const lightning = require('lightning');
4+
const { logger } = require('../logger');
55

66
const { authenticatedLndGrpc } = lightning;
77

@@ -47,4 +47,4 @@ const { lnd } = authenticatedLndGrpc({
4747
socket,
4848
});
4949

50-
export { lnd };
50+
module.exports = lnd;

Diff for: ln/hold_invoice.ts renamed to ln/hold_invoice.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import { randomBytes, createHash } from 'crypto';
2-
import * as lightning from 'lightning';
3-
import { lnd } from './connect'
4-
import { logger } from '../logger';
1+
const { createHash, randomBytes } = require('crypto');
2+
const lightning = require('lightning');
3+
const lnd = require('./connect');
4+
const { logger } = require('../logger');
55

6-
const createHoldInvoice = async ({ description, amount } : { description: string, amount: number }) => {
6+
const createHoldInvoice = async ({ description, amount }) => {
77
try {
88
const randomSecret = () => randomBytes(32);
9-
const sha256 = (buffer: Buffer): string => createHash('sha256').update(buffer).digest('hex');
9+
const sha256 = buffer => createHash('sha256').update(buffer).digest('hex');
1010
// We create a random secret
1111
const secret = randomSecret();
1212
const expiresAt = new Date();
1313
expiresAt.setSeconds(expiresAt.getSeconds() + 3600);
1414

1515
const hash = sha256(secret);
16-
const cltv_delta = Number(process.env.HOLD_INVOICE_CLTV_DELTA);
16+
const cltv_delta = parseInt(process.env.HOLD_INVOICE_CLTV_DELTA);
1717
const { request, id } = await lightning.createHodlInvoice({
1818
cltv_delta,
1919
lnd,
2020
description,
2121
id: hash,
2222
tokens: amount,
23-
expires_at: expiresAt.toISOString(),
23+
expires_at: expiresAt,
2424
});
2525

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

33-
const settleHoldInvoice = async ( { secret }: { secret: string } ) => {
33+
const settleHoldInvoice = async ({ secret }) => {
3434
try {
3535
await lightning.settleHodlInvoice({ lnd, secret });
3636
} catch (error) {
3737
logger.error(error);
3838
}
3939
};
4040

41-
const cancelHoldInvoice = async ( { hash }: { hash: string } ) => {
41+
const cancelHoldInvoice = async ({ hash }) => {
4242
try {
4343
await lightning.cancelHodlInvoice({ lnd, id: hash });
4444
} catch (error) {
4545
logger.error(error);
4646
}
4747
};
4848

49-
const getInvoice = async ( { hash }: { hash: string } ) => {
49+
const getInvoice = async ({ hash }) => {
5050
try {
5151
return await lightning.getInvoice({ lnd, id: hash });
5252
} catch (error) {
5353
logger.error(error);
5454
}
5555
};
5656

57-
export {
57+
module.exports = {
5858
createHoldInvoice,
5959
settleHoldInvoice,
6060
cancelHoldInvoice,

Diff for: ln/index.ts renamed to ln/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import {
1+
const {
22
createHoldInvoice,
33
settleHoldInvoice,
44
cancelHoldInvoice,
55
getInvoice,
6-
} from './hold_invoice';
7-
import { subscribeInvoice, payHoldInvoice } from './subscribe_invoice';
6+
} = require('./hold_invoice');
7+
const { subscribeInvoice, payHoldInvoice } = require('./subscribe_invoice');
88
const subscribeProbe = require('./subscribe_probe');
9-
import { resubscribeInvoices } from './resubscribe_invoices';
9+
const resubscribeInvoices = require('./resubscribe_invoices');
1010
const { payRequest, payToBuyer, isPendingPayment } = require('./pay_request');
11-
import { getInfo } from './info';
11+
const { getInfo } = require('./info');
1212

13-
export {
13+
module.exports = {
1414
createHoldInvoice,
1515
subscribeInvoice,
1616
resubscribeInvoices,

Diff for: ln/info.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const lightning = require('lightning');
2+
const lnd = require('./connect');
3+
const { logger } = require('../logger');
4+
5+
const getInfo = async () => {
6+
try {
7+
return await lightning.getWalletInfo({ lnd });
8+
} catch (error) {
9+
logger.error(error);
10+
}
11+
};
12+
13+
module.exports = { getInfo };

Diff for: ln/info.ts

-11
This file was deleted.

Diff for: ln/resubscribe_invoices.ts renamed to ln/resubscribe_invoices.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { Telegraf } from 'telegraf';
2-
import { MainContext } from "../bot/start";
3-
import { getInvoices, GetInvoicesResult } from 'lightning';
4-
import { lnd } from './connect';
5-
import { subscribeInvoice } from './subscribe_invoice';
6-
import { Order } from '../models';
7-
import { logger } from "../logger";
1+
const { getInvoices } = require('lightning');
2+
const lnd = require('./connect');
3+
const { subscribeInvoice } = require('./subscribe_invoice');
4+
const { Order } = require('../models');
5+
const { logger } = require('../logger');
86

9-
const resubscribeInvoices = async (bot: Telegraf<MainContext>) => {
7+
const resubscribeInvoices = async bot => {
108
try {
119
let invoicesReSubscribed = 0;
12-
const isHeld = (invoice: any) => !!invoice.is_held;
10+
const isHeld = invoice => !!invoice.is_held;
1311
const unconfirmedInvoices = (
1412
await getInvoices({
1513
lnd,
@@ -31,9 +29,9 @@ const resubscribeInvoices = async (bot: Telegraf<MainContext>) => {
3129
}
3230
logger.info(`Invoices resubscribed: ${invoicesReSubscribed}`);
3331
} catch (error) {
34-
logger.error(`ResubscribeInvoices catch: ${String(error)}`);
32+
logger.error(`ResubcribeInvoice catch: ${error.toString()}`);
3533
return false;
3634
}
3735
};
3836

39-
export { resubscribeInvoices };
37+
module.exports = resubscribeInvoices;

Diff for: ln/subscribe_invoice.ts renamed to ln/subscribe_invoice.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
import { Telegraf } from "telegraf";
2-
import { MainContext } from "../bot/start";
3-
import { subscribeToInvoice } from 'lightning'
4-
import { Order, User } from '../models';
1+
const { subscribeToInvoice } = require('lightning');
2+
const { Order, User } = require('../models');
53
const { payToBuyer } = require('./pay_request');
6-
import { lnd } from "./connect";
7-
import * as messages from '../bot/messages';
4+
const lnd = require('./connect');
5+
const messages = require('../bot/messages');
86
const ordersActions = require('../bot/ordersActions');
97
const { getUserI18nContext, getEmojiRate, decimalRound } = require('../util');
10-
import { logger } from "../logger";
11-
import { IOrder } from "../models/order";
8+
const { logger } = require('../logger');
129
const OrderEvents = require('../bot/modules/events/orders');
1310

14-
const subscribeInvoice = async (bot: Telegraf<MainContext>, id: string, resub: boolean) => {
11+
const subscribeInvoice = async (bot, id, resub) => {
1512
try {
1613
const sub = subscribeToInvoice({ id, lnd });
1714
sub.on('invoice_updated', async invoice => {
1815
if (invoice.is_held && !resub) {
1916
const order = await Order.findOne({ hash: invoice.id });
20-
if (order === null) throw Error("Order was not found in DB");
2117
logger.info(
2218
`Order ${order._id} Invoice with hash: ${id} is being held!`
2319
);
2420
const buyerUser = await User.findOne({ _id: order.buyer_id });
2521
const sellerUser = await User.findOne({ _id: order.seller_id });
26-
if (buyerUser === null || sellerUser === null) throw Error("buyer or seller was not found in DB");
2722
order.status = 'ACTIVE';
2823
// This is the i18n context we need to pass to the message
2924
const i18nCtxBuyer = await getUserI18nContext(buyerUser);
@@ -53,13 +48,12 @@ const subscribeInvoice = async (bot: Telegraf<MainContext>, id: string, resub: b
5348
rate
5449
);
5550
}
56-
order.invoice_held_at = new Date();
51+
order.invoice_held_at = Date.now();
5752
order.save();
5853
OrderEvents.orderUpdated(order);
5954
}
6055
if (invoice.is_confirmed) {
6156
const order = await Order.findOne({ hash: id });
62-
if (order === null) throw Error("Order was not found in DB");
6357
logger.info(
6458
`Order ${order._id} - Invoice with hash: ${id} was settled!`
6559
);
@@ -78,14 +72,13 @@ const subscribeInvoice = async (bot: Telegraf<MainContext>, id: string, resub: b
7872
}
7973
};
8074

81-
const payHoldInvoice = async (bot: Telegraf<MainContext>, order: IOrder) => {
75+
const payHoldInvoice = async (bot, order) => {
8276
try {
8377
order.status = 'PAID_HOLD_INVOICE';
8478
await order.save();
8579
OrderEvents.orderUpdated(order);
8680
const buyerUser = await User.findOne({ _id: order.buyer_id });
8781
const sellerUser = await User.findOne({ _id: order.seller_id });
88-
if (buyerUser === null || sellerUser === null) throw Error("buyer or seller was not found in DB");
8982
// We need two i18n contexts to send messages to each user
9083
const i18nCtxBuyer = await getUserI18nContext(buyerUser);
9184
const i18nCtxSeller = await getUserI18nContext(sellerUser);
@@ -145,4 +138,4 @@ const payHoldInvoice = async (bot: Telegraf<MainContext>, order: IOrder) => {
145138
}
146139
};
147140

148-
export { subscribeInvoice, payHoldInvoice };
141+
module.exports = { subscribeInvoice, payHoldInvoice };

0 commit comments

Comments
 (0)