aboutsummaryrefslogtreecommitdiffstats
path: root/db.js
diff options
context:
space:
mode:
Diffstat (limited to 'db.js')
0 files changed, 0 insertions, 0 deletions
erver.git/commit/routes/lnurl.js?id=692315c0d4259a274322ca579a7b6118d033bf7a'>692315c0 ^
adf35d73 ^
692315c0 ^
4c9987e1 ^
766864b4 ^
4c9987e1 ^
4c9987e1 ^

692315c0 ^

4efa82b3 ^



692315c0 ^

753147ba ^
dc389137 ^



753147ba ^


dc389137 ^

692315c0 ^



4efa82b3 ^



f695cbd6 ^

b67ca07b ^
f695cbd6 ^
692315c0 ^
f695cbd6 ^



692315c0 ^

4efa82b3 ^

1c184f2a ^
57fa1382 ^
4efa82b3 ^
924c8370 ^
766864b4 ^
924c8370 ^


b67ca07b ^
924c8370 ^



4c9987e1 ^
924c8370 ^
a7322928 ^
1c184f2a ^

924c8370 ^
766864b4 ^
924c8370 ^
b67ca07b ^
924c8370 ^

38ea39b1 ^
924c8370 ^

692315c0 ^

4efa82b3 ^




7a84d255 ^






62873c94 ^
7a84d255 ^
62873c94 ^
0b7f6a88 ^
7a84d255 ^










a7322928 ^
a7322928 ^
0b7f6a88 ^
7a84d255 ^

57fa1382 ^
7a84d255 ^

7a84d255 ^

945efc15 ^
0b7f6a88 ^
7a84d255 ^


b67ca07b ^
0b7f6a88 ^
4efa82b3 ^



0b7f6a88 ^








692315c0 ^
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
                               
                                         
                                                 

                          
                                         
                                
                                      
                                           
 

                                 

                



                          

                                                             
 



                                                            


                                                              

     



                                                                              



                          

                            
                                                           
                   
 



                                      

    

                          
                           
                                                                
            
         
                                                


                                                     
                                                   



                                  
 
                
                          

                    
                 
                                   
                                           
                          

                 
                                                                     

                           

    




                               






                                                           
                                              
                                     
                             
                                                   










                                                            
       
 
                                                  

                                            
                         

                                

             
 
                                                                             


                           
    
 



                          








                                                                        
  
import { g, s } from "$lib/db";
import { err, warn } from "$lib/logging";
import { bail, getUser, fail } from "$lib/utils";
import { v4 } from "uuid";
import got from "got";
import { generate } from "$lib/invoices";
import { bech32 } from "bech32";
import { types } from "$lib/payments";
import { COINOS_PUBKEY } from "$lib/nostr";

let { URL } = process.env;
let host = URL.split("/").at(-1);

export default {
  async encode(req, res) {
    let {
      query: { address },
    } = req;
    let [name, domain] = address.split("@");
    let url = `https://${domain}/.well-known/lnurlp/${name}`;

    try {
      let r = await got(url).json();
      if (r.tag !== "payRequest") fail("not an ln address");
    } catch (e) {
      let m = `failed to lookup lightning address ${address}`;
      warn(m);
      return bail(res, m);
    }

    let enc = bech32.encode("lnurl", bech32.toWords(Buffer.from(url)), 20000);
    res.send(enc);
  },

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

      res.send(await got(url).json());
    } catch (e) {
      bail(res, e.message);
    }
  },

  async lnurlp(req, res) {
    let {
      params: { username },
      query: { minSendable = 1000, maxSendable = 100000000000 },
    } = req;
    try {
      let { id: uid } = await getUser(username);

      let metadata = JSON.stringify([
        ["text/plain", `Paying ${username}@${host}`],
        ["text/identifier", `${username}@${host}`],
      ]);

      let id = v4();
      await s(`lnurl:${id}`, uid);

      res.send({
        allowsNostr: true,
        minSendable,
        maxSendable,
        metadata,
        nostrPubkey: COINOS_PUBKEY,
        callback: `${URL}/api/lnurl/${id}`,
        tag: "payRequest",
      });
    } catch (e) {
      warn("problem generating lnurlp request", username, e.message);
      bail(res, e.message);
    }
  },

  async lnurl(req, res) {
    let {
      params: { id },
      query: { amount, nostr },
    } = req;
    try {
      let uid = await g(`lnurl:${id}`);
      let user = await getUser(uid);
      if (!user) fail("user not found");
      let { username } = user;
      username = username.replace(/\s/g, "").toLowerCase();

      let memo = `Paying ${username}@${host}`;
      let metadata = JSON.stringify([
        ["text/plain", memo],
        ["text/identifier", `${username}@${host}`],
      ]);

      if (nostr) {
        try {
          let event = JSON.parse(decodeURIComponent(nostr));
          // TODO: validate the event
          await s(`zap:${id}`, event);
          metadata = nostr;
        } catch (e) {
          err("problem handling zap", e.message);
        }
      }

      let { id: iid, text: pr } = await generate({
        invoice: {
          amount: Math.round(amount / 1000),
          memo: metadata,
          type: types.lightning,
        },
        user,
      });

      res.send({ pr, routes: [], verify: `${URL}/api/lnurl/verify/${iid}` });
    } catch (e) {
      bail(res, e.message);
    }
  },

  async verify(req, res) {
    let {
      params: { id },
    } = req;
    let inv = await g(`invoice:${id}`);

    if (!inv) return res.send({ status: "ERROR", reason: "Not found" });

    let { received, amount, preimage } = inv;
    let settled = received >= amount;

    res.send({ status: "OK", settled, preimage });
  },
};