Hello world
This commit is contained in:
65
lib/lndhub.js
Normal file
65
lib/lndhub.js
Normal 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;
|
||||
}
|
||||
}
|
||||
59
lib/utils.js
Normal file
59
lib/utils.js
Normal file
@@ -0,0 +1,59 @@
|
||||
import { xml } from "@xmpp/client";
|
||||
|
||||
export function randomHex () {
|
||||
return Math.random().toString(16).slice(2, 8);
|
||||
}
|
||||
|
||||
export function pingXml (to) {
|
||||
return xml(
|
||||
"iq", { to, id: `ping-${randomHex()}`, type: "get" },
|
||||
xml("ping", { xmlns: "urn:xmpp:ping" })
|
||||
)
|
||||
}
|
||||
|
||||
export function isPingResponse (stanza) {
|
||||
return stanza.attrs.id.match(/^ping/) &&
|
||||
stanza.attrs.type === "result";
|
||||
}
|
||||
|
||||
export function roomDirectMessageXml (room, nick, messageBody) {
|
||||
return xml(
|
||||
"message", { type: "chat", to: `${room}/${nick}` },
|
||||
xml("body", {}, messageBody)
|
||||
);
|
||||
}
|
||||
|
||||
export function grantVoiceMessageXml (room, nick) {
|
||||
return xml(
|
||||
"iq", { id: `voice-${randomHex()}`, to: room, type: "set" },
|
||||
xml("query", { xmlns: "http://jabber.org/protocol/muc#admin" },
|
||||
xml("item", { nick: nick, role: "participant" }
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
export function parseForm (stanza) {
|
||||
const form = stanza.getChild("x", "jabber:x:data");
|
||||
if (!form) return false;
|
||||
|
||||
const fieldElements = form.getChildren("field");
|
||||
const fields = [];
|
||||
|
||||
for (const e of fieldElements) {
|
||||
fields.push({
|
||||
name: e.attrs.var,
|
||||
value: e.getChild("value").text()
|
||||
})
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
export function isVoiceRequest (stanza) {
|
||||
const formFields = parseForm(stanza);
|
||||
|
||||
return !!formFields && formFields.find(f => {
|
||||
return f.name === "FORM_TYPE" &&
|
||||
f.value === "http://jabber.org/protocol/muc#request"
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user