From f7307a8e2d627006dd6e72fe9c1e2e4d5c6e312a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 13 Feb 2023 13:03:36 -0800 Subject: nostr fixes --- lib/nostr.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'lib/nostr.js') diff --git a/lib/nostr.js b/lib/nostr.js index 065fafb0..b6f5f50d 100644 --- a/lib/nostr.js +++ b/lib/nostr.js @@ -5,12 +5,13 @@ import store from "$lib/store"; import { g, s, db } from "$lib/db"; import { nada, wait } from "$lib/utils"; +export let coinos; export let pool; export let fillPool = () => { pool = RelayPool(config.relays); pool.on("open", relay => { - if (relay.url.includes(config.relay)) coinos = relay; + if (relay.url.includes(config.nostr)) coinos = relay; relay.subscribe("live", { limit: 1 }); }); @@ -61,7 +62,7 @@ export let fillPool = () => { coinos.send(ev); } else if (sub.includes("follows")) { let pubkey = sub.split(":")[0]; - await s(`${pubkey}:follows`, JSON.stringify(ev.tags)); + await s(`${pubkey}:follows`, ev.tags); for (let f of ev.tags) { let [_, followPubkey] = f; await db.sAdd(`${followPubkey}:followers`, pubkey); @@ -83,7 +84,6 @@ export let fillPool = () => { }); }; -export let coinos; let now = () => Math.round(Date.now() / 1000); let timeouts = {}; @@ -156,9 +156,14 @@ export let q = async ( let seen = []; -let getUser = async pubkey => - JSON.parse(await g(`user:${pubkey}`)) || { - username: pubkey.substr(0, 6), - pubkey, - anon: true - }; +let getUser = async pubkey => { + let id = await g(`user:${pubkey}`); + let user = await g(`user:${id}`); + return ( + user || { + username: pubkey.substr(0, 6), + pubkey, + anon: true + } + ); +}; -- cgit v1.2.3 From 12bf0c93d17d191a7bb58cfa72f9f94ee2c4d2af Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 15 Feb 2023 19:58:29 -0800 Subject: messages endpoint --- .gitignore | 1 + index.js | 2 ++ lib/nostr.js | 10 +++++++--- routes/nostr.js | 31 +++++++++++++++++++++++++++++++ routes/payments.js | 30 ++++++++++++++++++++++++++++++ routes/users.js | 2 +- 6 files changed, 72 insertions(+), 4 deletions(-) (limited to 'lib/nostr.js') diff --git a/.gitignore b/.gitignore index 1a6529fe..a29190de 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ data config.js compose.yml +.env diff --git a/index.js b/index.js index 51866cda..8189144d 100644 --- a/index.js +++ b/index.js @@ -37,6 +37,7 @@ app.get("/nostr.json", nostr.identities); app.get("/:pubkey/followers", nostr.followers); app.get("/:pubkey/follows", nostr.follows); app.get("/:pubkey/notes", nostr.notes); +app.get("/:a/:b/:since/messages", nostr.messages); app.get("/event/:id", nostr.event); app.post("/event", nostr.broadcast); @@ -52,6 +53,7 @@ app.get("/payments/:hash", auth, payments.get); app.post("/parse", payments.parse); app.get("/pot/:name", payments.pot); app.post("/take", auth, payments.take); +app.post("/buy", auth, payments.buy); app.get("/encode", lnurl.encode); app.get("/decode", lnurl.decode); diff --git a/lib/nostr.js b/lib/nostr.js index b6f5f50d..fdea9d62 100644 --- a/lib/nostr.js +++ b/lib/nostr.js @@ -60,6 +60,10 @@ export let fillPool = () => { await s(`user:${pubkey}`, JSON.stringify(user)); coinos.send(ev); + } else if (sub.includes("messages")) { + let pubkey = sub.split(":")[0]; + await db.sAdd(`${pubkey}:messages`, ev.id); + await s(`ev:${ev.id}`, ev); } else if (sub.includes("follows")) { let pubkey = sub.split(":")[0]; await s(`${pubkey}:follows`, ev.tags); @@ -76,7 +80,7 @@ export let fillPool = () => { } else if (sub.includes("notes")) { let pubkey = sub.split(":")[0]; await db.sAdd(pubkey, ev.id); - await s(`ev:${ev.id}`, JSON.stringify(ev)); + await s(`ev:${ev.id}`, ev); } } catch (e) { console.log(e); @@ -96,8 +100,8 @@ export let q = async ( let start = Date.now(); let seen = []; let rejected; - query.since = await g(`since:${sub}`); - if (now() - query.since < since) return r(); + // query.since = await g(`since:${sub}`); + // if (now() - query.since < since) return r(); let done = { [sub]: [] }; diff --git a/routes/nostr.js b/routes/nostr.js index 53ad4c29..ad9f55aa 100644 --- a/routes/nostr.js +++ b/routes/nostr.js @@ -49,6 +49,37 @@ export default { res.send(events.map(e => ({ ...e, user }))); }, + async messages({ params: { pubkey, since = 0 } }, res) { + let params = { + kinds: [4], + authors: [pubkey] + }; + + let opts = { since }; + + await q(`${pubkey}:messages`, params, opts).catch(nada); + + params = { + kinds: [4], + "#p": [pubkey] + }; + + await q(`${pubkey}:messages`, params, opts).catch(nada); + + let messages = await db.sMembers(`${pubkey}:messages`); + messages = await Promise.all( + messages.map(async id => { + let m = await g(`ev:${id}`); + let k = m.pubkey === pubkey ? m.tags[0].p : m.pubkey; + let uid = await g(`user:${k}`); + m.user = await g(`user:${uid}`); + return m; + }) + ); + + res.send(messages); + }, + async broadcast(req, res) { let { event } = req.body; pool.send(["EVENT", event]); diff --git a/routes/payments.js b/routes/payments.js index 60e3bdd2..6619c031 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -253,4 +253,34 @@ export default { res.send({ txid }); }, + + async buy({ body: { amount, number, month, year, cvc } }, res) { + let stripe = "https://api.stripe.com/v1"; + let { STRIPE: username } = process.env; + + let { id: source } = await got.post(`${stripe}/tokens`, { + form: { + "card[number]": number, + "card[exp_month]": month, + "card[exp_year]": year, + "card[cvc]": cvc + }, + username + }); + + let { status } = await got.post(`${stripe}/charges`, { + form: { + amount, + currency: "cad", + source, + description: "starter coupon" + }, + username + }); + + if (status !== "succeeded") + throw error(500, { message: "Card payment failed" }); + + res.send(status); + } }; diff --git a/routes/users.js b/routes/users.js index c6fb848d..c48c7599 100644 --- a/routes/users.js +++ b/routes/users.js @@ -171,7 +171,7 @@ export default { ]; for (let a of attributes) { - if (body[a]) user[a] = body[a]; + if (typeof body[a] !== "undefined") user[a] = body[a]; } if (password && password === confirm) { -- cgit v1.2.3 From d01995b438fc2bde316f9cc7020c01d71cd9d288 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 16 Feb 2023 22:43:43 -0800 Subject: emit messages --- lib/nostr.js | 9 ++++++++- routes/nostr.js | 10 +++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'lib/nostr.js') diff --git a/lib/nostr.js b/lib/nostr.js index fdea9d62..2eb59ae7 100644 --- a/lib/nostr.js +++ b/lib/nostr.js @@ -1,6 +1,6 @@ import config from "$config"; import { Relay, RelayPool } from "nostr"; -import { broadcast } from "$lib/sockets"; +import { broadcast, emit } from "$lib/sockets"; import store from "$lib/store"; import { g, s, db } from "$lib/db"; import { nada, wait } from "$lib/utils"; @@ -40,6 +40,13 @@ export let fillPool = () => { // coinos.send(["EVENT", ev]); ev.user = await getUser(pubkey); // broadcast("event", ev); + + if (ev.kind === 4) { + let uid = await g(`user:${ev.tags[0][1]}`); + if (uid) ev.recipient = await g(`user:${uid}`); + ev.author = ev.user; + emit(uid, "event", ev); + } } } else if (sub.includes("profile")) { let { pubkey } = ev; diff --git a/routes/nostr.js b/routes/nostr.js index 47ceaa25..8d6f6c34 100644 --- a/routes/nostr.js +++ b/routes/nostr.js @@ -70,9 +70,13 @@ export default { messages = await Promise.all( messages.map(async id => { let m = await g(`ev:${id}`); - let k = m.pubkey === pubkey ? m.tags[0][1] : m.pubkey; - let uid = await g(`user:${k}`); - m.user = await g(`user:${uid}`); + + let aid = await g(`user:${m.pubkey}`); + m.author = await g(`user:${aid}`); + + let rid = await g(`user:${m.tags[0][1]}`); + m.recipient = await g(`user:${rid}`); + return m; }) ); -- cgit v1.2.3 From a311401e50cbc70f5d613097607cb55466aa4061 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 21 Feb 2023 08:25:42 -0800 Subject: prettier --- lib/db.js | 6 +++--- lib/nostr.js | 2 +- routes/info.js | 2 +- routes/invoices.js | 2 +- routes/nostr.js | 2 +- routes/users.js | 13 ++++++++++--- 6 files changed, 17 insertions(+), 10 deletions(-) (limited to 'lib/nostr.js') diff --git a/lib/db.js b/lib/db.js index cc9c3919..05917645 100644 --- a/lib/db.js +++ b/lib/db.js @@ -17,11 +17,11 @@ export let g = async k => { }; export let s = (k, v) => { if (k === "user:null") { - warn("###### NULL USER #######") + warn("###### NULL USER #######"); console.trace(); - } + } db.set(k, JSON.stringify(v)); -} +}; let retries = {}; export let t = async (k, f) => { diff --git a/lib/nostr.js b/lib/nostr.js index 2eb59ae7..604d207d 100644 --- a/lib/nostr.js +++ b/lib/nostr.js @@ -46,7 +46,7 @@ export let fillPool = () => { if (uid) ev.recipient = await g(`user:${uid}`); ev.author = ev.user; emit(uid, "event", ev); - } + } } } else if (sub.includes("profile")) { let { pubkey } = ev; diff --git a/routes/info.js b/routes/info.js index eda028d2..45408269 100644 --- a/routes/info.js +++ b/routes/info.js @@ -7,7 +7,7 @@ export default { let lnwallet; let b = 0; - for await (let k of db.scanIterator({ MATCH: "balance:*" })) { + for await (let k of db.scanIterator({ MATCH: "balance:*" })) { b += parseInt(await db.get(k)); } diff --git a/routes/invoices.js b/routes/invoices.js index bbbae760..d52b34cc 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -14,7 +14,7 @@ export default { if (invoice) { invoice.user = await g(`user:${invoice.uid}`); invoice.id = hash; - } else if(config.classic) { + } else if (config.classic) { invoice = await got(`${config.classic}/invoice/${hash}`).json(); if (!invoice) fail("invoice not found"); invoice.id = invoice.uuid; diff --git a/routes/nostr.js b/routes/nostr.js index 8d6f6c34..eff956d1 100644 --- a/routes/nostr.js +++ b/routes/nostr.js @@ -74,7 +74,7 @@ export default { let aid = await g(`user:${m.pubkey}`); m.author = await g(`user:${aid}`); - let rid = await g(`user:${m.tags[0][1]}`); + let rid = await g(`user:${m.tags[0][1]}`); m.recipient = await g(`user:${rid}`); return m; diff --git a/routes/users.js b/routes/users.js index 955a5ca7..099e3540 100644 --- a/routes/users.js +++ b/routes/users.js @@ -231,7 +231,14 @@ export default { }).json(); for (let p of payments) { - let n = pick(p, ["amount", "fee", "confirmed", "rate", "currency", "preimage"]); + let n = pick(p, [ + "amount", + "fee", + "confirmed", + "rate", + "currency", + "preimage" + ]); n.id = v4(); n.created = parseISO(p.createdAt).getTime(); n.type = p.network; @@ -391,9 +398,9 @@ export default { async lower(req, res) { for await (let k of db.scanIterator({ MATCH: "payment:*" })) { let p = await g(k); - if (p.ref && p.ref.startsWith('2dad97')) console.log(k, p); + if (p.ref && p.ref.startsWith("2dad97")) console.log(k, p); } res.send("ok"); - }, + } }; -- cgit v1.2.3