forked from kopykatA/lntxbot
/
helpers.go
412 lines (353 loc) · 8.77 KB
/
helpers.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package main
import (
"context"
"crypto/rand"
"database/sql"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/fiatjaf/lightningd-gjson-rpc"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/kballard/go-shellquote"
"github.com/skip2/go-qrcode"
"github.com/tidwall/gjson"
"gopkg.in/jmcvetta/napping.v3"
)
var dollarPrice = struct {
lastUpdate time.Time
rate float64
}{time.Now(), 0}
var nodeAliases = make(map[string]string)
func makeLabel(userId int, messageId interface{}, preimage string) string {
return fmt.Sprintf("%s.%d.%v.%s", s.ServiceId, userId, messageId, preimage)
}
func parseLabel(label string) (messageId, userId int, preimage string, ok bool) {
ok = false
parts := strings.Split(label, ".")
if len(parts) == 4 {
userId, err = strconv.Atoi(parts[1])
if err == nil {
ok = true
}
messageId, err = strconv.Atoi(parts[2])
if err == nil {
ok = true
}
preimage = parts[3]
}
return
}
func qrImagePath(label string) string {
return filepath.Join(os.TempDir(), s.ServiceId+".invoice."+label+".png")
}
func searchForInvoice(message tgbotapi.Message) (bolt11 string, ok bool) {
text := message.Text
if text == "" {
text = message.Caption
}
if bolt11, ok = getBolt11(text); ok {
return
}
// receiving a picture, try to decode the qr code
if message.Photo != nil && len(*message.Photo) > 0 {
log.Debug().Msg("got photo, looking for qr code.")
photos := *message.Photo
photo := photos[len(photos)-1]
photourl, err := bot.GetFileDirectURL(photo.FileID)
if err != nil {
log.Warn().Err(err).Str("fileid", photo.FileID).
Msg("failed to get photo URL.")
return
}
p := &url.Values{}
p.Set("fileurl", photourl)
var r []struct {
Type string `json:"type"`
Symbol []struct {
Data string `json:"data"`
Error string `json:"error"`
} `json:"symbol"`
}
_, err = napping.Get("https://api.qrserver.com/v1/read-qr-code/", p, &r, nil)
if err != nil {
log.Warn().Err(err).Str("url", photourl).Msg("failed to call qrserver")
return
}
if len(r) == 0 || len(r[0].Symbol) == 0 {
log.Warn().Str("url", photourl).Msg("invalid response from qrserver")
return
}
if r[0].Symbol[0].Error != "" {
log.Debug().Str("err", r[0].Symbol[0].Error).
Str("url", photourl).Msg("qrserver failed to decode")
return
}
text = r[0].Symbol[0].Data
log.Debug().Str("data", text).Msg("got qr code data")
return getBolt11(text)
}
return
}
func getBolt11(text string) (bolt11 string, ok bool) {
text = strings.ToLower(text)
argv, err := shellquote.Split(text)
if err != nil {
return
}
for _, arg := range argv {
if strings.HasPrefix(arg, "lightning:") {
arg = arg[10:]
}
if strings.HasPrefix(arg, "lnbc") {
return arg, true
}
}
return
}
func decodeInvoice(invoice string) (inv gjson.Result, nodeAlias, usd string, err error) {
inv, err = ln.Call("decodepay", invoice)
if err != nil {
return
}
if inv.Get("code").Int() != 0 {
err = errors.New(inv.Get("message").String())
return
}
nodeAlias = getNodeAlias(inv.Get("payee").String())
usd = getDollarPrice(inv.Get("msatoshi").Int())
return
}
func getNodeAlias(id string) string {
begin:
if alias, ok := nodeAliases[id]; ok {
return alias
}
if id == "" {
return "~"
}
res, err := ln.Call("listnodes", id)
if err != nil {
return "~"
}
alias := res.Get("nodes.0.alias").String()
if alias == "" {
alias = "~"
}
nodeAliases[id] = alias
goto begin
}
func getDollarPrice(msats int64) string {
rate, err := getDollarRate()
if err != nil {
return "~ USD"
}
return fmt.Sprintf("%.3f USD", float64(msats)/rate)
}
func getDollarRate() (rate float64, err error) {
begin:
if dollarPrice.rate > 0 && dollarPrice.lastUpdate.After(time.Now().Add(-time.Hour)) {
// it's fine
return dollarPrice.rate, nil
}
resp, err := http.Get("https://www.bitstamp.net/api/v2/ticker/btcusd")
if err != nil || resp.StatusCode >= 300 {
return
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
srate := gjson.GetBytes(b, "last").String()
btcrate, err := strconv.ParseFloat(srate, 64)
if err != nil {
return
}
// we want the msat -> dollar rate, not dollar -> btc
dollarPrice.rate = 1 / (btcrate / 100000000000)
dollarPrice.lastUpdate = time.Now()
goto begin
}
func makeInvoice(
u User,
sats int,
desc string,
messageId interface{},
preimage string,
) (bolt11 string, qrpath string, err error) {
log.Debug().Str("user", u.Username).Str("desc", desc).Int("sats", sats).
Msg("generating invoice")
if preimage == "" {
preimage, err = randomPreimage()
if err != nil {
return
}
}
label := makeLabel(u.Id, messageId, preimage)
// make invoice
res, err := ln.CallWithCustomTimeout(time.Second*40, "invoice", map[string]interface{}{
"msatoshi": sats * 1000,
"label": label,
"description": desc + " [" + s.ServiceId + "/" + u.AtName() + "]",
"expiry": int(s.InvoiceTimeout / time.Second),
"preimage": preimage,
})
if err != nil {
return
}
bolt11 = res.Get("bolt11").String()
// generate qr code
err = qrcode.WriteFile(strings.ToUpper(bolt11), qrcode.Medium, 256, qrImagePath(label))
if err != nil {
log.Warn().Err(err).Str("invoice", bolt11).
Msg("failed to generate qr.")
err = nil
} else {
qrpath = qrImagePath(label)
}
return
}
func messageFromError(err error, prefix string) string {
var msg string
switch terr := err.(type) {
case lightning.ErrorTimeout:
msg = fmt.Sprintf("Operation has timed out after %d seconds.", terr.Seconds)
case lightning.ErrorCommand:
msg = terr.Msg
case lightning.ErrorConnect, lightning.ErrorConnectionBroken:
msg = "Problem connecting to our node. Please try again in a minute."
case lightning.ErrorJSONDecode:
msg = "Error reading response from lightningd."
default:
msg = err.Error()
}
return prefix + ": " + msg
}
func randomPreimage() (string, error) {
hex := []rune("0123456789abcdef")
b := make([]rune, 64)
for i := range b {
r, err := rand.Int(rand.Reader, big.NewInt(16))
if err != nil {
return "", err
}
b[i] = hex[r.Int64()]
}
return string(b), nil
}
func parseUsername(message *tgbotapi.Message, value interface{}) (u *User, display string, err error) {
var username string
var user User
var uid int
switch val := value.(type) {
case []string:
if len(val) > 0 {
username = strings.Join(val, " ")
}
case string:
username = val
case int:
uid = val
}
if intval, err := strconv.Atoi(username); err == nil {
uid = intval
}
if username != "" {
username = strings.ToLower(username)
}
if username == "" && uid == 0 {
err = errors.New("Couldn't parse username.")
return
}
// check entities for user type
for _, entity := range *message.Entities {
if entity.Type == "text_mention" && entity.User != nil {
// user without username
uid = entity.User.ID
display = strings.TrimSpace(entity.User.FirstName + " " + entity.User.LastName)
user, err = ensureTelegramId(uid)
u = &user
return
}
if entity.Type == "mention" {
// user with username
uname := username[1:]
display = uname
user, err = ensureUsername(uname)
u = &user
return
}
}
// if the user identifier passed was neither @someone (mention) nor a text_mention
// (for users without usernames but still painted blue and autocompleted by telegram)
// and we have a uid that means it's the case where just a numeric id was given and nothing
// more.
if uid != 0 {
user, err = ensureTelegramId(uid)
display = user.AtName()
u = &user
return
}
return
}
func fromManyToOne(sats int, toId int, fromIds []int,
desc, receiverMessage, giverMessage string,
) (receiver User, err error) {
txn, err := pg.BeginTxx(context.TODO(),
&sql.TxOptions{Isolation: sql.LevelSerializable})
if err != nil {
return
}
defer txn.Rollback()
receiver, _ = loadUser(toId, 0)
giverNames := make([]string, 0, len(fromIds))
msats := sats * 1000
var (
vdesc = &sql.NullString{}
vlabel = &sql.NullString{}
)
vdesc.Scan(desc)
for _, fromId := range fromIds {
if fromId == toId {
continue
}
_, err = txn.Exec(`
INSERT INTO lightning.transaction
(from_id, to_id, amount, description, label)
VALUES ($1, $2, $3, $4, $5)
`, fromId, toId, msats, vdesc, vlabel)
if err != nil {
return
}
var balance int
err = txn.Get(&balance, `
SELECT balance::int FROM lightning.balance WHERE account_id = $1
`, fromId)
if err != nil {
return
}
if balance < 0 {
err = errors.New("insufficient balance")
return
}
giver, _ := loadUser(fromId, 0)
giverNames = append(giverNames, giver.AtName())
giver.notify(fmt.Sprintf(giverMessage, sats, receiver.AtName()))
}
err = txn.Commit()
if err != nil {
return
}
receiver.notify(
fmt.Sprintf(receiverMessage,
sats*len(fromIds), strings.Join(giverNames, " ")),
)
return
}