This repository has been archived by the owner on Aug 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.go
163 lines (143 loc) · 4.51 KB
/
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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/fiatjaf/go-lnurl"
"github.com/fiatjaf/relayer"
"github.com/gorilla/mux"
"github.com/lnbits/relampago"
)
const PRICE_SAT = 500
func handleWebpage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`
<meta charset=utf-8>
<title>expensive relay</title>
<h1>expensive relay</h1>
<a href="https://github.com/fiatjaf/expensive-relay">https://github.com/fiatjaf/expensive-relay</a>
<p>this is a nostr relay that only accepts events published from keys that pay a registration fee. this is an antispam measure. you can still be banned if you're spamming or doing something bad.</p>
<p>to register your nostr public key, type it below and click the button. or send ` + strconv.Itoa(PRICE_SAT) + ` satoshis to <code><yourpubkey>@` + relay.Domain + `</code>.</p>
<form>
<label>
nostr public key:
<input name=pubkey />
</label>
<button>Get Invoice</button>
</form>
<p id=message></p>
<a id=link><canvas id=qr /></a>
<code id=invoice></code>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrious/4.0.2/qrious.min.js"></script>
<script>
document.querySelector('form').addEventListener('submit', async ev => {
ev.preventDefault()
let res = await (await fetch('/.well-known/lnurlp/' + ev.target.pubkey.value + '?amount=` + strconv.Itoa(PRICE_SAT*1000) + `')).text()
let { pr, reason } = JSON.parse(res)
if (pr) {
invoice.innerHTML = pr
link.href = 'lightning:' + pr
new QRious({
element: qr,
value: pr.toUpperCase(),
size: 300
});
} else {
message.innerHTML = reason
}
})
</script>
<style>
body {
margin: 10px auto;
width: 800px;
max-width: 90%;
}
</style>
`))
}
func handleLnurlRegister(w http.ResponseWriter, r *http.Request) {
pubkey := mux.Vars(r)["pubkey"]
if v, err := hex.DecodeString(pubkey); err != nil || len(v) != 32 {
json.NewEncoder(w).Encode(lnurl.LNURLResponse{
Status: "ERROR",
Reason: "invalid pubkey " + pubkey,
})
return
}
metadata, _ := json.Marshal([][]string{
{"text/plain", "registration for pubkey " + pubkey},
})
if amount := r.URL.Query().Get("amount"); amount == "" {
json.NewEncoder(w).Encode(lnurl.LNURLPayParams{
LNURLResponse: lnurl.LNURLResponse{Status: "OK"},
Callback: fmt.Sprintf("https://%s/.well-known/lnurlp/%s",
relay.Domain, pubkey),
MinSendable: PRICE_SAT * 1000,
MaxSendable: PRICE_SAT * 1000,
EncodedMetadata: string(metadata),
Tag: "payRequest",
})
} else {
msat, err := strconv.Atoi(amount)
if err != nil || msat != PRICE_SAT*1000 {
json.NewEncoder(w).Encode(lnurl.ErrorResponse("invalid amount " + amount))
return
}
h := sha256.Sum256(metadata)
inv, err := relay.ln.CreateInvoice(relampago.InvoiceParams{
Msatoshi: int64(msat),
DescriptionHash: h[:],
})
if err != nil {
json.NewEncoder(w).Encode(
lnurl.ErrorResponse("failed to create invoice: " + err.Error()))
return
}
var invoice string
if err := relay.db.Get(&invoice, `
INSERT INTO registered_users (pubkey, invoice) VALUES ($1, $2)
ON CONFLICT (pubkey) DO UPDATE SET invoice = CASE
WHEN registered_users.registered_at IS NULL THEN excluded.invoice
ELSE registered_users.invoice
END
RETURNING invoice
`, pubkey, inv.CheckingID); err != nil {
json.NewEncoder(w).Encode(
lnurl.ErrorResponse("failed to save invoice: " + err.Error()))
return
}
if invoice != inv.CheckingID {
json.NewEncoder(w).Encode(lnurl.ErrorResponse("user is already registered"))
return
}
json.NewEncoder(w).Encode(lnurl.LNURLPayValues{
LNURLResponse: lnurl.LNURLResponse{Status: "OK"},
PR: inv.Invoice,
Routes: make([]struct{}, 0),
Disposable: lnurl.TRUE,
SuccessAction: lnurl.Action("Public Key "+pubkey+" registered!", ""),
})
}
}
func handlePaidInvoice(status relampago.InvoiceStatus) {
if status.Exists && status.Paid {
var pubkey string
err := relay.db.Get(&pubkey, `
UPDATE registered_users
SET registered_at = extract(epoch from now())
WHERE invoice = $1
RETURNING pubkey
`, status.CheckingID)
if err != nil {
relayer.Log.Warn().Err(err).Str("invoice", status.CheckingID).
Msg("failed to register user")
} else if pubkey != "" {
relayer.Log.Warn().Str("pubkey", pubkey).Str("invoice", status.CheckingID).
Msg("user registered")
}
}
}