From ec55b1fac8caf0bb10f90a2b0dfc94e949629aaf Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 6 Mar 2025 11:49:10 -0300 Subject: [PATCH] nip19: decode to pointer. --- nip19/pointer.go | 28 +++++++++++++++++++++++++++- nip19/utils.go | 4 +++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/nip19/pointer.go b/nip19/pointer.go index 4eef924..ef400aa 100644 --- a/nip19/pointer.go +++ b/nip19/pointer.go @@ -1,6 +1,10 @@ package nip19 -import "github.com/nbd-wtf/go-nostr" +import ( + "fmt" + + "github.com/nbd-wtf/go-nostr" +) func EncodePointer(pointer nostr.Pointer) string { switch v := pointer.(type) { @@ -16,3 +20,25 @@ func EncodePointer(pointer nostr.Pointer) string { } return "" } + +func ToPointer(code string) (nostr.Pointer, error) { + prefix, data, err := Decode(code) + if err != nil { + return nil, err + } + + switch prefix { + case "npub": + return nostr.ProfilePointer{PublicKey: data.(string)}, nil + case "nprofile": + return data.(nostr.ProfilePointer), nil + case "nevent": + return data.(nostr.EventPointer), nil + case "note": + return nostr.EventPointer{ID: data.(string)}, nil + case "naddr": + return data.(nostr.EntityPointer), nil + default: + return nil, fmt.Errorf("unexpected prefix '%s' to '%s'", prefix, code) + } +} diff --git a/nip19/utils.go b/nip19/utils.go index 0d2718a..bbcb985 100644 --- a/nip19/utils.go +++ b/nip19/utils.go @@ -1,6 +1,8 @@ package nip19 -import "github.com/nbd-wtf/go-nostr" +import ( + "github.com/nbd-wtf/go-nostr" +) func NeventFromRelayEvent(ie nostr.RelayEvent) string { v, _ := EncodeEvent(ie.ID, []string{ie.Relay.URL}, ie.PubKey)