Refactor public key handling and decoding

Added schnorr public key parsing and hex package functions. Simplified subdomain decoding
This commit is contained in:
dd dd 2024-07-27 23:33:35 +02:00
parent 8ad87406cb
commit 65fa12c998
2 changed files with 35 additions and 7 deletions

View File

@ -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.

View File

@ -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 {