This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
instawallet.go
67 lines (58 loc) · 1.74 KB
/
instawallet.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
package main
import (
"fmt"
"net/http"
"github.com/fiatjaf/go-lnurl"
"github.com/lnbits/infinity/services"
rp "github.com/lnbits/relampago"
)
func instawallet(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("lightning")
raw, lnurlParams, err := lnurl.HandleLNURL(code)
if err != nil {
if raw == "" {
raw = "~"
}
http.Error(w, fmt.Sprintf("failed to fetch lnurl voucher params from %s (%s): %s", code, raw, err.Error()), 400)
return
}
if params, ok := lnurlParams.(lnurl.LNURLWithdrawResponse); !ok {
http.Error(w, fmt.Sprintf("lnurl '%s' is not a valid lnurl-withdraw voucher that can be claimed", raw), 400)
return
} else {
// create user and wallet
user, err := services.CreateUser()
if err != nil {
http.Error(w, fmt.Sprintf("failed to create user: %s", err.Error()), 500)
return
}
wallet, err := services.CreateWallet(user.ID, "from-voucher")
if err != nil {
http.Error(w, fmt.Sprintf("failed to create wallet: %s", err.Error()), 500)
return
}
// create invoice
invoice, err := services.CreateInvoice(wallet.ID, services.CreateInvoiceParams{
InvoiceParams: rp.InvoiceParams{
Msatoshi: params.MaxWithdrawable,
Description: params.DefaultDescription,
},
Tag: "voucher-claim",
})
if err != nil {
http.Error(w, fmt.Sprintf("failed to create invoice: %s", err.Error()), 500)
return
}
// send invoice to lnurl-withdraw callback
callback := params.CallbackURL
qs := callback.Query()
qs.Set("k1", params.K1)
qs.Set("pr", invoice.Bolt11)
callback.RawQuery = qs.Encode()
http.Get(callback.String())
// redirect to wallet interface
http.Redirect(w, r,
fmt.Sprintf("%s/wallet/%s?key=%s", s.ServiceURL, wallet.ID, user.MasterKey),
http.StatusFound)
}
}