diff options
| author | Adam Soltys <[email protected]> | 2023-02-22 18:08:27 +0000 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-02-22 18:08:35 +0000 |
| commit | dc38913797c587499926ed292d296dcbf44ee627 (patch) | |
| tree | 8a06c4144f63fb91c16c28efce423534a46b2d74 | |
| parent | 94f8e7a27cdc5922919d85d8051d24ada1f06922 (diff) | |
| download | coinos-server-dc38913797c587499926ed292d296dcbf44ee627.tar.gz coinos-server-dc38913797c587499926ed292d296dcbf44ee627.zip | |
handle socket and lnurl lookup errors
| -rw-r--r-- | lib/sockets.js | 2 | ||||
| -rw-r--r-- | routes/lnurl.js | 26 |
2 files changed, 18 insertions, 10 deletions
diff --git a/lib/sockets.js b/lib/sockets.js index 11d8ea9e..458c77a9 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -130,6 +130,8 @@ export async function socketServer(app) { } }); + ws.on("error", console.log); + ws.on("close", function(code, reason) { try { const uid = users[id]; diff --git a/routes/lnurl.js b/routes/lnurl.js index c276d3cb..8d45d301 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -1,6 +1,6 @@ import { g, s } from "$lib/db"; import { l } from "$lib/logging"; -import { fail } from "$lib/utils"; +import { bail, fail } from "$lib/utils"; import { v4 } from "uuid"; import got from "got"; import { generate } from "$lib/invoices"; @@ -19,8 +19,14 @@ export default { async encode({ query: { address } }, res) { let [name, domain] = address.split("@"); let url = `https://${domain}/.well-known/lnurlp/${name}`; - let r = await got(url).json(); - if (r.tag !== "payRequest") fail("not an ln address"); + + try { + let r = await got(url).json(); + if (r.tag !== "payRequest") fail("not an ln address"); + } catch (e) { + return bail(res, "failed to lookup lightning address"); + } + let enc = bech32.encode("lnurl", bech32.toWords(Buffer.from(url)), 20000); res.send(enc); }, @@ -37,7 +43,7 @@ export default { let uid = await g(`user:${username.toLowerCase()}`); if (!uid) { let u = await got(`${classic}/admin/migrate/${username}?zero=true`, { - headers: { authorization: `Bearer ${admin}` } + headers: { authorization: `Bearer ${admin}` }, }).json(); if (!u) fail("user not found"); @@ -58,7 +64,7 @@ export default { let metadata = JSON.stringify([ ["text/plain", `Paying ${username}@${host}`], - ["text/identifier", `${username}@${host}`] + ["text/identifier", `${username}@${host}`], ]); let id = v4(); @@ -69,7 +75,7 @@ export default { maxSendable: 100000000000, metadata, callback: `${URL}/api/lnurl/${id}`, - tag: "payRequest" + tag: "payRequest", }); }, @@ -83,21 +89,21 @@ export default { let metadata = JSON.stringify([ ["text/plain", `Paying ${username}@${host}`], - ["text/identifier", `${username}@${host}`] + ["text/identifier", `${username}@${host}`], ]); ({ text: pr } = await generate({ invoice: { amount: Math.round(amount / 1000), - type: types.lightning + type: types.lightning, }, memo: metadata, - user + user, })); await s(`lnurlp:${id}`, pr); } res.send({ pr, routes: [] }); - } + }, }; |
