-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlnbits_api.js
More file actions
203 lines (184 loc) · 5.68 KB
/
lnbits_api.js
File metadata and controls
203 lines (184 loc) · 5.68 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require('dotenv').config()
const axios = require('axios')
const QRcode = require('qrcode')
const Jimp = require("jimp");
const jsQR = require("jsqr");
const admin_key = process.env.ADMIN_KEY || ""
const invoice_key = process.env.INVOICE_KEY || ""
const url = process.env.LNBITS_URL || ""
let invoice_headers = {"X-Api-Key": invoice_key }
let admin_headers = {"X-Api-Key": admin_key }
let json_content = {"Content-type" : "application/json"}
const base_url = url + "/api/v1/"
console.log("BASE URL", base_url)
async function getWalletInfo() {
let msg = ''
try {
let response = await axios.get(base_url + "wallet", { headers: invoice_headers})
if (response.status == 200) {
console.log(response.data)
msg = "*Your Balance:* " + response.data.balance/1000 + " sats \n\n"
msg += "*Wallet Name:* " + response.data.name
return msg
}
} catch (err) {
console.log(err.response.status)
console.log(err.response.data)
msg = "Error fetching data. Try again later."
return msg
}
}
async function getBalance() {
let msg = ''
try {
let response = await axios.get(base_url + "wallet", { headers: invoice_headers})
if (response.status == 200) {
console.log(response.data)
msg = response.data.balance/1000
return msg
}
} catch (err) {
console.log(err.response.status)
console.log(err.response.data)
msg = "error"
return msg
}
}
async function createInvoice(amount) {
let msg = ''
let full_header = Object.assign(invoice_headers, json_content)
console.log("\n" + base_url + "payments")
console.log(full_header)
try {
let payload = {
"out": false,
"amount": amount*100,
"memo": '',
"webhook": '',
"unit": ""}
let response = await axios({
method: 'post',
url: base_url + "payments",
data: payload,
headers: full_header
})
if (response.status == 201) { // status created
//console.log(response.data)
// response.data contains payment_hash, payment_request, checking_id, lnurl_response
// return payment_request values as it is the bolt11
return response.data.payment_request
}
} catch (err) {
console.log(err.response.status)
msg = "Error"
return msg
}
}
async function decodeInvoice(invoice) {
let msg = ''
let full_header = Object.assign(invoice_headers, json_content)
console.log("\n" + base_url + "payments/decode")
console.log(full_header)
try {
let payload = {
"data": invoice
}
console.log(payload)
let response = await axios(
{
method: 'post',
url: base_url + "payments/decode",
data: payload,
headers: full_header
})
if (response.status == 200) {
console.log("inside response 200")
console.log(response.data)
return response.data
} else {
return response.message
}
} catch (err) {
console.log(err.response.status)
console.log(err.response.data)
msg = "Error fetching data. Try again later."
return msg
}
}
async function checkInvoice(payment_hash) {
let msg = ''
try {
let response = await axios.get(base_url + "payments/" + payment_hash, { headers: invoice_headers})
if (response.status == 200) {
//console.log(response.data)
return response.data
}
} catch (err) {
//console.log(err.response.status)
//console.log(err.response.data)
msg = "Error fetching data. Try again later."
return msg
}
}
// NOTE: this enables anyone who visits the bot to pay w/your account
// so this is for demo purposes only.
async function payInvoice(invoice) {
let msg = ''
let payload = {
"out": true,
"bolt11": invoice }
let full_header = Object.assign(admin_headers, json_content)
try {
let response = await axios({
method: 'post',
url: base_url + "payments",
data: payload,
headers: full_header
})
if (response.status == 200) {
console.log(response.data)
msg += response.data
return msg
}
} catch (err) {
console.log(err.response.status)
console.log(err.response.data)
msg = "Error fetching data. Try again later."
return msg
}
}
const generateQR = async text => {
// NOTE: sending as form data as data url in a stream does not seem to work with TG api as of this writing.
// If any known solutions, please feel free to make a pull request.
try {
const path = '/tmp/qr.png' // make this dynamic
await QRcode.toFile(path, text, {
errorCorrectionLevel: 'H'
})
return path
} catch (err) {
console.error(err)
return "error"
}
}
async function decodeQRFromUrl(url) {
try {
const response = await axios.get(url, { responseType: 'arraybuffer'})
const buffer = Buffer.from(response.data, "utf-8")
const img2 = await Jimp.read(buffer)
const value = await jsQR(img2.bitmap.data, img2.bitmap.width, img2.bitmap.height)
return value.data
} catch (error) {
return "error"
}
}
module.exports = {
getWalletInfo,
getBalance,
createInvoice,
decodeInvoice,
payInvoice,
checkInvoice,
generateQR,
decodeQRFromUrl
}