Skip to content

Commit

Permalink
sdk: validate nip05.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Nov 27, 2024
1 parent a1a4c04 commit 2519cab
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions sdk/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2519cab

Please sign in to comment.