aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lnurl.js
blob: 634880331fe05575c3281c8f5e900daa5ad180b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { g, s } from "$lib/db";
import { l } from "$lib/logging";
import { fail } from "$lib/utils";
import { v4 } from "uuid";
import got from "got";
import { generate } from "$lib/invoices";
import { bech32 } from "bech32";

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");
    let enc = bech32.encode("lnurl", bech32.toWords(Buffer.from(url)), 20000);
    res.send(enc);
  },

  async decode({ query: { text } }, res) {
    let url = Buffer.from(
      bech32.fromWords(bech32.decode(text, 20000).words)
    ).toString();

    res.send(await got(url).json());
  },

  async lnurlp({ params: { username } }, res) {
    let uid = await g(`user:${username}`);
    if (!uid) fail("user not found");

    let id = v4();
    await s(`lnurl:${id}`, uid);
    let { URL } = process.env;
    let host = URL.split("/").at(-1);

    res.send({
      metadata: [
        ["text/plain", `Paying ${username}@${host}`],
        ["text/identifier", `${username}@${host}`]
      ],
      callback: `${URL}/lnurl/${id}`,
      tag: "payRequest"
    });
  },

  async lnurl({ params: { id }, query: { amount } }, res) {
    let pr = await g(`lnurlp:${id}`);

    if (!pr) {
      let uid = await g(`lnurl:${id}`);
      let user = await g(`user:${uid}`);

      ({ text: pr } = await generate({
        invoice: {
          amount,
          type: types.lightning
        },
        user
      }));

      await s(`lnurlp:${id}`, pr);
    }

    res.send({ pr });
  }
};