aboutsummaryrefslogtreecommitdiffstats
path: root/lib/nwc.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-10-31 09:29:04 -0700
committerAdam Soltys <[email protected]>2024-10-31 09:34:42 -0700
commitaa4cd39c943b67169def224e224bd07b815b70ec (patch)
tree2af857ac971705f4d20eb017864f0365d0b57ea6 /lib/nwc.ts
parentbc9c04f6519f32d3b444d4df0b276eba960480a9 (diff)
downloadcoinos-server-aa4cd39c943b67169def224e224bd07b815b70ec.tar.gz
coinos-server-aa4cd39c943b67169def224e224bd07b815b70ec.zip
biome lint
Diffstat (limited to 'lib/nwc.ts')
-rw-r--r--lib/nwc.ts82
1 files changed, 40 insertions, 42 deletions
diff --git a/lib/nwc.ts b/lib/nwc.ts
index 2b2649a6..76474c26 100644
--- a/lib/nwc.ts
+++ b/lib/nwc.ts
@@ -10,13 +10,13 @@ import type { UnsignedEvent } from "nostr-tools";
import { generate } from "$lib/invoices";
import ln from "$lib/ln";
-let result = (result) => ({ result });
-let error = (error) => ({ error });
+const result = (result) => ({ result });
+const error = (error) => ({ error });
-let methods = ["pay_invoice", "get_balance", "get_info", "make_invoice"];
+const methods = ["pay_invoice", "get_balance", "get_info", "make_invoice"];
export default () => {
- let r = new Relay("ws://strfry:7777");
+ const r = new Relay("ws://strfry:7777");
r.on("open", (_) => {
r.subscribe("nwc", { kinds: [23194], "#p": [serverPubkey] });
@@ -26,18 +26,18 @@ export default () => {
try {
if (sub !== "nwc") return;
let { content, pubkey } = ev;
- let sk = nip19.decode(config.nostrKey).data as Uint8Array;
- let { params, method } = JSON.parse(
+ const sk = nip19.decode(config.nostrKey).data as Uint8Array;
+ const { params, method } = JSON.parse(
await nip04.decrypt(sk, pubkey, content),
);
if (!methods.includes(method)) return;
- let uid = await g(pubkey);
- let user = await g(`user:${uid}`);
+ const uid = await g(pubkey);
+ const user = await g(`user:${uid}`);
- let result = await handle(method, params, user);
- let payload = JSON.stringify({ result_type: method, ...result });
+ const result = await handle(method, params, user);
+ const payload = JSON.stringify({ result_type: method, ...result });
content = await nip04.encrypt(sk, pubkey, payload);
let response: UnsignedEvent = {
@@ -59,19 +59,19 @@ export default () => {
});
};
-let handle = (method, params, user) =>
+const handle = (method, params, user) =>
({
async pay_invoice() {
- let { invoice: pr } = params;
- let { amount_msat, payee } = await ln.decode(pr);
- let { id } = await ln.getinfo();
- let amount = Math.round(amount_msat / 1000);
+ const { invoice: pr } = params;
+ const { amount_msat, payee } = await ln.decode(pr);
+ const { id } = await ln.getinfo();
+ const amount = Math.round(amount_msat / 1000);
if (payee === id) {
- let invoice = await getInvoice(pr);
- let recipient = await g(`user:${invoice.uid}`);
+ const invoice = await getInvoice(pr);
+ const recipient = await g(`user:${invoice.uid}`);
- let { id: preimage } = await sendInternal({
+ const { id: preimage } = await sendInternal({
amount,
invoice,
recipient,
@@ -79,8 +79,8 @@ let handle = (method, params, user) =>
});
if (invoice.memo.includes("9734")) {
- let { invoices } = await ln.listinvoices({ invstring: pr });
- let inv = invoices[0];
+ const { invoices } = await ln.listinvoices({ invstring: pr });
+ const inv = invoices[0];
inv.payment_preimage = preimage;
inv.paid_at = Math.floor(Date.now() / 1000);
try {
@@ -90,32 +90,30 @@ let handle = (method, params, user) =>
}
}
-
return result({ preimage });
- } else {
- await sendLightning({
- amount,
- user,
- pr,
- maxfee: 50,
- });
+ }
+ 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;
- return result({ preimage });
- }
- await sleep(2000);
+ 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 });
}
-
- return error({ code: "INTERNAL", message: "Payment timed out" });
+ await sleep(2000);
}
+
+ return error({ code: "INTERNAL", message: "Payment timed out" });
},
async get_info() {
- let { alias, blockheight, color } = await ln.getinfo();
+ const { alias, blockheight, color } = await ln.getinfo();
return result({
alias,
color,
@@ -133,16 +131,16 @@ let handle = (method, params, user) =>
},
async make_invoice() {
- let { amount, description, description_hash, expiry } = params;
+ const { amount, description, description_hash, expiry } = params;
- let invoice = {
+ const invoice = {
amount: Math.round(amount / 1000),
type: "lightning",
memo: description,
expiry,
};
- let { hash, created: created_at } = await generate({ invoice, user });
+ const { hash, created: created_at } = await generate({ invoice, user });
return result({
type: "incoming",