diff options
| -rw-r--r-- | lib/nostr.ts | 56 | ||||
| -rw-r--r-- | lib/payments.ts | 19 | ||||
| -rw-r--r-- | routes/payments.ts | 19 |
3 files changed, 57 insertions, 37 deletions
diff --git a/lib/nostr.ts b/lib/nostr.ts index c57409aa..35a864d0 100644 --- a/lib/nostr.ts +++ b/lib/nostr.ts @@ -6,7 +6,7 @@ import { fail, sleep, wait } from "$lib/utils"; import { nip04, nip19, finalizeEvent } from "nostr-tools"; import type { UnsignedEvent } from "nostr-tools"; import { emit } from "$lib/sockets"; -import { sendLightning } from "$lib/payments"; +import { sendInternal, sendLightning } from "$lib/payments"; import ln from "$lib/ln"; import { hex } from "@scure/base"; @@ -289,35 +289,49 @@ r.on("event", async (sub, ev) => { await nip04.decrypt(sk, pubkey, content), ); - let { invoice: pr } = params; - let { amount_msat } = await ln.decode(pr); - let amount = Math.round(amount_msat / 1000); let uid = await g(pubkey); let user = await g(`user:${uid}`); let ret = { result_type: method, result: undefined, error: undefined }; if (method === "pay_invoice") { - await sendLightning({ - amount, - user, - pr, - maxfee: 50, - }); + let { invoice: pr } = params; + let { amount_msat, payee } = await ln.decode(pr); + let { id } = await ln.getInfo(); + let amount = Math.round(amount_msat / 1000); - for (let i = 0; i < 10; i++) { - let { pays } = await ln.listpays(pr); - let p = pays.find((p) => p.status === "complete"); - if (p) { - let { preimage } = p; - ret.result = { preimage }; - break; + if (payee === id) { + let inv = await g(pr); + let recipient = await g(`user:${inv.uid}`); + await sendInternal({ + amount, + recipient, + sender: user, + }); + + ret.result = { preimage: recipient.id}; + } else { + await sendLightning({ + amount, + user, + pr, + maxfee: 50, + }); + + for (let i = 0; i < 10; i++) { + let { pays } = await ln.listpays(pr); + let p = pays.find((p) => p.status === "complete"); + if (p) { + let { preimage } = p; + ret.result = { preimage }; + break; + } + await sleep(2000); } - await sleep(2000); - } - if (!ret.result) - ret.error = { code: "INTERNAL", message: "Payment timed out" }; + if (!ret.result) + ret.error = { code: "INTERNAL", message: "Payment timed out" }; + } } else if (method === "get_info") { let { alias, blockheight, color } = await ln.getInfo(); ret.result = { diff --git a/lib/payments.ts b/lib/payments.ts index 075cfab8..8f197044 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -1,5 +1,6 @@ import locales from "$lib/locales/index"; import config from "$config"; +import { generate } from "$lib/invoices"; import { emit } from "$lib/sockets"; import { v4 } from "uuid"; import { notify } from "$lib/notifications"; @@ -11,6 +12,7 @@ import { fail, fiat, getInvoice, + getUser, sleep, SATS, sats, @@ -603,6 +605,21 @@ export let sendLightning = async ({ } }; +export let sendInternal = async ({ amount, recipient, sender }) => { + let inv = await generate({ + invoice: { amount, type: "lightning" }, + user: recipient, + sender, + }); + + let { hash } = inv; + let memo; + + let p = await debit({ hash, amount, memo, user: sender }); + await credit({ hash, amount, memo, ref: recipient.id }); + return p; +}; + let getAddressType = async (a) => { try { await bc.getAddressInfo(a); @@ -627,7 +644,7 @@ export let build = async ({ }) => { let type = await getAddressType(address); if (!account) account = user.id; - let node = + let node = account === user.id ? rpc(config[type]) : rpc({ ...config[type], wallet: account }); diff --git a/routes/payments.ts b/routes/payments.ts index dbf5a8fd..2046cfb4 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -1,10 +1,9 @@ import config from "$config"; import { emit } from "$lib/sockets"; -import { generate } from "$lib/invoices"; import { v4 } from "uuid"; import { db, g, s, t } from "$lib/db"; import { l, err, warn } from "$lib/logging"; -import { bail, fail, getInvoice, sats, SATS, getUser } from "$lib/utils"; +import { bail, fail, getInvoice, getUser, sats, SATS } from "$lib/utils"; import { requirePin } from "$lib/auth"; import { completePayment, @@ -13,6 +12,7 @@ import { credit, types, build, + sendInternal, sendLightning, sendOnchain, } from "$lib/payments"; @@ -505,19 +505,8 @@ export default { body: { username, amount }, user: sender, } = req; - let recipient = await getUser(username); - let inv = await generate({ - invoice: { amount, type: "lightning" }, - user: recipient, - sender, - }); - - let { hash } = inv; - let memo; - let p = await debit({ hash, amount, memo, user: sender }); - await credit({ hash, amount, memo, ref: recipient.id }); - - res.send(p); + let recipient = await getUser(username); + res.send(await sendInternal({ amount, sender, recipient })); }, }; |
