aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ecash.js
blob: 4ce8912a995a92348662c4c980664b0b6f5eccad (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import config from "$config";
import {
  CashuMint,
  CashuWallet,
  getDecodedToken,
  getEncodedToken,
  MintQuoteState,
} 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({
    token: [{ mint: config.mintUrl, proofs }],
  });

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

let ext = async (mint) => {
  let issuer = new CashuMint(mint);
  let { pubkey: issuerPk } = await issuer.getInfo();
  let { pubkey: ourPk } = await m.getInfo();
  return issuerPk !== ourPk;
};

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

  if (await ext(mint)) fail("Unable to receive from other mints");

  let rcvd = await w.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 w.send(amount, proofs);
  let rcvd = await w.receive(enc(send));
  await s(`cash`, enc(returnChange));
  return enc(rcvd);
}

export async function check(token) {
  let { mint, proofs } = dec(token);
  let total = proofs.reduce((a, b) => a + b.amount, 0);

  let external = await ext(mint);

  let r = await w.checkProofsSpent(proofs);
  let spent = r.reduce((a, b) => a + b.amount, 0);

  return { total, spent, mint, external };
}