mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-26 17:52:20 +01:00
support author on nevent.
This commit is contained in:
parent
c982ad0ab1
commit
332ae1ff7f
@ -71,6 +71,8 @@ func Decode(bech32string string) (prefix string, value any, err error) {
|
||||
result.ID = hex.EncodeToString(v)
|
||||
case TLVRelay:
|
||||
result.Relays = append(result.Relays, string(v))
|
||||
case TLVAuthor:
|
||||
result.Author = hex.EncodeToString(v)
|
||||
default:
|
||||
// ignore
|
||||
}
|
||||
@ -173,10 +175,10 @@ func EncodeProfile(publicKeyHex string, relays []string) (string, error) {
|
||||
return encode("nprofile", bits5)
|
||||
}
|
||||
|
||||
func EncodeEvent(eventIdHex string, relays []string) (string, error) {
|
||||
func EncodeEvent(eventIdHex string, relays []string, author string) (string, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
id, err := hex.DecodeString(eventIdHex)
|
||||
if err != nil {
|
||||
if err != nil || len(id) != 32 {
|
||||
return "", fmt.Errorf("invalid id '%s': %w", eventIdHex, err)
|
||||
}
|
||||
writeTLVEntry(buf, TLVDefault, id)
|
||||
@ -185,6 +187,10 @@ func EncodeEvent(eventIdHex string, relays []string) (string, error) {
|
||||
writeTLVEntry(buf, TLVRelay, []byte(url))
|
||||
}
|
||||
|
||||
if pubkey, _ := hex.DecodeString(author); len(pubkey) == 32 {
|
||||
writeTLVEntry(buf, TLVAuthor, pubkey)
|
||||
}
|
||||
|
||||
bits5, err := convertBits(buf.Bytes(), 8, 5, true)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to convert bits: %w", err)
|
||||
|
@ -169,3 +169,40 @@ func TestDecodeNaddrWithoutRelays(t *testing.T) {
|
||||
t.Error("relays should have been an empty array")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeDecodeNEventTestEncodeDecodeNEvent(t *testing.T) {
|
||||
nevent, err := EncodeEvent(
|
||||
"45326f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194",
|
||||
[]string{"wss://banana.com"},
|
||||
"7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751abb88",
|
||||
)
|
||||
if err != nil {
|
||||
t.Errorf("shouldn't error: %s", err)
|
||||
}
|
||||
|
||||
prefix, res, err := Decode(nevent)
|
||||
if err != nil {
|
||||
t.Errorf("shouldn't error: %s", err)
|
||||
}
|
||||
|
||||
if prefix != "nevent" {
|
||||
t.Errorf("should have 'nevent' prefix, not '%s'", prefix)
|
||||
}
|
||||
|
||||
ep, ok := res.(nostr.EventPointer)
|
||||
if !ok {
|
||||
t.Errorf("'%s' should be an nevent, not %v", nevent, res)
|
||||
}
|
||||
|
||||
if ep.Author != "7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751abb88" {
|
||||
t.Error("wrong author")
|
||||
}
|
||||
|
||||
if ep.ID != "45326f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194" {
|
||||
t.Error("wrong id")
|
||||
}
|
||||
|
||||
if len(ep.Relays) != 1 || ep.Relays[0] != "wss://banana.com" {
|
||||
t.Error("wrong relay")
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ type ProfilePointer struct {
|
||||
type EventPointer struct {
|
||||
ID string
|
||||
Relays []string
|
||||
Author string
|
||||
}
|
||||
|
||||
type EntityPointer struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user