From 01e1686265def675445c0f74a4b92ec4ffd71492 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 20 Sep 2024 10:04:08 -0700 Subject: rename account id --- lib/invoices.ts | 14 +++++++------- lib/migrate.ts | 5 ++++- lib/payments.ts | 52 ++++++++++++++++++++++++++-------------------------- routes/invoices.ts | 1 + routes/payments.ts | 14 +++++++------- routes/users.ts | 6 ++++-- 6 files changed, 49 insertions(+), 43 deletions(-) diff --git a/lib/invoices.ts b/lib/invoices.ts index 0094c6d7..4626e257 100644 --- a/lib/invoices.ts +++ b/lib/invoices.ts @@ -13,7 +13,7 @@ let lq = rpc(config.liquid); export let generate = async ({ invoice, user }) => { let { bolt11, - account, + aid, currency, expiry, fiat, @@ -25,19 +25,18 @@ export let generate = async ({ invoice, user }) => { memo, memoPrompt, prompt, - type, + type = types.lightning, webhook, secret, } = invoice; + let account = await g(`account:${aid}`); amount = parseInt(amount || 0); tip = parseInt(tip) || null; if (user) user = await getUser(user.username); if (!user) fail("user not provided"); - if (account === user.id) account = undefined; - let rates = await g("rates"); if (!currency) currency = user.currency; if (!rate) rate = rates[currency]; @@ -51,8 +50,9 @@ export let generate = async ({ invoice, user }) => { let hash, text; - if (account) { - let node = rpc({ ...config.bitcoin, wallet: account }); + if (account.seed) { + type = "bitcoin"; + let node = rpc({ ...config[type], wallet: aid }); hash = await node.getNewAddress(); text = bip21(hash, invoice); } else if (type === types.lightning) { @@ -91,7 +91,7 @@ export let generate = async ({ invoice, user }) => { invoice = { amount, - account, + aid, created: Date.now(), currency, hash, diff --git a/lib/migrate.ts b/lib/migrate.ts index a289dee2..d26c8581 100644 --- a/lib/migrate.ts +++ b/lib/migrate.ts @@ -21,7 +21,10 @@ async function migrate(id) { let accounts = await db.lLen(`${user.id}:accounts`); if (!accounts) { - await db.set(`account:${user.id}`, JSON.stringify({ id: user.id, name: "Cash" })); + await db.set( + `account:${user.id}`, + JSON.stringify({ id: user.id, name: "Cash", type: types.ecash }), + ); await db.lPush(`${user.id}:accounts`, user.id); } return user; diff --git a/lib/payments.ts b/lib/payments.ts index 66e725c7..e2b44ea9 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -43,7 +43,7 @@ export let types = { }; export let debit = async ({ - account = undefined, + aid = undefined, hash, amount, fee = 0, @@ -93,11 +93,11 @@ export let debit = async ({ ? Math.round((amount + fee + tip) * config.fee) : 0; - if (account) ourfee = 0; + if (aid) ourfee = 0; ourfee = await db.debit( - `balance:${account || uid}`, - `credit:${type}:${account ? 0 : uid}`, + `balance:${aid || uid}`, + `credit:${type}:${aid ? 0 : uid}`, amount || 0, tip || 0, fee || 0, @@ -109,7 +109,7 @@ export let debit = async ({ let id = v4(); let p = { id, - account, + aid, amount: -amount, fee, hash, @@ -129,7 +129,7 @@ export let debit = async ({ await s(`payment:${hash}`, id); await s(`payment:${id}`, p); - await db.lPush(`${account || uid}:payments`, id); + await db.lPush(`${aid || uid}:payments`, id); l(user.username, "sent", type, amount); //emit(user.id, "payment", p); @@ -143,7 +143,7 @@ export let credit = async ({ memo = undefined, ref = undefined, type = types.internal, - account = undefined, + aid = undefined, }) => { amount = parseInt(amount) || 0; @@ -180,7 +180,7 @@ export let credit = async ({ let id = v4(); let p = { - account, + aid, id, iid: inv.id, hash, @@ -220,8 +220,8 @@ export let credit = async ({ m.set(`invoice:${inv.id}`, JSON.stringify(inv)) .set(`payment:${p.id}`, JSON.stringify(p)) - .lPush(`${account || uid}:payments`, p.id) - .incrBy(`${balanceKey}:${account || uid}`, amount) + .lPush(`${aid || uid}:payments`, p.id) + .incrBy(`${balanceKey}:${aid || uid}`, amount) .exec(); emit(uid, "payment", p); @@ -317,7 +317,7 @@ export let completePayment = async (p, user) => { } }; -let pay = async ({ account, amount, to, user }) => { +let pay = async ({ aid, amount, to, user }) => { amount = parseInt(amount) || 0; let lnurl, pr; if (to.includes("@") && to.includes(".")) { @@ -341,7 +341,7 @@ let pay = async ({ account, amount, to, user }) => { return pr ? await sendLightning({ user, pr, amount, maxfee }) - : await sendOnchain({ account, amount, address: to, user, subtract: true }); + : await sendOnchain({ aid, amount, address: to, user, subtract: true }); }; export let decode = async (hex) => { @@ -363,15 +363,15 @@ export let decode = async (hex) => { }; export let sendOnchain = async (params) => { - let { account, hex, rate, user, signed } = params; - if (!account) account = user.id; + let { aid, hex, rate, user, signed } = params; + if (!aid) aid = user.id; if (!hex) ({ hex } = await build(params)); let { tx, type } = await decode(hex); let node = - account === user.id + aid === user.id ? rpc(config[type]) - : rpc({ ...config[type], wallet: account }); + : rpc({ ...config[type], wallet: aid }); let { txid } = tx; try { @@ -428,7 +428,7 @@ export let sendOnchain = async (params) => { } of tx.vout) { total += sats(value); let invoice = await g(`invoice:${address}`); - if (invoice?.account === account) + if (invoice?.aid === aid) fail("Cannot send to internal address"); if ((await node.getAddressInfo(address)).ismine) { @@ -442,7 +442,7 @@ export let sendOnchain = async (params) => { let amount = total - change; let p = await debit({ - account, + aid, hash: txid, amount, fee, @@ -511,7 +511,7 @@ export let sendLightning = async ({ let invoice = await getInvoice(hash); if (invoice) { - if (invoice.account === user.id) fail("Cannot send to self"); + if (invoice.aid === user.id) fail("Cannot send to self"); hash = pr; } else { let r; @@ -634,7 +634,7 @@ let getAddressType = async (a) => { }; export let build = async ({ - account, + aid, amount, address, feeRate, @@ -642,11 +642,11 @@ export let build = async ({ user, }) => { let type = await getAddressType(address); - if (!account) account = user.id; + if (!aid) aid = user.id; let node = - account === user.id + aid === user.id ? rpc(config[type]) - : rpc({ ...config[type], wallet: account }); + : rpc({ ...config[type], wallet: aid }); amount = parseInt(amount); if (amount < 0) fail("invalid amount"); @@ -686,13 +686,13 @@ export let build = async ({ else throw e; } - let balance = await g(`balance:${account}`); + let balance = await g(`balance:${aid}`); let ourfee = Math.round(amount * config.fee); - let credit = await g(`credit:${type}:${account}`); + let credit = await g(`credit:${type}:${aid}`); let covered = Math.min(credit, ourfee); ourfee -= covered; - if (account) ourfee = 0; + if (aid) ourfee = 0; if (subtract || amount + fee + ourfee > balance) { if (amount <= fee + ourfee + dust) fail( diff --git a/routes/invoices.ts b/routes/invoices.ts index 4cbd9660..98a851ed 100644 --- a/routes/invoices.ts +++ b/routes/invoices.ts @@ -36,6 +36,7 @@ export default { try { res.send(await generate({ invoice, user })); } catch (e) { + console.log(e) err("problem generating invoice", e.message); bail(res, e.message); } diff --git a/routes/payments.ts b/routes/payments.ts index 2046cfb4..02056eeb 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -73,13 +73,13 @@ export default { async list(req, res) { let { user: { id }, - query: { account, start, end, limit, offset }, + query: { aid, start, end, limit, offset }, } = req; if (limit) limit = parseInt(limit); offset = parseInt(offset) || 0; - let payments = (await db.lRange(`${account || id}:payments`, 0, -1)) || []; + let payments = (await db.lRange(`${aid || id}:payments`, 0, -1)) || []; payments = ( await Promise.all( payments.map(async (id) => { @@ -282,15 +282,15 @@ export default { if (typeof p === "string") p = await g(`payment:${p}`); if (!p) { - let account; + let aid; let bal = await g(`balance:${wallet}`); - if (bal !== null) account = wallet; + if (bal !== null) aid = wallet; await credit({ hash: address, amount: sats(amount), ref: `${txid}:${vout}`, type, - account, + aid, }); } else if (confirmations >= 1) { let id = `payment:${txid}:${vout}`; @@ -312,8 +312,8 @@ export default { .multi() .set(`invoice:${iid}`, JSON.stringify(invoice)) .set(`payment:${p.id}`, JSON.stringify(p)) - .decrBy(`pending:${p.account || p.uid}`, p.amount) - .incrBy(`balance:${p.account || p.uid}`, p.amount) + .decrBy(`pending:${p.aid || p.uid}`, p.amount) + .incrBy(`balance:${p.aid || p.uid}`, p.amount) .exec(); emit(p.uid, "payment", p); diff --git a/routes/users.ts b/routes/users.ts index 6550d90f..9746f36a 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -619,13 +619,15 @@ export default { let id = v4(); let account = { id, seed, type }; - await bc.createWallet({ + let node = rpc(config[type]); + + await node.createWallet({ wallet_name: id, descriptors: true, disable_private_keys: true, }); - let node = rpc({ ...config.bitcoin, wallet: id }); + node = rpc({ ...config[type], wallet: id }); let descriptors = []; for (let i of [0, 1]) { -- cgit v1.2.3