Skip to content

Commit

Permalink
dedicated Kind type.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jan 1, 2024
1 parent 5938a71 commit 9624183
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 52 deletions.
4 changes: 2 additions & 2 deletions binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func UnmarshalBinary(data []byte, evt *Event) (err error) {
copy(evt.PubKey[:], data[32:64])
copy(evt.Sig[:], data[64:128])
evt.CreatedAt = nostr.Timestamp(binary.BigEndian.Uint32(data[128:132]))
evt.Kind = binary.BigEndian.Uint16(data[132:134])
evt.Kind = nostr.Kind(binary.BigEndian.Uint16(data[132:134]))
contentLength := int(binary.BigEndian.Uint16(data[134:136]))
evt.Content = string(data[136 : 136+contentLength])

Expand Down Expand Up @@ -54,7 +54,7 @@ func MarshalBinary(evt *Event) []byte {
copy(buf[32:64], evt.PubKey[:])
copy(buf[64:128], evt.Sig[:])
binary.BigEndian.PutUint32(buf[128:132], uint32(evt.CreatedAt))
binary.BigEndian.PutUint16(buf[132:134], evt.Kind)
binary.BigEndian.PutUint16(buf[132:134], uint16(evt.Kind))
binary.BigEndian.PutUint16(buf[134:136], uint16(len(content)))
copy(buf[136:], content)

Expand Down
6 changes: 3 additions & 3 deletions binary/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Event struct {
PubKey [32]byte
Sig [64]byte
ID [32]byte
Kind uint16
Kind nostr.Kind
CreatedAt nostr.Timestamp
Content string
Tags nostr.Tags
Expand All @@ -20,7 +20,7 @@ func BinaryEvent(evt *nostr.Event) *Event {
bevt := Event{
Tags: evt.Tags,
Content: evt.Content,
Kind: uint16(evt.Kind),
Kind: evt.Kind,
CreatedAt: evt.CreatedAt,
}

Expand All @@ -35,7 +35,7 @@ func (bevt *Event) ToNormalEvent() *nostr.Event {
return &nostr.Event{
Tags: bevt.Tags,
Content: bevt.Content,
Kind: int(bevt.Kind),
Kind: bevt.Kind,
CreatedAt: bevt.CreatedAt,
ID: hex.EncodeToString(bevt.ID[:]),
PubKey: hex.EncodeToString(bevt.PubKey[:]),
Expand Down
2 changes: 1 addition & 1 deletion binary/hybrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Unmarshal(data []byte, evt *nostr.Event) (err error) {
evt.PubKey = hex.EncodeToString(data[32:64])
evt.Sig = hex.EncodeToString(data[64:128])
evt.CreatedAt = nostr.Timestamp(binary.BigEndian.Uint32(data[128:132]))
evt.Kind = int(binary.BigEndian.Uint16(data[132:134]))
evt.Kind = nostr.Kind(binary.BigEndian.Uint16(data[132:134]))
contentLength := int(binary.BigEndian.Uint16(data[134:136]))
evt.Content = string(data[136 : 136+contentLength])

Expand Down
37 changes: 1 addition & 36 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Event struct {
ID string `json:"id"`
PubKey string `json:"pubkey"`
CreatedAt Timestamp `json:"created_at"`
Kind int `json:"kind"`
Kind Kind `json:"kind"`
Tags Tags `json:"tags"`
Content string `json:"content"`
Sig string `json:"sig"`
Expand All @@ -23,41 +23,6 @@ type Event struct {
extra map[string]any
}

const (
KindProfileMetadata int = 0
KindTextNote int = 1
KindRecommendServer int = 2
KindContactList int = 3
KindEncryptedDirectMessage int = 4
KindDeletion int = 5
KindRepost int = 6
KindReaction int = 7
KindChannelCreation int = 40
KindChannelMetadata int = 41
KindChannelMessage int = 42
KindChannelHideMessage int = 43
KindChannelMuteUser int = 44
KindFileMetadata int = 1063
KindZapRequest int = 9734
KindZap int = 9735
KindMuteList int = 10000
KindPinList int = 10001
KindRelayListMetadata int = 10002
KindNWCWalletInfo int = 13194
KindClientAuthentication int = 22242
KindNWCWalletRequest int = 23194
KindNWCWalletResponse int = 23195
KindNostrConnect int = 24133
KindCategorizedPeopleList int = 30000
KindCategorizedBookmarksList int = 30001
KindProfileBadges int = 30008
KindBadgeDefinition int = 30009
KindStallDefinition int = 30017
KindProductDefinition int = 30018
KindArticle int = 30023
KindApplicationSpecificData int = 30078
)

// Event Stringer interface, just returns the raw JSON as a string.
func (evt Event) String() string {
j, _ := easyjson.Marshal(evt)
Expand Down
4 changes: 2 additions & 2 deletions event_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
// this filters for messages tagged with the user, mainly replies.
t["p"] = []string{v.(string)}
filters = []nostr.Filter{{
Kinds: []int{nostr.KindTextNote},
Kinds: []nostr.Kind{nostr.KindTextNote},
Tags: t,
// limit = 3, get the three most recent notes
Limit: 3,
Expand Down
2 changes: 1 addition & 1 deletion filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Filters []Filter

type Filter struct {
IDs []string `json:"ids,omitempty"`
Kinds []int `json:"kinds,omitempty"`
Kinds []Kind `json:"kinds,omitempty"`
Authors []string `json:"authors,omitempty"`
Tags TagMap `json:"-,omitempty"`
Since *Timestamp `json:"since,omitempty"`
Expand Down
7 changes: 3 additions & 4 deletions filter_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions kinds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nostr

type Kind uint16

const (
KindProfileMetadata Kind = 0
KindTextNote Kind = 1
KindRecommendServer Kind = 2
KindContactList Kind = 3
KindEncryptedDirectMessage Kind = 4
KindDeletion Kind = 5
KindRepost Kind = 6
KindReaction Kind = 7
KindChannelCreation Kind = 40
KindChannelMetadata Kind = 41
KindChannelMessage Kind = 42
KindChannelHideMessage Kind = 43
KindChannelMuteUser Kind = 44
KindFileMetadata Kind = 1063
KindZapRequest Kind = 9734
KindZap Kind = 9735
KindMuteList Kind = 10000
KindPinList Kind = 10001
KindRelayListMetadata Kind = 10002
KindNWCWalletInfo Kind = 13194
KindClientAuthentication Kind = 22242
KindNWCWalletRequest Kind = 23194
KindNWCWalletResponse Kind = 23195
KindNostrConnect Kind = 24133
KindCategorizedPeopleList Kind = 30000
KindCategorizedBookmarksList Kind = 30001
KindProfileBadges Kind = 30008
KindBadgeDefinition Kind = 30009
KindStallDefinition Kind = 30017
KindProductDefinition Kind = 30018
KindArticle Kind = 30023
KindApplicationSpecificData Kind = 30078
)
5 changes: 3 additions & 2 deletions nson/nson.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func Unmarshal(data string, evt *nostr.Event) (err error) {
// kind
kindChars := int(nsonDescriptors[0])
kindStart := NSON_VALUES_START + nsonSize + 9 // len(`","kind":`)
evt.Kind, _ = strconv.Atoi(data[kindStart : kindStart+kindChars])
kind, _ := strconv.ParseUint(data[kindStart:kindStart+kindChars], 10, 16)
evt.Kind = nostr.Kind(kind)

// content
contentChars := int(binary.BigEndian.Uint16(nsonDescriptors[1:3]))
Expand Down Expand Up @@ -151,7 +152,7 @@ func Marshal(evt *nostr.Event) (string, error) {
tagBuilder.WriteString(`]}`)
nsonBuf = nsonBuf[0 : nsonIndex+1]

kind := strconv.Itoa(evt.Kind)
kind := strconv.FormatUint(uint64(evt.Kind), 10)
kindChars := len(kind)
nsonBuf[0] = uint8(kindChars)

Expand Down

0 comments on commit 9624183

Please sign in to comment.