diff options
| author | Adam Soltys <[email protected]> | 2024-11-22 22:42:35 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-11-22 22:42:35 -0800 |
| commit | b82f045fe567f25c7d0a9fbb5163853373cd616c (patch) | |
| tree | 0630fc14b88c740992bf04be5bfcd4fb4334f129 | |
| parent | 41ff5eb1261ad0878b080b5a85f6553223108429 (diff) | |
| download | coinos-server-b82f045fe567f25c7d0a9fbb5163853373cd616c.tar.gz coinos-server-b82f045fe567f25c7d0a9fbb5163853373cd616c.zip | |
ecash receive
| -rw-r--r-- | index.ts | 1 | ||||
| -rw-r--r-- | lib/ecash.ts | 23 | ||||
| -rw-r--r-- | lib/invoices.ts | 4 | ||||
| -rw-r--r-- | routes/ecash.ts | 23 |
4 files changed, 51 insertions, 0 deletions
@@ -131,6 +131,7 @@ app.post("/cash", ecash.save); app.post("/claim", auth, ecash.claim); app.post("/mint", auth, ecash.mint); app.post("/melt", auth, ecash.melt); +app.post("/ecash/:id", ecash.receive); app.post("/echo", (req, res) => { console.log("echo", req.body); diff --git a/lib/ecash.ts b/lib/ecash.ts index 7c44bec0..1a214b46 100644 --- a/lib/ecash.ts +++ b/lib/ecash.ts @@ -6,11 +6,16 @@ import { CashuMint, CashuWallet, MintQuoteState, + PaymentRequest, + PaymentRequestTransport, + PaymentRequestTransportType, getDecodedToken, getEncodedToken, getEncodedTokenV4, } from "@cashu/cashu-ts"; +import { v4 } from "uuid"; +const { URL } = process.env; const m = new CashuMint(config.mintUrl); const w = new CashuWallet(m); @@ -92,3 +97,21 @@ export async function init(amount = 100000) { console.log(e); } } + +export function request(uuid, amount, memo) { + const target = `${URL}/ecash/${uuid}`; + const tags = [["n", "17"]]; + + const { POST: type } = PaymentRequestTransportType; + const transport = [{ type, target, tags }]; + const unit = "sat"; + + return new PaymentRequest( + transport, + uuid, + amount, + unit, + config.mintUrl, + memo, + ).toEncodedRequest(); +} diff --git a/lib/invoices.ts b/lib/invoices.ts index 30ae8402..5e59fd83 100644 --- a/lib/invoices.ts +++ b/lib/invoices.ts @@ -1,5 +1,6 @@ import config from "$config"; import { db, g, s } from "$lib/db"; +import { request } from "$lib/ecash"; import ln from "$lib/ln"; import { types } from "$lib/payments"; import { emit } from "$lib/sockets"; @@ -101,6 +102,9 @@ export const generate = async ({ invoice, user }) => { text = bip21(hash, invoice); } else if (type === types.internal) { hash = id; + } else if (type === types.ecash) { + hash = id; + text = request(id, amount, memo); } else { fail(`unrecognized type ${type}`); } diff --git a/routes/ecash.ts b/routes/ecash.ts index 6aac01a8..a807cdb5 100644 --- a/routes/ecash.ts +++ b/routes/ecash.ts @@ -4,6 +4,7 @@ import { err, l } from "$lib/logging"; import { credit, debit, types } from "$lib/payments"; import { emit } from "$lib/sockets"; import { bail, fail } from "$lib/utils"; +import { getEncodedToken } from "@cashu/cashu-ts"; import { v4 } from "uuid"; const { ecash: type } = types; @@ -140,4 +141,26 @@ export default { bail(res, e.message); } }, + + async receive(req, res) { + try { + const { payload } = req.body; + const { id, proofs, mint, memo } = payload; + const { uid: ref } = await g(`invoice:${id}`); + + const amount = await claim( + getEncodedToken({ + mint, + proofs, + }), + ); + + await credit({ hash: id, amount, memo, ref, type }); + + res.send({ id }); + } catch (e) { + err(e.message); + bail(res, e.message); + } + }, }; |
