diff options
| author | Adam Soltys <[email protected]> | 2024-09-16 15:59:34 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-09-16 15:59:56 -0700 |
| commit | e4444d467ddd38ff204b17e862649d6a047bfea0 (patch) | |
| tree | 585fdbe90f48bd513c1b58111233779964b1e92a | |
| parent | c89d85b4bdebe1b3a096a880b624654e11431ad9 (diff) | |
| download | coinos-server-e4444d467ddd38ff204b17e862649d6a047bfea0.tar.gz coinos-server-e4444d467ddd38ff204b17e862649d6a047bfea0.zip | |
nwc
| -rwxr-xr-x | bun.lockb | bin | 207855 -> 208922 bytes | |||
| -rw-r--r-- | lib/nostr.ts | 53 | ||||
| -rw-r--r-- | lib/utils.ts | 4 | ||||
| -rw-r--r-- | package.json | 2 |
4 files changed, 48 insertions, 11 deletions
| Binary files differ diff --git a/lib/nostr.ts b/lib/nostr.ts index 172e3ce0..3a0dafb5 100644 --- a/lib/nostr.ts +++ b/lib/nostr.ts @@ -3,8 +3,10 @@ import { l } from "$lib/logging"; import { Relay, RelayPool, calculateId, signId, getPublicKey } from "nostr"; import { g, s, db } from "$lib/db"; import { fail, wait } from "$lib/utils"; -import { nip19 } from "nostr-tools"; +import { nip04, nip19 } from "nostr-tools"; import { emit } from "$lib/sockets"; +import { sendLightning } from "$lib/payments"; +import ln from "$lib/ln"; export const COINOS_PUBKEY = getPublicKey(nip19.decode(config.nostrKey).data); export let coinos; @@ -269,11 +271,44 @@ export async function handleZap(invoice) { await Promise.allSettled(relays.map((r) => send(ev, r))); } -// let damus = new Relay("ws://strfry:7777"); -// damus.on("open", (relay) => { -// damus.subscribe("nwc", { limit: 1, kinds: [23194] }); -// }); -// -// damus.on("event", async (sub, ev) => { -// console.log("EV", ev); -// }); +let r = new Relay("ws://strfry:7777"); +r.on("open", (_) => { + r.subscribe("nwc", { kinds: [23194], "#p": [COINOS_PUBKEY] }); +}); + +r.on("event", async (sub, ev) => { + if (sub !== "nwc") return; + let { content, pubkey } = ev; + let sk = nip19.decode(config.nostrKey).data as Uint8Array; + let { params, method } = JSON.parse(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}`); + + if (method === "pay_invoice") { + let { ref: preimage } = await sendLightning({ + amount, + user, + pr, + maxfee: 5000, + }); + + let res = { result_type: method, result: { preimage } }; + let content = nip04.encrypt(sk, pubkey, JSON.stringify(res)); + + let rev = { + kind: 24195, + pubkey, + tags: [ + ["p", COINOS_PUBKEY], + ["e", ev.id], + ], + content, + }; + + r.send(["EVENT", rev]); + } +}); diff --git a/lib/utils.ts b/lib/utils.ts index b0875873..2eab7f67 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -2,6 +2,7 @@ import { randomBytes, bytesToHex } from "@noble/hashes/utils"; import migrate from "$lib/migrate"; import { g, s } from "$lib/db"; import config from "$config"; +import { getPublicKey } from "nostr"; export let fail = (msg) => { throw new Error(msg); @@ -71,7 +72,8 @@ export let getUser = async (username) => { let user = await migrate(username); if (user && !user.nwc) { user.nwc = bytesToHex(randomBytes(32)); - s(`user:${user.id}`, user); + await s(getPublicKey(user.nwc), user.id); + await s(`user:${user.id}`, user); } return user; diff --git a/package.json b/package.json index 0e2bf2d3..e61ed771 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "lnurl": "^0.24.2", "mqtt": "^5.0.2", "nostr": "^0.2.7", - "nostr-tools": "^1.17.0", + "nostr-tools": "^2.7.2", "otplib": "^12.0.1", "passport-jwt": "^4.0.0", "passport-local": "1.0.0", |
