-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathpointers.go
194 lines (166 loc) · 5.6 KB
/
pointers.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
package nostr
import (
"fmt"
"strconv"
"strings"
)
// Pointer is an interface for different types of Nostr pointers.
//
// In this context, a "pointer" is a reference to an event or profile potentially including
// relays and other metadata that might help find it.
type Pointer interface {
// AsTagReference returns the pointer as a string as it would be seen in the value of a tag (i.e. the tag's second item).
AsTagReference() string
// AsTag converts the pointer with all the information available to a tag that can be included in events.
AsTag() Tag
// AsFilter converts the pointer to a Filter that can be used to query for it on relays.
AsFilter() Filter
MatchesEvent(Event) bool
}
var (
_ Pointer = (*ProfilePointer)(nil)
_ Pointer = (*EventPointer)(nil)
_ Pointer = (*EntityPointer)(nil)
_ Pointer = (*ExternalPointer)(nil)
)
// ProfilePointer represents a pointer to a Nostr profile.
type ProfilePointer struct {
PublicKey string `json:"pubkey"`
Relays []string `json:"relays,omitempty"`
}
// ProfilePointerFromTag creates a ProfilePointer from a "p" tag (but it doesn't have to be necessarily a "p" tag, could be something else).
func ProfilePointerFromTag(refTag Tag) (ProfilePointer, error) {
pk := refTag[1]
if !IsValidPublicKey(pk) {
return ProfilePointer{}, fmt.Errorf("invalid pubkey '%s'", pk)
}
pointer := ProfilePointer{
PublicKey: pk,
}
if len(refTag) > 2 {
if relay := (refTag)[2]; IsValidRelayURL(relay) {
pointer.Relays = []string{relay}
}
}
return pointer, nil
}
// MatchesEvent checks if the pointer matches an event.
func (ep ProfilePointer) MatchesEvent(_ Event) bool { return false }
func (ep ProfilePointer) AsTagReference() string { return ep.PublicKey }
func (ep ProfilePointer) AsFilter() Filter { return Filter{Authors: []string{ep.PublicKey}} }
func (ep ProfilePointer) AsTag() Tag {
if len(ep.Relays) > 0 {
return Tag{"p", ep.PublicKey, ep.Relays[0]}
}
return Tag{"p", ep.PublicKey}
}
// EventPointer represents a pointer to a nostr event.
type EventPointer struct {
ID string `json:"id"`
Relays []string `json:"relays,omitempty"`
Author string `json:"author,omitempty"`
Kind int `json:"kind,omitempty"`
}
// EventPointerFromTag creates an EventPointer from an "e" tag (but it could be other tag name, it isn't checked).
func EventPointerFromTag(refTag Tag) (EventPointer, error) {
id := refTag[1]
if !IsValid32ByteHex(id) {
return EventPointer{}, fmt.Errorf("invalid id '%s'", id)
}
pointer := EventPointer{
ID: id,
}
if len(refTag) > 2 {
if relay := (refTag)[2]; IsValidRelayURL(relay) {
pointer.Relays = []string{relay}
}
if len(refTag) > 3 && IsValidPublicKey(refTag[3]) {
pointer.Author = (refTag)[3]
} else if len(refTag) > 4 && IsValidPublicKey(refTag[4]) {
pointer.Author = (refTag)[4]
}
}
return pointer, nil
}
func (ep EventPointer) MatchesEvent(evt Event) bool { return evt.ID == ep.ID }
func (ep EventPointer) AsTagReference() string { return ep.ID }
func (ep EventPointer) AsFilter() Filter { return Filter{IDs: []string{ep.ID}} }
// AsTag converts the pointer to a Tag.
func (ep EventPointer) AsTag() Tag {
if len(ep.Relays) > 0 {
if ep.Author != "" {
return Tag{"e", ep.ID, ep.Relays[0], ep.Author}
} else {
return Tag{"e", ep.ID, ep.Relays[0]}
}
}
return Tag{"e", ep.ID}
}
// EntityPointer represents a pointer to a nostr entity (addressable event).
type EntityPointer struct {
PublicKey string `json:"pubkey"`
Kind int `json:"kind,omitempty"`
Identifier string `json:"identifier,omitempty"`
Relays []string `json:"relays,omitempty"`
}
// EntityPointerFromTag creates an EntityPointer from an "a" tag (but it doesn't check if the tag is really "a", it could be anything).
func EntityPointerFromTag(refTag Tag) (EntityPointer, error) {
spl := strings.SplitN(refTag[1], ":", 3)
if len(spl) != 3 {
return EntityPointer{}, fmt.Errorf("invalid addr ref '%s'", refTag[1])
}
if !IsValidPublicKey(spl[1]) {
return EntityPointer{}, fmt.Errorf("invalid addr pubkey '%s'", spl[1])
}
kind, err := strconv.Atoi(spl[0])
if err != nil || kind > (1<<16) {
return EntityPointer{}, fmt.Errorf("invalid addr kind '%s'", spl[0])
}
pointer := EntityPointer{
Kind: kind,
PublicKey: spl[1],
Identifier: spl[2],
}
if len(refTag) > 2 {
if relay := (refTag)[2]; IsValidRelayURL(relay) {
pointer.Relays = []string{relay}
}
}
return pointer, nil
}
// MatchesEvent checks if the pointer matches an event.
func (ep EntityPointer) MatchesEvent(evt Event) bool {
return ep.PublicKey == evt.PubKey &&
ep.Kind == evt.Kind &&
evt.Tags.GetD() == ep.Identifier
}
func (ep EntityPointer) AsTagReference() string {
return fmt.Sprintf("%d:%s:%s", ep.Kind, ep.PublicKey, ep.Identifier)
}
func (ep EntityPointer) AsFilter() Filter {
return Filter{
Kinds: []int{ep.Kind},
Authors: []string{ep.PublicKey},
Tags: TagMap{"d": []string{ep.Identifier}},
}
}
func (ep EntityPointer) AsTag() Tag {
if len(ep.Relays) > 0 {
return Tag{"a", ep.AsTagReference(), ep.Relays[0]}
}
return Tag{"a", ep.AsTagReference()}
}
// ExternalPointer represents a pointer to a Nostr profile.
type ExternalPointer struct {
Thing string
}
// ExternalPointerFromTag creates a ExternalPointer from an "i" tag
func ExternalPointerFromTag(refTag Tag) (ExternalPointer, error) {
return ExternalPointer{refTag[1]}, nil
}
func (ep ExternalPointer) MatchesEvent(_ Event) bool { return false }
func (ep ExternalPointer) AsTagReference() string { return ep.Thing }
func (ep ExternalPointer) AsFilter() Filter { return Filter{} }
func (ep ExternalPointer) AsTag() Tag {
return Tag{"i", ep.Thing}
}