nip19: decode to pointer.

This commit is contained in:
fiatjaf 2025-03-06 11:49:10 -03:00
parent 4fb6fcd9a2
commit ec55b1fac8
2 changed files with 30 additions and 2 deletions

View File

@ -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)
}
}

View File

@ -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)