mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-05-18 06:29:57 +02:00
nip46: support nip05 identifier in client.
This commit is contained in:
parent
e4f0509689
commit
e0ba846a03
@ -2,7 +2,6 @@ package nip05
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -18,9 +17,31 @@ type WellKnownResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func QueryIdentifier(ctx context.Context, fullname string) (*nostr.ProfilePointer, error) {
|
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, "@")
|
spl := strings.Split(fullname, "@")
|
||||||
|
|
||||||
var name, domain string
|
var domain string
|
||||||
switch len(spl) {
|
switch len(spl) {
|
||||||
case 1:
|
case 1:
|
||||||
name = "_"
|
name = "_"
|
||||||
@ -29,17 +50,13 @@ func QueryIdentifier(ctx context.Context, fullname string) (*nostr.ProfilePointe
|
|||||||
name = spl[0]
|
name = spl[0]
|
||||||
domain = spl[1]
|
domain = spl[1]
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("not a valid nip-05 identifier")
|
return resp, name, fmt.Errorf("not a valid nip-05 identifier")
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Index(domain, ".") == -1 {
|
|
||||||
return nil, fmt.Errorf("hostname doesn't have a dot")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, "GET",
|
req, err := http.NewRequestWithContext(ctx, "GET",
|
||||||
fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name), nil)
|
fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name), nil)
|
||||||
if err != 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{
|
client := &http.Client{
|
||||||
@ -49,32 +66,16 @@ func QueryIdentifier(ctx context.Context, fullname string) (*nostr.ProfilePointe
|
|||||||
}
|
}
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("request failed: %w", err)
|
return resp, name, fmt.Errorf("request failed: %w", err)
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
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, fmt.Errorf("failed to decode json response: %w", err)
|
return resp, name, fmt.Errorf("failed to decode json response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pubkey, ok := result.Names[name]
|
return resp, name, nil
|
||||||
if !ok {
|
|
||||||
return &nostr.ProfilePointer{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NormalizeIdentifier(fullname string) string {
|
func NormalizeIdentifier(fullname string) string {
|
||||||
|
@ -34,26 +34,37 @@ type BunkerClient struct {
|
|||||||
func ConnectBunker(
|
func ConnectBunker(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
clientSecretKey string,
|
clientSecretKey string,
|
||||||
bunkerURL string,
|
bunkerURLOrNIP05 string,
|
||||||
pool *nostr.SimplePool,
|
pool *nostr.SimplePool,
|
||||||
) (*BunkerClient, error) {
|
) (*BunkerClient, error) {
|
||||||
parsed, err := url.Parse(bunkerURL)
|
parsed, err := url.Parse(bunkerURLOrNIP05)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid url: %w", err)
|
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)
|
return nil, fmt.Errorf("wrong scheme '%s', must be bunker://", parsed.Scheme)
|
||||||
}
|
}
|
||||||
|
|
||||||
target := parsed.Host
|
|
||||||
if !nostr.IsValidPublicKey(target) {
|
if !nostr.IsValidPublicKey(target) {
|
||||||
return nil, fmt.Errorf("'%s' is not a valid public key hex", 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 {
|
if pool == nil {
|
||||||
pool = nostr.NewSimplePool(ctx)
|
pool = nostr.NewSimplePool(ctx)
|
||||||
}
|
}
|
||||||
|
26
nip46/wellknownnostrjson.go
Normal file
26
nip46/wellknownnostrjson.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user