diff options
| -rw-r--r-- | lib/db.js | 2 | ||||
| -rw-r--r-- | lib/lightning.js | 9 | ||||
| -rw-r--r-- | lib/nostr.js | 1 | ||||
| -rw-r--r-- | lib/payments.js | 5 | ||||
| -rw-r--r-- | lib/register.js | 3 | ||||
| -rw-r--r-- | lib/upload.js | 2 | ||||
| -rw-r--r-- | routes/payments.js | 2 | ||||
| -rw-r--r-- | routes/users.js | 39 |
8 files changed, 41 insertions, 22 deletions
@@ -1,4 +1,4 @@ -import config from "$config" +import config from "$config"; import { sleep } from "$lib/utils"; import { createClient } from "redis"; diff --git a/lib/lightning.js b/lib/lightning.js index c8daf8c8..2915b7c7 100644 --- a/lib/lightning.js +++ b/lib/lightning.js @@ -8,6 +8,7 @@ import { g, s, t, db } from "$lib/db"; import { credit, types } from "$lib/payments"; export async function listenForLightning() { + let event = await ln.waitanyinvoice(await g("pay_index")); let { payment_hash: hash, bolt11, @@ -15,7 +16,7 @@ export async function listenForLightning() { status, msatoshi_received, payment_preimage: preimage - } = await ln.waitanyinvoice(await g("pay_index")); + } = event; await s("pay_index", pay_index); setTimeout(listenForLightning); @@ -30,11 +31,7 @@ export async function listenForLightning() { let invoice = await g(`invoice:${hash}`); if (!invoice) return warn("received lightning with no invoice", bolt11); - let { currency, memo, rate, tip, uid } = invoice; - let amount = parseInt(received) - tip; - if (amount < 0) throw new Error("amount out of range"); - - await credit(hash, amount, memo, preimage, types.lightning); + await credit(hash, received, invoice.memo, preimage, types.lightning); } catch (e) { console.log(e); err("problem receiving lightning payment", e.message); diff --git a/lib/nostr.js b/lib/nostr.js index 23ea937b..065fafb0 100644 --- a/lib/nostr.js +++ b/lib/nostr.js @@ -15,6 +15,7 @@ export let fillPool = () => { }); pool.on("event", async (relay, sub, ev) => { + if (!coinos) return; if (timeouts[sub]) timeouts[sub].extend(ev); try { let content; diff --git a/lib/payments.js b/lib/payments.js index 47cb11f4..6fd595d4 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -36,8 +36,8 @@ export let debit = async ( if (!amount || amount < 0) fail("Amount must be greater than zero"); let ourfee = [types.bitcoin, types.lightning].includes(type) - ? 0 - : Math.round(amount * config.fee); + ? Math.round(amount * config.fee) + : 0; await t(`balance:${uid}`, async (balance, db) => { await t(`credit:${type}:${uid}`, async (credit, db) => { @@ -85,6 +85,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { amount = parseInt(amount); let invoice = await g(`invoice:${hash}`); if (!invoice) return; + if (!memo) ({ memo } = invoice); let user = await g(`user:${invoice.uid}`); diff --git a/lib/register.js b/lib/register.js index a3ebfc86..a222a416 100644 --- a/lib/register.js +++ b/lib/register.js @@ -8,7 +8,7 @@ import { s, g } from "$lib/db"; import { got } from "got"; export default async (user, ip, requireChallenge) => { - let { password, username } = user; + let { password, pubkey, username } = user; l("registering", username); if (!username) throw new Error("Username required"); @@ -54,6 +54,7 @@ export default async (user, ip, requireChallenge) => { await s(`user:${id}`, user); await s(`user:${user.username}`, id); + await s(`user:${pubkey}`, id); l("new user", user.username); return user; diff --git a/lib/upload.js b/lib/upload.js index 2f8180a0..954e8d4d 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -12,7 +12,7 @@ export default async (req, res) => { params: { type }, user } = req; - console.log("TYPE", type) + console.log("TYPE", type); if (!["banner", "profile"].includes(type)) throw new Error("invalid upload"); let data = await req.file(); diff --git a/routes/payments.js b/routes/payments.js index 5c3944ce..970bc187 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -31,7 +31,7 @@ export default { await got .post(`${config.classic}/admin/credit`, { - json: { username, amount, source }, + json: { username, amount, source }, headers: { authorization: `Bearer ${config.admin}` } }) .json(); diff --git a/routes/users.js b/routes/users.js index e3264937..10cc358d 100644 --- a/routes/users.js +++ b/routes/users.js @@ -23,6 +23,7 @@ export default { async me({ user }, res) { try { user.balance = await g(`balance:${user.id}`); + user.prompt = !!user.prompt; res.send(pick(user, whitelist)); } catch (e) { console.log("problem fetching user", e); @@ -33,7 +34,9 @@ export default { async get({ params: { key } }, res) { try { if (key.startsWith("npub")) { - key = Buffer.from(fromWords(decode(key).words)).toString("hex"); + try { + key = Buffer.from(fromWords(decode(key).words)).toString("hex"); + } catch (e) {} } let user = await g(`user:${key}`); @@ -43,7 +46,9 @@ export default { if (!user && key.length === 64) { user = { - username: key.substr(0, 6), + currency: "USD", + username: key, + display: key.substr(0, 6), pubkey: key, anon: true }; @@ -63,6 +68,8 @@ export default { "id" ]; + user.prompt = !!user.prompt; + res.send(pick(user, whitelist)); } catch (e) { console.log(e); @@ -197,7 +204,7 @@ export default { headers: { authorization: `Bearer ${config.admin}` } }).json(); - let { balance } = user; + let { balance, pubkey } = user; if (!user) fail(); uid = user.uuid; @@ -209,6 +216,7 @@ export default { migrated: true }; + await s(`user:${pubkey}`, uid); await s(`user:${username}`, uid); await s(`user:${uid}`, user); await s(`balance:${uid}`, balance); @@ -238,6 +246,7 @@ export default { u = { id: u.uuid, about: u.address, ...pick(u, fields) }; delete u.address; + await s(`user:${u.pubkey}`, u.id); await s(`user:${p.with.username}`, u.id); await s(`user:${u.id}`, u); @@ -346,12 +355,22 @@ export default { } }, - async contacts({ user: { uuid } }, res) { - let payments = (await g(`${uuid}:payments`)) || []; - res.send( - Promise.all( - [...new Set(payments.map(p => p.with_id))].map(id => g(`user:${id}`)) - ) - ); + async contacts({ user: { id } }, res) { + let i = (await g(`${id}:cindex`)) || 0; + let payments = (await db.lRange(`${id}:payments`, i, -1)) || []; + await db.incrBy(`${id}:cindex`, payments.length); + + let contacts = (await g(`${id}:contacts`)) || []; + + for (let { ref } of ( + await Promise.all(payments.map(async id => await g(`payment:${id}`))) + ).filter(p => p.type === types.internal && p.ref)) { + !~contacts.findIndex(({ id }) => id === ref) && + contacts.push(await g(`user:${ref}`)); + } + + await s(`${id}:contacts`, contacts); + + res.send(contacts); } }; |
