aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-08-18 21:39:59 -0700
committerAdam Soltys <[email protected]>2024-08-18 21:39:59 -0700
commit0c55be9b7ac1067314826944655f0b4f08a6afa4 (patch)
treedd978a771ef9a2800db107bb1b170cf4616ad82d
parent6bce7c842ef5665edfce9bebadfb62b95ae40111 (diff)
downloadcoinos-server-0c55be9b7ac1067314826944655f0b4f08a6afa4.tar.gz
coinos-server-0c55be9b7ac1067314826944655f0b4f08a6afa4.zip
ecash routes
-rw-r--r--index.js1
-rw-r--r--lib/ecash.js24
-rw-r--r--routes/ecash.js61
3 files changed, 55 insertions, 31 deletions
diff --git a/index.js b/index.js
index 04a6e228..d280ee41 100644
--- a/index.js
+++ b/index.js
@@ -118,6 +118,7 @@ app.post("/shopify/:id", shopify);
app.post("/hidepay", admin, users.hidepay);
app.post("/unlimit", admin, users.unlimit);
+app.post("/claim", auth, ecash.claim);
app.post("/mint", auth, ecash.mint);
let host = process.env.HOST || "0.0.0.0";
diff --git a/lib/ecash.js b/lib/ecash.js
index d47513a4..0e4a3c1a 100644
--- a/lib/ecash.js
+++ b/lib/ecash.js
@@ -4,6 +4,7 @@ import {
CashuWallet,
getDecodedToken,
getEncodedToken,
+ MintQuoteState,
} from "@cashu/cashu-ts";
import { g, s } from "$lib/db";
@@ -16,27 +17,18 @@ let enc = (proofs) =>
let dec = (token) => getDecodedToken(token).token[0];
-export async function melt(user, amount) {
- let { proofs } = dec(await g(`cash:${user.id}`));
+export async function claim(token) {
let { proofs: current } = dec(await g(`cash`));
+ let rcvd = await wallet.receive(token);
- let { send, returnChange } = await wallet.send(amount, proofs);
-
- await s(`cash`, enc([...current, ...send]));
- await s(`cash:${user.id}`, enc(returnChange));
+ await s(`cash`, enc([...current, ...rcvd]));
+ return rcvd.reduce((a, b) => a + b.amount, 0);
}
-export async function mint(user, amount) {
+export async function mint(amount) {
let { proofs } = dec(await g(`cash`));
- let { proofs: current } = dec(await g(`cash:${user.id}`));
-
let { send, returnChange } = await wallet.send(amount, proofs);
-
- await s(`cash:${user.id}`, enc([...current, ...send]));
+ let rcvd = await wallet.receive(enc(send));
await s(`cash`, enc(returnChange));
-}
-
-export async function balance(user) {
- let { proofs } = dec(await g(`cash:${user.id}`));
- return proofs.reduce((a, b) => a + b.amount, 0);
+ return enc(rcvd);
}
diff --git a/routes/ecash.js b/routes/ecash.js
index 6ca570bf..e1877440 100644
--- a/routes/ecash.js
+++ b/routes/ecash.js
@@ -1,18 +1,49 @@
-import { CashuMint, CashuWallet, MintQuoteState } from '@cashu/cashu-ts';
+import { g, s } from "$lib/db";
+import { claim, mint } from "$lib/ecash";
+import { bail } from "$lib/utils";
+import { debit, credit, types } from "$lib/payments";
+import { v4 } from "uuid";
+import store from "$lib/store";
+
+let { ecash: type } = types;
export default {
- async mint({ body }, res) {
- const mintUrl = 'http://mint:3338'; // the mint URL
- const mint = new CashuMint(mintUrl);
- const wallet = new CashuWallet(mint);
- const mintQuote = await wallet.createMintQuote(64);
- console.log(mintQuote)
- // pay the invoice here before you continue...
- const mintQuoteChecked = await wallet.checkMintQuote(mintQuote.quote);
- console.log(mintQuoteChecked)
- if (mintQuoteChecked.state == MintQuoteState.PAID) {
- const { proofs } = await wallet.mintTokens(64, mintQuote.quote);
- console.log(proofs)
+ async claim({ body: { token }, user }, res) {
+ try {
+ let amount = await claim(token);
+
+ let memo;
+ let hash = v4();
+ let { currency, id: uid } = user;
+ await s(`invoice:${hash}`, {
+ currency,
+ id: hash,
+ hash,
+ rate: store.rates[currency],
+ uid,
+ received: 0,
+ });
+
+ await credit(hash, amount, memo, user.id, type);
+
+ res.send({ ok: true });
+ } catch (e) {
+ bail(res, e.message);
+ }
+ },
+
+ async mint({ body: { amount }, user }, res) {
+ try {
+ let hash = v4();
+
+ let p = await debit({ hash, amount, user, type });
+ let token = await mint(amount);
+ p.memo = token;
+ await s(`payment:${p.id}`, p);
+
+ res.send({ token });
+ } catch (e) {
+ bail(res, e.message);
}
- }
-}
+ },
+};