aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-09-16 23:16:58 -0700
committerAdam Soltys <[email protected]>2024-09-16 23:16:58 -0700
commitf1cd8904fb7231d43f16b7bc27f45b8b31a03fde (patch)
tree5243a0c70dc65e1d36957b41feaa41e6f4352f27 /lib
parent50cd51473f8cecc8b6893d9dff3142ca13b249e5 (diff)
downloadcoinos-server-f1cd8904fb7231d43f16b7bc27f45b8b31a03fde.tar.gz
coinos-server-f1cd8904fb7231d43f16b7bc27f45b8b31a03fde.zip
internal payments
Diffstat (limited to 'lib')
-rw-r--r--lib/nostr.ts56
-rw-r--r--lib/payments.ts19
2 files changed, 53 insertions, 22 deletions
diff --git a/lib/nostr.ts b/lib/nostr.ts
index c57409aa..35a864d0 100644
--- a/lib/nostr.ts
+++ b/lib/nostr.ts
@@ -6,7 +6,7 @@ import { fail, sleep, wait } from "$lib/utils";
import { nip04, nip19, finalizeEvent } from "nostr-tools";
import type { UnsignedEvent } from "nostr-tools";
import { emit } from "$lib/sockets";
-import { sendLightning } from "$lib/payments";
+import { sendInternal, sendLightning } from "$lib/payments";
import ln from "$lib/ln";
import { hex } from "@scure/base";
@@ -289,35 +289,49 @@ r.on("event", async (sub, ev) => {
await nip04.decrypt(sk, pubkey, content),
);
- let { invoice: pr } = params;
- let { amount_msat } = await ln.decode(pr);
- let amount = Math.round(amount_msat / 1000);
let uid = await g(pubkey);
let user = await g(`user:${uid}`);
let ret = { result_type: method, result: undefined, error: undefined };
if (method === "pay_invoice") {
- await sendLightning({
- amount,
- user,
- pr,
- maxfee: 50,
- });
+ let { invoice: pr } = params;
+ let { amount_msat, payee } = await ln.decode(pr);
+ let { id } = await ln.getInfo();
+ let amount = Math.round(amount_msat / 1000);
- for (let i = 0; i < 10; i++) {
- let { pays } = await ln.listpays(pr);
- let p = pays.find((p) => p.status === "complete");
- if (p) {
- let { preimage } = p;
- ret.result = { preimage };
- break;
+ if (payee === id) {
+ let inv = await g(pr);
+ let recipient = await g(`user:${inv.uid}`);
+ await sendInternal({
+ amount,
+ recipient,
+ sender: user,
+ });
+
+ ret.result = { preimage: recipient.id};
+ } else {
+ await sendLightning({
+ amount,
+ user,
+ pr,
+ maxfee: 50,
+ });
+
+ for (let i = 0; i < 10; i++) {
+ let { pays } = await ln.listpays(pr);
+ let p = pays.find((p) => p.status === "complete");
+ if (p) {
+ let { preimage } = p;
+ ret.result = { preimage };
+ break;
+ }
+ await sleep(2000);
}
- await sleep(2000);
- }
- if (!ret.result)
- ret.error = { code: "INTERNAL", message: "Payment timed out" };
+ if (!ret.result)
+ ret.error = { code: "INTERNAL", message: "Payment timed out" };
+ }
} else if (method === "get_info") {
let { alias, blockheight, color } = await ln.getInfo();
ret.result = {
diff --git a/lib/payments.ts b/lib/payments.ts
index 075cfab8..8f197044 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -1,5 +1,6 @@
import locales from "$lib/locales/index";
import config from "$config";
+import { generate } from "$lib/invoices";
import { emit } from "$lib/sockets";
import { v4 } from "uuid";
import { notify } from "$lib/notifications";
@@ -11,6 +12,7 @@ import {
fail,
fiat,
getInvoice,
+ getUser,
sleep,
SATS,
sats,
@@ -603,6 +605,21 @@ export let sendLightning = async ({
}
};
+export let sendInternal = async ({ amount, recipient, sender }) => {
+ let inv = await generate({
+ invoice: { amount, type: "lightning" },
+ user: recipient,
+ sender,
+ });
+
+ let { hash } = inv;
+ let memo;
+
+ let p = await debit({ hash, amount, memo, user: sender });
+ await credit({ hash, amount, memo, ref: recipient.id });
+ return p;
+};
+
let getAddressType = async (a) => {
try {
await bc.getAddressInfo(a);
@@ -627,7 +644,7 @@ export let build = async ({
}) => {
let type = await getAddressType(address);
if (!account) account = user.id;
- let node =
+ let node =
account === user.id
? rpc(config[type])
: rpc({ ...config[type], wallet: account });