aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ecash.js
blob: 0e4a3c1a2d6d5e5bb58a2266dcd2d58a4b1b5a82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import config from "$config";
import {
  CashuMint,
  CashuWallet,
  getDecodedToken,
  getEncodedToken,
  MintQuoteState,
} from "@cashu/cashu-ts";
import { g, s } from "$lib/db";

let wallet = new CashuWallet(new CashuMint(config.mintUrl));

let enc = (proofs) =>
  getEncodedToken({
    token: [{ mint: config.mintUrl, proofs }],
  });

let dec = (token) => getDecodedToken(token).token[0];

export async function claim(token) {
  let { proofs: current } = dec(await g(`cash`));
  let rcvd = await wallet.receive(token);

  await s(`cash`, enc([...current, ...rcvd]));
  return rcvd.reduce((a, b) => a + b.amount, 0);
}

export async function mint(amount) {
  let { proofs } = dec(await g(`cash`));
  let { send, returnChange } = await wallet.send(amount, proofs);
  let rcvd = await wallet.receive(enc(send));
  await s(`cash`, enc(returnChange));
  return enc(rcvd);
}