Skip to content

Commit

Permalink
udpate keys + tests + old reference to go-libp2p-core (deprecated)
Browse files Browse the repository at this point in the history
  • Loading branch information
cortze committed Mar 20, 2024
1 parent 99cd78c commit c556c49
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
60 changes: 41 additions & 19 deletions pkg/utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
Expand All @@ -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)
}
1 change: 0 additions & 1 deletion pkg/utils/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func TestKeyConverters(t *testing.T) {

// Basic Geth privKey
ogECDSA, err := GenerateECDSAPrivKey()
require.NoError(t, err)
Expand Down
22 changes: 1 addition & 21 deletions pkg/utils/multiaddress.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c556c49

Please sign in to comment.