diff options
| author | Adam Soltys <[email protected]> | 2023-11-23 23:08:09 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-11-23 23:34:35 -0800 |
| commit | 13f3c21b9ddf9884f190a6fcf7acf50440c82547 (patch) | |
| tree | d29a3d8c91b9f5df5e38990800a3259732dc557b | |
| parent | 04a941b76b916384a3215502d362f4f3b51074a5 (diff) | |
| download | coinos-server-13f3c21b9ddf9884f190a6fcf7acf50440c82547.tar.gz coinos-server-13f3c21b9ddf9884f190a6fcf7acf50440c82547.zip | |
use payment ids
| -rw-r--r-- | lib/lightning.js | 8 | ||||
| -rw-r--r-- | lib/payments.js | 15 | ||||
| -rw-r--r-- | routes/payments.js | 6 | ||||
| -rw-r--r-- | routes/shopify.js | 1 | ||||
| -rw-r--r-- | routes/users.js | 87 |
5 files changed, 20 insertions, 97 deletions
diff --git a/lib/lightning.js b/lib/lightning.js index 1788fd12..77a429ed 100644 --- a/lib/lightning.js +++ b/lib/lightning.js @@ -8,7 +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 event = await ln.waitanyinvoice((await g("pay_index")) || 0); let { bolt11, pay_index, @@ -31,8 +31,10 @@ export async function listenForLightning() { let invoice = await g(`invoice:${id}`); if (!invoice) return warn("received lightning with no invoice", bolt11); - let payment = await g(`payment:${bolt11}`); - if (payment) return warn("already processed", bolt11); + let p = await g(`payment:${bolt11}`); + if (typeof p === "string") p = await g(`payment:${p}`); + + if (p) return warn("already processed", bolt11); await credit(bolt11, received, invoice.memo, preimage, types.lightning); } catch (e) { diff --git a/lib/payments.js b/lib/payments.js index 148a6ea1..369d0aec 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -133,7 +133,9 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { warn("rate slipped", hash, invoice.rate, equivalentRate); } + let id = v4(); let p = { + id, hash, amount: parseInt(amount - tip), uid, @@ -153,12 +155,12 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { let balance = "balance"; if (type === types.bitcoin) { let [txid, vout] = ref.split(":").slice(-2); - p.id = `${txid}:${vout}`; p.confirmed = false; balance = "pending"; + await s(`payment:${txid}:${vout}`, id); } else { - p.id = hash; - } + await s(`payment:${hash}`, id); + } let m = await db.multi(); @@ -192,17 +194,18 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { export let confirm = async (address, txid, vout) => { let id = `payment:${txid}:${vout}`; let p = await g(id); + if (typeof p === "string") p = await g(`payment:${p}`); if (!p) return db.sAdd("missed", id); if (p.confirmed) return; p.confirmed = true; emit(p.uid, "payment", p); - l("confirming", txid, p.uid); + l("confirming", txid); - await db + let r = await db .multi() - .set(id, JSON.stringify(p)) + .set(`payment:${p.id}`, JSON.stringify(p)) .decrBy(`pending:${p.uid}`, p.amount) .incrBy(`balance:${p.uid}`, p.amount) .exec(); diff --git a/routes/payments.js b/routes/payments.js index 1a1dfaa9..7cda38d7 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -221,6 +221,7 @@ export default { async get({ params: { hash } }, res) { let p = await g(`payment:${hash}`); + if (typeof p === "string") p = await g(`payment:${p}`); if (p.type === types.internal) p.with = await g(`user:${p.ref}`); res.send(p); }, @@ -316,6 +317,8 @@ export default { for (let { address, amount, category, vout } of details) { if (category !== "receive") continue; let p = await g(`payment:${txid}:${vout}`); + if (typeof p === "string") p = await g(`payment:${p}`); + if (!p) { await credit( address, @@ -331,6 +334,7 @@ export default { } res.send({}); } catch (e) { + console.log(e); warn(`problem processing ${txid}`); bail(res, e.message); } @@ -527,7 +531,7 @@ export default { let u = await g(k); let id = k.split(":")[1]; - if (typeof u === "String") { + if (typeof u === "string") { id = u; u = await g(`user:${u}`); } diff --git a/routes/shopify.js b/routes/shopify.js index aaecbed0..5d972372 100644 --- a/routes/shopify.js +++ b/routes/shopify.js @@ -14,6 +14,7 @@ let query = `mutation orderMarkAsPaid($input: OrderMarkAsPaidInput!) { export default async ({ body: { hash }, params: { id } }, res) => { let p = await g(`payment:${hash}`); + if (typeof p === "string") p = await g(`payment:${p}`); let user = await g(`user:${p.uid}`); try { diff --git a/routes/users.js b/routes/users.js index 67428abc..75bff8b5 100644 --- a/routes/users.js +++ b/routes/users.js @@ -273,93 +273,6 @@ export default { let uid = await g(`user:${username}`); let user = await getUser(username); - if (classic && !(user && user.migrated)) { - uid = user ? user.id : v4(); - - try { - let { token } = await got - .post(`${classic}/login`, { json: { username, password } }) - .json(); - - if (!token) fail(); - - user = await got(`${classic}/admin/migrate/${username}?zero=true`, { - headers: { authorization: `Bearer ${config.admin}` }, - }).json(); - - let { balance } = user; - if (!user) fail(); - - uid = user.uuid; - - user = { - ...pick(user, fields), - id: uid, - about: user.address, - migrated: true, - haspin: !!user.pin, - }; - - await s(`user:${username}`, uid); - await s(`user:${uid}`, user); - await s(`balance:${uid}`, balance); - - let payments = await got(`${classic}/payments`, { - headers: { authorization: `Bearer ${token}` }, - }).json(); - - for (let p of payments) { - let n = pick(p, [ - "amount", - "fee", - "confirmed", - "rate", - "currency", - "preimage", - ]); - n.id = v4(); - n.created = parseISO(p.createdAt).getTime(); - n.type = p.network; - if (!["bitcoin", "lightning"].includes(n.type)) - n.type = types.internal; - - if (n.type === types.internal) { - if (!p.with) continue; - - let id = await g(`user:${p.with.username.toLowerCase()}`); - let u = id && (await g(`user:${id}`)); - - if (!u) { - u = await got( - `${classic}/admin/migrate/${p.with.username.toLowerCase()}`, - { - headers: { authorization: `Bearer ${config.admin}` }, - } - ).json(); - - u = { id: u.uuid, about: u.address, ...pick(u, fields) }; - delete u.address; - - await s(`user:${p.with.username.toLowerCase()}`, u.id); - await s(`user:${u.id}`, u); - - l("added missing user", u.username); - } - - if (!(u && u.id)) continue; - n.ref = u.id; - } - - await s(`payment:${n.id}`, n); - await db.lPush(`${uid}:payments`, n.id); - } - - l("migrated user", user.username); - } catch (e) { - warn("classic login failed", username); - } - } - if ( !user || (user.password && |
