-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
251 lines (209 loc) · 6.01 KB
/
main.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/fiatjaf/eventstore/sqlite3"
"github.com/fiatjaf/khatru"
"github.com/fiatjaf/khatru/policies"
"github.com/joho/godotenv"
"github.com/nbd-wtf/go-nostr"
decodepay "github.com/nbd-wtf/ln-decodepay"
"log"
"net/http"
"regexp"
"time"
)
type Description struct {
PubKey string `json:"pubkey"`
Content string `json:"content"`
ID string `json:"id"`
CreatedAt int64 `json:"created_at"`
Sig string `json:"sig"`
Kind int `json:"kind"`
Tags [][]string `json:"tags"`
}
var (
relays = []string{
"wss://relay.snort.social",
"wss://nos.lol",
"wss://nostr.mom",
"wss://nostr.wine",
"wss://relay.damus.io",
"wss://relay.nostr.band",
"wss://relay.primal.net",
}
botPubkey string
relay = khatru.NewRelay()
pool = nostr.NewSimplePool(context.Background())
port = 3456
)
func main() {
relay.Info.Name = "PPE Relay"
relay.Info.PubKey = "f1f9b0996d4ff1bf75e79e4cc8577c89eb633e68415c7faf74cf17a07bf80bd8"
relay.Info.Description = "Pay-Per-Event Relay."
godotenv.Load(".env")
botPubkey, _ = nostr.GetPublicKey(GetEnv("BOT_PRIVATE_KEY"))
db := sqlite3.SQLite3Backend{DatabaseURL: "./db/db"}
if err := db.Init(); err != nil {
panic(err)
}
relay.RejectEvent = append(relay.RejectEvent,
policies.RejectEventsWithBase64Media,
policies.EventIPRateLimiter(5, time.Minute*1, 30),
policies.RestrictToSpecifiedKinds([]uint16{1, 30023}...),
)
relay.RejectFilter = append(relay.RejectFilter,
policies.NoEmptyFilters,
policies.NoComplexFilters,
)
relay.RejectConnection = append(relay.RejectConnection,
policies.ConnectionRateLimiter(10, time.Minute*2, 30),
)
relay.RejectEvent = append(relay.RejectEvent, func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
userPaidAmount := GetZapsTotalFromUser(event.PubKey)
userNotesCount := GetStoredEventsCountFromUser(event.PubKey, db)
if userPaidAmount < (userNotesCount + 1) {
return true, "no sufficient balance; top up"
}
return false, ""
})
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
fmt.Printf("Running on :%v", port)
go HandleBotCommands(db)
http.ListenAndServe(fmt.Sprintf(":%v", port), relay)
}
func GetZapEventsFromUser(pubkey string) map[string]*nostr.Event {
ctx := context.Background()
events := make(map[string]*nostr.Event)
tags := make(nostr.TagMap)
tags["p"] = []string{botPubkey}
filter := nostr.Filter{
Kinds: []int{nostr.KindZap},
Tags: tags,
}
for event := range pool.SubManyEose(ctx, relays, []nostr.Filter{filter}) {
zapRequest, err := GetZapRequestFromZapEvent(event.Event)
if err != nil {
continue
} else if zapRequest.PubKey == pubkey {
events[event.ID] = event.Event
}
}
return events
}
func GetZapRequestFromZapEvent(event *nostr.Event) (*Description, error) {
var descriptionJSON string
for _, tag := range event.Tags {
if len(tag) > 1 && tag[0] == "description" {
descriptionJSON = tag[1]
break
}
}
if descriptionJSON == "" {
return nil, errors.New("description tag not found")
}
var description Description
err := json.Unmarshal([]byte(descriptionJSON), &description)
if err != nil {
fmt.Printf("Error parsing description: %v\n", err)
return nil, fmt.Errorf("error parsing description: %v", err)
}
return &description, nil
}
func GetZapsTotalFromUser(pubkey string) int64 {
zapEvents := GetZapEventsFromUser(pubkey)
total := int64(0)
for _, event := range zapEvents {
bolt11, err := ValueFromTag(event, "bolt11")
if err != nil {
continue
} else if bolt11 != nil {
decoded, err := decodepay.Decodepay(*bolt11)
if err != nil {
continue
} else {
total += decoded.MSatoshi
}
}
}
return total / 1000
}
func GetStoredEventsCountFromUser(pubkey string, db sqlite3.SQLite3Backend) int64 {
ctx := context.Background()
filter := nostr.Filter{
Authors: []string{pubkey},
}
iCtx, cancel := context.WithCancel(ctx)
defer cancel()
count, err := db.CountEvents(iCtx, filter)
if err != nil {
log.Fatalf("Failed to query events: %v", err)
}
return count
}
func GetRemainingUserBalance(pubkey string, db sqlite3.SQLite3Backend) int64 {
userPaidAmount := GetZapsTotalFromUser(pubkey)
userNotesCount := GetStoredEventsCountFromUser(pubkey, db)
remainingBalance := userPaidAmount - userNotesCount
return remainingBalance
}
func HandleBotCommands(db sqlite3.SQLite3Backend) {
ctx := context.Background()
tags := make(nostr.TagMap)
tags["p"] = []string{botPubkey}
filter := nostr.Filter{
Kinds: []int{nostr.KindTextNote},
Tags: tags,
}
for event := range pool.SubMany(ctx, relays, []nostr.Filter{filter}) {
if !BotCommandFulfilled(event.ID) {
balanceRequest, _ := regexp.MatchString(`(?mi)\bbalance\b`, event.Content)
if balanceRequest {
userBalance := GetRemainingUserBalance(event.PubKey, db)
response := fmt.Sprintf("Your balance is %v sats.", userBalance)
PublishCommandResponseEvent(event.Event, response)
}
}
}
}
func BotCommandFulfilled(ID string) bool {
ctx := context.Background()
tags := make(nostr.TagMap)
tags["e"] = []string{ID}
filter := nostr.Filter{
Kinds: []int{nostr.KindTextNote},
Tags: tags,
Authors: []string{botPubkey},
}
for range pool.SubManyEose(ctx, relays, []nostr.Filter{filter}) {
return true
}
return false
}
func PublishCommandResponseEvent(ev *nostr.Event, content string) {
event := nostr.Event{
PubKey: botPubkey,
CreatedAt: nostr.Now(),
Kind: nostr.KindTextNote,
Content: content,
Tags: []nostr.Tag{[]string{"e", ev.ID}, []string{"p", ev.PubKey}},
}
event.Sign(GetEnv("BOT_PRIVATE_KEY"))
ctx := context.Background()
for _, url := range relays {
relay, err := nostr.RelayConnect(ctx, url)
if err != nil {
fmt.Println(err)
continue
}
if err := relay.Publish(ctx, event); err != nil {
fmt.Println(err)
continue
}
fmt.Printf("published to %s\n", url)
}
}