aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xbun.lockbbin210299 -> 213547 bytes
-rw-r--r--index.js3
-rw-r--r--lib/ecash.js42
-rw-r--r--lib/migrate.js4
-rw-r--r--lib/payments.js10
-rw-r--r--package.json1
-rw-r--r--routes/ecash.js18
7 files changed, 77 insertions, 1 deletions
diff --git a/bun.lockb b/bun.lockb
index 4fb794a2..7668cf5a 100755
--- a/bun.lockb
+++ b/bun.lockb
Binary files differ
diff --git a/index.js b/index.js
index c5190c7d..04a6e228 100644
--- a/index.js
+++ b/index.js
@@ -9,6 +9,7 @@ import { getRates } from "$lib/rates";
import { sendHeartbeat } from "$lib/sockets";
import { err } from "$lib/logging";
+import ecash from "$routes/ecash";
import email from "$routes/email";
import info from "$routes/info";
import locations from "$routes/locations";
@@ -117,6 +118,8 @@ app.post("/shopify/:id", shopify);
app.post("/hidepay", admin, users.hidepay);
app.post("/unlimit", admin, users.unlimit);
+app.post("/mint", auth, ecash.mint);
+
let host = process.env.HOST || "0.0.0.0";
let port = process.env.PORT || 3119;
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);
+}
diff --git a/lib/migrate.js b/lib/migrate.js
index 15ce4a16..cee5e946 100644
--- a/lib/migrate.js
+++ b/lib/migrate.js
@@ -5,6 +5,7 @@ import { wait } from "$lib/utils";
let queued = {};
let migrating = {};
+
async function migrate(id) {
try {
delete queued[id];
@@ -12,6 +13,7 @@ async function migrate(id) {
let user = await g(`user:${id}`);
if (typeof user === "string") user = await g(`user:${user}`);
+
if (user) return user;
user = await ga(`user:${id}`);
@@ -92,7 +94,7 @@ async function migrate(id) {
while ((iid = await archive.rPop(`${id}:invoices`))) {
let inv = await ga(`invoice:${iid}`);
let pos = await db.lPos(`${id}:invoices`, iid);
- if (!inv || pos) continue;
+ if (!inv || pos) continue;
await db.lPush(`${id}:invoices`, iid);
await s(`invoice:${iid}`, inv);
diff --git a/lib/payments.js b/lib/payments.js
index d3250778..c7fd41b0 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -12,6 +12,7 @@ import {
fail,
fiat,
getInvoice,
+ getUser,
sleep,
wait,
SATS,
@@ -27,9 +28,15 @@ import api from "$lib/api";
import lq from "$lib/liquid";
import bc from "$lib/bitcoin";
import ln from "$lib/ln";
+import { balance, melt, mint } from "$lib/ecash";
let { URL } = process.env;
+setTimeout(
+ async () => console.log("bob", await balance(await getUser("bob"))),
+ 1000,
+);
+
export let types = {
internal: "internal",
bitcoin: "bitcoin",
@@ -83,6 +90,7 @@ export let debit = async ({
? Math.round((amount + fee + tip) * config.fee)
: 0;
+ await melt(user, amount + fee + tip + ourfee);
ourfee = await db.debit(
`balance:${uid}`,
`credit:${type}:${uid}`,
@@ -180,6 +188,8 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
inv.preimage = ref;
}
+ await mint(user, amount);
+
let balanceKey = "balance";
if ([types.bitcoin, types.liquid].includes(type)) {
let [txid, vout] = ref.split(":").slice(-2);
diff --git a/package.json b/package.json
index c30aac0a..62756470 100644
--- a/package.json
+++ b/package.json
@@ -9,6 +9,7 @@
},
"dependencies": {
"@aws-sdk/client-ses": "^3.449.0",
+ "@cashu/cashu-ts": "^1.0.1",
"@fastify/cors": "^8.3.0",
"@fastify/multipart": "^8.0.0",
"@fastify/passport": "^2.4.0",
diff --git a/routes/ecash.js b/routes/ecash.js
new file mode 100644
index 00000000..6ca570bf
--- /dev/null
+++ b/routes/ecash.js
@@ -0,0 +1,18 @@
+import { CashuMint, CashuWallet, MintQuoteState } from '@cashu/cashu-ts';
+
+export default {
+ async mint({ body }, res) {
+ const mintUrl = 'http://mint:3338'; // the mint URL
+ const mint = new CashuMint(mintUrl);
+ const wallet = new CashuWallet(mint);
+ const mintQuote = await wallet.createMintQuote(64);
+ console.log(mintQuote)
+ // pay the invoice here before you continue...
+ const mintQuoteChecked = await wallet.checkMintQuote(mintQuote.quote);
+ console.log(mintQuoteChecked)
+ if (mintQuoteChecked.state == MintQuoteState.PAID) {
+ const { proofs } = await wallet.mintTokens(64, mintQuote.quote);
+ console.log(proofs)
+ }
+ }
+}