From 65fa12c9985e7b7b5c0dff9696ade2e7f6ba9ec9 Mon Sep 17 00:00:00 2001 From: dd dd Date: Sat, 27 Jul 2024 23:33:35 +0200 Subject: [PATCH] Refactor public key handling and decoding Added schnorr public key parsing and hex package functions. Simplified subdomain decoding --- exit/exit.go | 22 +++++++++++++++++++++- netstr/conn.go | 20 ++++++++++++++------ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/exit/exit.go b/exit/exit.go index 7d5feeb..7b49751 100644 --- a/exit/exit.go +++ b/exit/exit.go @@ -3,11 +3,14 @@ package exit import ( "crypto/tls" "encoding/base32" + "encoding/hex" "fmt" "github.com/asmogo/nws/config" "github.com/asmogo/nws/netstr" "github.com/asmogo/nws/protocol" "github.com/asmogo/nws/socks5" + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/schnorr" "github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr/nip04" "github.com/nbd-wtf/go-nostr/nip19" @@ -109,7 +112,13 @@ func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit { domain = fmt.Sprintf("%s.%s", domain, base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString([]byte(relayUrl))) } } - domain = strings.ToLower(fmt.Sprintf("%s.%s.nostr", domain, fmt.Sprintf("%s.%s", exit.publicKey[:32], exit.publicKey[32:]))) + + decoded, err := GetPublicKey(exit.config.NostrPrivateKey) + if err != nil { + panic(err) + } + + domain = strings.ToLower(fmt.Sprintf("%s.%s.nostr", domain, decoded)) slog.Info("created exit node", "profile", profile, "domain", domain) // setup subscriptions err = exit.setSubscriptions(ctx) @@ -119,6 +128,17 @@ func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit { return exit } +func GetPublicKey(sk string) (string, error) { + b, err := hex.DecodeString(sk) + if err != nil { + return "", err + } + + _, pk := btcec.PrivKeyFromBytes(b) + + return base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(schnorr.SerializePubKey(pk)), nil +} + // setSubscriptions sets up subscriptions for the Exit node to receive incoming events from the specified relays. // It first obtains the public key using the configured Nostr private key. // Then it calls the `handleSubscription` method to open a subscription to the relays with the specified filters. diff --git a/netstr/conn.go b/netstr/conn.go index 4889b8d..fd0d2a7 100644 --- a/netstr/conn.go +++ b/netstr/conn.go @@ -5,8 +5,10 @@ import ( "context" "encoding/base32" "encoding/base64" + "encoding/hex" "fmt" "github.com/asmogo/nws/protocol" + "github.com/btcsuite/btcd/btcec/v2/schnorr" "github.com/google/uuid" "github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr/nip04" @@ -253,10 +255,7 @@ func ParseDestinationDomain(destination string) (string, []string, error) { } var subdomains []string split := strings.Split(url.SubName, ".") - for i, subdomain := range split { - if i == len(split)-1 { - break - } + for _, subdomain := range split { decodedSubDomain, err := base32.HexEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(subdomain)) if err != nil { continue @@ -265,8 +264,17 @@ func ParseDestinationDomain(destination string) (string, []string, error) { } // base32 decode the subdomain - domain := split[len(split)-1] + url.Name - return domain, subdomains, nil + decodedPubKey, err := base32.HexEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(url.Name)) + + if err != nil { + return "", nil, err + } + pk, err := schnorr.ParsePubKey(decodedPubKey) + if err != nil { + return "", nil, err + } + + return hex.EncodeToString(pk.SerializeCompressed())[2:], subdomains, nil } func (nc *NostrConnection) Close() error {