aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-18 23:14:48 -0800
committerAdam Soltys <[email protected]>2023-01-18 23:14:48 -0800
commitef3679051214a9b3047b68f236eac656edcf8d38 (patch)
tree426eda7cf7e36026c60da2ae28ce7c61804518f8 /routes
parent90ddef9f0dd93e8f980eb728cf0519d9215163f0 (diff)
downloadcoinos-server-ef3679051214a9b3047b68f236eac656edcf8d38.tar.gz
coinos-server-ef3679051214a9b3047b68f236eac656edcf8d38.zip
bitcoin invoices
Diffstat (limited to 'routes')
-rw-r--r--routes/invoices.js38
-rw-r--r--routes/payments.js7
2 files changed, 30 insertions, 15 deletions
diff --git a/routes/invoices.js b/routes/invoices.js
index acd9457e..1287de88 100644
--- a/routes/invoices.js
+++ b/routes/invoices.js
@@ -2,6 +2,8 @@ import store from "$lib/store";
import { l } from "$lib/logging";
import { emit } from "$lib/sockets";
import { db, g, s } from "$lib/db";
+import { bip21, fail } from "$lib/utils";
+import bc from "$lib/bitcoin";
import ln from "$lib/ln";
export default {
@@ -12,49 +14,57 @@ export default {
},
async create({ body: { invoice, user }, user: me }, res) {
- let { currency, tip, amount, rate, request_id } = invoice;
+ let { currency, tip, amount, rate, request_id, type } = invoice;
- if (amount < 0) throw new Error("amount out of range");
- if (tip > 5 * amount || tip < 0) throw new Error("tip amount out of range");
+ if (amount < 0) fail("amount out of range");
+ if (tip > 5 * amount || tip < 0) fail("tip amount out of range");
tip = tip || 0;
if (!user) user = me;
let uid = await g(`user:${user.username}`);
user = await g(`user:${uid}`);
- if (!user) throw new Error("user not provided");
+ if (!user) fail("user not provided");
if (!currency) currency = user.currency;
if (!rate) rate = store.rates[currency];
- if (amount < 0) throw new Error("invalid amount");
+ if (amount < 0) fail("invalid amount");
- let { payment_hash: hash, bolt11: text } = await ln.invoice(
- amount ? `${amount + tip}sat` : "any",
- new Date(),
- "",
- 3600
- );
+ let hash, text;
+ if (type === "lightning") {
+ let amt = amount ? `${amount + tip}sat` : "any";
+ let r = await ln.invoice(amt, new Date(), "", 3600);
+
+ hash = r.payment_hash;
+ text = r.bolt11;
+ } else if (type === "bitcoin") {
+ hash = await bc.getNewAddress();
+ text = bip21(hash, invoice);
+ } else {
+ fail("unrecognized type");
+ }
invoice = {
...invoice,
- hash,
amount,
+ hash,
+ text,
currency,
rate,
tip,
uid,
- text,
received: 0,
created: Date.now()
};
+
l(
"creating invoice",
user.username,
amount,
tip,
currency,
- `${text.substr(0, 8)}..${text.substr(-6)}`
+ invoice.hash
);
await s(`invoice:${hash}`, invoice);
diff --git a/routes/payments.js b/routes/payments.js
index ba701bef..fc506685 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -265,7 +265,7 @@ export default {
},
async withdraw({ body: { name, amount }, user }, res) {
- amount = parseInt(amount)
+ amount = parseInt(amount);
await t(`pot:${name}`, async balance => {
await new Promise(r => setTimeout(r, 100));
if (balance < amount) fail("Insufficient funds");
@@ -282,5 +282,10 @@ export default {
await db.lPush(`pot:${name}:payments`, hash);
res.send({ payment });
+ },
+
+ async bitcoin({ body }, res) {
+ console.log(body);
+ res.send({});
}
};