diff options
| -rw-r--r-- | docker-compose.yml | 2 | ||||
| -rw-r--r-- | lib/db.js | 17 | ||||
| -rw-r--r-- | lib/lightning.js | 12 | ||||
| -rw-r--r-- | lib/rates.js | 1 | ||||
| -rw-r--r-- | lib/sockets.js | 1 | ||||
| -rw-r--r-- | lib/utils.js | 2 | ||||
| -rw-r--r-- | routes/nostr.js | 2 | ||||
| -rw-r--r-- | routes/users.js | 97 |
8 files changed, 68 insertions, 66 deletions
diff --git a/docker-compose.yml b/docker-compose.yml index 11047bf4..0555775d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: image: cs container_name: app depends_on: - - rd + - db - bc - cl command: "pnpm dev" @@ -1,8 +1,6 @@ import { createClient } from "redis"; -export const db = createClient({ - url: "redis://rd" -}); +export const db = createClient({ url: "redis://db" }); db.on("error", err => console.log("db error", err)); @@ -12,3 +10,16 @@ export default db; export let g = async k => JSON.parse(await db.get(k)); export let s = (k, v) => db.set(k, JSON.stringify(v)); + +let retries = {}; +export let t = async (k, f) => { + retries[k] = retries[k] ? retries[k]++ : 0; + await db.watch(k); + let o = f(await g(k)); + let m = await db.multi(); + await s(k, o); + + if (retries[k] < 10) { + if (!(await m.exec())) setTimeout(() => t(k, f), 100); + } else throw new Error("could not obtain lock"); +}; diff --git a/lib/lightning.js b/lib/lightning.js index 2a8e8053..be5929b7 100644 --- a/lib/lightning.js +++ b/lib/lightning.js @@ -8,8 +8,8 @@ import { g, s, db } from "$lib/db"; export async function listenForLightning() { let { - payment_hash, - bolt11: request, + payment_hash: hash, + bolt11, pay_index, status, msatoshi_received, @@ -25,10 +25,10 @@ export async function listenForLightning() { if (!preimage) return; - let invoice = await g(`invoice:${payment_hash}`); - if (!invoice) return warn("received lightning with no invoice", request); + let invoice = await g(`invoice:${hash}`); + if (!invoice) return warn("received lightning with no invoice", bolt11); - let { hash, currency, memo, rate, tip, uid } = invoice; + let { currency, memo, rate, tip, uid } = invoice; let amount = parseInt(received) - tip; if (amount < 0) throw new Error("amount out of range"); @@ -50,7 +50,7 @@ export async function listenForLightning() { invoice.status = "paid"; invoice.received += received; - await s(`invoice:${payment_hash}`, invoice); + await s(`invoice:${hash}`, invoice); let t = async () => { await db.watch(`balance:${uid}`); diff --git a/lib/rates.js b/lib/rates.js index 6f87c78e..7cc24e6b 100644 --- a/lib/rates.js +++ b/lib/rates.js @@ -21,6 +21,7 @@ let connect = () => { store.last = msg.c; } catch (e) { + console.log(e) err("binance message error", e.message); } }; diff --git a/lib/sockets.js b/lib/sockets.js index e803d5b0..a7d392a9 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -83,6 +83,7 @@ export async function socketServer(app) { let { id: uid } = jwt.decode(token); if (!uid) throw new Error("Invalid JWT token"); let user = await g(`user:${uid}`); + console.log("uid", uid) if (!user) throw new Error("User not found"); if (!store.sockets[uid]) store.sockets[uid] = {}; diff --git a/lib/utils.js b/lib/utils.js index e53bf649..8d2648f2 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -26,4 +26,4 @@ export const SATS = 100000000; export const toSats = n => Math.round(n * SATS); export const uniq = (a, k) => [...new Map(a.map(x => [k(x), x])).values()]; -export const pick = (O, K) => K.reduce((o, k) => ((o[k] = O[k]), o), {}); +export const pick = (O, K) => K.reduce((o, k) => (typeof O[k] !== 'undefined' && (o[k] = O[k]), o), {}); diff --git a/routes/nostr.js b/routes/nostr.js index 35b40c07..1cf9d595 100644 --- a/routes/nostr.js +++ b/routes/nostr.js @@ -1,7 +1,7 @@ import { nada, uniq } from "$lib/utils"; import config from "$config"; import { g, s, db } from "$lib/db"; -import { q } from "$lib/nostr"; +import { pool, q } from "$lib/nostr"; export default { async event({ params: { id } }, res) { diff --git a/routes/users.js b/routes/users.js index 53b02d02..c5ac61b1 100644 --- a/routes/users.js +++ b/routes/users.js @@ -117,69 +117,58 @@ export default { res.send({}); }, - async update( - { user, body: { confirm, password, pin, newpin, username } }, - res - ) { - try { - let { user } = req; + async update({ user, body }, res) { + l("updating user", user.username); - l("updating user", user.username); + let { confirm, password, pin, newpin, username } = body; - if (user.pin && !(pin === user.pin)) throw new Error("Pin required"); - if (typeof newpin !== "undefined") user.pin = newpin; - if (!user.pin) user.pin = null; + if (user.pin && !(pin === user.pin)) throw new Error("Pin required"); + if (typeof newpin !== "undefined") user.pin = newpin; + if (!user.pin) user.pin = null; - let exists; - if (username) exists = await g(`user:${username}`); + let exists; + if (username) exists = await g(`user:${username}`); - let token; - if (user.username !== username && exists) { - err("username taken", username, user.username, exists.username); - throw new Error("Username taken"); - } else if (username) { - if (user.username !== username) - l("changing username", user.username, username); - user.username = username; - } + let token; + if (user.username !== username && exists) { + err("username taken", username, user.username, exists.username); + throw new Error("Username taken"); + } else if (username) { + if (user.username !== username) + l("changing username", user.username, username); + user.username = username; + } - let attributes = [ - "address", - "cipher", - "currencies", - "currency", - "display", - "email", - "fiat", - "locktime", - "prompt", - "pubkey", - "salt", - "seed", - "tokens", - "twofa", - "unit" - ]; + let attributes = [ + "address", + "cipher", + "currencies", + "currency", + "display", + "email", + "fiat", + "locktime", + "prompt", + "pubkey", + "salt", + "seed", + "tokens", + "twofa", + ]; - for (let a of attributes) { - if (req.body[a]) user[a] = req.body[a]; - } - - if (password && password === confirm) { - user.password = await bcrypt.hash(password, 1); - } + for (let a of attributes) { + if (body[a]) user[a] = body[a]; + } - await s(`user:${user.id}`, user); + if (password && password === confirm) { + user.password = await bcrypt.hash(password, 1); + } - user.haspin = !!user.pin; + user.haspin = !!user.pin; + await s(`user:${user.id}`, user); - emit(user.username, "user", user); - res.send({ user, token }); - } catch (e) { - console.log(e); - err("error updating user", e.message); - res.code(500).send(e.message); - } + emit(user.username, "user", user); + res.send({ user, token }); }, async login(req, res) { |
