Compare commits
5 Commits
d34c8ff0cd
...
v1.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4ab769453
|
||
|
|
ae44874671
|
||
|
|
833ab50616
|
||
|
|
35844b577c
|
||
|
|
b51bbd446c
|
38
index.js
38
index.js
@@ -42,16 +42,24 @@ function startPingInterval () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function waitForInvoicePaid (room, nick, invoice, retries=0) {
|
function waitForInvoicePaid (room, nick, invoice, retries=0) {
|
||||||
|
let timeout = 3000;
|
||||||
|
|
||||||
if (retries >= 300) {
|
if (retries >= 300) {
|
||||||
console.log(`Invoice not paid after 15 minutes: ${invoice.payment_hash}`);
|
console.log(`Invoice not paid after 15 minutes: ${invoice.payment_hash}`);
|
||||||
return false;
|
let timeout = 5000;
|
||||||
|
if (retries >= 500) {
|
||||||
|
// TODO ask user to request voice again to create a new invoice
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lndhub.getInvoice(invoice.payment_hash).then(async data => {
|
lndhub.getInvoice(invoice.payment_hash).then(async data => {
|
||||||
if (data.is_paid) {
|
if (data && data.is_paid) {
|
||||||
console.log(`Invoice paid: ${invoice.payment_hash}`);
|
console.log(`Invoice paid: ${invoice.payment_hash}`);
|
||||||
await grantVoice(room, nick);
|
await grantVoice(room, nick);
|
||||||
} else {
|
} else {
|
||||||
setTimeout(waitForInvoicePaid, 3000, room, nick, invoice, retries++);
|
if (!data) console.warn('Fetching invoice status failed');
|
||||||
|
setTimeout(waitForInvoicePaid, timeout, room, nick, invoice, retries++);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -85,10 +93,14 @@ async function handleVoiceRequest (stanza) {
|
|||||||
console.log(`${nick} requested voice in ${room}`);
|
console.log(`${nick} requested voice in ${room}`);
|
||||||
|
|
||||||
const invoice = await lndhub.createInvoice(config.amounts.voice, `Donation for ${room}`);
|
const invoice = await lndhub.createInvoice(config.amounts.voice, `Donation for ${room}`);
|
||||||
console.log(`Created lightning invoice for ${nick}: ${invoice.payment_hash}`)
|
if (invoice) {
|
||||||
|
console.log(`Created lightning invoice for ${nick}: ${invoice.payment_hash}`)
|
||||||
await respondToVoiceRequest(room, nick, invoice);
|
await respondToVoiceRequest(room, nick, invoice);
|
||||||
waitForInvoicePaid(room, nick, invoice);
|
waitForInvoicePaid(room, nick, invoice);
|
||||||
|
} else {
|
||||||
|
console.warn(`Invoice creation failed!`);
|
||||||
|
// TODO notify ops contact
|
||||||
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
@@ -105,10 +117,16 @@ async function connectXmpp () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function connectLndhub () {
|
function connectLndhub () {
|
||||||
return lndhub.auth(config.lndhub.username, config.lndhub.password).then(() => {
|
return lndhub.auth(config.lndhub.username, config.lndhub.password).then(connected => {
|
||||||
console.log("Connected to Lndhub");
|
if (connected) {
|
||||||
|
console.log("Connected to Lndhub");
|
||||||
|
// automatically refresh auth token once a day
|
||||||
|
setInterval(lndhub.reauth, 86400000);
|
||||||
|
} else {
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.warn(`Lndhub auth failed: ${err}`);
|
console.error(`Lndhub connection failed: ${err}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,61 +5,82 @@ export default class Lndhub {
|
|||||||
this.baseUrl = baseUrl;
|
this.baseUrl = baseUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async callEndpoint (method, path, payload) {
|
||||||
|
const options = { method, headers: { 'Content-Type': 'application/json' } };
|
||||||
|
|
||||||
|
if (path !== '/auth') {
|
||||||
|
options.headers['Authorization'] = `Bearer ${this.accessToken}`;
|
||||||
|
}
|
||||||
|
if (typeof payload !== 'undefined') {
|
||||||
|
options.body = JSON.stringify(payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await fetch(`${this.baseUrl}${path}`, options);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleErroredRequest (result, retryFunction, args=[]) {
|
||||||
|
console.warn('API request failed:', result.message);
|
||||||
|
if (result.code === 1) {
|
||||||
|
return this.reauth().then(connected => {
|
||||||
|
if (connected) {
|
||||||
|
console.warn('Lndhub reconnected, trying again...');
|
||||||
|
return this[retryFunction](...args);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async auth (username, password) {
|
async auth (username, password) {
|
||||||
const payload = { "login": username, "password": password };
|
const payload = { "login": username, "password": password };
|
||||||
|
const data = await this.callEndpoint('post', '/auth', payload);
|
||||||
|
|
||||||
const res = await fetch(`${this.baseUrl}/auth`, {
|
if (data.error) {
|
||||||
method: 'post',
|
console.warn('Lndhub connection failed:', data.message);
|
||||||
body: JSON.stringify(payload),
|
return false;
|
||||||
headers: { 'Content-Type': 'application/json' }
|
} else {
|
||||||
});
|
this.refreshToken = data.refresh_token;
|
||||||
const data = await res.json();
|
this.accessToken = data.access_token;
|
||||||
|
return true;
|
||||||
this.refreshToken = data.refresh_token;
|
}
|
||||||
this.accessToken = data.access_token;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async reauth (refreshToken) {
|
async reauth (refreshToken) {
|
||||||
const payload = { "refresh_token": this.refreshToken };
|
const payload = { "refresh_token": this.refreshToken };
|
||||||
|
const data = await this.callEndpoint('post', '/auth', payload);
|
||||||
|
|
||||||
const res = await fetch(`${this.baseUrl}/auth`, {
|
if (data.error) {
|
||||||
method: 'post',
|
console.warn('Lndhub re-auth failed:', data.message);
|
||||||
body: JSON.stringify(payload),
|
return false;
|
||||||
headers: { 'Content-Type': 'application/json' }
|
} else {
|
||||||
});
|
if (this.accessToken !== data.access_token) {
|
||||||
const data = await res.json();
|
console.log('Lndhub access token refreshed');
|
||||||
|
}
|
||||||
this.accessToken = data.access_token;
|
this.accessToken = data.access_token;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async createInvoice (amount, description) {
|
async createInvoice (amount, description) {
|
||||||
const payload = { amount, description };
|
const payload = { amount, description };
|
||||||
|
let data = await this.callEndpoint('post', '/v2/invoices', payload);
|
||||||
|
|
||||||
const res = await fetch(`${this.baseUrl}/v2/invoices`, {
|
if (data.error) {
|
||||||
method: 'post',
|
return this.handleErroredRequest(data, 'createInvoice', Array.from(arguments));
|
||||||
body: JSON.stringify(payload),
|
} else {
|
||||||
headers: {
|
return data;
|
||||||
'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) {
|
async getInvoice (paymentHash) {
|
||||||
const res = await fetch(`${this.baseUrl}/v2/invoices/${paymentHash}`, {
|
const data = await this.callEndpoint('get', `/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;
|
if (data.error) {
|
||||||
|
return this.handleErroredRequest(data, 'getInvoice', Array.from(arguments));
|
||||||
|
} else {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "xmpp-lightning-antispam",
|
"name": "xmpp-lightning-antispam",
|
||||||
"version": "1.0.0",
|
"version": "1.1.0",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "xmpp-lightning-antispam",
|
"name": "xmpp-lightning-antispam",
|
||||||
"version": "1.0.0",
|
"version": "1.1.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@xmpp/client": "^0.13.1",
|
"@xmpp/client": "^0.13.1",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xmpp-lightning-antispam",
|
"name": "xmpp-lightning-antispam",
|
||||||
"version": "1.0.0",
|
"version": "1.1.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
|||||||
Reference in New Issue
Block a user