aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/nwc.ts27
-rw-r--r--lib/payments.ts41
2 files changed, 62 insertions, 6 deletions
diff --git a/lib/nwc.ts b/lib/nwc.ts
index 1c0c0eb6..3f6f24e7 100644
--- a/lib/nwc.ts
+++ b/lib/nwc.ts
@@ -4,7 +4,7 @@ import { generate } from "$lib/invoices";
import ln from "$lib/ln";
import { err } from "$lib/logging";
import { handleZap, serverPubkey } from "$lib/nostr";
-import { sendInternal, sendLightning } from "$lib/payments";
+import { sendInternal, sendKeysend, sendLightning } from "$lib/payments";
import { getInvoice, sleep } from "$lib/utils";
import { Relay } from "nostr";
import { finalizeEvent, nip04, nip19 } from "nostr-tools";
@@ -122,7 +122,7 @@ const handle = (method, params, user) =>
}
await sendLightning({
amount,
- maxfee: Math.max(5, Math.round(amount * 0.005)),
+ fee: Math.max(5, Math.round(amount * 0.005)),
user,
pr,
});
@@ -140,6 +140,29 @@ const handle = (method, params, user) =>
return error({ code: "INTERNAL", message: "Payment timed out" });
},
+ async pay_keysend() {
+ const { amount, pubkey } = params;
+
+ await sendKeysend({
+ amount,
+ pubkey,
+ fee: Math.max(5, Math.round(amount * 0.005)),
+ user,
+ });
+
+ for (let i = 0; i < 10; i++) {
+ const { pays } = await ln.listpays(pr);
+ const p = pays.find((p) => p.status === "complete");
+ if (p) {
+ const { preimage } = p;
+ return result({ preimage });
+ }
+ await sleep(2000);
+ }
+
+ return error({ code: "INTERNAL", message: "Payment timed out" });
+ },
+
async get_info() {
const { alias, blockheight, color } = await ln.getinfo();
return result({
diff --git a/lib/payments.ts b/lib/payments.ts
index fb394550..2b344817 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -281,18 +281,18 @@ const pay = async ({ aid = undefined, amount, to, user }) => {
).toString();
}
- const maxfee = Math.max(5, Math.round(amount * 0.005));
+ const fee = Math.max(5, Math.round(amount * 0.005));
if (lnurl) {
- amount -= maxfee;
+ amount -= fee;
const { callback } = (await got(lnurl).json()) as any;
({ pr } = (await got(`${callback}?amount=${amount * 1000}`).json()) as any);
} else if (to.startsWith("ln")) {
- amount -= maxfee;
+ amount -= fee;
pr = to;
}
return pr
- ? await sendLightning({ user, pr, amount, maxfee })
+ ? await sendLightning({ user, pr, amount, fee })
: await sendOnchain({ aid, amount, address: to, user, subtract: true });
};
@@ -415,6 +415,39 @@ export const sendOnchain = async (params) => {
}
};
+export const sendKeysend = async ({
+ amount,
+ pubkey,
+ fee,
+ memo = undefined,
+ user,
+}) => {
+ let p = await debit({
+ hash: pubkey,
+ amount,
+ fee,
+ memo,
+ user,
+ type: types.lightning,
+ });
+
+ fee = Math.max(parseInt(fee || 0), 5);
+ if (fee < 0) fail("Fee cannot be negative");
+
+ const r = await ln.keysend({
+ pubkey,
+ amount_msat: amount * 1000,
+ maxfee: fee * 1000,
+ retry_for: 10,
+ });
+
+ try {
+ if (r.status === "complete") p = await finalize(r, p);
+ } catch (e) {
+ warn("failed to process payment", p.id);
+ }
+};
+
export const sendLightning = async ({
user,
pr,