diff options
| author | Adam Soltys <[email protected]> | 2024-08-26 21:17:43 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-08-26 21:17:43 -0700 |
| commit | fe62cadd6fb4a7e49f7d59401fc74ae0411e32a7 (patch) | |
| tree | aa8f34e1692bd2d638a2c996eeadb58671d07aea /lib/ecash.ts | |
| parent | 00e43ddc0bf3d6dd719fc9295d156567fa2fecfb (diff) | |
| download | coinos-server-fe62cadd6fb4a7e49f7d59401fc74ae0411e32a7.tar.gz coinos-server-fe62cadd6fb4a7e49f7d59401fc74ae0411e32a7.zip | |
typescript
Diffstat (limited to 'lib/ecash.ts')
| -rw-r--r-- | lib/ecash.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/ecash.ts b/lib/ecash.ts new file mode 100644 index 00000000..4ce8912a --- /dev/null +++ b/lib/ecash.ts @@ -0,0 +1,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 }; +} |
