mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-30 02:20:37 +02:00
nip46: support nip05 identifier in client.
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
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
|
||||
}
|
Reference in New Issue
Block a user