From c556c49397f1416e27c03678c2138cf75a27db00 Mon Sep 17 00:00:00 2001 From: Mikel Cortes Date: Wed, 20 Mar 2024 18:58:38 +0100 Subject: [PATCH] udpate keys + tests + old reference to go-libp2p-core (deprecated) --- pkg/utils/keys.go | 60 +++++++++++++++++++++++++------------ pkg/utils/keys_test.go | 1 - pkg/utils/multiaddress.go | 22 +------------- pkg/utils/useragent_test.go | 4 +-- 4 files changed, 44 insertions(+), 43 deletions(-) diff --git a/pkg/utils/keys.go b/pkg/utils/keys.go index 332692a..1946fb2 100644 --- a/pkg/utils/keys.go +++ b/pkg/utils/keys.go @@ -4,9 +4,8 @@ import ( "crypto/ecdsa" "crypto/rand" "encoding/hex" - gcrypto "github.com/ethereum/go-ethereum/crypto" - "github.com/libp2p/go-libp2p-core/crypto" + "github.com/libp2p/go-libp2p/core/crypto" "github.com/pkg/errors" ) @@ -24,18 +23,19 @@ func ParseECDSAPrivateKey(strKey string) (*ecdsa.PrivateKey, error) { return gcrypto.HexToECDSA(strKey) } -func AdaptSecp256k1FromECDSA(ecdsaKey *ecdsa.PrivateKey) (*crypto.Secp256k1PrivateKey, error) { - secpKey := (*crypto.Secp256k1PrivateKey)(ecdsaKey) - return secpKey, nil +func AdaptSecp256k1FromECDSA(ecdsaKey *ecdsa.PrivateKey) (crypto.PrivKey, error) { + privBytes := gcrypto.FromECDSA(ecdsaKey) + privKey, err := crypto.UnmarshalSecp256k1PrivateKey(privBytes) + return privKey, err } // Export Private Key to a string -func Secp256k1ToString(inputKey *crypto.Secp256k1PrivateKey) string { +func Secp256k1ToString(inputKey crypto.PrivKey) string { keyBytes, _ := inputKey.Raw() return hex.EncodeToString(keyBytes) } -func AdaptECDSAFromSecp256k1(privKey *crypto.Secp256k1PrivateKey) (*ecdsa.PrivateKey, error) { +func AdaptECDSAFromSecp256k1(privKey crypto.PrivKey) (*ecdsa.PrivateKey, error) { privBytes, err := privKey.Raw() if err != nil { return nil, errors.Wrap(err, "unable to get bytes from libp2p privkey") @@ -44,33 +44,55 @@ func AdaptECDSAFromSecp256k1(privKey *crypto.Secp256k1PrivateKey) (*ecdsa.Privat } // taken from Prysm https://github.com/prysmaticlabs/prysm/blob/616cfd33908df1e479c5dd0980367ede8de82a5d/crypto/ecdsa/utils.go#L38 -func ConvertECDSAPubkeyToSecp2561k(pubkey *ecdsa.PublicKey) (*crypto.Secp256k1PublicKey, error) { +func ConvertECDSAPubkeyToSecp2561k(pubkey *ecdsa.PublicKey) (crypto.PubKey, error) { pubBytes := gcrypto.FromECDSAPub(pubkey) secp256k1, err := crypto.UnmarshalSecp256k1PublicKey(pubBytes) if err != nil { return nil, errors.Wrap(err, "unable to unmarshal libp2p key from geth pubkey bytes") } - return secp256k1.(*crypto.Secp256k1PublicKey), nil + return secp256k1, nil } -func IsLibp2pValidEthereumPrivateKey(privkey *crypto.Secp256k1PrivateKey) bool { - tempKey, _ := ecdsa.GenerateKey(gcrypto.S256(), rand.Reader) - return privkey.IsOnCurve(tempKey.X, tempKey.Y) +func IsLibp2pValidEthereumPrivateKey(privkey crypto.PrivKey) bool { + secp256privKey, _ := privkey.(*crypto.Secp256k1PrivateKey) + privBytes, err := secp256privKey.Raw() + if err != nil { + return false + } + ethCurve := gcrypto.S256() + ethPrivKey, err := gcrypto.ToECDSA(privBytes) + if err != nil { + return false + } + return ethCurve.IsOnCurve(ethPrivKey.X, ethPrivKey.Y) } -func IsLibp2pValidEthereumPublicKey(pubkey *crypto.Secp256k1PublicKey) bool { - temPubkey, _ := ecdsa.GenerateKey(gcrypto.S256(), rand.Reader) - return pubkey.Curve.IsOnCurve(temPubkey.X, temPubkey.Y) +func IsLibp2pValidEthereumPublicKey(pubkey crypto.PubKey) bool { + secp256pubKey, _ := pubkey.(*crypto.Secp256k1PublicKey) + pubBytes, err := secp256pubKey.Raw() + if err != nil { + return false + } + ethCurve := gcrypto.S256() + ecdsaPubKey, err := gcrypto.UnmarshalPubkey(pubBytes) + if err != nil { + return true + } + return ethCurve.IsOnCurve(ecdsaPubKey.X, ecdsaPubKey.Y) } func IsGethValidEthereumPrivateKey(privkey *ecdsa.PrivateKey) bool { // create new geth-crypto key to get the curve - tempKey, _ := ecdsa.GenerateKey(gcrypto.S256(), rand.Reader) - return privkey.Curve.IsOnCurve(tempKey.X, tempKey.Y) + /* + tempKey, _ := ecdsa.GenerateKey(gcrypto.S256(), rand.Reader) + return privkey.Curve.IsOnCurve(tempKey.X, tempKey.Y) + */ + ethCurve := gcrypto.S256() + return ethCurve.IsOnCurve(privkey.X, privkey.Y) } func IsGethValidEthereumPublicKey(pubkey *ecdsa.PublicKey) bool { // create new geth-crypto key to get the curve - tempKey, _ := ecdsa.GenerateKey(gcrypto.S256(), rand.Reader) - return pubkey.Curve.IsOnCurve(tempKey.X, tempKey.Y) + ethCurve := gcrypto.S256() + return ethCurve.IsOnCurve(pubkey.X, pubkey.Y) } diff --git a/pkg/utils/keys_test.go b/pkg/utils/keys_test.go index f420cc4..501c17f 100644 --- a/pkg/utils/keys_test.go +++ b/pkg/utils/keys_test.go @@ -7,7 +7,6 @@ import ( ) func TestKeyConverters(t *testing.T) { - // Basic Geth privKey ogECDSA, err := GenerateECDSAPrivKey() require.NoError(t, err) diff --git a/pkg/utils/multiaddress.go b/pkg/utils/multiaddress.go index 4929293..70b3b16 100644 --- a/pkg/utils/multiaddress.go +++ b/pkg/utils/multiaddress.go @@ -1,17 +1,13 @@ package utils import ( - "crypto/ecdsa" - "encoding/hex" - "fmt" "net" "strconv" "strings" "github.com/pkg/errors" - "github.com/libp2p/go-libp2p-core/crypto" - "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p/core/peer" ma "github.com/multiformats/go-multiaddr" ) @@ -108,22 +104,6 @@ func CheckValidIP(ip string) bool { return parsedIP != nil } -func ParsePubkey(v string) (*ecdsa.PublicKey, error) { - if strings.HasPrefix(v, "0x") { - v = v[2:] - } - pubKeyBytes, err := hex.DecodeString(v) - if err != nil { - return nil, fmt.Errorf("cannot parse public key, expected hex string: %v", err) - } - var pub crypto.PubKey - pub, err = crypto.UnmarshalSecp256k1PublicKey(pubKeyBytes) - if err != nil { - return nil, fmt.Errorf("cannot parse public key, invalid public key (Secp256k1): %v", err) - } - return (*ecdsa.PublicKey)((pub).(*crypto.Secp256k1PublicKey)), nil -} - func GetPublicAddrsFromAddrArray(mAddrs []ma.Multiaddr) ma.Multiaddr { // loop to check if which is the public ip var finalAddr ma.Multiaddr diff --git a/pkg/utils/useragent_test.go b/pkg/utils/useragent_test.go index a8d8e2d..9c8bc3c 100644 --- a/pkg/utils/useragent_test.go +++ b/pkg/utils/useragent_test.go @@ -102,14 +102,14 @@ var Eth2TestClients []clientInfoTest = []clientInfoTest{ }, { userAgent: "erigon/lightclient", - clientname: "erigon", + clientName: "erigon", clientVersion: "unknown", clientOS: "unknown", clientArch: "unknown", }, { userAgent: "erigon", - clientname: "erigon", + clientName: "erigon", clientVersion: "unknown", clientOS: "unknown", clientArch: "unknown",