Skip to content

Commit

Permalink
implement nip-70 protected events
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Nov 28, 2024
1 parent 27e34f5 commit 8c03434
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
12 changes: 9 additions & 3 deletions realy/add-event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package realy

import (
"fmt"
"net/http"
"regexp"
"strings"
Expand All @@ -16,8 +17,7 @@ var nip20prefixmatcher = regexp.MustCompile(`^\w+: `)

// AddEvent has a business rule to add an event to the event store
func AddEvent(c Ctx, rl relay.I, ev *event.T, hr *http.Request, origin S,
authedPubkey B) (accepted bool,
message B) {
authedPubkey B) (accepted bool, message B) {
if ev == nil {
return false, normalize.Invalid.F("empty event")
}
Expand All @@ -30,7 +30,13 @@ func AddEvent(c Ctx, rl relay.I, ev *event.T, hr *http.Request, origin S,
if !accept {
return false, normalize.Blocked.F(notice)
}

if ev.Tags.ContainsProtectedMarker() {
if len(authedPubkey) == 0 || !equals(ev.PubKey, authedPubkey) {
return false, B(fmt.Sprintf(
"event with relay marker tag '-' may only be published by matching npub: %0x is not %0x",
authedPubkey, ev.PubKey))
}
}
if ev.Kind.IsEphemeral() {
// do not store ephemeral events
} else {
Expand Down
2 changes: 1 addition & 1 deletion realy/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.16
v1.2.17
2 changes: 2 additions & 0 deletions relayinfo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ var (
NIP58 = Badges
RelayListMetadata = NIP{"Relay List Metadata", 65}
NIP65 = RelayListMetadata
ProtectedEvents = NIP{"Protected Events", 70}
NIP70 = ProtectedEvents
ModeratedCommunities = NIP{"Moderated Communities", 72}
NIP72 = ModeratedCommunities
ZapGoals = NIP{"Zap Goals", 75}
Expand Down
11 changes: 11 additions & 0 deletions tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ func (t *T) Intersects(f *T) (has bool) {
return matches == 0
}

// ContainsProtectedMarker returns true if an event may only be published to the relay by a user
// authed with the same pubkey as in the event. This is for implementing relayinfo.NIP70.
func (t *T) ContainsProtectedMarker() (does bool) {
for _, v := range t.t {
if equals(v.Key(), B("-")) {
return true
}
}
return
}

// ContainsAny returns true if any of the strings given in `values` matches any of the tag
// elements.
func (t *T) ContainsAny(tagName B, values *tag.T) bool {
Expand Down

0 comments on commit 8c03434

Please sign in to comment.