mirror of
https://github.com/asmogo/nws.git
synced 2025-01-18 18:11:33 +00:00
Refactor key decoding logic to use common function
This commit is contained in:
parent
87d15abf37
commit
95cb6b5b71
10
exit/exit.go
10
exit/exit.go
@ -4,6 +4,7 @@ import (
|
||||
"encoding/base32"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/ekzyis/nip44"
|
||||
"log/slog"
|
||||
"net"
|
||||
"strings"
|
||||
@ -14,7 +15,6 @@ import (
|
||||
"github.com/asmogo/nws/socks5"
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
||||
"github.com/ekzyis/nip44"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/nbd-wtf/go-nostr/nip19"
|
||||
"github.com/puzpuzpuz/xsync/v3"
|
||||
@ -228,13 +228,9 @@ func (e *Exit) ListenAndServe(ctx context.Context) {
|
||||
// routes the message to the appropriate handler based on its protocol type.
|
||||
func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) {
|
||||
// hex decode the target public key
|
||||
targetPublicKeyBytes, err := hex.DecodeString("02" + msg.PubKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// hex decode the private key
|
||||
privateKeyBytes, err := hex.DecodeString(e.config.NostrPrivateKey)
|
||||
privateKeyBytes, targetPublicKeyBytes, err := protocol.GetEncryptionKeys(e.config.NostrPrivateKey, msg.PubKey)
|
||||
if err != nil {
|
||||
slog.Error("could not get encryption keys", "error", err)
|
||||
return
|
||||
}
|
||||
sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes)
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/hex"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -79,14 +78,9 @@ func (e *Exit) handleCertificateEvent(
|
||||
if privateKeyEvent == nil {
|
||||
return tls.Certificate{}, errNoCertificateEvent
|
||||
}
|
||||
targetPublicKeyBytes, err := hex.DecodeString("02" + privateKeyEvent.PubKey)
|
||||
privateKeyBytes, targetPublicKeyBytes, err := protocol.GetEncryptionKeys(e.config.NostrPrivateKey, msg.PubKey)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("could not decode target public key: %w", err)
|
||||
}
|
||||
// hex decode the private key
|
||||
privateKeyBytes, err := hex.DecodeString(e.config.NostrPrivateKey)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, fmt.Errorf("could not decode private key: %w", err)
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes)
|
||||
if err != nil {
|
||||
|
@ -129,14 +129,9 @@ func (nc *NostrConnection) handleNostrRead(buffer []byte) (int, error) {
|
||||
}
|
||||
nc.readIDs = append(nc.readIDs, event.ID)
|
||||
// hex decode the target public key
|
||||
targetPublicKeyBytes, err := hex.DecodeString("02" + event.PubKey)
|
||||
privateKeyBytes, targetPublicKeyBytes, err := protocol.GetEncryptionKeys(nc.privateKey, event.PubKey)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("could not decode target public key: %w", err)
|
||||
}
|
||||
// hex decode the private key
|
||||
privateKeyBytes, err := hex.DecodeString(nc.privateKey)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("could not decode private key: %w", err)
|
||||
return 0, fmt.Errorf("could not get encryption keys: %w", err)
|
||||
}
|
||||
sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes)
|
||||
if err != nil {
|
||||
|
17
protocol/nip44.go
Normal file
17
protocol/nip44.go
Normal file
@ -0,0 +1,17 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func GetEncryptionKeys(privateKey, publicKey string) ([]byte, []byte, error) {
|
||||
targetPublicKeyBytes, err := hex.DecodeString("02" + publicKey)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
privateKeyBytes, err := hex.DecodeString(privateKey)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return privateKeyBytes, targetPublicKeyBytes, nil
|
||||
}
|
@ -3,7 +3,6 @@ package protocol
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"encoding/hex"
|
||||
"github.com/ekzyis/nip44"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
@ -66,15 +65,9 @@ func (s *EventSigner) CreateSignedEvent(
|
||||
tags nostr.Tags,
|
||||
opts ...MessageOption,
|
||||
) (nostr.Event, error) {
|
||||
// hex decode the target public key
|
||||
targetPublicKeyBytes, err := hex.DecodeString("02" + targetPublicKey)
|
||||
privateKeyBytes, targetPublicKeyBytes, err := GetEncryptionKeys(s.privateKey, targetPublicKey)
|
||||
if err != nil {
|
||||
return nostr.Event{}, fmt.Errorf("could not decode target public key: %w", err)
|
||||
}
|
||||
// hex decode the private key
|
||||
privateKeyBytes, err := hex.DecodeString(s.privateKey)
|
||||
if err != nil {
|
||||
return nostr.Event{}, fmt.Errorf("could not decode private key: %w", err)
|
||||
return nostr.Event{}, fmt.Errorf("could not get encryption keys: %w", err)
|
||||
}
|
||||
sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user