-
Notifications
You must be signed in to change notification settings - Fork 0
/
libln.js
97 lines (81 loc) · 3.22 KB
/
libln.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module.exports = {
getNodeInfo: getNodeInfo,
subToInvoice: subToInvoice,
getNewInvoiceInfo: getNewInvoiceInfo,
subToInvoices: subToInvoices,
getInvoiceResult: getInvoiceResult
};
const db = require('./library');
const {getWalletInfo} = require('ln-service');
// https://github.com/alexbosworth/ln-service#all-methods
// Get info about the Lightning node
async function getNodeInfo() {
try {
const walletInfo = await getWalletInfo({ lnd });
// console.log(`info: ${JSON.stringify(walletInfo, null, 2)}`);
console.log(`lightning node: ${JSON.stringify(walletInfo.alias, null, 2)}`);
console.log(`peers_count: ${JSON.stringify(walletInfo.peers_count, null, 2)}`);
console.log(`public_key: ${JSON.stringify(walletInfo.public_key, null, 2)}`);
console.log("\nReconnect any websocket browser windows...");
return walletInfo;
} catch (err) {
console.error(`Error getting node info: ${err.message}`)
}
}
const {subscribeToInvoice} = require('ln-service');
const {once} = require('events');
async function subToInvoice(id) {
// console.log(`Waiting for Payment...`);
try {
const sub = subscribeToInvoice({ id: id, lnd: lnd });
const [invoice] = await once(sub, 'invoice_updated');
// console.log(`invoice: ${JSON.stringify(invoice, null, 2)}`);
return invoice;
} catch (err) {
console.error(`Error creating invoice info: ${err.message}`)
}
}
const {getInvoice} = require('ln-service');
const {createInvoice} = require('ln-service');
async function getNewInvoiceInfo(inv) {
// console.log(`Creating New Invoice...`);
try {
const invoice = await createInvoice(inv);
// console.log(`invoice: ${JSON.stringify(invoice, null, 2)}`);
// subToInvoice(invoice.id);
//waitForPayment(invoice.id);
return invoice;
} catch (err) {
console.error(`Error creating invoice info: ${err.message}`)
}
}
// https://github.com/alexbosworth/ln-service#subscribetoinvoices
// const {subscribeToInvoice} = require('ln-service');
const {subscribeToInvoices} = require('ln-service');
async function subToInvoices() {
// console.log(`Waiting for Payments...`);
try {
const sub = subscribeToInvoices({ lnd: lnd });
sub.on('invoice_updated', (invoice) => {
// console.log(`invoice payment: ${JSON.stringify(invoice, null, 2)}`);
if (invoice.is_confirmed) { // Invoice was paid
delPendingPaymentID(invoice.id); // Delete it so our busy polling will exit and not timeout
}
});
// const [invoice] = await once(sub, 'invoice_updated');
// console.log(`invoice payment: ${JSON.stringify(invoice, null, 2)}`);
// See if it's paying a known invoice
// Clear the pending_payment
} catch (err) {
console.error(`Error creating invoice info: ${err.message}`)
}
}
async function getInvoiceResult(id) {
try {
const invoiceDetails = await getInvoice({id, lnd});
// console.log(`invoiceDetails: ${JSON.stringify(invoiceDetails, null, 2)}`);
return invoiceDetails;
} catch (err) {
console.error(`Error getting invoice info: ${err.message}`)
}
}