diff options
| author | Adam Soltys <[email protected]> | 2025-03-19 16:23:23 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2025-03-23 21:30:01 -0700 |
| commit | 173fadc3e3113f82533818665413a431c16ea3d6 (patch) | |
| tree | 3049665fa88b97dccd800c0172a1e993f8931243 | |
| parent | 51fe575b9977fb787a3094237b83ee27dd3d03be (diff) | |
| download | coinos-server-173fadc3e3113f82533818665413a431c16ea3d6.tar.gz coinos-server-173fadc3e3113f82533818665413a431c16ea3d6.zip | |
use cln rest
| -rwxr-xr-x | bun.lockb | bin | 209762 -> 209258 bytes | |||
| -rw-r--r-- | lib/cln.ts | 12 | ||||
| -rw-r--r-- | lib/invoices.ts | 2 | ||||
| -rw-r--r-- | lib/lightning.ts | 10 | ||||
| -rw-r--r-- | lib/ln.ts | 21 | ||||
| -rw-r--r-- | lib/notifications.ts | 2 | ||||
| -rw-r--r-- | lib/nwc.ts | 4 | ||||
| -rw-r--r-- | lib/payments.ts | 8 | ||||
| -rw-r--r-- | package.json | 1 | ||||
| -rw-r--r-- | routes/nostr.ts | 4 | ||||
| -rw-r--r-- | routes/payments.ts | 16 |
11 files changed, 60 insertions, 20 deletions
| Binary files differ diff --git a/lib/cln.ts b/lib/cln.ts new file mode 100644 index 00000000..248f40e7 --- /dev/null +++ b/lib/cln.ts @@ -0,0 +1,12 @@ +const method = "POST"; +const str = (p, params) => JSON.stringify({ method: p.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(), params }); +const f = (a) => (a.length === 1 && typeof a[0] === "object" && !Array.isArray(a[0]) ? a[0] : a); +const h = (r) => { if (!r) return; if (r.error) throw r.error; return r.result; }; + +export default ({ host, rune }) => { + const u = `http://${host}/v1/${method}`; + const headers = { rune }; + const p = (body) => fetch(u, { method, headers, body }).then((r) => r.json()); + const get = (_, prop) => (...args) => p(str(prop, f(args))).then(h); + return new Proxy({}, { get }); +}; diff --git a/lib/invoices.ts b/lib/invoices.ts index f6ad65e8..f0e3a2d7 100644 --- a/lib/invoices.ts +++ b/lib/invoices.ts @@ -70,7 +70,7 @@ export const generate = async ({ invoice, user }) => { let r; if (bolt11) { const { id: nodeid } = await ln.getinfo(); - r = await ln.decode(bolt11); + r = await ln.decode({ string: bolt11 }); if (r.payee !== nodeid) fail("invalid invoice"); amount = Math.round(r.amount_msat / 1000); r.bolt11 = bolt11; diff --git a/lib/lightning.ts b/lib/lightning.ts index 8f5f44bc..4ef07f68 100644 --- a/lib/lightning.ts +++ b/lib/lightning.ts @@ -7,7 +7,15 @@ import { PaymentType } from "$lib/types"; import { getInvoice, getPayment, getUser } from "$lib/utils"; export async function listenForLightning() { - const inv = await ln.waitanyinvoice((await g("pay_index")) || 0); + let inv; + try { + inv = await ln.waitanyinvoice({ + lastpay_index: (await g("pay_index")) || 0, + }); + } catch (e) { + return setTimeout(listenForLightning, 2000); + } + const { local_offer_id, bolt11, @@ -1,5 +1,20 @@ import config from "$config"; -const { LightningClient } = (await import("@asoltys/clightning-client")).default; -export default new LightningClient(config.lightning); -export const lnb = new LightningClient(config.lightningb); +const method = "POST"; +const kb = (s) => s.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); +const str = (o) => JSON.stringify(o); +const j = (r) => r.json(); + +const RPC = ({ host, rune }) => { + const u = `http://${host}/v1`; + const headers = { rune }; + const p = (cmd, body) => { + console.log(cmd, body); + return fetch(`${u}/${cmd}`, { method, headers, body }); + } + const get = (_, cmd) => (args) => p(kb(cmd), str(args)).then(j); + return new Proxy({}, { get }); +}; + +export default RPC(config.lightning); +export const lnb = RPC(config.lightningb); diff --git a/lib/notifications.ts b/lib/notifications.ts index d5b4f73f..0e1dc28b 100644 --- a/lib/notifications.ts +++ b/lib/notifications.ts @@ -85,7 +85,7 @@ export const nwcNotify = async (p) => { const pubkeys = await db.sMembers(`${user.id}:apps`); if (pubkeys.length) { let payment_hash = ""; - if (p.type === "lightning") ({ payment_hash } = await ln.decode(p.hash)); + if (p.type === "lightning") ({ payment_hash } = await ln.decode({ string: p.hash })); for (const pubkey of pubkeys) { const { notify } = await g(`app:${pubkey}`); if (!notify) continue; @@ -103,7 +103,7 @@ const handle = (method, params, ev, app, user) => ({ async pay_invoice() { const { invoice: pr } = params; - const { amount_msat, payee } = await ln.decode(pr); + const { amount_msat, payee } = await ln.decode({ bolt11: pr }); const { id } = await ln.getinfo(); const amount = Math.round(amount_msat / 1000); const { max_amount, max_fee, budget_renewal, pubkey, created } = app; @@ -203,7 +203,7 @@ const handle = (method, params, ev, app, user) => await db.lPush(`${pubkey}:payments`, pid); for (let i = 0; i < 10; i++) { - const { pays } = await ln.listpays(pr); + const { pays } = await ln.listpays({ bolt11: pr }); const p = pays.find((p) => p.status === "complete"); if (p) { const { preimage } = p; diff --git a/lib/payments.ts b/lib/payments.ts index 07004fa2..6bc7aab1 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -526,7 +526,7 @@ export const sendLightning = async ({ } let { type, invoice_amount_msat, amount_msat, invoice_node_id, payee } = - await ln.decode(pr); + await ln.decode({ string: pr }); if (type.includes("bolt12")) { amount_msat = invoice_amount_msat; payee = invoice_node_id; @@ -539,7 +539,7 @@ export const sendLightning = async ({ fee = Math.max(parseInt(fee || 0), minfee); if (fee < 0) fail("Fee cannot be negative"); - const { pays } = await ln.listpays(pr); + const { pays } = await ln.listpays({ bolt11: pr }); if (pays.find((p) => p.status === "complete")) fail("Invoice has already been paid"); @@ -829,7 +829,7 @@ export const check = async () => { for (const pr of payments) { const p = await getPayment(pr); if (!p || Date.now() - p.created < 10000) continue; - const { pays } = await ln.listpays(pr); + const { pays } = await ln.listpays({ bolt11: pr }); const failed = !pays.length || pays.every((p) => p.status === "failed"); const completed = pays.find((p) => p.status === "complete"); @@ -858,7 +858,7 @@ const finalize = async (r, p) => { nwcNotify(p); const maxfee = p.fee; - const { amount_msat } = await ln.decode(p.hash); + const { amount_msat } = await ln.decode({ string: p.hash }); p.fee = Math.round((r.amount_sent_msat - amount_msat) / 1000); p.ref = preimage; diff --git a/package.json b/package.json index 8881ff03..8338fde2 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "@noble/hashes": "^1.4.0", "bech32": "^2.0.0", "buffer-crc32": "^0.2.13", - "clightning-client": "github:asoltys/lightning-client-js#master", "date-fns": "^2.21.3", "fastify": "^4.28.1", "file-type": "^18.0.0", diff --git a/routes/nostr.ts b/routes/nostr.ts index 9c61bf16..a0204b91 100644 --- a/routes/nostr.ts +++ b/routes/nostr.ts @@ -53,7 +53,7 @@ export default { const { pubkey } = JSON.parse(description); let amount = 0; try { - const { amount_msat } = await ln.decode(bolt11); + const { amount_msat } = await ln.decode({ string: bolt11 }); if (amount_msat) amount = Math.round(amount_msat / 1000); } catch (e) {} @@ -183,7 +183,7 @@ export default { const { pubkey } = JSON.parse(description); let amount = 0; try { - const { amount_msat } = await ln.decode(bolt11); + const { amount_msat } = await ln.decode({ string: bolt11 }); if (amount_msat) amount = Math.round(amount_msat / 1000); } catch (e) {} diff --git a/routes/payments.ts b/routes/payments.ts index dd41b730..2c492c9d 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -198,7 +198,7 @@ export default { await s("nodes", nodes); } - const decoded = await ln.decode(payreq); + const decoded = await ln.decode({ string: payreq }); let amount_msat; let payee; @@ -209,7 +209,7 @@ export default { } else ({ amount_msat, payee } = decoded); const node = nodes.find((n) => n.nodeid === payee); - const alias = node ? node.alias : payee.substr(0, 12); + const alias = node?.alias || payee.substr(0, 12); const amount = Math.round(amount_msat / 1000); let ourfee = Math.round(amount * config.fee); @@ -522,7 +522,7 @@ export default { if (r.reason) fail(r.reason); const { pr } = r; - const { payee } = await ln.decode(pr); + const { payee } = await ln.decode({ string: pr }); const { id } = await ln.getinfo(); let p; @@ -610,11 +610,17 @@ export default { async decode(req, res) { const { bolt11 } = req.params; - res.send(await ln.decode(bolt11)); + res.send(await ln.decode({ string: bolt11 })); }, async fetchinvoice(req, res) { const { amount, offer } = req.body; - res.send(await ln.fetchinvoice(offer, amount ? amount * 1000 : null)); + + res.send( + await ln.fetchinvoice({ + offer, + amount_msat: amount ? amount * 1000 : null, + }), + ); }, }; |
