This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
forked from 22388o/etleneum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
account_handlers.go
303 lines (257 loc) · 8.88 KB
/
account_handlers.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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/fiatjaf/etleneum/data"
"github.com/fiatjaf/go-lnurl"
lightning "github.com/fiatjaf/lightningd-gjson-rpc"
"github.com/tidwall/gjson"
"gopkg.in/antage/eventsource.v1"
)
func lnurlSession(w http.ResponseWriter, r *http.Request) {
var es eventsource.EventSource
session := r.URL.Query().Get("session")
if session == "" {
session = lnurl.RandomK1()
} else {
// check session validity as k1
b, err := hex.DecodeString(session)
if err != nil || len(b) != 32 {
session = lnurl.RandomK1()
} else {
// finally try to fetch an existing stream
ies, ok := userstreams.Get(session)
if ok {
es = ies.(eventsource.EventSource)
}
}
}
if es == nil {
es = eventsource.New(
&eventsource.Settings{
Timeout: 5 * time.Second,
CloseOnTimeout: true,
IdleTimeout: 1 * time.Minute,
},
func(r *http.Request) [][]byte {
return [][]byte{
[]byte("X-Accel-Buffering: no"),
[]byte("Cache-Control: no-cache"),
[]byte("Content-Type: text/event-stream"),
[]byte("Connection: keep-alive"),
[]byte("Access-Control-Allow-Origin: *"),
}
},
)
userstreams.Set(session, es)
go func() {
for {
time.Sleep(25 * time.Second)
es.SendEventMessage("", "keepalive", "")
}
}()
}
go func() {
time.Sleep(100 * time.Millisecond)
es.SendRetryMessage(3 * time.Second)
}()
accountId := rds.Get("auth-session:" + session).Val()
if accountId != "" {
// we're logged already, so send account information
balance := data.GetAccountBalance(accountId)
go func() {
time.Sleep(100 * time.Millisecond)
es.SendEventMessage(`{"account": "`+accountId+`", "balance": `+strconv.FormatInt(balance, 10)+`, "can_withdraw": `+strconv.FormatInt(balanceWithReserve(balance), 10)+`, "secret": "`+getAccountSecret(accountId)+`"}`, "auth", "")
}()
// also renew this session
rds.Expire("auth-session:"+session, time.Hour*24*30)
}
// always send lnurls because we need lnurl-withdraw even if we're
// logged already
go func() {
time.Sleep(100 * time.Millisecond)
auth, _ := lnurl.LNURLEncode(s.ServiceURL + "/lnurl/auth?tag=login&k1=" + session)
withdraw, _ := lnurl.LNURLEncode(s.ServiceURL + "/lnurl/withdraw?session=" + session)
es.SendEventMessage(`{"auth": "`+auth+`", "withdraw": "`+withdraw+`"}`, "lnurls", "")
}()
es.ServeHTTP(w, r)
}
func lnurlAuth(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
k1 := params.Get("k1")
sig := params.Get("sig")
key := params.Get("key")
if ok, err := lnurl.VerifySignature(k1, sig, key); !ok {
log.Debug().Err(err).Str("k1", k1).Str("sig", sig).Str("key", key).
Msg("failed to verify lnurl-auth signature")
json.NewEncoder(w).Encode(lnurl.ErrorResponse("signature verification failed."))
return
}
session := k1
log.Debug().Str("session", session).Str("pubkey", key).Msg("valid login")
// there must be a valid auth session (meaning an eventsource client) one otherwise something is wrong
ies, ok := userstreams.Get(session)
if !ok {
json.NewEncoder(w).Encode(lnurl.ErrorResponse("there's no browser session to authorize."))
return
}
// assign the account id to this session on redis
if rds.Set("auth-session:"+session, key, time.Hour*24*30).Err() != nil {
json.NewEncoder(w).Encode(lnurl.ErrorResponse("failed to save session."))
return
}
es := ies.(eventsource.EventSource)
// notify browser
es.SendEventMessage(`{"session": "`+k1+`", "account": "`+key+`", "balance": `+strconv.FormatInt(data.GetAccountBalance(key), 10)+`, "secret": "`+getAccountSecret(key)+`"}`, "auth", "")
json.NewEncoder(w).Encode(lnurl.OkResponse())
}
func refreshBalance(w http.ResponseWriter, r *http.Request) {
session := r.URL.Query().Get("session")
// get account id from session
accountId, err := rds.Get("auth-session:" + session).Result()
if err != nil {
log.Error().Err(err).Str("session", session).
Msg("failed to get session from redis on refresh")
w.WriteHeader(500)
return
}
// get balance
balance := data.GetAccountBalance(accountId)
if ies, ok := userstreams.Get(session); ok {
ies.(eventsource.EventSource).SendEventMessage(`{"account": "`+accountId+`", "balance": `+strconv.FormatInt(balance, 10)+`, "can_withdraw": `+strconv.FormatInt(balanceWithReserve(balance), 10)+`, "secret": "`+getAccountSecret(accountId)+`"}`, "auth", "")
}
w.WriteHeader(200)
}
func lnurlWithdraw(w http.ResponseWriter, r *http.Request) {
accountId, err := getAccountIdFromLNURLWithdraw(r)
if err != nil {
json.NewEncoder(w).Encode(err.Error())
return
}
// get balance
balance := balanceWithReserve(data.GetAccountBalance(accountId))
json.NewEncoder(w).Encode(lnurl.LNURLWithdrawResponse{
LNURLResponse: lnurl.LNURLResponse{Status: "OK"},
Tag: "withdrawRequest",
Callback: fmt.Sprintf("%s/lnurl/withdraw/callback?%s",
s.ServiceURL, r.URL.RawQuery),
K1: hex.EncodeToString(make([]byte, 32)), // we don't care
MaxWithdrawable: balance,
MinWithdrawable: MIN_WITHDRAWABLE,
DefaultDescription: fmt.Sprintf("etleneum.com %s balance withdraw", accountId),
BalanceCheck: getStaticLNURLWithdraw(accountId),
})
}
func lnurlWithdrawCallback(w http.ResponseWriter, r *http.Request) {
accountId, err := getAccountIdFromLNURLWithdraw(r)
if err != nil {
json.NewEncoder(w).Encode(lnurl.ErrorResponse(err.Error()))
return
}
// LUD-15, save URL to notify the user later
balanceNotify, _ := url.Parse(r.URL.Query().Get("balanceNotify"))
if balanceNotify != nil && balanceNotify.Host != "" {
data.UpdateAccountMetadata(accountId, func(am *data.AccountMetadata) {
am.BalanceNotify = balanceNotify.String()
})
}
bolt11 := r.URL.Query().Get("pr")
if s.FreeMode {
json.NewEncoder(w).Encode(lnurl.OkResponse())
return
}
// decode invoice
inv, err := ln.Call("decodepay", bolt11)
if err != nil {
json.NewEncoder(w).Encode(lnurl.ErrorResponse("failed to decode invoice."))
return
}
amount := inv.Get("msatoshi").Int()
log.Debug().Str("bolt11", bolt11).Str("account", accountId).
Int64("amount", amount).
Msg("got a withdraw payment request")
// add a pending withdrawal
hash := inv.Get("payment_hash").String()
if err := data.CheckBalanceAddWithdrawal(
accountId,
amount,
bolt11,
hash,
); err != nil {
log.Warn().Err(err).Str("account", accountId).Msg("can't withdraw")
json.NewEncoder(w).Encode(lnurl.ErrorResponse("can't withdraw: " + err.Error()))
return
}
// actually send the payment
go func() {
var (
listpays gjson.Result
)
payresp, err := ln.CallWithCustomTimeout(time.Hour*24*30, "pay",
map[string]interface{}{
"bolt11": bolt11,
"label": "etleneum withdraw " + accountId,
"maxfeepercent": 0.7,
"exemptfee": 0,
"retry_for": 20,
})
log.Debug().Err(err).Str("resp", payresp.String()).
Str("account", accountId).Str("bolt11", bolt11).
Msg("withdraw pay result")
if _, ok := err.(lightning.ErrorCommand); ok {
goto failure
}
if payresp.Get("status").String() == "complete" {
// calculate actual fee
lnfee := payresp.Get("msatoshi_sent").Int() - payresp.Get("msatoshi").Int()
platformfee := int64(payresp.Get("msatoshi").Float() * 0.001)
fee := lnfee + platformfee
// mark as fulfilled
if data.FulfillWithdraw(accountId, amount, fee, hash); err != nil {
log.Error().Err(err).Str("accountId", accountId).
Msg("error marking payment as fulfilled")
}
return
}
// call listpays to check failure
listpays, _ = ln.Call("listpays", bolt11)
if listpays.Get("pays.#").Int() == 1 && listpays.Get("pays.0.status").String() != "failed" {
// not a failure -- but also not a success
// we don't know what happened, maybe it's pending, so don't do anything
log.Debug().Str("bolt11", bolt11).
Msg("we don't know what happened with this payment")
// notify browser
if ies, ok := userstreams.Get(r.URL.Query().Get("session")); ok {
ies.(eventsource.EventSource).SendEventMessage("We don't know what happened with the payment.", "error", "")
}
return
} else if listpays.Get("pays.#").Int() > 1 {
// this should not happen
log.Debug().Str("bolt11", bolt11).Msg("this should not happen")
return
}
// if we reached this point then it's because the payment has failed
failure:
// delete attempt since it has undoubtely failed
if err := data.CancelWithdraw(accountId, amount, hash); err != nil {
log.Error().Err(err).Str("accountId", accountId).
Msg("error deleting withdrawal attempt")
}
// notify browser
if ies, ok := userstreams.Get(r.URL.Query().Get("session")); ok {
ies.(eventsource.EventSource).SendEventMessage("Payment failed.", "error", "")
}
}()
json.NewEncoder(w).Encode(lnurl.OkResponse())
}
func logout(w http.ResponseWriter, r *http.Request) {
session := r.URL.Query().Get("session")
rds.Del("auth-session:" + session)
userstreams.Remove(session)
w.WriteHeader(200)
}