aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2025-01-17 11:28:30 -0800
committerAdam Soltys <[email protected]>2025-01-17 11:28:30 -0800
commit06308bb12edff8e51b189b57d9f5a147959d743a (patch)
tree2dad64b95eb0976f581783a2a83a5c5d0be69134
parent2e28b65fc3d790d9fece0f3aa22f35b2012d9739 (diff)
downloadcoinos-server-06308bb12edff8e51b189b57d9f5a147959d743a.tar.gz
coinos-server-06308bb12edff8e51b189b57d9f5a147959d743a.zip
send zaps for internal payments
-rw-r--r--lib/invoices.ts5
-rw-r--r--lib/nostr.ts2
-rw-r--r--lib/payments.ts13
-rw-r--r--routes/nostr.ts2
-rw-r--r--routes/payments.ts14
5 files changed, 25 insertions, 11 deletions
diff --git a/lib/invoices.ts b/lib/invoices.ts
index d121353c..4710beb2 100644
--- a/lib/invoices.ts
+++ b/lib/invoices.ts
@@ -32,7 +32,6 @@ export const generate = async ({ invoice, user }) => {
secret,
} = invoice;
-
amount = parseInt(amount || 0);
tip = parseInt(tip) || null;
@@ -102,11 +101,11 @@ export const generate = async ({ invoice, user }) => {
await s(`invoice:${r.offer_id}`, id);
} else if (type === types.bitcoin) {
- address_type ||= "bech32"
+ address_type ||= "bech32";
hash = await bc.getNewAddress({ address_type });
text = bip21(hash, invoice);
} else if (type === types.liquid) {
- address_type ||= "blech32"
+ address_type ||= "blech32";
hash = await lq.getNewAddress({ address_type });
text = bip21(hash, invoice);
} else if (type === types.internal) {
diff --git a/lib/nostr.ts b/lib/nostr.ts
index f5d85ea0..4fb79dfd 100644
--- a/lib/nostr.ts
+++ b/lib/nostr.ts
@@ -1,6 +1,6 @@
import config from "$config";
import { db, g, s } from "$lib/db";
-import { l } from "$lib/logging";
+import { l, warn } from "$lib/logging";
import { count, scan, sync } from "$lib/strfry";
import { fail } from "$lib/utils";
import { finalizeEvent, getPublicKey, nip19 } from "nostr-tools";
diff --git a/lib/payments.ts b/lib/payments.ts
index d88dc6aa..5f818ae4 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -4,6 +4,7 @@ import { db, g, s } from "$lib/db";
import { generate } from "$lib/invoices";
import ln from "$lib/ln";
import { err, l, warn } from "$lib/logging";
+import { handleZap } from "$lib/nostr";
import { notify } from "$lib/notifications";
import {
SATS,
@@ -547,6 +548,7 @@ export const sendLightning = async ({
export const sendInternal = async ({
amount,
invoice = undefined,
+ memo = undefined,
recipient,
sender,
}) => {
@@ -557,10 +559,17 @@ export const sendInternal = async ({
});
const { hash } = invoice;
- let memo;
-
const p = await debit({ hash, amount, memo, user: sender });
await credit({ hash, amount, memo, ref: sender.id });
+
+ if (invoice.memo?.includes("9734")) {
+ const { invoices } = await ln.listinvoices({ invstring: hash });
+ const inv = invoices[0];
+ inv.payment_preimage = p.id;
+ inv.paid_at = Math.floor(Date.now() / 1000);
+ handleZap(inv).catch(console.log);
+ }
+
return p;
};
diff --git a/routes/nostr.ts b/routes/nostr.ts
index 0ebc879b..9660227e 100644
--- a/routes/nostr.ts
+++ b/routes/nostr.ts
@@ -255,7 +255,7 @@ export default {
const callback = await getZapEndpoint({ content } as Event);
const url = new URL(callback);
url.searchParams.set("amount", (amount * 1000).toString());
- url.searchParams.set("nostr", encodeURIComponent(JSON.stringify(event)));
+ url.searchParams.set("nostr", JSON.stringify(event));
const json = await got(url.toString()).json();
res.send(json);
diff --git a/routes/payments.ts b/routes/payments.ts
index ed533c76..75d667f2 100644
--- a/routes/payments.ts
+++ b/routes/payments.ts
@@ -55,9 +55,10 @@ export default {
let p;
+ const invoice = await getInvoice(payreq || hash);
+ const recipient = invoice ? await getUser(invoice.uid) : undefined;
if (payreq) {
- const invoice = await getInvoice(payreq);
- if (invoice && invoice.uid !== (await g("user:mint"))) {
+ if (invoice && recipient.username !== "mint") {
if (invoice.aid === user.id) fail("Cannot send to self");
hash = payreq;
if (!amount) ({ amount } = invoice);
@@ -68,8 +69,13 @@ export default {
if (!p) {
if (hash) {
- p = await debit({ hash, amount, memo, user });
- await credit({ hash, amount, memo, ref: user.id });
+ p = await sendInternal({
+ invoice,
+ amount,
+ memo,
+ recipient,
+ sender: user,
+ });
} else if (fund) {
p = await debit({ hash, amount, memo: fund, user, type: types.fund });
await db.incrBy(`fund:${fund}`, amount);