aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ecash.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-08-16 14:27:56 -0700
committerAdam Soltys <[email protected]>2024-08-16 14:27:56 -0700
commitf8cfd7e189c62c6325a126df1dda3b24cb1bbc36 (patch)
tree08d12f40589b3675ddb0453d586b83a79ac68691 /lib/ecash.js
parente17a531ce983a3e2fcc233a606b9b3678e22266a (diff)
downloadcoinos-server-f8cfd7e189c62c6325a126df1dda3b24cb1bbc36.tar.gz
coinos-server-f8cfd7e189c62c6325a126df1dda3b24cb1bbc36.zip
transfer ecash
Diffstat (limited to 'lib/ecash.js')
-rw-r--r--lib/ecash.js42
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);
+}