Skip to content

Commit

Permalink
nip46: support nip05 identifier in client.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Feb 29, 2024
1 parent e4f0509 commit e0ba846
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 35 deletions.
55 changes: 28 additions & 27 deletions nip05/nip05.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nip05

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -18,9 +17,31 @@ type WellKnownResponse struct {
}

func QueryIdentifier(ctx context.Context, fullname string) (*nostr.ProfilePointer, error) {
result, name, err := Fetch(ctx, fullname)
if err != nil {
return nil, err
}

pubkey, ok := result.Names[name]
if !ok {
return nil, fmt.Errorf("no entry for name '%s'", name)
}

if !nostr.IsValidPublicKey(pubkey) {
return nil, fmt.Errorf("got an invalid public key '%s'", pubkey)
}

relays, _ := result.Relays[pubkey]
return &nostr.ProfilePointer{
PublicKey: pubkey,
Relays: relays,
}, nil
}

func Fetch(ctx context.Context, fullname string) (resp WellKnownResponse, name string, err error) {
spl := strings.Split(fullname, "@")

var name, domain string
var domain string
switch len(spl) {
case 1:
name = "_"
Expand All @@ -29,17 +50,13 @@ func QueryIdentifier(ctx context.Context, fullname string) (*nostr.ProfilePointe
name = spl[0]
domain = spl[1]
default:
return nil, fmt.Errorf("not a valid nip-05 identifier")
}

if strings.Index(domain, ".") == -1 {
return nil, fmt.Errorf("hostname doesn't have a dot")
return resp, name, fmt.Errorf("not a valid nip-05 identifier")
}

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)
return resp, name, fmt.Errorf("failed to create a request: %w", err)
}

client := &http.Client{
Expand All @@ -49,32 +66,16 @@ func QueryIdentifier(ctx context.Context, fullname string) (*nostr.ProfilePointe
}
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
return resp, name, fmt.Errorf("request failed: %w", err)
}
defer res.Body.Close()

var result WellKnownResponse
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode json response: %w", err)
}

pubkey, ok := result.Names[name]
if !ok {
return &nostr.ProfilePointer{}, nil
return resp, name, fmt.Errorf("failed to decode json response: %w", err)
}

if len(pubkey) == 64 {
if _, err := hex.DecodeString(pubkey); err != nil {
return &nostr.ProfilePointer{}, nil
}
}

relays, _ := result.Relays[pubkey]

return &nostr.ProfilePointer{
PublicKey: pubkey,
Relays: relays,
}, nil
return resp, name, nil
}

func NormalizeIdentifier(fullname string) string {
Expand Down
27 changes: 19 additions & 8 deletions nip46/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,37 @@ type BunkerClient struct {
func ConnectBunker(
ctx context.Context,
clientSecretKey string,
bunkerURL string,
bunkerURLOrNIP05 string,
pool *nostr.SimplePool,
) (*BunkerClient, error) {
parsed, err := url.Parse(bunkerURL)
parsed, err := url.Parse(bunkerURLOrNIP05)
if err != nil {
return nil, fmt.Errorf("invalid url: %w", err)
}

if parsed.Scheme != "bunker" {
// assume it's a bunker url (will fail later if not)
secret := parsed.Query().Get("secret")
relays := parsed.Query()["relay"]
target := parsed.Host

if parsed.Scheme == "" {
// could be a NIP-05
pubkey, relays_, err := queryWellKnownNostrJson(ctx, bunkerURLOrNIP05)
if err != nil {
return nil, fmt.Errorf("failed to query nip05: %w", err)
}
target = pubkey
relays = relays_
} else if parsed.Scheme == "bunker" {
// this is what we were expecting, so just move on
} else {
// otherwise fail here
return nil, fmt.Errorf("wrong scheme '%s', must be bunker://", parsed.Scheme)
}

target := parsed.Host
if !nostr.IsValidPublicKey(target) {
return nil, fmt.Errorf("'%s' is not a valid public key hex", target)
}

secret := parsed.Query().Get("secret")
relays := parsed.Query()["relay"]

if pool == nil {
pool = nostr.NewSimplePool(ctx)
}
Expand Down
26 changes: 26 additions & 0 deletions nip46/wellknownnostrjson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nip46

import (
"context"
"fmt"

"github.com/nbd-wtf/go-nostr/nip05"
)

func queryWellKnownNostrJson(ctx context.Context, fullname string) (pubkey string, relays []string, err error) {
result, name, err := nip05.Fetch(ctx, fullname)
if err != nil {
return "", nil, err
}

pubkey, ok := result.Names[name]
if !ok {
return "", nil, fmt.Errorf("no entry found for the '%s' name", name)
}
relays, _ = result.NIP46[pubkey]
if !ok {
return "", nil, fmt.Errorf("no bunker relays found for the '%s' name", name)
}

return pubkey, relays, nil
}

0 comments on commit e0ba846

Please sign in to comment.