diff --git a/nip19/nip19.go b/nip19/nip19.go index 7e64d58..dccd2d4 100644 --- a/nip19/nip19.go +++ b/nip19/nip19.go @@ -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) diff --git a/nip19/nip19_test.go b/nip19/nip19_test.go index 5b976de..438f281 100644 --- a/nip19/nip19_test.go +++ b/nip19/nip19_test.go @@ -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") + } +} diff --git a/pointers.go b/pointers.go index 8efa8cd..2daade0 100644 --- a/pointers.go +++ b/pointers.go @@ -8,6 +8,7 @@ type ProfilePointer struct { type EventPointer struct { ID string Relays []string + Author string } type EntityPointer struct {