-
Notifications
You must be signed in to change notification settings - Fork 5
/
lnbits.js
121 lines (99 loc) · 3.11 KB
/
lnbits.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { requestInvoice } = require('lnurl-pay');
const axios = require('axios');
const config = require('./config');
const checkServer = async () => {
try {
const response = await axios.get(`https://${config.ln_host}/api/v1/wallet`, {
headers: { 'X-API-KEY': config.ln_api_key },
});
return response.status === 200;
} catch (error) {
console.error(error);
return false;
}
};
const createInvoice = async (amount, memo, expiry) => {
try {
const data = {
unit: 'sat',
internal: false,
out: false,
amount: amount,
memo: memo || `invoice for ${amount} sats`,
expiry: expiry || 180,
};
const getInvoice = await axios.post(`https://${config.ln_host}/api/v1/payments`, data, {
headers: { 'X-API-KEY': config.ln_api_key },
});
const invoiceData = getInvoice.data;
invoiceData.qrcode = `https://${config.ln_host}/api/v1/qrcode/${invoiceData.payment_request}`;
invoiceData.paylink = `lightning:${invoiceData.payment_request}`;
invoiceData.wslink = `wss://${config.ln_host}/api/v1/ws/${config.ln_wallet}`;
invoiceData.amount = data.amount;
invoiceData.memo = data.memo;
invoiceData.expiry = data.expiry;
console.log(`invoice: ${JSON.stringify(invoiceData, null, 2)}`);
return invoiceData;
}
catch (error) {
console.log(error);
}
return null;
};
const checkPayment = async (payment_hash) => {
try {
const getCheck = await axios.get(`https://${config.ln_host}/api/v1/payments/${payment_hash}`, {
headers: { 'X-API-KEY': config.ln_api_key },
});
const check = getCheck.data;
console.log(`checkPayment: ${JSON.stringify(check, null, 2)}`);
return check;
}
catch (error) {
console.log(`checkPayment: ${error}`);
}
return null;
};
const checkAddress = async (address) => {
try {
const getCheck = await axios.get(`https://${config.ln_host}/api/v1/lnurlscan/${address}`, {
headers: { 'X-API-KEY': config.ln_api_key },
});
return getCheck.data;
}
catch (error) {
console.log(error);
}
return null;
};
const sendPayment = async (address, amount, comment) => {
try {
const scan = await checkAddress(address);
if (comment.length > scan.commentAllowed) {
comment = comment.substring(0, scan.commentAllowed);
}
const { invoice } = await requestInvoice({
lnUrlOrAddress: address,
tokens: amount,
comment: comment,
});
const data = {
out: true,
bolt11: invoice,
};
const getPayment = await axios.post(`https://${config.ln_host}/api/v1/payments`, data, {
headers: { 'X-API-KEY': config.ln_admin_key }
});
return getPayment.data;
}
catch (error) {
console.log(error);
}
};
module.exports = {
checkServer,
createInvoice,
checkPayment,
checkAddress,
sendPayment,
};