Hello world

This commit is contained in:
Râu Cao
2023-11-02 13:59:06 +01:00
commit 67d2bd55fd
7 changed files with 5067 additions and 0 deletions

65
lib/lndhub.js Normal file
View File

@@ -0,0 +1,65 @@
import fetch from 'node-fetch';
export default class Lndhub {
constructor (baseUrl) {
this.baseUrl = baseUrl;
}
async auth (username, password) {
const payload = { "login": username, "password": password };
const res = await fetch(`${this.baseUrl}/auth`, {
method: 'post',
body: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' }
});
const data = await res.json();
this.refreshToken = data.refresh_token;
this.accessToken = data.access_token;
}
async reauth (refreshToken) {
const payload = { "refresh_token": this.refreshToken };
const res = await fetch(`${this.baseUrl}/auth`, {
method: 'post',
body: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' }
});
const data = await res.json();
this.accessToken = data.access_token;
}
async createInvoice (amount, description) {
const payload = { amount, description };
const res = await fetch(`${this.baseUrl}/v2/invoices`, {
method: 'post',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessToken}`
}
});
const data = await res.json();
// TODO re-auth if token has expired
return data;
}
async getInvoice (paymentHash) {
const res = await fetch(`${this.baseUrl}/v2/invoices/${paymentHash}`, {
method: 'get',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessToken}`
}
});
const data = await res.json();
// TODO re-auth if token has expired
return data;
}
}