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; } }