diff options
| author | Adam Soltys <[email protected]> | 2020-06-17 11:04:18 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-06-17 11:04:18 -0700 |
| commit | 139000e710e7cd5f76bc272fb8940305dc6475f1 (patch) | |
| tree | b5dd5964eb5d5e4fc02d122da8d995d2ae9947e3 /routes | |
| parent | ed0da89c7d98195294ced6d71812183a04e24c76 (diff) | |
| download | coinos-server-139000e710e7cd5f76bc272fb8940305dc6475f1.tar.gz coinos-server-139000e710e7cd5f76bc272fb8940305dc6475f1.zip | |
lnurl channel flow
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/lightning/channel.js | 39 | ||||
| -rw-r--r-- | routes/lightning/channelRequest.js | 20 | ||||
| -rw-r--r-- | routes/lightning/decode.js | 15 | ||||
| -rw-r--r-- | routes/payments.js | 3 |
4 files changed, 77 insertions, 0 deletions
diff --git a/routes/lightning/channel.js b/routes/lightning/channel.js new file mode 100644 index 00000000..c5ab6a02 --- /dev/null +++ b/routes/lightning/channel.js @@ -0,0 +1,39 @@ +const axios = require("axios"); + +module.exports = async (req, res) => { + const { params } = req.body; + const [pubkey, host] = params.uri.split("@"); + + l.info("connecting to peer", req.user.username, pubkey, host); + let result; + try { + result = await lna.connectPeer({ + addr: { pubkey, host }, + perm: true + }); + } catch (e) { + if (!e.message.includes('already connected')) { + l.error("problem connecting to peer", e.message); + return res.status(500).send(e.message); + } + } + + const { callback, k1 } = params; + let remoteid; + try { + remoteid = (await lna.getInfo({})).identity_pubkey; + } catch(e) { + l.error("problem getting lightning node info", e.message); + return res.status(500).send(e.message); + } + + let url = `${callback}?k1=${k1}&remoteid=${remoteid}&private=0`; + + try { + let response = await axios.get(url); + res.send(response.data); + } catch(e) { + l.error("problem sending channel request", e.message); + return res.status(500).send({ status: "ERROR", reason: e.message }); + } +}; diff --git a/routes/lightning/channelRequest.js b/routes/lightning/channelRequest.js new file mode 100644 index 00000000..9ba74009 --- /dev/null +++ b/routes/lightning/channelRequest.js @@ -0,0 +1,20 @@ +module.exports = async (req, res) => { + let { localAmt, pushAmt } = req.body; + + pushAmt = 0; + + try { + if (localAmt < 20000) throw new Error("amount must be greater than 20000 SAT"); + if (localAmt > 1000000) throw new Error("amount must be <= 1000000"); + + const result = await lnurlServer.generateNewUrl("channelRequest", { + localAmt, + pushAmt + }); + + res.send(result); + } catch (e) { + l.error("problem generating channel request", e.message); + res.status(500).send(e.message); + } +}; diff --git a/routes/lightning/decode.js b/routes/lightning/decode.js new file mode 100644 index 00000000..b7dba120 --- /dev/null +++ b/routes/lightning/decode.js @@ -0,0 +1,15 @@ +const axios = require('axios'); +const lnurl = require('lnurl'); + +module.exports = async (req, res) => { + let { text } = req.query; + const decoded = lnurl.decode(text); + + try { + let result = await axios.get(decoded); + res.send(result.data); + } catch(e) { + l.error(e.message); + res.status(500).send(e.message); + } +}; diff --git a/routes/payments.js b/routes/payments.js index 8f731f78..1840c381 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -34,6 +34,9 @@ const { join } = require("path"); lnb = lnd(config.lnb); } + app.get("/lightning/decode", require("./lightning/decode")); + app.post("/lightning/channel", auth, require("./lightning/channel")); + app.post("/lightning/channelRequest", auth, require("./lightning/channelRequest")); app.post("/lightning/invoice", auth, require("./lightning/invoice")); app.post("/lightning/query", auth, require("./lightning/query")); app.post("/lightning/send", auth, require("./lightning/send")); |
