aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-12-12 04:58:09 +0000
committerAdam Soltys <[email protected]>2023-12-12 04:58:12 +0000
commit63fe7c28cc433f211a13634a65b7d9ac17949311 (patch)
treea9c357646abf1c18761bf5f2184fe862696dfd20
parent21fef6cd20d1ca81200d443b75a63817b5fcd868 (diff)
downloadcoinos-server-63fe7c28cc433f211a13634a65b7d9ac17949311.tar.gz
coinos-server-63fe7c28cc433f211a13634a65b7d9ac17949311.zip
allow undefined amount for lightning payments
-rw-r--r--lib/payments.js23
-rw-r--r--routes/payments.js36
2 files changed, 31 insertions, 28 deletions
diff --git a/lib/payments.js b/lib/payments.js
index 14bdf7ae..f53fc2c4 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -5,7 +5,7 @@ import { v4 } from "uuid";
import { notify } from "$lib/notifications";
import { db, g, s, t } from "$lib/db";
import { l, err, warn } from "$lib/logging";
-import { fail, wait } from "$lib/utils";
+import { fail, wait, SATS } from "$lib/utils";
import { callWebhook } from "$lib/webhooks";
import dollarsToWords from "dollars-to-words";
import got from "got";
@@ -23,7 +23,7 @@ export const types = {
bitcoin: "bitcoin",
lightning: "lightning",
fund: "fund",
- classic: "classic"
+ classic: "classic",
};
export let debit = async (
@@ -91,7 +91,7 @@ export let debit = async (
type,
ref,
tip,
- created: Date.now()
+ created: Date.now(),
};
await s(`payment:${id}`, p);
@@ -150,7 +150,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
tip,
type,
confirmed: true,
- created: Date.now()
+ created: Date.now(),
};
if (type === types.bitcoin) invoice.pending += amount;
@@ -190,7 +190,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }).format(
p.amount
),
- link: `${process.env.URL}/payment/${p.id}`
+ link: `${process.env.URL}/payment/${p.id}`,
});
}
@@ -231,8 +231,10 @@ export let payLightning = async (user, payreq, amount, maxfee, memo) => {
let p;
let hash = payreq;
- amount = parseInt(amount);
- if (amount < 0) fail("Invalid amount");
+ if (typeof amount !== "undefined") {
+ amount = parseInt(amount);
+ if (amount < 0 || amount > SATS || isNaN(amount)) fail("Invalid amount");
+ }
maxfee = maxfee ? parseInt(maxfee) : 0;
if (maxfee < 0) fail("Max fee cannot be negative");
@@ -292,13 +294,13 @@ export let payLightning = async (user, payreq, amount, maxfee, memo) => {
let interval = setInterval(check, 5000);
try {
- l("paying lightning invoice", payreq);
+ l("paying lightning invoice", payreq.substr(-8), total, amount, maxfee);
r = await ln.pay({
bolt11: payreq.replace(/\s/g, "").toLowerCase(),
amount_msat: amount_msat ? undefined : amount * 1000,
maxfee: maxfee * 1000,
- retry_for: 5
+ retry_for: 5,
});
if (!(r.status === "complete" && r.payment_preimage))
@@ -306,8 +308,7 @@ export let payLightning = async (user, payreq, amount, maxfee, memo) => {
l("payment completed", p.id, r.payment_preimage);
- p.amount = -amount;
- p.tip = total - amount;
+ p.amount = -total;
p.fee = Math.round((r.amount_sent_msat - r.amount_msat) / 1000);
p.ref = r.payment_preimage;
diff --git a/routes/payments.js b/routes/payments.js
index 98c24b33..36972928 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -21,7 +21,7 @@ let catchUp = async () => {
try {
if (seen.includes(txid)) continue;
await got.post(`http://localhost:${process.env.PORT || 3119}/bitcoin`, {
- json: { txid, wallet: config.bitcoin.wallet }
+ json: { txid, wallet: config.bitcoin.wallet },
});
seen.push(txid);
@@ -42,8 +42,10 @@ export default {
try {
if (await g("freeze")) fail("Problem sending payment");
- amount = parseInt(amount);
- if (amount < 0) fail("Invalid amount");
+ if (typeof amount !== "undefined") {
+ amount = parseInt(amount);
+ if (amount < 0 || amount > SATS || isNaN(amount)) fail("Invalid amount");
+ }
await requirePin({ body, user });
@@ -57,7 +59,7 @@ export default {
await got
.post(`${config.classic}/admin/credit`, {
json: { username, amount, source },
- headers: { authorization: `Bearer ${config.admin}` }
+ headers: { authorization: `Bearer ${config.admin}` },
})
.json();
} else if (payreq) {
@@ -119,8 +121,8 @@ export default {
(((b.amount || 0) + (b.tip || 0) - (b.fee || 0) - (b.ourfee || 0)) *
b.rate) /
SATS
- ).toFixed(2)
- }
+ ).toFixed(2),
+ },
}),
{}
);
@@ -181,7 +183,7 @@ export default {
k1: name,
defaultDescription: `Withdraw from coinos fund ${name}`,
minWithdrawable: 0,
- maxWithdrawable
+ maxWithdrawable,
});
},
@@ -204,7 +206,7 @@ export default {
hash: iid,
rate: store.rates[currency],
uid: user.id,
- received: 0
+ received: 0,
});
}
@@ -272,7 +274,7 @@ export default {
let tx = await bc.fundRawTransaction(raw, {
feeRate,
subtractFeeFromOutputs,
- replaceable
+ replaceable,
});
let fee = sats(tx.fee);
@@ -315,7 +317,7 @@ export default {
for (let {
scriptPubKey: { address },
- value
+ value,
} of tx.vout) {
total += sats(value);
let iid = await g(`invoice:${address}`);
@@ -348,13 +350,13 @@ export default {
"card[number]": number,
"card[exp_month]": month,
"card[exp_year]": year,
- "card[cvc]": cvc
+ "card[cvc]": cvc,
};
let { id: source } = await got
.post(`${stripe}/tokens`, {
form,
- username
+ username,
})
.json();
@@ -363,13 +365,13 @@ export default {
amount,
currency,
source,
- description: "starter coupon"
+ description: "starter coupon",
};
let r = await got
.post(`${stripe}/charges`, {
form,
- username
+ username,
})
.json();
@@ -382,7 +384,7 @@ export default {
id,
hash: id,
uid: user.id,
- received: 0
+ received: 0,
});
amount = sats(amount / store.rates[currency]);
let uid = await g("user:coinos");
@@ -402,7 +404,7 @@ export default {
"estimatesmartfee",
"echo",
"getblockchaininfo",
- "getnetworkinfo"
+ "getnetworkinfo",
];
if (!whitelist.includes(method)) fail("unsupported method");
@@ -535,5 +537,5 @@ export default {
console.log(e);
bail(res, e.message);
}
- }
+ },
};