diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/nwc.ts | 19 | ||||
| -rw-r--r-- | lib/payments.ts | 22 |
2 files changed, 20 insertions, 21 deletions
@@ -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)); |
