diff options
| author | Adam Soltys <[email protected]> | 2024-10-04 09:29:21 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-10-10 12:17:05 -0700 |
| commit | 372c635714f6e7de8a0da135980697b49f207f2c (patch) | |
| tree | 5c5af794a4ca18b0919cc363f65fd398bc051b8b | |
| parent | 5127adbb54b7db6eccf27cc4e498d9dfcdb7a7a4 (diff) | |
| download | coinos-server-372c635714f6e7de8a0da135980697b49f207f2c.tar.gz coinos-server-372c635714f6e7de8a0da135980697b49f207f2c.zip | |
wip socket communication
| -rw-r--r-- | index.ts | 5 | ||||
| -rw-r--r-- | lib/nostr.ts | 26 | ||||
| -rw-r--r-- | lib/strfry.ts | 30 | ||||
| -rw-r--r-- | lib/utils.ts | 2 | ||||
| -rwxr-xr-x | listener.sh | 11 | ||||
| -rw-r--r-- | routes/nostr.ts | 37 | ||||
| -rw-r--r-- | routes/users.ts | 18 |
7 files changed, 88 insertions, 41 deletions
@@ -22,6 +22,11 @@ import rates from "$routes/rates"; import shopify from "$routes/shopify"; import users from "$routes/users"; +import sf from "$lib/strfry"; +console.log(await sf( + `scan '{"authors":["c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5"]}'`, +)); + try { fillPool(); getLocations(); diff --git a/lib/nostr.ts b/lib/nostr.ts index 90c33694..44febae3 100644 --- a/lib/nostr.ts +++ b/lib/nostr.ts @@ -6,7 +6,7 @@ import { fail, wait } from "$lib/utils"; import { nip19, finalizeEvent } from "nostr-tools"; import { emit } from "$lib/sockets"; import { hex } from "@scure/base"; -import { Relay as ntr } from 'nostr-tools/relay' +import { Relay as ntr } from "nostr-tools/relay"; export const serverPubkey = hex.encode( getPublicKey(nip19.decode(config.nostrKey).data), @@ -24,7 +24,7 @@ export let fillPool = () => { }); pool.on("event", async (_, sub, ev) => { - if (!coinos && coinos.ws.readyState !== WebSocket.OPEN) return; + if (!coinos || coinos.ws.readyState !== WebSocket.OPEN) return; if (timeouts[sub]) timeouts[sub].extend(ev); try { let content; @@ -73,7 +73,9 @@ export let fillPool = () => { updated: ev.created_at, }; - await s(`user:${pubkey}`, JSON.stringify(user)); + console.log("SAVING", pubkey); + + await s(`user:${pubkey}`, user); } else if (sub.includes("messages")) { let pubkey = sub.split(":")[0]; await db.sAdd(`${pubkey}:messages`, ev.id); @@ -108,11 +110,7 @@ export let fillPool = () => { let now = () => Math.round(Date.now() / 1000); let timeouts = {}; -export let q = async ( - sub, - query, - { timeout = 20000, since = 3600, eager = 200 }, -) => +export let q = async (sub, query, { timeout = 20000, eager = 20000 } = {}) => new Promise(async (r, j) => { let start = Date.now(); let seen = []; @@ -269,9 +267,11 @@ export async function handleZap(invoice) { l("sending receipt"); - await Promise.allSettled(relays.map(async (url) => { - let r = await ntr.connect(url); - await r.publish(signed); - r.close() - })); + await Promise.allSettled( + relays.map(async (url) => { + let r = await ntr.connect(url); + await r.publish(signed); + r.close(); + }), + ); } diff --git a/lib/strfry.ts b/lib/strfry.ts new file mode 100644 index 00000000..ebc14ed3 --- /dev/null +++ b/lib/strfry.ts @@ -0,0 +1,30 @@ +import { connect } from "net"; + +export default (cmd) => + new Promise((r, j) => { + let results = []; + let c = `/app/strfry ${cmd} 2>/dev/null\n`; + let client = connect("/sockets/ctrl", () => client.write(c)); + + let s = ""; + client.on("data", (data) => { + s += data.toString(); + // console.log("S", s); + + try { + let parts = s.split("}"); + + for (let i = 0; i < parts.length - 1; i++) { + let completeJson = parts[i] + "}"; + results.push(JSON.parse(completeJson)); + } + + s = parts[parts.length - 1]; + } catch (err) { + console.error("Error parsing JSON:", err.message); + } + }); + + client.on("error", (e) => j(e.message)); + client.on("close", () => r(results)); + }); diff --git a/lib/utils.ts b/lib/utils.ts index e79f459f..a805f343 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -71,7 +71,7 @@ export let fields = [ export let getUser = async (username) => { if (username === "undefined") fail("invalid user"); let user = await migrate(username); - if (user && !user.nwc) { + if (user && !user.anon && !user.nwc) { user.nwc = bytesToHex(randomBytes(32)); await s(getPublicKey(user.nwc), user.id); await s(`user:${user.id}`, user); diff --git a/listener.sh b/listener.sh new file mode 100755 index 00000000..1d68bb4a --- /dev/null +++ b/listener.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Check if the socket file exists +if [ -S /sockets/ctrl ]; then + echo "Socket exists. Unlinking..." + rm /sockets/ctrl +fi + +# Start socat +socat UNIX-LISTEN:/sockets/ctrl,reuseaddr,fork SYSTEM:'sh -c "cat | while read line; do output=\$(eval \$line 2>&1); echo \$output; done"' + diff --git a/routes/nostr.ts b/routes/nostr.ts index 9bbff33d..d3c293c6 100644 --- a/routes/nostr.ts +++ b/routes/nostr.ts @@ -27,8 +27,7 @@ export default { let { params: { pubkey }, } = req; - let uid = await g(`user:${pubkey}`); - let user = await g(`user:${uid}`); + let user = await getUser(pubkey); if (!user) user = { @@ -121,24 +120,29 @@ export default { if (tagsonly) return res.send(tags); let follows = []; + + console.log("L", tags.length); + for (let f of tags) { let [_, pubkey] = f; + let user = await getUser(pubkey); - q(`${pubkey}:profile:f1`, { - limit: 1, - kinds: [0], - authors: [pubkey], - }).catch(nada); - - let uid = await g(`user:${pubkey}`); - let user = await g(`user:${uid}`); + if (!(user && user.updated)) { + console.log("Q", pubkey) + q(`${pubkey}:profile:f1`, { + limit: 1, + kinds: [0], + authors: [pubkey], + }).catch(nada); + } - if (!user) + if (!user) { user = { username: pubkey.substr(0, 6), pubkey, anon: true, }; + } follows.push(user); } @@ -172,17 +176,14 @@ export default { ).catch(nada); for (let pubkey of pubkeys) { - let uid = await g(`user:${pubkey}`); - let user = await g(`user:${uid}`); - if (!user || !user.updated) { - await q(`${pubkey}:profile:f2`, { + let user = await getUser(pubkey); + + if (!user?.updated) { + q(`${pubkey}:profile:f2`, { limit: 1, kinds: [0], authors: [pubkey], }).catch(nada); - - uid = await g(`user:${pubkey}`); - user = await g(`user:${uid}`); } if (!user) diff --git a/routes/users.ts b/routes/users.ts index 6b18da28..18ef7e3b 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -11,12 +11,11 @@ import register from "$lib/register"; import { requirePin } from "$lib/auth"; import { v4 } from "uuid"; import { reconcile, types } from "$lib/payments"; -import { bech32 } from "bech32"; import { mail, templates } from "$lib/mail"; import upload from "$lib/upload"; import rpc from "@coinos/rpc"; - -let { encode, decode, fromWords, toWords } = bech32; +import { nip19 } from "nostr-tools"; +import { hex } from "@scure/base"; export default { upload, @@ -26,8 +25,7 @@ export default { try { user.balance = await g(`balance:${user.id}`); user.prompt = !!user.prompt; - if (user.pubkey) - user.npub = encode("npub", toWords(Buffer.from(user.pubkey, "hex"))); + if (user.pubkey) user.npub = nip19.npubEncode(user.pubkey); res.send(pick(user, whitelist)); } catch (e) { @@ -80,7 +78,7 @@ export default { try { if (key.startsWith("npub")) { try { - key = Buffer.from(fromWords(decode(key).words)).toString("hex"); + key = hex.encode(nip19.decode(key).data as Uint8Array); } catch (e) {} } @@ -115,8 +113,7 @@ export default { "username", ]; - if (user.pubkey) - user.npub = encode("npub", toWords(Buffer.from(user.pubkey, "hex"))); + if (user.pubkey) user.npub = nip19.npubEncode(user.pubkey); user.prompt = !!user.prompt; res.send(pick(user, whitelist)); @@ -193,6 +190,9 @@ export default { let { confirm, password, pin, newpin, pubkey, username } = body; + console.log("PK", pubkey); + console.log("SK", body.nsec); + if (user.pin && !(pin === user.pin)) fail("Pin required"); if (typeof newpin !== "undefined" && newpin.length === 6) user.pin = newpin; @@ -629,7 +629,7 @@ export default { wallet_name: id, descriptors: true, disable_private_keys: true, - load_on_startup: true + load_on_startup: true, }); node = rpc({ ...config[type], wallet: id }); |
