Refactor key decoding logic to use common function

This commit is contained in:
dd dd 2024-09-02 21:30:56 +02:00
parent 87d15abf37
commit 95cb6b5b71
5 changed files with 26 additions and 31 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/base32" "encoding/base32"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/ekzyis/nip44"
"log/slog" "log/slog"
"net" "net"
"strings" "strings"
@ -14,7 +15,6 @@ import (
"github.com/asmogo/nws/socks5" "github.com/asmogo/nws/socks5"
"github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr" "github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/ekzyis/nip44"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
"github.com/puzpuzpuz/xsync/v3" "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. // routes the message to the appropriate handler based on its protocol type.
func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) { func (e *Exit) processMessage(ctx context.Context, msg nostr.IncomingEvent) {
// hex decode the target public key // hex decode the target public key
targetPublicKeyBytes, err := hex.DecodeString("02" + msg.PubKey) privateKeyBytes, targetPublicKeyBytes, err := protocol.GetEncryptionKeys(e.config.NostrPrivateKey, msg.PubKey)
if err != nil {
return
}
// hex decode the private key
privateKeyBytes, err := hex.DecodeString(e.config.NostrPrivateKey)
if err != nil { if err != nil {
slog.Error("could not get encryption keys", "error", err)
return return
} }
sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes) sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes)

View File

@ -7,7 +7,6 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"crypto/x509/pkix" "crypto/x509/pkix"
"encoding/hex"
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt" "fmt"
@ -79,14 +78,9 @@ func (e *Exit) handleCertificateEvent(
if privateKeyEvent == nil { if privateKeyEvent == nil {
return tls.Certificate{}, errNoCertificateEvent return tls.Certificate{}, errNoCertificateEvent
} }
targetPublicKeyBytes, err := hex.DecodeString("02" + privateKeyEvent.PubKey) privateKeyBytes, targetPublicKeyBytes, err := protocol.GetEncryptionKeys(e.config.NostrPrivateKey, msg.PubKey)
if err != nil { if err != nil {
return tls.Certificate{}, fmt.Errorf("could not decode target public key: %w", err) return tls.Certificate{}, 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)
} }
sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes) sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes)
if err != nil { if err != nil {

View File

@ -129,14 +129,9 @@ func (nc *NostrConnection) handleNostrRead(buffer []byte) (int, error) {
} }
nc.readIDs = append(nc.readIDs, event.ID) nc.readIDs = append(nc.readIDs, event.ID)
// hex decode the target public key // 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 { if err != nil {
return 0, fmt.Errorf("could not decode target public key: %w", err) return 0, fmt.Errorf("could not get encryption keys: %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)
} }
sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes) sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes)
if err != nil { if err != nil {

17
protocol/nip44.go Normal file
View 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
}

View File

@ -3,7 +3,6 @@ package protocol
import ( import (
"fmt" "fmt"
"encoding/hex"
"github.com/ekzyis/nip44" "github.com/ekzyis/nip44"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
) )
@ -66,15 +65,9 @@ func (s *EventSigner) CreateSignedEvent(
tags nostr.Tags, tags nostr.Tags,
opts ...MessageOption, opts ...MessageOption,
) (nostr.Event, error) { ) (nostr.Event, error) {
// hex decode the target public key privateKeyBytes, targetPublicKeyBytes, err := GetEncryptionKeys(s.privateKey, targetPublicKey)
targetPublicKeyBytes, err := hex.DecodeString("02" + targetPublicKey)
if err != nil { if err != nil {
return nostr.Event{}, fmt.Errorf("could not decode target public key: %w", err) return nostr.Event{}, fmt.Errorf("could not get encryption keys: %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)
} }
sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes) sharedKey, err := nip44.GenerateConversationKey(privateKeyBytes, targetPublicKeyBytes)
if err != nil { if err != nil {