-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathlnurl.go
393 lines (357 loc) · 11.2 KB
/
lnurl.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/LightningTipBot/LightningTipBot/internal/lnbits"
lnurl "github.com/fiatjaf/go-lnurl"
log "github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode"
"github.com/tidwall/gjson"
tb "gopkg.in/tucnak/telebot.v2"
)
const (
lnurlReceiveInfoText = "👇 You can use this LNURL to receive payments."
lnurlResolvingUrlMessage = "🧮 Resolving address..."
lnurlGettingUserMessage = "🧮 Preparing payment..."
lnurlPaymentFailed = "🚫 Payment failed: %s"
lnurlInvalidAmountMessage = "🚫 Invalid amount."
lnurlInvalidAmountRangeMessage = "🚫 Amount must be between %d and %d sat."
lnurlNoUsernameMessage = "🚫 You need to set a Telegram username to receive via LNURL."
lnurlEnterAmountMessage = "⌨️ Enter an amount between %d and %d sat."
lnurlHelpText = "📖 Oops, that didn't work. %s\n\n" +
"*Usage:* `/lnurl [amount] <lnurl>`\n" +
"*Example:* `/lnurl LNURL1DP68GUR...`"
)
// lnurlHandler is invoked on /lnurl command
func (bot TipBot) lnurlHandler(m *tb.Message) {
// commands:
// /lnurl
// /lnurl <LNURL>
// or /lnurl <amount> <LNURL>
log.Infof("[lnurlHandler] %s", m.Text)
// if only /lnurl is entered, show the lnurl of the user
if m.Text == "/lnurl" {
bot.lnurlReceiveHandler(m)
return
}
// assume payment
// HandleLNURL by fiatjaf/go-lnurl
msg := bot.trySendMessage(m.Sender, lnurlResolvingUrlMessage)
_, params, err := HandleLNURL(m.Text)
if err != nil {
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, "could not resolve LNURL."))
log.Errorln(err)
return
}
var payParams LnurlStateResponse
switch params.(type) {
case lnurl.LNURLPayResponse1:
payParams = LnurlStateResponse{LNURLPayResponse1: params.(lnurl.LNURLPayResponse1)}
log.Infof("[lnurlHandler] %s", payParams.Callback)
default:
err := fmt.Errorf("invalid LNURL type.")
log.Errorln(err)
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, err))
// bot.trySendMessage(m.Sender, err.Error())
return
}
user, err := GetUser(m.Sender, bot)
if err != nil {
log.Errorln(err)
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, "database error."))
return
}
// if no amount is in the command, ask for it
amount, err := decodeAmountFromCommand(m.Text)
if err != nil || amount < 1 {
// set LNURLPayResponse1 in the state of the user
paramsJson, err := json.Marshal(payParams)
if err != nil {
log.Errorln(err)
return
}
SetUserState(user, bot, lnbits.UserStateLNURLEnterAmount, string(paramsJson))
bot.tryDeleteMessage(msg)
// Let the user enter an amount and return
bot.trySendMessage(m.Sender, fmt.Sprintf(lnurlEnterAmountMessage, payParams.MinSendable/1000, payParams.MaxSendable/1000), tb.ForceReply)
} else {
// amount is already present in the command
// set also amount in the state of the user
// todo: this is repeated code, could be shorter
payParams.Amount = amount
paramsJson, err := json.Marshal(payParams)
if err != nil {
log.Errorln(err)
// bot.trySendMessage(m.Sender, err.Error())
return
}
SetUserState(user, bot, lnbits.UserStateConfirmLNURLPay, string(paramsJson))
bot.tryDeleteMessage(msg)
// directly go to confirm
bot.lnurlPayHandler(m)
}
}
func (bot *TipBot) UserGetLightningAddress(user *tb.User) (string, error) {
if len(user.Username) > 0 {
return fmt.Sprintf("%s@%s", strings.ToLower(user.Username), strings.ToLower(Configuration.Bot.LNURLHostUrl.Hostname())), nil
} else {
return "", fmt.Errorf("user has no username")
}
}
func (bot *TipBot) UserGetLNURL(user *tb.User) (string, error) {
name := strings.ToLower(strings.ToLower(user.Username))
if len(name) == 0 {
return "", fmt.Errorf("user has no username.")
}
callback := fmt.Sprintf("%s/.well-known/lnurlp/%s", Configuration.Bot.LNURLHostName, name)
log.Debugf("[lnurlReceiveHandler] %s's LNURL: %s", GetUserStr(user), callback)
lnurlEncode, err := lnurl.LNURLEncode(callback)
if err != nil {
return "", err
}
return lnurlEncode, nil
}
// lnurlReceiveHandler outputs the LNURL of the user
func (bot TipBot) lnurlReceiveHandler(m *tb.Message) {
lnurlEncode, err := bot.UserGetLNURL(m.Sender)
if err != nil {
errmsg := fmt.Sprintf("[lnurlReceiveHandler] Failed to get LNURL: %s", err)
log.Errorln(errmsg)
bot.telegram.Send(m.Sender, lnurlNoUsernameMessage)
}
// create qr code
qr, err := qrcode.Encode(lnurlEncode, qrcode.Medium, 256)
if err != nil {
errmsg := fmt.Sprintf("[lnurlReceiveHandler] Failed to create QR code for LNURL: %s", err)
log.Errorln(errmsg)
return
}
bot.trySendMessage(m.Sender, lnurlReceiveInfoText)
// send the lnurl data to user
bot.trySendMessage(m.Sender, &tb.Photo{File: tb.File{FileReader: bytes.NewReader(qr)}, Caption: fmt.Sprintf("`%s`", lnurlEncode)})
}
func (bot TipBot) lnurlEnterAmountHandler(m *tb.Message) {
user, err := GetUser(m.Sender, bot)
if err != nil {
log.Errorln(err)
// bot.trySendMessage(m.Sender, err.Error())
ResetUserState(user, bot)
return
}
if user.StateKey == lnbits.UserStateLNURLEnterAmount {
a, err := strconv.Atoi(m.Text)
if err != nil {
log.Errorln(err)
bot.trySendMessage(m.Sender, lnurlInvalidAmountMessage)
ResetUserState(user, bot)
return
}
amount := int64(a)
var stateResponse LnurlStateResponse
err = json.Unmarshal([]byte(user.StateData), &stateResponse)
if err != nil {
log.Errorln(err)
ResetUserState(user, bot)
return
}
// amount not in allowed range from LNURL
if amount > (stateResponse.MaxSendable/1000) || amount < (stateResponse.MinSendable/1000) {
err = fmt.Errorf("amount not in range")
log.Errorln(err)
bot.trySendMessage(m.Sender, fmt.Sprintf(lnurlInvalidAmountRangeMessage, stateResponse.MinSendable/1000, stateResponse.MaxSendable/1000))
ResetUserState(user, bot)
return
}
stateResponse.Amount = a
state, err := json.Marshal(stateResponse)
if err != nil {
log.Errorln(err)
ResetUserState(user, bot)
return
}
SetUserState(user, bot, lnbits.UserStateConfirmLNURLPay, string(state))
bot.lnurlPayHandler(m)
}
}
// LnurlStateResponse saves the state of the user for an LNURL payment
type LnurlStateResponse struct {
lnurl.LNURLPayResponse1
Amount int `json:"amount"`
}
// lnurlPayHandler is invoked when the user has delivered an amount and is ready to pay
func (bot TipBot) lnurlPayHandler(c *tb.Message) {
msg := bot.trySendMessage(c.Sender, lnurlGettingUserMessage)
user, err := GetUser(c.Sender, bot)
if err != nil {
log.Errorln(err)
// bot.trySendMessage(c.Sender, err.Error())
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, "database error."))
return
}
if user.StateKey == lnbits.UserStateConfirmLNURLPay {
client, err := getHttpClient()
if err != nil {
log.Errorln(err)
// bot.trySendMessage(c.Sender, err.Error())
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, err))
return
}
var stateResponse LnurlStateResponse
err = json.Unmarshal([]byte(user.StateData), &stateResponse)
if err != nil {
log.Errorln(err)
// bot.trySendMessage(c.Sender, err.Error())
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, err))
return
}
callbackUrl, err := url.Parse(stateResponse.Callback)
if err != nil {
log.Errorln(err)
// bot.trySendMessage(c.Sender, err.Error())
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, err))
return
}
qs := callbackUrl.Query()
qs.Set("amount", strconv.Itoa(stateResponse.Amount*1000))
callbackUrl.RawQuery = qs.Encode()
res, err := client.Get(callbackUrl.String())
if err != nil {
log.Errorln(err)
// bot.trySendMessage(c.Sender, err.Error())
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, err))
return
}
var response2 lnurl.LNURLPayResponse2
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Errorln(err)
// bot.trySendMessage(c.Sender, err.Error())
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, err))
return
}
json.Unmarshal(body, &response2)
if len(response2.PR) < 1 {
bot.tryEditMessage(msg, fmt.Sprintf(lnurlPaymentFailed, "could not receive invoice (wrong address?)."))
return
}
bot.telegram.Delete(msg)
c.Text = fmt.Sprintf("/pay %s", response2.PR)
bot.confirmPaymentHandler(c)
}
}
func getHttpClient() (*http.Client, error) {
client := http.Client{}
if Configuration.Bot.HttpProxy != "" {
proxyUrl, err := url.Parse(Configuration.Bot.HttpProxy)
if err != nil {
log.Errorln(err)
return nil, err
}
client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
}
return &client, nil
}
func (bot TipBot) cancelLnUrlHandler(c *tb.Callback) {
}
// from https://github.com/fiatjaf/go-lnurl
func HandleLNURL(rawlnurl string) (string, lnurl.LNURLParams, error) {
var err error
var rawurl string
if name, domain, ok := lnurl.ParseInternetIdentifier(rawlnurl); ok {
isOnion := strings.Index(domain, ".onion") == len(domain)-6
rawurl = domain + "/.well-known/lnurlp/" + name
if isOnion {
rawurl = "http://" + rawurl
} else {
rawurl = "https://" + rawurl
}
} else if strings.HasPrefix(rawlnurl, "http") {
rawurl = rawlnurl
} else {
foundUrl, ok := lnurl.FindLNURLInText(rawlnurl)
if !ok {
return "", nil,
errors.New("invalid bech32-encoded lnurl: " + rawlnurl)
}
rawurl, err = lnurl.LNURLDecode(foundUrl)
if err != nil {
return "", nil, err
}
}
parsed, err := url.Parse(rawurl)
if err != nil {
return rawurl, nil, err
}
query := parsed.Query()
switch query.Get("tag") {
case "login":
value, err := lnurl.HandleAuth(rawurl, parsed, query)
return rawurl, value, err
case "withdrawRequest":
if value, ok := lnurl.HandleFastWithdraw(query); ok {
return rawurl, value, nil
}
}
client, err := getHttpClient()
if err != nil {
return "", nil, err
}
resp, err := client.Get(rawurl)
if err != nil {
return rawurl, nil, err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return rawurl, nil, err
}
j := gjson.ParseBytes(b)
if j.Get("status").String() == "ERROR" {
return rawurl, nil, lnurl.LNURLErrorResponse{
URL: parsed,
Reason: j.Get("reason").String(),
Status: "ERROR",
}
}
switch j.Get("tag").String() {
case "withdrawRequest":
value, err := lnurl.HandleWithdraw(j)
return rawurl, value, err
case "payRequest":
value, err := lnurl.HandlePay(j)
return rawurl, value, err
case "channelRequest":
value, err := lnurl.HandleChannel(j)
return rawurl, value, err
default:
return rawurl, nil, errors.New("unknown response tag " + j.String())
}
}
func (bot *TipBot) sendToLightningAddress(m *tb.Message, address string, amount int) error {
split := strings.Split(address, "@")
if len(split) != 2 {
return fmt.Errorf("lightning address format wrong")
}
host := strings.ToLower(split[1])
name := strings.ToLower(split[0])
// convert address scheme into LNURL Bech32 format
callback := fmt.Sprintf("https://%s/.well-known/lnurlp/%s", host, name)
log.Infof("[sendToLightningAddress] %s: callback: %s", GetUserStr(m.Sender), callback)
lnurl, err := lnurl.LNURLEncode(callback)
if err != nil {
return err
}
if amount > 0 {
m.Text = fmt.Sprintf("/lnurl %d %s", amount, lnurl)
} else {
m.Text = fmt.Sprintf("/lnurl %s", lnurl)
}
bot.lnurlHandler(m)
return nil
}