From 581dc3b5b16e3dd1823bbf95357e5e8c49b0c5e0 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 3 Sep 2024 14:34:08 -0700 Subject: support multiple token versions --- lib/ecash.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'lib') 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); -- cgit v1.2.3