-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
212 lines (179 loc) · 5.48 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
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
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"text/template"
"time"
"github.com/bitvora/go-bitvora"
)
type WebhookPayload struct {
Event string `json:"event"`
Data struct {
AmountSats int `json:"amount_sats"`
ChainTxID *string `json:"chain_tx_id"`
CreatedAt time.Time `json:"created_at"`
FeeSats float64 `json:"fee_sats"`
ID string `json:"id"`
LightningInvoiceID string `json:"lightning_invoice_id"`
Metadata *Metadata `json:"metadata"`
NetworkType string `json:"network_type"`
RailType string `json:"rail_type"`
Recipient string `json:"recipient"`
Status string `json:"status"`
UpdatedAt time.Time `json:"updated_at"`
} `json:"data"`
}
type Metadata struct {
Npub string `json:"npub"`
}
type Response struct {
Status int `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
type PageData struct {
Header string
Description string
Price string
}
func JSONResponse(w http.ResponseWriter, status int, message string, data interface{}) {
response := Response{
Status: status,
Message: message,
Data: data,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func handleGenerateInvoice(w http.ResponseWriter, r *http.Request) {
bitvoraApiKey := os.Getenv("BITVORA_API_KEY")
pricePerYearStr := os.Getenv("PRICE_PER_YEAR")
pricePerYearFloat, err := strconv.ParseFloat(pricePerYearStr, 64)
if err != nil {
JSONResponse(w, http.StatusBadRequest, "Invalid price per year", nil)
return
}
// Parse the JSON body
var requestData struct {
Npub string `json:"npub"`
}
err = json.NewDecoder(r.Body).Decode(&requestData)
if err != nil || requestData.Npub == "" {
JSONResponse(w, http.StatusBadRequest, "Missing or invalid npub", nil)
return
}
npub := requestData.Npub
metadata := map[string]string{
"npub": npub,
}
bitvoraClient := bitvora.NewBitvoraClient(bitvora.Mainnet, bitvoraApiKey)
invoice, err := bitvoraClient.CreateLightningInvoice(pricePerYearFloat, string(bitvora.SATS), "1 year subscription", 3600, metadata)
if err != nil {
JSONResponse(w, http.StatusInternalServerError, "Error creating invoice", nil)
return
}
var response struct {
Invoice string `json:"invoice"`
}
response.Invoice = invoice.Data.PaymentRequest
createSubscription(npub)
JSONResponse(w, http.StatusOK, "Invoice generated", response)
}
func handleHomePage(w http.ResponseWriter, r *http.Request) {
// Define the variables to pass to the template
data := PageData{
Header: os.Getenv("RELAY_NAME"),
Description: os.Getenv("RELAY_DESCRIPTION"),
Price: os.Getenv("PRICE_PER_YEAR"),
}
// Parse the template file
tmpl, err := template.ParseFiles("static/index.html")
if err != nil {
http.Error(w, "Unable to load template", http.StatusInternalServerError)
return
}
// Render the template with the provided data
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, "Unable to render template", http.StatusInternalServerError)
}
}
func handleBitvoraWebhook(w http.ResponseWriter, r *http.Request) {
secret := os.Getenv("BITVORA_WEBHOOK_SECRET")
signature := r.Header.Get("bitvora-signature")
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}
payload := string(bodyBytes)
if validateWebhookSignature(payload, signature, secret) {
fmt.Println("Valid signature")
// Process webhook
w.WriteHeader(http.StatusOK)
var webhookPayload WebhookPayload
if err := json.Unmarshal(bodyBytes, &webhookPayload); err != nil {
fmt.Println("Error parsing webhook payload:", err)
http.Error(w, "Error parsing webhook payload", http.StatusBadRequest)
return
}
if webhookPayload.Event == "deposit.lightning.completed" {
log.Println("Received deposit.lightning.completed event", webhookPayload.Data.ID)
var metadata Metadata
if webhookPayload.Data.Metadata != nil {
metadata = *webhookPayload.Data.Metadata
if metadata.Npub != "" {
npub := metadata.Npub
setPaidSubscription(npub)
loadWhitelist()
} else {
fmt.Println("No npub in metadata")
return
}
}
}
} else {
fmt.Println("Invalid signature")
http.Error(w, "Invalid signature", http.StatusUnauthorized)
}
}
func handlePollPayment(w http.ResponseWriter, r *http.Request) {
// Ensure the request method is POST
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// Parse the JSON body
var requestData struct {
Npub string `json:"npub"`
}
err := json.NewDecoder(r.Body).Decode(&requestData)
if err != nil || requestData.Npub == "" {
JSONResponse(w, http.StatusBadRequest, "Missing or invalid npub", nil)
return
}
// Process the payment polling
active := pollPayment(requestData.Npub)
var response struct {
Active bool `json:"active"`
}
response.Active = active
JSONResponse(w, http.StatusOK, "Payment status", response)
}
func validateWebhookSignature(payload, signature, secret string) bool {
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(payload))
expectedSignature := hex.EncodeToString(h.Sum(nil))
return expectedSignature == signature
}