diff options
| author | Adam Soltys <[email protected]> | 2023-01-20 11:40:10 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-01-20 11:40:10 -0800 |
| commit | 5dd47b9b2cb5a9359c57079205da68a675e10b45 (patch) | |
| tree | 48f5065cbd9781342c4ed52300b1d68c2eb7f0e7 | |
| parent | 5264f702a2217fe8815cb5275aca14a672f6d4bb (diff) | |
| download | coinos-server-5dd47b9b2cb5a9359c57079205da68a675e10b45.tar.gz coinos-server-5dd47b9b2cb5a9359c57079205da68a675e10b45.zip | |
endpoint renaming
| -rw-r--r-- | docker-compose.yml | 8 | ||||
| -rw-r--r-- | index.js | 2 | ||||
| -rw-r--r-- | lib/payments.js | 29 | ||||
| -rw-r--r-- | routes/invoices.js | 4 | ||||
| -rw-r--r-- | routes/payments.js | 32 |
5 files changed, 51 insertions, 24 deletions
diff --git a/docker-compose.yml b/docker-compose.yml index 3cf2b5aa..421b6a45 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -48,6 +48,14 @@ services: volumes: - ./data/lightningc:/root/.lightning restart: always + cld: + image: elementsproject/lightningd:latest + container_name: cld + environment: + LIGHTNINGD_NETWORK: regtest + volumes: + - ./data/lightningd:/root/.lightning + restart: always db: image: eqalpha/keydb container_name: db @@ -47,7 +47,7 @@ app.post("/invoice", optional, invoices.create); app.post("/payments", auth, payments.create); app.get("/payments", auth, payments.list); app.get("/payments/:hash", auth, payments.get); -app.post("/query", auth, payments.query); +app.post("/parse", payments.parse); app.get("/pot/:name", payments.pot); app.post("/take", auth, payments.take); app.post("/bitcoin", payments.bitcoin); diff --git a/lib/payments.js b/lib/payments.js index e12f7858..af08cb84 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -16,15 +16,20 @@ export const types = { }; export let debit = async ( - user, + hash, amount, memo, - to, + user, type = types.internal, id = v4() ) => { + console.log("HASH", hash) + let ref; + let invoice = await g(`invoice:${hash}`); + if (invoice) ref = invoice.uid; + console.log("REF", ref) + let { id: uid, currency, username } = user; - to = to && to.id; amount = parseInt(amount); @@ -46,22 +51,20 @@ export let debit = async ( rate: store.rates[currency], currency, type, - to, + ref, created: Date.now() }; - if (!to) delete p.to; - - await s(`payment:${hash}`, p); - await db.lPush(`${uid}:payments`, hash); + await s(`payment:${id}`, p); + await db.lPush(`${uid}:payments`, id); - l("sent internal", user.username, amount); + l("sent", type, user.username, amount); emit(user.id, "payment", p); return p; }; -export let credit = async (hash, amount, memo, from, type = "internal") => { +export let credit = async (hash, amount, memo, ref, type = types.internal) => { amount = parseInt(amount); let invoice = await g(`invoice:${hash}`); if (!invoice) return; @@ -77,7 +80,7 @@ export let credit = async (hash, amount, memo, from, type = "internal") => { rate: store.rates[currency], currency, memo, - from, + ref, type, confirmed: true, created: Date.now() @@ -93,7 +96,7 @@ export let credit = async (hash, amount, memo, from, type = "internal") => { let balance = "balance"; if (type === types.bitcoin) { - let [txid, vout] = from.split(":").slice(-2); + let [txid, vout] = ref.split(":").slice(-2); p.id = `${txid}:${vout}`; p.confirmed = false; balance = "pending"; @@ -109,7 +112,7 @@ export let credit = async (hash, amount, memo, from, type = "internal") => { .incrBy(`${balance}:${uid}`, amount) .exec(); - l("received internal", username, amount); + l("received", type, username, amount); emit(uid, "payment", p); notify(user, `Received ${amount} sats`); callWebhook(invoice, p); diff --git a/routes/invoices.js b/routes/invoices.js index 62106f3a..8fbb25c1 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -4,6 +4,7 @@ import { emit } from "$lib/sockets"; import { db, g, s } from "$lib/db"; import { bip21, fail } from "$lib/utils"; import { types } from "$lib/payments"; +import { v4 } from "uuid"; import bc from "$lib/bitcoin"; import ln from "$lib/ln"; @@ -33,6 +34,7 @@ export default { if (amount < 0) fail("invalid amount"); let hash, text; + console.log("TYPE", type) if (type === types.lightning) { let amt = amount ? `${amount + tip}sat` : "any"; let r = await ln.invoice(amt, new Date(), "", 3600); @@ -42,6 +44,8 @@ export default { } else if (type === types.bitcoin) { hash = await bc.getNewAddress(); text = bip21(hash, invoice); + } else if (type === types.internal) { + hash = v4(); } else { fail("unrecognized type"); } diff --git a/routes/payments.js b/routes/payments.js index d044f1e8..b92a6f81 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -18,16 +18,14 @@ export default { let { amount, hash, name, memo, tip } = body; await requirePin({ body, user }); - let invoice, recipient; - - let p = await debit(user, amount, memo, recipient); + let p = await debit(hash, amount, memo, user); if (hash) { await credit(hash, amount, memo, user.id); } else { let pot = name || v4(); await db.incrBy(`pot:${pot}`, amount); - await db.lPush(`pot:${pot}:payments`, p.hash); + await db.lPush(`pot:${pot}:payments`, p.id); l("funded pot", pot); } @@ -43,8 +41,13 @@ export default { // if (end) where.createdAt[Op.lte] = new Date(parseInt(end)); let payments = (await db.lRange(`${id}:payments`, 0, -1)) || []; - payments = await Promise.all(payments.map(hash => g(`payment:${hash}`))); - payments = payments.filter(p => p); + payments = await Promise.all( + payments.map(async id => { + let p = await g(`payment:${id}`); + if (p.type === types.internal) p.with = await g(`user:${p.ref}`); + return p; + }) + ); res.send({ payments, total: payments.length }); }, @@ -52,7 +55,7 @@ export default { res.send(await g(`payment:${hash}`)); }, - async query({ body: { payreq } }, res) { + async parse({ body: { payreq } }, res) { let hour = 1000 * 60 * 60; let { last } = store.nodes; let { nodes } = store; @@ -61,11 +64,14 @@ export default { store.nodes = nodes; let twoWeeksAgo = new Date(new Date().setDate(new Date().getDate() - 14)); - let { msatoshi, payee } = await ln.decodepay(payreq); + let decoded = await ln.decodepay(payreq); + let { msatoshi, payee, routes } = decoded; let node = nodes.find(n => n.nodeid === payee); let alias = node ? node.alias : payee.substr(0, 12); + let r = await ln.getroute(payee, msatoshi, 5); + console.log(routes, r); - res.send({ alias, amount: Math.round(msatoshi / 1000) }); + res.send({ alias, amount: Math.round(msatoshi / 1000), routes }); }, async pot({ params: { name } }, res) { @@ -108,7 +114,13 @@ export default { if (confirmations > 0) { await confirm(address, txid, vout); } else { - await credit(address, sats(amount), "", `${txid}:${vout}`, types.bitcoin); + await credit( + address, + sats(amount), + "", + `${txid}:${vout}`, + types.bitcoin + ); } } } |
