aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-10-24 13:59:34 -0700
committerAdam Soltys <[email protected]>2024-10-24 13:59:34 -0700
commitd09712ef8f8ea8218ce229b233fc0013e6c06f7a (patch)
treeec0b548baef30c0b95b068ec8117672a49ccdd54
parentac161f60e08e7e32c46d98aa3288f4f1b3d6e2d8 (diff)
downloadcoinos-server-d09712ef8f8ea8218ce229b233fc0013e6c06f7a.tar.gz
coinos-server-d09712ef8f8ea8218ce229b233fc0013e6c06f7a.zip
save ecash id
-rw-r--r--lib/ecash.ts77
-rw-r--r--lib/ln.ts8
-rw-r--r--routes/ecash.ts61
-rw-r--r--routes/invoices.ts6
4 files changed, 87 insertions, 65 deletions
diff --git a/lib/ecash.ts b/lib/ecash.ts
index 8f91e70c..d9d6728e 100644
--- a/lib/ecash.ts
+++ b/lib/ecash.ts
@@ -1,67 +1,92 @@
import config from "$config";
+import { g, s } from "$lib/db";
+import { lnb } from "$lib/ln";
+import { fail, wait } from "$lib/utils";
import {
CashuMint,
CashuWallet,
+ MintQuoteState,
getDecodedToken,
getEncodedToken,
getEncodedTokenV4,
} from "@cashu/cashu-ts";
-import { g, s } from "$lib/db";
-import { fail } from "$lib/utils";
-let m = new CashuMint(config.mintUrl);
-let w = new CashuWallet(m);
+const m = new CashuMint(config.mintUrl);
+const w = new CashuWallet(m);
-let enc = (proofs, v = 4) =>
+const enc = (proofs, v = 4) =>
(v === 4 ? getEncodedTokenV4 : getEncodedToken)({
token: [{ mint: config.mintUrl, proofs }],
});
-let dec = (token) => getDecodedToken(token).token[0];
+const 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();
+const ext = async (mint) => {
+ const issuer = new CashuMint(mint);
+ const { pubkey: issuerPk } = await issuer.getInfo();
+ const { pubkey: ourPk } = await m.getInfo();
return issuerPk !== ourPk;
};
export async function get(id, v = 4) {
- let token = await g(`cash:${id}`);
+ const token = await g(`cash:${id}`);
if (v < 4) return enc(dec(token).proofs, 3);
return token;
}
export async function claim(token) {
- let { proofs: current } = dec(await g(`cash`));
- let { mint } = dec(token);
+ const { proofs: current } = dec(await g("cash"));
+ const { mint } = dec(token);
if (await ext(mint)) fail("Unable to receive from other mints");
- let rcvd = await w.receive(token);
+ const rcvd = await w.receive(token);
- await s(`cash`, enc([...current, ...rcvd]));
+ await s("cash", enc([...current, ...rcvd]));
return rcvd.reduce((a, b) => a + b.amount, 0);
}
export async function mint(amount, v = 4) {
- let { proofs } = dec(await g(`cash`));
- let { send, returnChange } = await w.send(amount, proofs);
- let rcvd = await w.receive(enc(send, v));
- let change = enc(returnChange, v);
- await s(`cash`, change);
+ const { proofs } = dec(await g("cash"));
+ const { send, returnChange } = await w.send(amount, proofs);
+ const rcvd = await w.receive(enc(send, v));
+ const change = enc(returnChange, v);
+ await s("cash", change);
return enc(rcvd, v);
}
export async function check(token) {
- let o = dec(token);
- let { mint, proofs } = o;
- let total = proofs.reduce((a, b) => a + b.amount, 0);
+ const o = dec(token);
+ const { mint, proofs } = o;
+ const total = proofs.reduce((a, b) => a + b.amount, 0);
- let external = await ext(mint);
+ const external = await ext(mint);
- let r = await w.checkProofsSpent(proofs);
- let spent = r.reduce((a, b) => a + b.amount, 0);
+ const r = await w.checkProofsSpent(proofs);
+ const spent = r.reduce((a, b) => a + b.amount, 0);
return { total, spent, mint, external };
}
+
+export async function init(amount = 10000) {
+ try {
+ await new Promise((r) => setTimeout(r, 2000));
+ const { quote, request } = await w.createMintQuote(amount);
+ await lnb.pay(request);
+
+ await wait(async () => {
+ const { state } = await w.checkMintQuote(quote);
+ return state === MintQuoteState.PAID;
+ });
+
+ const { proofs } = await w.mintTokens(amount, quote);
+
+ const cash = getEncodedTokenV4({
+ token: [{ mint: config.mintUrl, proofs }],
+ });
+
+ await s("cash", cash);
+ } catch (e) {
+ console.log(e);
+ }
+}
diff --git a/lib/ln.ts b/lib/ln.ts
index c4445b26..fcc6adce 100644
--- a/lib/ln.ts
+++ b/lib/ln.ts
@@ -1,7 +1,5 @@
import config from "$config";
-let ln;
-let { LightningClient } = (await import("clightning-client")).default;
-ln = new LightningClient(config.lightning);
-
-export default ln;
+const { LightningClient } = (await import("clightning-client")).default;
+export default new LightningClient(config.lightning);
+export const lnb = new LightningClient(config.lightningb);
diff --git a/routes/ecash.ts b/routes/ecash.ts
index 48b03fd3..cc617138 100644
--- a/routes/ecash.ts
+++ b/routes/ecash.ts
@@ -1,20 +1,20 @@
-import { g, s, db } from "$lib/db";
-import { get, claim, mint, check } from "$lib/ecash";
+import { db, g, s } from "$lib/db";
+import { check, claim, get, mint } from "$lib/ecash";
+import { err, l } from "$lib/logging";
+import { credit, debit, types } from "$lib/payments";
+import { emit } from "$lib/sockets";
import { bail, fail } from "$lib/utils";
-import { debit, credit, types } from "$lib/payments";
import { v4 } from "uuid";
-import { l, err } from "$lib/logging";
-import { emit } from "$lib/sockets";
-let { ecash: type } = types;
+const { ecash: type } = types;
export default {
async save(req, res) {
- let {
+ const {
body: { token },
} = req;
try {
- let id = v4();
+ const id = v4();
await s(`cash:${id}`, token);
res.send({ id });
} catch (e) {
@@ -24,12 +24,12 @@ export default {
},
async get(req, res) {
- let {
+ const {
params: { id, version },
} = req;
try {
- let token = await get(id, version);
- let status = await check(token);
+ const token = await get(id, version);
+ const status = await check(token);
res.send({ token, status });
} catch (e) {
err(e.message);
@@ -38,17 +38,17 @@ export default {
},
async claim(req, res) {
- let {
+ const {
body: { token },
user,
} = req;
try {
- let amount = await claim(token);
+ const amount = await claim(token);
let memo;
- let hash = v4();
- let { currency, id: uid } = user;
- let rates = await g("rates");
+ const hash = v4();
+ const { currency, id: uid } = user;
+ const rates = await g("rates");
await s(`invoice:${hash}`, {
currency,
id: hash,
@@ -68,18 +68,17 @@ export default {
},
async mint(req, res) {
- let {
+ const {
body: { amount },
user,
} = req;
try {
- let id = v4();
- let hash = v4();
+ const id = v4();
+ const hash = v4();
- let memo, rate;
- let p = await debit({ hash, amount, user, type, memo, rate });
- let token = await mint(amount);
- p.memo = token;
+ const p = await debit({ hash, amount, user, type });
+ const token = await mint(amount);
+ p.memo = id;
await s(`payment:${p.id}`, p);
s(`cash:${id}`, token);
@@ -97,11 +96,11 @@ export default {
} = req;
try {
amount = Math.round(amount / 1000);
- let ref = preimage;
- let { lightning: type } = types;
+ const ref = preimage;
+ const { lightning: type } = types;
if (user.username !== "mint") fail("unauthorized");
- let { id: uid, currency } = user;
- let ourfee = await db.debit(
+ const { id: uid, currency } = user;
+ const ourfee = await db.debit(
`balance:${uid}`,
`credit:${type}:${uid}`,
amount || 0,
@@ -110,13 +109,13 @@ export default {
0,
);
- let rates = await g("rates");
- let rate = rates[currency];
+ const rates = await g("rates");
+ const rate = rates[currency];
if (ourfee.err) fail(ourfee.err);
- let id = v4();
- let p = {
+ const id = v4();
+ const p = {
id,
amount: -amount,
hash,
diff --git a/routes/invoices.ts b/routes/invoices.ts
index acad5ce7..c571f4de 100644
--- a/routes/invoices.ts
+++ b/routes/invoices.ts
@@ -1,11 +1,11 @@
import { g } from "$lib/db";
import { generate } from "$lib/invoices";
-import { bail, pick } from "$lib/utils";
import { err } from "$lib/logging";
+import { bail, pick } from "$lib/utils";
export default {
async get(req, res) {
- let {
+ const {
params: { id },
} = req;
let invoice = await g(`invoice:${id}`);
@@ -31,7 +31,7 @@ export default {
async create(req, res) {
let { body, user } = req;
if (body.user) user = body.user;
- let { invoice } = body;
+ const { invoice } = body;
try {
res.send(await generate({ invoice, user }));