aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-09-03 14:34:08 -0700
committerAdam Soltys <[email protected]>2024-09-03 14:34:08 -0700
commit581dc3b5b16e3dd1823bbf95357e5e8c49b0c5e0 (patch)
treea8eb0c0b44800acc8b0898cd254de69750783562
parent48f9e8b0de151edcf49903277d714d939f0b3d8f (diff)
downloadcoinos-server-581dc3b5b16e3dd1823bbf95357e5e8c49b0c5e0.tar.gz
coinos-server-581dc3b5b16e3dd1823bbf95357e5e8c49b0c5e0.zip
support multiple token versions
-rw-r--r--lib/ecash.ts25
-rw-r--r--routes/ecash.ts9
2 files changed, 21 insertions, 13 deletions
diff --git a/lib/ecash.ts b/lib/ecash.ts
index 4ce8912a..8f91e70c 100644
--- a/lib/ecash.ts
+++ b/lib/ecash.ts
@@ -4,17 +4,16 @@ import {
CashuWallet,
getDecodedToken,
getEncodedToken,
- MintQuoteState,
+ getEncodedTokenV4,
} from "@cashu/cashu-ts";
import { g, s } from "$lib/db";
-import ln from "$lib/ln";
import { fail } from "$lib/utils";
let m = new CashuMint(config.mintUrl);
let w = new CashuWallet(m);
-let enc = (proofs) =>
- getEncodedToken({
+let enc = (proofs, v = 4) =>
+ (v === 4 ? getEncodedTokenV4 : getEncodedToken)({
token: [{ mint: config.mintUrl, proofs }],
});
@@ -27,6 +26,12 @@ let ext = async (mint) => {
return issuerPk !== ourPk;
};
+export async function get(id, v = 4) {
+ let token = await g(`cash:${id}`);
+ if (v < 4) return enc(dec(token).proofs, 3);
+ return token;
+}
+
export async function claim(token) {
let { proofs: current } = dec(await g(`cash`));
let { mint } = dec(token);
@@ -39,16 +44,18 @@ export async function claim(token) {
return rcvd.reduce((a, b) => a + b.amount, 0);
}
-export async function mint(amount) {
+export async function mint(amount, v = 4) {
let { proofs } = dec(await g(`cash`));
let { send, returnChange } = await w.send(amount, proofs);
- let rcvd = await w.receive(enc(send));
- await s(`cash`, enc(returnChange));
- return enc(rcvd);
+ let rcvd = await w.receive(enc(send, v));
+ let change = enc(returnChange, v);
+ await s(`cash`, change);
+ return enc(rcvd, v);
}
export async function check(token) {
- let { mint, proofs } = dec(token);
+ let o = dec(token);
+ let { mint, proofs } = o;
let total = proofs.reduce((a, b) => a + b.amount, 0);
let external = await ext(mint);
diff --git a/routes/ecash.ts b/routes/ecash.ts
index 1f9e48ea..af7f842d 100644
--- a/routes/ecash.ts
+++ b/routes/ecash.ts
@@ -1,5 +1,5 @@
import { g, s, db } from "$lib/db";
-import { claim, mint, check } from "$lib/ecash";
+import { get, claim, mint, check } from "$lib/ecash";
import { bail, fail } from "$lib/utils";
import { debit, credit, types } from "$lib/payments";
import { v4 } from "uuid";
@@ -25,10 +25,10 @@ export default {
async get(req, res) {
let {
- params: { id },
+ params: { id, version },
} = req;
try {
- let token = await g(`cash:${id}`);
+ let token = await get(id, version);
let status = await check(token);
res.send({ token, status });
} catch (e) {
@@ -76,7 +76,8 @@ export default {
let id = v4();
let hash = v4();
- let p = await debit({ hash, amount, user, type });
+ let memo, rate;
+ let p = await debit({ hash, amount, user, type, memo, rate });
let token = await mint(amount);
p.memo = token;
await s(`payment:${p.id}`, p);