From 3af9cda96d50831b8f53a62771cf3796c488d2c7 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Mon, 9 Dec 2019 21:41:25 +0000 Subject: [PATCH 1/5] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 56c6b5f..7c0f865 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,12 @@ Add config vars : * `MACAROON`: hex-encoded `admin.macaroon` * `TLSCERT`: hex-encoded `tls.cert` +### Reference client implementation + +Can be used in ReactNative or Nodejs environment + +* https://github.com/BlueWallet/BlueWallet/blob/master/class/lightning-custodian-wallet.js + ### Tests From daeae3c72d6b6124a28cb208600baddcf187505e Mon Sep 17 00:00:00 2001 From: Overtorment Date: Sat, 14 Dec 2019 19:24:14 +0000 Subject: [PATCH 2/5] FIX: added some caching; skip checking old invoices --- class/User.js | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/class/User.js b/class/User.js index 5eddfb4..1e91054 100644 --- a/class/User.js +++ b/class/User.js @@ -4,6 +4,11 @@ var crypto = require('crypto'); var lightningPayReq = require('bolt11'); import { BigNumber } from 'bignumber.js'; +// static cache: +let _invoice_ispaid_cache = {}; +let _listtransactions_cache = false; +let _listtransactions_cache_expiry_ts = 0; + export class User { /** * @@ -244,17 +249,21 @@ export class User { invoice.payment_hash = tag.data; } } - invoice.ispaid = !!(await this.getPaymentHashPaid(invoice.payment_hash)); + + invoice.ispaid = _invoice_ispaid_cache[invoice.payment_hash] || !!(await this.getPaymentHashPaid(invoice.payment_hash)); if (!invoice.ispaid) { - // TODO: check if expired - // attempting to lookup invoice - let lookup_info = await this.lookupInvoice(invoice.payment_hash); - invoice.ispaid = lookup_info.settled; - if (invoice.ispaid) { - // so invoice was paid after all - await this.setPaymentHashPaid(invoice.payment_hash); - await this.clearBalanceCache(); + if (decoded && decoded.timestamp > ((+new Date()) / 1000 - 3600 * 24 * 5)) { + // if invoice is not too old we query lnd to find out if its paid + let lookup_info = await this.lookupInvoice(invoice.payment_hash); + invoice.ispaid = lookup_info.settled; // TODO: start using `state` instead as its future proof, and this one might get deprecated + if (invoice.ispaid) { + // so invoice was paid after all + await this.setPaymentHashPaid(invoice.payment_hash); + await this.clearBalanceCache(); + } } + } else { + _invoice_ispaid_cache[invoice.payment_hash] = true; } invoice.amt = decoded.satoshis; @@ -333,12 +342,15 @@ export class User { * @private */ async _listtransactions() { - const key = 'listtransactions'; - let response = await this._redis.get(key); + let response = _listtransactions_cache; if (response) { + if (+new Date() > _listtransactions_cache_expiry_ts) { + // invalidate cache + response = _listtransactions_cache = false; + } + try { - let json = JSON.parse(response); - return json; + return JSON.parse(response); } catch (_) { // nop } @@ -356,8 +368,8 @@ export class User { time: tx.time, }); } - await this._redis.set(key, JSON.stringify(ret)); - await this._redis.expire(key, 5 * 60); + _listtransactions_cache = JSON.stringify(ret); + _listtransactions_cache_expiry_ts = +new Date() + 5 * 60 * 1000; // 5 min return ret; } From dd409cc04fc42cfe4c0cb0d61210f50ea1f470c6 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Sat, 14 Dec 2019 19:26:06 +0000 Subject: [PATCH 3/5] REF --- controllers/website.js | 2 +- scripts/important-channels.js | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/controllers/website.js b/controllers/website.js index f64628d..66234cc 100644 --- a/controllers/website.js +++ b/controllers/website.js @@ -66,7 +66,7 @@ const pubkey2name = { '0237fefbe8626bf888de0cad8c73630e32746a22a2c4faa91c1d9877a3826e1174': '1.ln.aantonop.com', '026c7d28784791a4b31a64eb34d9ab01552055b795919165e6ae886de637632efb': 'LivingRoomOfSatoshi', '02816caed43171d3c9854e3b0ab2cf0c42be086ff1bd4005acc2a5f7db70d83774': 'ln.pizza', - '024a2e265cd66066b78a788ae615acdc84b5b0dec9efac36d7ac87513015eaf6ed': 'Bitrefill.com/lightning', + '0254ff808f53b2f8c45e74b70430f336c6c76ba2f4af289f48d6086ae6e60462d3': 'bitrefill thor', '02a0bc43557fae6af7be8e3a29fdebda819e439bea9c0f8eb8ed6a0201f3471ca9': 'LightningPeachHub', '02d4531a2f2e6e5a9033d37d548cff4834a3898e74c3abe1985b493c42ebbd707d': 'coinfinity.co', '02d23fa6794d8fd056c757f3c8f4877782138dafffedc831fc570cab572620dc61': 'paywithmoon.com', diff --git a/scripts/important-channels.js b/scripts/important-channels.js index 26fde6a..334f694 100644 --- a/scripts/important-channels.js +++ b/scripts/important-channels.js @@ -3,8 +3,12 @@ let important_channels = { '03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f': 'ACINQ', '03abf6f44c355dec0d5aa155bdbdd6e0c8fefe318eff402de65c6eb2e1be55dc3e': 'OpenNode', '0242a4ae0c5bef18048fbecf995094b74bfb0f7391418d71ed394784373f41e4f3': 'coingate.com', - '0232e20e7b68b9b673fb25f48322b151a93186bffe4550045040673797ceca43cf': 'zigzag.io', - '024a2e265cd66066b78a788ae615acdc84b5b0dec9efac36d7ac87513015eaf6ed': 'Bitrefill.com/lightning', + '0254ff808f53b2f8c45e74b70430f336c6c76ba2f4af289f48d6086ae6e60462d3': 'bitrefill thor', + '025f1456582e70c4c06b61d5c8ed3ce229e6d0db538be337a2dc6d163b0ebc05a5': 'paywithmoon.com', + '02c91d6aa51aa940608b497b6beebcb1aec05be3c47704b682b3889424679ca490': 'lnbig 21', + '0279c22ed7a068d10dc1a38ae66d2d6461e269226c60258c021b1ddcdfe4b00bc4': 'ln1.satoshilabs.com', + '026c7d28784791a4b31a64eb34d9ab01552055b795919165e6ae886de637632efb': 'LivingRoomOfSatoshi', + '02816caed43171d3c9854e3b0ab2cf0c42be086ff1bd4005acc2a5f7db70d83774': 'ln.pizza', }; let lightning = require('../lightning'); @@ -16,7 +20,7 @@ lightning.listChannels({}, function(err, response) { } let lightningListChannels = response; for (let channel of lightningListChannels.channels) { - if (channel.capacity < 5000000) { + if (0 && channel.capacity < 5000000) { console.log( 'lncli closechannel', channel.channel_point.replace(':', ' '), From 41736cccd84498a759494ea843224a9a6d2e2291 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Sat, 14 Dec 2019 19:51:44 +0000 Subject: [PATCH 4/5] FIX: change default ln invoice expiry hour -> day --- controllers/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/api.js b/controllers/api.js index 94bc797..e7c5855 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -100,7 +100,7 @@ router.post('/addinvoice', postLimiter, async function(req, res) { if (!req.body.amt || /*stupid NaN*/ !(req.body.amt > 0)) return errorBadArguments(res); - lightning.addInvoice({ memo: req.body.memo, value: req.body.amt }, async function(err, info) { + lightning.addInvoice({ memo: req.body.memo, value: req.body.amt, expiry: 3600 * 24 }, async function(err, info) { if (err) return errorLnd(res); info.pay_req = info.payment_request; // client backwards compatibility From 1e17b9925bb1956d0cb9e4fc4ad700bf36a26299 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Sat, 21 Dec 2019 15:00:36 +0000 Subject: [PATCH 5/5] FIX --- class/User.js | 15 ++++++++------- package-lock.json | 2 +- package.json | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/class/User.js b/class/User.js index 1e91054..cc92305 100644 --- a/class/User.js +++ b/class/User.js @@ -252,7 +252,7 @@ export class User { invoice.ispaid = _invoice_ispaid_cache[invoice.payment_hash] || !!(await this.getPaymentHashPaid(invoice.payment_hash)); if (!invoice.ispaid) { - if (decoded && decoded.timestamp > ((+new Date()) / 1000 - 3600 * 24 * 5)) { + if (decoded && decoded.timestamp > +new Date() / 1000 - 3600 * 24 * 5) { // if invoice is not too old we query lnd to find out if its paid let lookup_info = await this.lookupInvoice(invoice.payment_hash); invoice.ispaid = lookup_info.settled; // TODO: start using `state` instead as its future proof, and this one might get deprecated @@ -347,12 +347,12 @@ export class User { if (+new Date() > _listtransactions_cache_expiry_ts) { // invalidate cache response = _listtransactions_cache = false; - } - - try { - return JSON.parse(response); - } catch (_) { - // nop + } else { + try { + return JSON.parse(response); + } catch (_) { + // nop + } } } @@ -370,6 +370,7 @@ export class User { } _listtransactions_cache = JSON.stringify(ret); _listtransactions_cache_expiry_ts = +new Date() + 5 * 60 * 1000; // 5 min + this._redis.set('listtransactions', _listtransactions_cache); // backup, will use later TODO return ret; } diff --git a/package-lock.json b/package-lock.json index 8a60a2c..41904fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "LndHub", - "version": "1.1.1", + "version": "1.1.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 438cd2d..d15c963 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "LndHub", - "version": "1.1.1", + "version": "1.1.3", "description": "", "main": "index.js", "scripts": {