You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

151 lines
3.6 KiB

package nostr
import (
"context"
"encoding/json"
"fmt"
"gitcitadel-online/internal/logger"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
)
// ShortenNpub shortens an npub to npub1...xyz format (first 8 + last 4 characters)
func ShortenNpub(npub string) string {
if len(npub) <= 12 {
return npub
}
return npub[:8] + "..." + npub[len(npub)-4:]
}
// PubkeyToNpub converts a hex pubkey to npub format
func PubkeyToNpub(pubkey string) (string, error) {
npub, err := nip19.EncodePublicKey(pubkey)
if err != nil {
return "", fmt.Errorf("failed to encode pubkey to npub: %w", err)
}
return npub, nil
}
// ShortenPubkey converts a pubkey to shortened npub format
func ShortenPubkey(pubkey string) string {
npub, err := PubkeyToNpub(pubkey)
if err != nil {
// Fallback: shorten hex pubkey
if len(pubkey) > 12 {
return pubkey[:8] + "..." + pubkey[len(pubkey)-4:]
}
return pubkey
}
return ShortenNpub(npub)
}
// Profile represents a parsed kind 0 profile event
type Profile struct {
Event *nostr.Event
Pubkey string
Name string
DisplayName string
About string
Picture string
Website string
NIP05 string
Lud16 string
Banner string
RawJSON map[string]interface{} // Store all other fields
}
// FetchProfile fetches a kind 0 profile event from an npub
func (c *Client) FetchProfile(ctx context.Context, npub string) (*Profile, error) {
// Decode npub to get pubkey
prefix, value, err := nip19.Decode(npub)
if err != nil {
return nil, fmt.Errorf("failed to decode npub: %w", err)
}
if prefix != "npub" {
return nil, fmt.Errorf("invalid npub prefix: expected 'npub', got '%s'", prefix)
}
pubkey, ok := value.(string)
if !ok {
return nil, fmt.Errorf("failed to parse npub: unexpected type")
}
// Create filter for kind 0 profile event
filter := nostr.Filter{
Kinds: []int{0},
Authors: []string{pubkey},
Limit: 1,
}
logger.WithFields(map[string]interface{}{
"npub": npub,
"pubkey": pubkey,
}).Debug("Fetching profile")
// Fetch the event
event, err := c.FetchEvent(ctx, filter)
if err != nil {
return nil, fmt.Errorf("failed to fetch profile event: %w", err)
}
// Parse the profile
profile, err := ParseProfile(event)
if err != nil {
return nil, fmt.Errorf("failed to parse profile: %w", err)
}
return profile, nil
}
// ParseProfile parses a kind 0 profile event
func ParseProfile(event *nostr.Event) (*Profile, error) {
if event.Kind != 0 {
return nil, fmt.Errorf("expected kind 0, got %d", event.Kind)
}
profile := &Profile{
Event: event,
Pubkey: event.PubKey,
RawJSON: make(map[string]interface{}),
}
// Parse JSON content
var content map[string]interface{}
if err := json.Unmarshal([]byte(event.Content), &content); err != nil {
return nil, fmt.Errorf("failed to parse profile JSON: %w", err)
}
// Extract common fields
if name, ok := content["name"].(string); ok {
profile.Name = name
}
if displayName, ok := content["display_name"].(string); ok {
profile.DisplayName = displayName
}
if about, ok := content["about"].(string); ok {
profile.About = about
}
if picture, ok := content["picture"].(string); ok {
profile.Picture = picture
}
if website, ok := content["website"].(string); ok {
profile.Website = website
}
if nip05, ok := content["nip05"].(string); ok {
profile.NIP05 = nip05
}
if lud16, ok := content["lud16"].(string); ok {
profile.Lud16 = lud16
}
if banner, ok := content["banner"].(string); ok {
profile.Banner = banner
}
// Store all fields in RawJSON for access to any other fields
profile.RawJSON = content
return profile, nil
}