diff --git a/sdk/metadata.go b/sdk/metadata.go index 529e4cf..92e46e0 100644 --- a/sdk/metadata.go +++ b/sdk/metadata.go @@ -9,6 +9,7 @@ import ( "time" "github.com/nbd-wtf/go-nostr" + "github.com/nbd-wtf/go-nostr/nip05" "github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/sdk/hints" ) @@ -26,6 +27,9 @@ type ProfileMetadata struct { Banner string `json:"banner,omitempty"` NIP05 string `json:"nip05,omitempty"` LUD16 string `json:"lud16,omitempty"` + + nip05Valid bool + nip05LastAttempt time.Time } func (p ProfileMetadata) Npub() string { @@ -53,6 +57,25 @@ func (p ProfileMetadata) ShortName() string { return p.NpubShort() } +func (p *ProfileMetadata) NIP05Valid(ctx context.Context) bool { + if p.NIP05 == "" { + return false + } + + now := time.Now() + if p.nip05LastAttempt.Before(now.AddDate(0, 0, -7)) { + // must revalidate + p.nip05LastAttempt = now + pp, err := nip05.QueryIdentifier(ctx, p.NIP05) + if err != nil { + p.nip05Valid = false + } else { + p.nip05Valid = pp.PublicKey == p.PubKey + } + } + return p.nip05Valid +} + // FetchProfileFromInput takes an nprofile, npub, nip05 or hex pubkey and returns a ProfileMetadata, // updating the hintsDB in the process with any eventual relay hints func (sys System) FetchProfileFromInput(ctx context.Context, nip19OrNip05Code string) (ProfileMetadata, error) {