diff options
Diffstat (limited to 'lib/ecash.js')
| -rw-r--r-- | lib/ecash.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/ecash.js b/lib/ecash.js new file mode 100644 index 00000000..d47513a4 --- /dev/null +++ b/lib/ecash.js @@ -0,0 +1,42 @@ +import config from "$config"; +import { + CashuMint, + CashuWallet, + getDecodedToken, + getEncodedToken, +} 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 melt(user, amount) { + let { proofs } = dec(await g(`cash:${user.id}`)); + let { proofs: current } = dec(await g(`cash`)); + + let { send, returnChange } = await wallet.send(amount, proofs); + + await s(`cash`, enc([...current, ...send])); + await s(`cash:${user.id}`, enc(returnChange)); +} + +export async function mint(user, amount) { + let { proofs } = dec(await g(`cash`)); + let { proofs: current } = dec(await g(`cash:${user.id}`)); + + let { send, returnChange } = await wallet.send(amount, proofs); + + await s(`cash:${user.id}`, enc([...current, ...send])); + await s(`cash`, enc(returnChange)); +} + +export async function balance(user) { + let { proofs } = dec(await g(`cash:${user.id}`)); + return proofs.reduce((a, b) => a + b.amount, 0); +} |
