aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--index.js2
-rw-r--r--lib/invoices.js14
-rw-r--r--lib/lightning.js3
-rw-r--r--lib/payments.js9
-rw-r--r--routes/invoices.js5
-rw-r--r--routes/payments.js25
6 files changed, 36 insertions, 22 deletions
diff --git a/index.js b/index.js
index 67164b6d..f61d3f20 100644
--- a/index.js
+++ b/index.js
@@ -45,7 +45,7 @@ app.post("/event", nostr.broadcast);
app.get("/locations", locations.list);
-app.get("/invoice/:hash", invoices.get);
+app.get("/invoice/:id", invoices.get);
app.post("/invoice", optional, invoices.create);
app.get("/invoice/classic/:username", invoices.classic);
diff --git a/lib/invoices.js b/lib/invoices.js
index 0bb57714..8ac49a79 100644
--- a/lib/invoices.js
+++ b/lib/invoices.js
@@ -36,6 +36,8 @@ export let generate = async ({ invoice, user, sender, memo }) => {
if (fiat) amount = Math.round((SATS * fiat) / rate);
if (amount < 0) fail("invalid amount");
+ let id = v4();
+
let hash, text;
if (type === types.lightning) {
let r = await ln.invoice({
@@ -52,7 +54,7 @@ export let generate = async ({ invoice, user, sender, memo }) => {
hash = await bc.getNewAddress();
text = bip21(hash, invoice);
} else if (type === types.internal) {
- hash = v4();
+ hash = id;
} else {
fail("unrecognized type");
}
@@ -62,6 +64,7 @@ export let generate = async ({ invoice, user, sender, memo }) => {
created: Date.now(),
currency,
hash,
+ id,
rate,
pending: 0,
received: 0,
@@ -75,16 +78,17 @@ export let generate = async ({ invoice, user, sender, memo }) => {
webhook
};
- l("creating invoice", user.username, amount, tip, currency, invoice.hash);
+ l("creating invoice", id, user.username, amount, tip, currency, invoice.hash);
- await s(`invoice:${hash}`, invoice);
- await db.lPush(`${user.id}:invoices`, hash);
+ await s(`invoice:${hash}`, id);
+ await s(`invoice:${id}`, invoice);
+ await db.lPush(`${user.id}:invoices`, id);
if (request_id) {
let request = await g(`request:${request_id}`);
if (request) {
let { invoice_id: prev } = request;
- request.invoice_id = hash;
+ request.invoice_id = id;
await s(`request:${request_id}`, request);
if (!prev) emit(request.requester_id, "invoice", invoice);
diff --git a/lib/lightning.js b/lib/lightning.js
index 0c5fd75e..1788fd12 100644
--- a/lib/lightning.js
+++ b/lib/lightning.js
@@ -27,7 +27,8 @@ export async function listenForLightning() {
if (!preimage) return;
- let invoice = await g(`invoice:${bolt11}`);
+ let id = await g(`invoice:${bolt11}`);
+ let invoice = await g(`invoice:${id}`);
if (!invoice) return warn("received lightning with no invoice", bolt11);
let payment = await g(`payment:${bolt11}`);
diff --git a/lib/payments.js b/lib/payments.js
index 19d044e2..733e04ce 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -38,7 +38,8 @@ export let debit = async (
let rate = store.rates[currency];
- let invoice = await g(`invoice:${hash}`);
+ let iid = await g(`invoice:${hash}`);
+ let invoice = await g(`invoice:${iid}`);
if (invoice) {
ref = invoice.uid;
@@ -91,6 +92,7 @@ export let debit = async (
hash,
ourfee,
memo,
+ iid,
uid,
confirmed: true,
rate,
@@ -113,7 +115,8 @@ export let debit = async (
export let credit = async (hash, amount, memo, ref, type = types.internal) => {
amount = parseInt(amount) || 0;
- let invoice = await g(`invoice:${hash}`);
+ let iid = await g(`invoice:${hash}`);
+ let invoice = await g(`invoice:${iid}`);
if (!invoice) return warn("failed to credit", hash, ", invoice not found");
let { tip } = invoice;
@@ -170,7 +173,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
if ([types.bitcoin, types.lightning].includes(type))
m.incrBy(`credit:${type}:${uid}`, Math.round(amount * config.fee));
- m.set(`invoice:${hash}`, JSON.stringify(invoice))
+ m.set(`invoice:${iid}`, JSON.stringify(invoice))
.set(`payment:${p.id}`, JSON.stringify(p))
.lPush(`${uid}:payments`, p.id)
.incrBy(`${balance}:${uid}`, amount)
diff --git a/routes/invoices.js b/routes/invoices.js
index 9f2e4c99..d1ef1a68 100644
--- a/routes/invoices.js
+++ b/routes/invoices.js
@@ -7,14 +7,13 @@ import config from "$config";
import ln from "$lib/ln";
export default {
- async get({ params: { hash } }, res) {
+ async get({ params: { id } }, res) {
let pr;
- let invoice = await g(`invoice:${hash}`);
+ let invoice = await g(`invoice:${id}`);
if (invoice) {
delete invoice.secret;
invoice.user = pick(await g(`user:${invoice.uid}`), ['id', 'profile', 'banner', 'currency', 'username']);
- invoice.id = hash;
} else if (config.classic) {
invoice = await got(`${config.classic}/invoice/${pr}`).json();
if (invoice) {
diff --git a/routes/payments.js b/routes/payments.js
index f83a9175..87096bdd 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -61,7 +61,8 @@ export default {
let total = amount;
let { amount_msat, payment_hash } = await ln.decode(payreq);
if (amount_msat) total = Math.round(amount_msat / 1000);
- let invoice = await g(`invoice:${payreq}`);
+ let iid = await g(`invoice:${payreq}`);
+ let invoice = await g(`invoice:${iid}`);
if (invoice) {
if (invoice.uid === user.id) fail("Cannot send to self");
@@ -270,7 +271,7 @@ export default {
});
},
- async take({ body: { name, amount, hash }, user }, res) {
+ async take({ body: { name, amount, id }, user }, res) {
try {
amount = parseInt(amount);
if (amount < 0) fail("Invalid amount");
@@ -283,11 +284,13 @@ export default {
.exec();
});
- if (!hash) {
- hash = v4();
+ if (!id) {
+ id = v4();
let { currency } = user;
- await s(`invoice:${hash}`, {
+ await s(`invoice:${id}`, {
currency,
+ id,
+ hash: id,
rate: store.rates[currency],
uid: user.id,
received: 0
@@ -310,7 +313,7 @@ export default {
for (let { address, amount, category, vout } of details) {
if (category !== "receive") continue;
let p = await g(`payment:${txid}:${vout}`);
- if (!p || confirmations < 1) {
+ if (!p) {
await credit(
address,
sats(amount),
@@ -318,7 +321,7 @@ export default {
`${txid}:${vout}`,
types.bitcoin
);
- } else {
+ } else if(confirmations >= 1) {
await confirm(address, txid, vout);
}
}
@@ -400,9 +403,10 @@ export default {
value
} of tx.vout) {
total += sats(value);
+ let iid = await g(`invoice:${address}`);
if (
(await bc.getAddressInfo(address)).ismine &&
- !(await g(`invoice:${address}`))
+ !iid
)
change += sats(value);
}
@@ -459,9 +463,12 @@ export default {
let { status } = r;
if (status === "succeeded") {
+ let id = v4();
let hash = r.id;
let memo = "stripe";
- await s(`invoice:${hash}`, {
+ await s(`invoice:${id}`, {
+ id,
+ hash: id,
uid: user.id,
received: 0
});