aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2025-03-05 17:24:55 +0000
committerAdam Soltys <[email protected]>2025-03-05 17:24:55 +0000
commit1411597923c5de50e804e564387ce51c5991f102 (patch)
treeab1ddc924acf839bbf59c83c9605dda63bb58493
parent1b83f8398d34994b62c88fada6dba6a183227cbe (diff)
downloadcoinos-server-1411597923c5de50e804e564387ce51c5991f102.tar.gz
coinos-server-1411597923c5de50e804e564387ce51c5991f102.zip
record payment_hash
-rw-r--r--index.ts3
-rw-r--r--lib/lightning.ts2
-rw-r--r--lib/nwc.ts20
-rw-r--r--lib/payments.ts2
4 files changed, 22 insertions, 5 deletions
diff --git a/index.ts b/index.ts
index b82ae4dc..6820dfaf 100644
--- a/index.ts
+++ b/index.ts
@@ -48,6 +48,9 @@ app.get("/invoices", auth, invoices.list);
app.post("/invoice", optional, invoices.create);
app.post("/sign", auth, invoices.sign);
+app.get("/assetlinks.json", (_,res) => {
+ res.sendFile('assetlinks.json');
+});
app.get("/nostr.json", nostr.identities);
app.get("/profile/:profile", nostr.profile);
app.get("/:pubkey/count", nostr.count);
diff --git a/lib/lightning.ts b/lib/lightning.ts
index 4d41d9eb..966a7954 100644
--- a/lib/lightning.ts
+++ b/lib/lightning.ts
@@ -14,6 +14,7 @@ export async function listenForLightning() {
bolt12,
description,
pay_index,
+ payment_hash,
amount_received_msat,
payment_preimage: preimage,
} = inv;
@@ -51,6 +52,7 @@ export async function listenForLightning() {
memo: invoice.memo,
ref: preimage,
type: bolt12 ? PaymentType.bolt12 : PaymentType.lightning,
+ payment_hash,
});
} catch (e) {
console.log(e);
diff --git a/lib/nwc.ts b/lib/nwc.ts
index 7039dc62..eed38cf3 100644
--- a/lib/nwc.ts
+++ b/lib/nwc.ts
@@ -56,7 +56,7 @@ export default () => {
await nip04.decrypt(serverSecret, pubkey, content),
);
- // db.sAdd("activeNwc", pubkey);
+ // console.log("nwc", method, params, pubkey);
if (!methods.includes(method)) return;
@@ -118,7 +118,7 @@ const handle = (method, params, ev, app, user) =>
const pids = await db.lRange(`${pubkey}:payments`, 0, -1);
let payments = await Promise.all(pids.map((pid) => g(`payment:${pid}`)));
payments = payments.filter(
- (p) => p.created > Date.now() - periods[budget_renewal],
+ (p) => p?.created > Date.now() - periods[budget_renewal],
);
const spent = payments.reduce(
@@ -131,8 +131,15 @@ const handle = (method, params, ev, app, user) =>
0,
);
- const whitelisted = await db.sIsMember("activeNwc", pubkey);
- if ((amount > 1000 && !(whitelisted || created)) || (max_amount > 0 && (spent + amount > max_amount))) {
+ if (!created) {
+ warn("old nwc token", pubkey, user?.username, spent, amount, max_amount);
+ return error({
+ code: "UNAUTHORIZED",
+ message: `This NWC connection is no longer valid please create a new one at https://coinos.io/settings/nostr`,
+ });
+ }
+
+ if ((max_amount > 0 && (spent + amount > max_amount))) {
warn("budget exceeded", pubkey, user?.username, spent, amount, max_amount);
return error({
code: "QUOTA_EXCEEDED",
@@ -294,6 +301,8 @@ const handle = (method, params, ev, app, user) =>
created_at: expires_at - week,
expires_at,
settled_at: settled_at || Math.round(settled / 1000),
+ state: settled ? "settled" : "pending",
+ status: settled ? "paid" : "pending",
});
}
@@ -355,10 +364,11 @@ const handle = (method, params, ev, app, user) =>
preimage: p.ref,
payment_hash,
amount: (Math.abs(p.amount) + p.tip) * 1000,
- fees_paid: p.fee * 1000,
+ fees_paid: (p?.fee || 0) * 1000,
created_at,
expires_at: created_at + week,
settled_at: p.amount > 0 ? created_at : undefined,
+ state: "settled",
metadata: {},
});
}
diff --git a/lib/payments.ts b/lib/payments.ts
index ad39f81c..342b9641 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -173,6 +173,7 @@ export const credit = async ({
ref = "",
type = PaymentType.internal,
aid = undefined,
+ payment_hash = undefined,
}) => {
amount = parseInt(amount) || 0;
@@ -227,6 +228,7 @@ export const credit = async ({
rate,
currency,
memo,
+ payment_hash,
ref,
tip,
type,