nip29: address updates, generate naddr1 codes.

This commit is contained in:
fiatjaf
2026-05-20 11:47:55 -03:00
parent e11e32e3e2
commit 5d9b5916d2
2 changed files with 21 additions and 18 deletions
+19 -16
View File
@@ -8,11 +8,18 @@ import (
"strings" "strings"
"fiatjaf.com/nostr" "fiatjaf.com/nostr"
"fiatjaf.com/nostr/nip19"
) )
type GroupAddress struct { type GroupAddress struct {
// URL of the relay that is hosting the group
Relay string 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 { func (gid GroupAddress) String() string {
@@ -20,6 +27,10 @@ func (gid GroupAddress) String() string {
return fmt.Sprintf("%s'%s", p.Host, gid.ID) 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 { func (gid GroupAddress) IsValid() bool {
return gid.Relay != "" && gid.ID != "" 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 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 { type Group struct {
Address GroupAddress Address GroupAddress
@@ -130,15 +133,15 @@ func (group Group) String() string {
} }
// NewGroup takes a group address in the form "<id>'<relay-hostname>" // NewGroup takes a group address in the form "<id>'<relay-hostname>"
func NewGroup(gadstr string) (Group, error) { func NewGroup(relayHost, groupId string) (Group, error) {
gad, err := ParseGroupAddress(gadstr) relayHost = nostr.NormalizeURL(relayHost)
if err != nil {
return Group{}, fmt.Errorf("invalid group id '%s': %w", gadstr, err)
}
return Group{ return Group{
Address: gad, Address: GroupAddress{
Name: gad.ID, Relay: relayHost,
ID: groupId,
},
Name: groupId,
Members: make(map[nostr.PubKey][]*Role), Members: make(map[nostr.PubKey][]*Role),
LiveKitParticipants: make([]nostr.PubKey, 0), LiveKitParticipants: make([]nostr.PubKey, 0),
}, nil }, nil
+2 -2
View File
@@ -15,7 +15,7 @@ const (
) )
func TestGroupEventBackAndForth(t *testing.T) { func TestGroupEventBackAndForth(t *testing.T) {
group1, _ := NewGroup("relay.com'xyz") group1, _ := NewGroup("relay.com", "xyz")
group1.Name = "banana" group1.Name = "banana"
group1.Private = true group1.Private = true
meta1 := group1.ToMetadataEvent() 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) 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) alicePub, _ := nostr.PubKeyFromHex(ALICE)
group2.Members[alicePub] = []*Role{{Name: "nada"}} group2.Members[alicePub] = []*Role{{Name: "nada"}}
admins2 := group2.ToAdminsEvent() admins2 := group2.ToAdminsEvent()