diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ecash.js | 42 | ||||
| -rw-r--r-- | lib/migrate.js | 4 | ||||
| -rw-r--r-- | lib/payments.js | 10 |
3 files changed, 55 insertions, 1 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); +} 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); |
