nip19: helper for building nevent from RelayEvent.

This commit is contained in:
fiatjaf 2025-02-07 18:01:59 -03:00
parent 1b31dd892e
commit f19efb4013
3 changed files with 39 additions and 33 deletions

34
nip19/helpers.go Normal file
View File

@ -0,0 +1,34 @@
package nip19
import (
"bytes"
)
const (
TLVDefault uint8 = 0
TLVRelay uint8 = 1
TLVAuthor uint8 = 2
TLVKind uint8 = 3
)
func readTLVEntry(data []byte) (typ uint8, value []byte) {
if len(data) < 2 {
return 0, nil
}
typ = data[0]
length := int(data[1])
if len(data) < 2+length {
return typ, nil
}
value = data[2 : 2+length]
return
}
func writeTLVEntry(buf *bytes.Buffer, typ uint8, value []byte) {
length := len(value)
buf.WriteByte(typ)
buf.WriteByte(uint8(length))
buf.Write(value)
}

View File

@ -1,34 +1,8 @@
package nip19
import (
"bytes"
)
import "github.com/nbd-wtf/go-nostr"
const (
TLVDefault uint8 = 0
TLVRelay uint8 = 1
TLVAuthor uint8 = 2
TLVKind uint8 = 3
)
func readTLVEntry(data []byte) (typ uint8, value []byte) {
if len(data) < 2 {
return 0, nil
}
typ = data[0]
length := int(data[1])
if len(data) < 2+length {
return typ, nil
}
value = data[2 : 2+length]
return
}
func writeTLVEntry(buf *bytes.Buffer, typ uint8, value []byte) {
length := len(value)
buf.WriteByte(typ)
buf.WriteByte(uint8(length))
buf.Write(value)
func NeventFromRelayEvent(ie nostr.RelayEvent) string {
v, _ := EncodeEvent(ie.ID, []string{ie.Relay.URL}, ie.PubKey)
return v
}

View File

@ -46,9 +46,7 @@ type RelayEvent struct {
Relay *Relay
}
func (ie RelayEvent) String() string {
return fmt.Sprintf("[%s] >> %s", ie.Relay.URL, ie.Event)
}
func (ie RelayEvent) String() string { return fmt.Sprintf("[%s] >> %s", ie.Relay.URL, ie.Event) }
type PoolOption interface {
ApplyPoolOption(*SimplePool)