take context.Context for nip-05 resolving.

This commit is contained in:
fiatjaf
2023-05-04 13:20:15 -03:00
parent 53e0935308
commit 1c118cd83e
2 changed files with 19 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
package nip05 package nip05
import ( import (
"context"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
@@ -20,7 +21,7 @@ type WellKnownResponse struct {
Relays key2RelaysMap `json:"relays"` // NIP-35 Relays key2RelaysMap `json:"relays"` // NIP-35
} }
func QueryIdentifier(fullname string) *nostr.ProfilePointer { func QueryIdentifier(ctx context.Context, fullname string) (*nostr.ProfilePointer, error) {
spl := strings.Split(fullname, "@") spl := strings.Split(fullname, "@")
var name, domain string var name, domain string
@@ -32,35 +33,41 @@ func QueryIdentifier(fullname string) *nostr.ProfilePointer {
name = spl[0] name = spl[0]
domain = spl[1] domain = spl[1]
default: default:
return nil return nil, fmt.Errorf("not a valid nip-05 identifier")
} }
if strings.Index(domain, ".") == -1 { if strings.Index(domain, ".") == -1 {
return nil return nil, fmt.Errorf("hostname doesn't have a dot")
} }
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name), nil)
if err != nil {
return nil, fmt.Errorf("failed to create a request: %w", err)
}
client := &http.Client{ client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse return http.ErrUseLastResponse
}, },
} }
res, err := client.Get(fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name)) res, err := client.Do(req)
if err != nil { if err != nil {
return nil return nil, fmt.Errorf("request failed: %w", err)
} }
var result WellKnownResponse var result WellKnownResponse
if err := json.NewDecoder(res.Body).Decode(&result); err != nil { if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
return nil return nil, fmt.Errorf("failed to decode json response: %w", err)
} }
pubkey, ok := result.Names[name] pubkey, ok := result.Names[name]
if !ok { if !ok {
return nil return nil, nil
} }
if len(pubkey) == 64 { if len(pubkey) == 64 {
if _, err := hex.DecodeString(pubkey); err != nil { if _, err := hex.DecodeString(pubkey); err != nil {
return nil return nil, nil
} }
} }
@@ -69,7 +76,7 @@ func QueryIdentifier(fullname string) *nostr.ProfilePointer {
return &nostr.ProfilePointer{ return &nostr.ProfilePointer{
PublicKey: pubkey, PublicKey: pubkey,
Relays: relays, Relays: relays,
} }, nil
} }
func NormalizeIdentifier(fullname string) string { func NormalizeIdentifier(fullname string) string {

View File

@@ -1,6 +1,7 @@
package sdk package sdk
import ( import (
"context"
"encoding/hex" "encoding/hex"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
@@ -9,7 +10,7 @@ import (
) )
// InputToProfile turns any npub/nprofile/hex/nip05 input into a ProfilePointer (or nil) // InputToProfile turns any npub/nprofile/hex/nip05 input into a ProfilePointer (or nil)
func InputToProfile(input string) *nostr.ProfilePointer { func InputToProfile(ctx context.Context, input string) *nostr.ProfilePointer {
// handle if it is a hex string // handle if it is a hex string
if len(input) == 64 { if len(input) == 64 {
if _, err := hex.DecodeString(input); err == nil { if _, err := hex.DecodeString(input); err == nil {
@@ -29,7 +30,7 @@ func InputToProfile(input string) *nostr.ProfilePointer {
} }
// handle nip05 ids, if that's the case // handle nip05 ids, if that's the case
pp := nip05.QueryIdentifier(input) pp, _ := nip05.QueryIdentifier(ctx, input)
if pp != nil { if pp != nil {
return pp return pp
} }