From 5d9b5916d20c3e448b86636a86570c434f4f0f90 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 20 May 2026 11:47:55 -0300 Subject: [PATCH] nip29: address updates, generate naddr1 codes. --- nip29/group.go | 35 +++++++++++++++++++---------------- nip29/nip29_test.go | 4 ++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/nip29/group.go b/nip29/group.go index 3cf877d..c256c7d 100644 --- a/nip29/group.go +++ b/nip29/group.go @@ -8,11 +8,18 @@ import ( "strings" "fiatjaf.com/nostr" + "fiatjaf.com/nostr/nip19" ) type GroupAddress struct { + // URL of the relay that is hosting the group Relay string - ID string + + // Group identifier ("d"/"h" tag) + ID string + + // Public key of the relay, used to publish kind:39000/etc events + Self nostr.PubKey } func (gid GroupAddress) String() string { @@ -20,6 +27,10 @@ func (gid GroupAddress) String() string { return fmt.Sprintf("%s'%s", p.Host, gid.ID) } +func (gid GroupAddress) Code() string { + return nip19.EncodeNaddr(gid.Self, 39000, gid.ID, []string{gid.Relay}) +} + func (gid GroupAddress) IsValid() bool { return gid.Relay != "" && gid.ID != "" } @@ -28,14 +39,6 @@ func (gid GroupAddress) Equals(gid2 GroupAddress) bool { return gid.Relay == gid2.Relay && gid.ID == gid2.ID } -func ParseGroupAddress(raw string) (GroupAddress, error) { - spl := strings.Split(raw, "'") - if len(spl) != 2 { - return GroupAddress{}, fmt.Errorf("invalid group id") - } - return GroupAddress{ID: spl[1], Relay: nostr.NormalizeURL(spl[0])}, nil -} - type Group struct { Address GroupAddress @@ -130,15 +133,15 @@ func (group Group) String() string { } // NewGroup takes a group address in the form "'" -func NewGroup(gadstr string) (Group, error) { - gad, err := ParseGroupAddress(gadstr) - if err != nil { - return Group{}, fmt.Errorf("invalid group id '%s': %w", gadstr, err) - } +func NewGroup(relayHost, groupId string) (Group, error) { + relayHost = nostr.NormalizeURL(relayHost) return Group{ - Address: gad, - Name: gad.ID, + Address: GroupAddress{ + Relay: relayHost, + ID: groupId, + }, + Name: groupId, Members: make(map[nostr.PubKey][]*Role), LiveKitParticipants: make([]nostr.PubKey, 0), }, nil diff --git a/nip29/nip29_test.go b/nip29/nip29_test.go index 5889baa..01afe2a 100644 --- a/nip29/nip29_test.go +++ b/nip29/nip29_test.go @@ -15,7 +15,7 @@ const ( ) func TestGroupEventBackAndForth(t *testing.T) { - group1, _ := NewGroup("relay.com'xyz") + group1, _ := NewGroup("relay.com", "xyz") group1.Name = "banana" group1.Private = true meta1 := group1.ToMetadataEvent() @@ -31,7 +31,7 @@ func TestGroupEventBackAndForth(t *testing.T) { } require.True(t, hasPrivate, "translation of group1 to metadata event failed: %s", meta1) - group2, _ := NewGroup("groups.com'abc") + group2, _ := NewGroup("groups.com", "abc") alicePub, _ := nostr.PubKeyFromHex(ALICE) group2.Members[alicePub] = []*Role{{Name: "nada"}} admins2 := group2.ToAdminsEvent()