aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/nwc.ts19
-rw-r--r--lib/payments.ts22
-rw-r--r--routes/invoices.ts2
-rw-r--r--routes/payments.ts5
4 files changed, 24 insertions, 24 deletions
diff --git a/lib/nwc.ts b/lib/nwc.ts
index 6ed926ff..ac9cf8a9 100644
--- a/lib/nwc.ts
+++ b/lib/nwc.ts
@@ -1,18 +1,17 @@
-import config from "$config";
import { db, g } from "$lib/db";
import { generate } from "$lib/invoices";
import ln from "$lib/ln";
import { err, l } from "$lib/logging";
-import { handleZap, serverPubkey } from "$lib/nostr";
+import { handleZap, serverPubkey, serverSecret } from "$lib/nostr";
import { sendInternal, sendKeysend, sendLightning } from "$lib/payments";
import { fail, getInvoice, sleep } from "$lib/utils";
import { Relay } from "nostr";
-import { finalizeEvent, nip04, nip19 } from "nostr-tools";
+import { finalizeEvent, nip04 } from "nostr-tools";
import type { UnsignedEvent } from "nostr-tools";
+import { hexToBytes } from "@noble/hashes/utils";
const result = (result) => ({ result });
const error = (error) => ({ error });
-const sk = nip19.decode(config.nostrKey).data as Uint8Array;
const methods = [
"pay_keysend",
@@ -40,10 +39,9 @@ export default () => {
["p", serverPubkey],
["notifications", "payment_received payment_sent"],
],
- pubkey: serverPubkey,
content: methods.join(" "),
},
- sk,
+ hexToBytes(serverSecret),
);
r.send(["EVENT", info]);
});
@@ -51,9 +49,11 @@ export default () => {
r.on("event", async (sub, ev) => {
try {
if (sub !== "nwc") return;
+ if (await db.sIsMember("handled", ev.id)) return;
+ db.sAdd("handled", ev.id);
let { content, pubkey } = ev;
const { params, method } = JSON.parse(
- await nip04.decrypt(sk, pubkey, content),
+ await nip04.decrypt(serverSecret, pubkey, content),
);
if (!methods.includes(method)) return;
@@ -65,7 +65,7 @@ export default () => {
const result = await handle(method, params, ev, app, user);
const payload = JSON.stringify({ result_type: method, ...result });
- content = await nip04.encrypt(sk, pubkey, payload);
+ content = await nip04.encrypt(serverSecret, pubkey, payload);
let response: UnsignedEvent = {
created_at: Math.floor(Date.now() / 1000),
@@ -78,9 +78,10 @@ export default () => {
content,
};
- response = await finalizeEvent(response, sk);
+ response = await finalizeEvent(response, hexToBytes(serverSecret));
r.send(["EVENT", response]);
} catch (e) {
+ console.log(e);
err(
"problem with nwc",
pubkey,
diff --git a/lib/payments.ts b/lib/payments.ts
index ce8d2dd7..7a862668 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -59,13 +59,12 @@ export const debit = async ({
user?.username?.toLowerCase().trim(),
);
- if (
- (await g("hardfreeze")) ||
- ((await g("freeze")) && type !== types.internal) ||
- (amount > (await g("limit")) && !whitelisted) ||
- amount > (await g(`${type}:limit`))
- ) {
- warn("Blocking", user.username, amount, hash, user.id);
+ let serverLimit = await g(`${type}:limit`);
+ let userLimit = await g("limit");
+ let frozen = (await g("hardfreeze")) || ((await g("freeze")) && type !== types.internal);
+
+ if (frozen || (amount > userLimit && !whitelisted) || amount > serverLimit) {
+ warn("Blocking", user.username, amount, hash, user.id, type, frozen, userLimit, serverLimit);
fail("Problem sending payment");
}
@@ -506,7 +505,6 @@ export const sendLightning = async ({
fail("Invalid amount");
}
- const total = amount;
const decoded = await ln.decode(pr);
const amount_msat = decoded.type.includes("bolt12")
? decoded.invoice_amount_msat
@@ -524,7 +522,7 @@ export const sendLightning = async ({
p = await debit({
hash: pr,
- amount: total,
+ amount: amount_msat ? Math.round(amount_msat / 1000) : amount,
fee,
memo,
user,
@@ -533,7 +531,7 @@ export const sendLightning = async ({
await db.sAdd("pending", pr);
- l("paying lightning invoice", pr.substr(-8), total, amount, fee);
+ l("paying lightning invoice", pr.substr(-8), amount, fee);
try {
const r = await ln.xpay({
@@ -887,8 +885,8 @@ const freezeCheck = async () => {
const lqthreshold = await g("liquid:threshold");
await s("lightning:limit", Math.max(lnbalance - lnthreshold, 0));
- await s("bitcoin:limit", Math.max(lqbalance - lqthreshold, 0));
- await s("liquid:limit", Math.max(bcbalance - bcthreshold, 0));
+ await s("bitcoin:limit", Math.max(bcbalance - bcthreshold, 0));
+ await s("liquid:limit", Math.max(lqbalance - lqthreshold, 0));
await s("fund:limit", Math.max(lnbalance - lnthreshold, 0));
await s("ecash:limit", Math.max(lnbalance - lnthreshold, 0));
diff --git a/routes/invoices.ts b/routes/invoices.ts
index 79f7b8f1..8b71208d 100644
--- a/routes/invoices.ts
+++ b/routes/invoices.ts
@@ -31,7 +31,7 @@ export default {
try {
res.send(await generate({ invoice, user }));
} catch (e) {
- err("problem generating invoice", e.message);
+ err("problem generating invoice", req.user?.username, body.user?.username, e.message);
bail(res, e.message);
}
},
diff --git a/routes/payments.ts b/routes/payments.ts
index 1e035b3c..9282723a 100644
--- a/routes/payments.ts
+++ b/routes/payments.ts
@@ -238,6 +238,7 @@ export default {
const {
params: { name },
} = req;
+ fail("disabled");
const balance = await g(`fund:${name}`);
res.send({
tag: "withdrawRequest",
@@ -255,6 +256,7 @@ export default {
user,
} = req;
try {
+ fail("disabled");
const { id: uid } = user;
amount = parseInt(amount);
if (amount < 0) fail("Invalid amount");
@@ -290,8 +292,7 @@ export default {
res.send({ payment });
} catch (e) {
- console.log(e);
- warn("problem withdrawing from fund", e.message);
+ warn("problem withdrawing from fund", user.username, e.message);
bail(res, e.message);
}
},