aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-11-21 10:47:25 -0800
committerAdam Soltys <[email protected]>2024-11-21 10:47:25 -0800
commit3b15d16339c9209f307f8453af91fba15f3d340c (patch)
treed9f4ca8879c6a0b29ca8f6ad795c2154387626fd /routes
parent5642b90b54084061cdbbf6f1dc70c38245b05d5f (diff)
downloadcoinos-server-3b15d16339c9209f307f8453af91fba15f3d340c.tar.gz
coinos-server-3b15d16339c9209f307f8453af91fba15f3d340c.zip
wip
Diffstat (limited to 'routes')
-rw-r--r--routes/ecash.ts62
1 files changed, 50 insertions, 12 deletions
diff --git a/routes/ecash.ts b/routes/ecash.ts
index cc617138..ff092374 100644
--- a/routes/ecash.ts
+++ b/routes/ecash.ts
@@ -1,13 +1,33 @@
import { db, g, s } from "$lib/db";
-import { check, claim, get, mint } from "$lib/ecash";
+import { check, claim, get, mint, request } from "$lib/ecash";
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 {
+ PaymentRequest,
+ PaymentRequestPayload,
+ PaymentRequestTransport,
+ PaymentRequestTransportType,
+ decodePaymentRequest,
+ getDecodedToken,
+} from "@cashu/cashu-ts";
import { v4 } from "uuid";
-
const { ecash: type } = types;
+const sendCash = async ({ amount, user }) => {
+ const id = v4();
+ const hash = v4();
+
+ const p = await debit({ hash, amount, user, type });
+ const token = await mint(amount);
+ p.memo = id;
+ await s(`payment:${p.id}`, p);
+ s(`cash:${id}`, token);
+
+ return { id, token };
+};
+
export default {
async save(req, res) {
const {
@@ -73,16 +93,7 @@ export default {
user,
} = req;
try {
- const id = v4();
- const hash = v4();
-
- const p = await debit({ hash, amount, user, type });
- const token = await mint(amount);
- p.memo = id;
- await s(`payment:${p.id}`, p);
- s(`cash:${id}`, token);
-
- res.send({ id });
+ res.send(await sendCash({ amount, user }));
} catch (e) {
err(e.message);
bail(res, e.message);
@@ -141,4 +152,31 @@ export default {
bail(res, e.message);
}
},
+
+ async pay(req, res) {
+ const { user } = req;
+ const { request } = req.body;
+ const { amount } = decodePaymentRequest(request);
+ const { id, token } = await sendCash({ amount, user });
+
+
+ const decodedToken = getDecodedToken(token);
+ if (!decodedToken) {
+ console.error("could not decode token");
+ return;
+ }
+ const proofs = token.getProofs(decodedToken);
+ const mint = token.getMint(decodedToken);
+ const paymentPayload: PaymentRequestPayload = {
+ id: request.id,
+ mint: mint,
+ unit: request.unit || "",
+ proofs: proofs,
+ };
+ const paymentPayloadString = JSON.stringify(paymentPayload);
+ try {
+ await nostrStore.sendNip17DirectMessageToNprofile(transport.target, paymentPayloadString);
+
+ res.send({ id, token });
+ },
};