mirror of
https://github.com/asmogo/nws.git
synced 2025-01-31 07:11:34 +00:00
Refactor public key handling and decoding
Added schnorr public key parsing and hex package functions. Simplified subdomain decoding
This commit is contained in:
parent
8ad87406cb
commit
65fa12c998
22
exit/exit.go
22
exit/exit.go
@ -3,11 +3,14 @@ package exit
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/base32"
|
"encoding/base32"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/asmogo/nws/config"
|
"github.com/asmogo/nws/config"
|
||||||
"github.com/asmogo/nws/netstr"
|
"github.com/asmogo/nws/netstr"
|
||||||
"github.com/asmogo/nws/protocol"
|
"github.com/asmogo/nws/protocol"
|
||||||
"github.com/asmogo/nws/socks5"
|
"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"
|
||||||
"github.com/nbd-wtf/go-nostr/nip04"
|
"github.com/nbd-wtf/go-nostr/nip04"
|
||||||
"github.com/nbd-wtf/go-nostr/nip19"
|
"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 = 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)
|
slog.Info("created exit node", "profile", profile, "domain", domain)
|
||||||
// setup subscriptions
|
// setup subscriptions
|
||||||
err = exit.setSubscriptions(ctx)
|
err = exit.setSubscriptions(ctx)
|
||||||
@ -119,6 +128,17 @@ func NewExit(ctx context.Context, exitNodeConfig *config.ExitConfig) *Exit {
|
|||||||
return 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.
|
// 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.
|
// 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.
|
// Then it calls the `handleSubscription` method to open a subscription to the relays with the specified filters.
|
||||||
|
@ -5,8 +5,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/base32"
|
"encoding/base32"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/asmogo/nws/protocol"
|
"github.com/asmogo/nws/protocol"
|
||||||
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
"github.com/nbd-wtf/go-nostr/nip04"
|
"github.com/nbd-wtf/go-nostr/nip04"
|
||||||
@ -253,10 +255,7 @@ func ParseDestinationDomain(destination string) (string, []string, error) {
|
|||||||
}
|
}
|
||||||
var subdomains []string
|
var subdomains []string
|
||||||
split := strings.Split(url.SubName, ".")
|
split := strings.Split(url.SubName, ".")
|
||||||
for i, subdomain := range split {
|
for _, subdomain := range split {
|
||||||
if i == len(split)-1 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
decodedSubDomain, err := base32.HexEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(subdomain))
|
decodedSubDomain, err := base32.HexEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(subdomain))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
@ -265,8 +264,17 @@ func ParseDestinationDomain(destination string) (string, []string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// base32 decode the subdomain
|
// base32 decode the subdomain
|
||||||
domain := split[len(split)-1] + url.Name
|
decodedPubKey, err := base32.HexEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(url.Name))
|
||||||
return domain, subdomains, nil
|
|
||||||
|
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 {
|
func (nc *NostrConnection) Close() error {
|
||||||
|
Loading…
Reference in New Issue
Block a user