aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2025-02-07 22:23:44 -0800
committerAdam Soltys <[email protected]>2025-02-07 22:23:44 -0800
commite68c302e1082da29020d1efa932f986032ae5ce2 (patch)
tree25a46c19ec2d528be4f2d8e4587765b1aaebd90a /lib
parenteae37f75a77236d7929d8ce1f573c0f5bd428c79 (diff)
downloadcoinos-server-e68c302e1082da29020d1efa932f986032ae5ce2.tar.gz
coinos-server-e68c302e1082da29020d1efa932f986032ae5ce2.zip
nwc apps endpoints
Diffstat (limited to 'lib')
-rw-r--r--lib/nwc.ts28
1 files changed, 19 insertions, 9 deletions
diff --git a/lib/nwc.ts b/lib/nwc.ts
index 4055a9f4..9e1ad597 100644
--- a/lib/nwc.ts
+++ b/lib/nwc.ts
@@ -52,11 +52,11 @@ export default () => {
if (!methods.includes(method)) return;
- const uid = await g(pubkey);
- const user = await g(`user:${uid}`);
+ const app = await g(pubkey);
+ const user = await g(`user:${app.uid}`);
try {
- const result = await handle(method, params, user, ev);
+ const result = await handle(method, params, ev, app, user);
const payload = JSON.stringify({ result_type: method, ...result });
content = await nip04.encrypt(sk, pubkey, payload);
@@ -77,7 +77,7 @@ export default () => {
err(
"problem with nwc",
pubkey,
- user?.username,
+ app?.name,
JSON.stringify(params),
e.message,
);
@@ -89,7 +89,7 @@ export default () => {
});
};
-const handle = (method, params, user, ev) =>
+const handle = (method, params, ev, app, user) =>
({
async pay_invoice() {
const { invoice: pr } = params;
@@ -102,13 +102,17 @@ const handle = (method, params, user, ev) =>
const recipient = await g(`user:${invoice.uid}`);
if (recipient?.username !== "mint") {
- const { id: preimage } = await sendInternal({
+ const { id: pid } = await sendInternal({
amount,
invoice,
recipient,
sender: user,
});
+ const preimage = pid;
+ if (app.pubkey !== user.pubkey)
+ await db.lPush(`${pubkey}:payments`, pid);
+
if (invoice.memo?.includes("9734")) {
const { invoices } = await ln.listinvoices({ invstring: pr });
const inv = invoices[0];
@@ -124,12 +128,15 @@ const handle = (method, params, user, ev) =>
return result({ preimage });
}
}
- await sendLightning({
+
+ const { id: pid } = await sendLightning({
amount,
user,
pr,
});
+ if (app.pubkey !== user.pubkey) await db.lPush(`${pubkey}:payments`, pid);
+
for (let i = 0; i < 10; i++) {
const { pays } = await ln.listpays(pr);
const p = pays.find((p) => p.status === "complete");
@@ -144,7 +151,8 @@ const handle = (method, params, user, ev) =>
},
async pay_keysend() {
- const { amount, pubkey, tlv_records: extratlvs } = params;
+ const { amount: amount_msat, pubkey, tlv_records: extratlvs } = params;
+ const amount = Math.round(amount_msat / 1000);
try {
const { payment_hash } = await sendKeysend({
@@ -276,7 +284,9 @@ const handle = (method, params, user, ev) =>
async list_transactions() {
const { from, until, limit = 10, offset = 0, type } = params;
- const payments = await db.lRange(`${user.id}:payments`, 0, -1);
+ const { pubkey } = app;
+ const k = pubkey === user.pubkey ? user.id : pubkey;
+ const payments = await db.lRange(`${k}:payments`, 0, -1);
let transactions = [];
for (const pid of payments) {