diff --git a/tools/crypto.go b/tools/crypto.go index fb712f2..a497043 100644 --- a/tools/crypto.go +++ b/tools/crypto.go @@ -1,7 +1,6 @@ package tools import ( - "bytes" "crypto/ed25519" "crypto/x509" "encoding/pem" @@ -14,16 +13,12 @@ import ( func LoadKeyFromFilePrivate() (crypto.PrivKey, error) { path := config.GetConfig().PrivateKeyPath + fmt.Println(path) data, err := os.ReadFile(path) if err != nil { return nil, err } - block, _ := pem.Decode(data) - if block == nil { - return nil, fmt.Errorf("failed to decode PEM") - } - keyAny, err := x509.ParsePKCS8PrivateKey(block.Bytes) if err != nil { return nil, err @@ -33,31 +28,16 @@ func LoadKeyFromFilePrivate() (crypto.PrivKey, error) { if !ok { return nil, fmt.Errorf("not an ed25519 key") } - - // Convert properly to libp2p key - privKey, _, err := crypto.GenerateEd25519Key( - bytes.NewReader(edKey.Seed()), - ) - if err != nil { - return nil, err - } - - return privKey, nil + return crypto.UnmarshalEd25519PrivateKey(edKey) } func LoadKeyFromFilePublic() (crypto.PubKey, error) { path := config.GetConfig().PublicKeyPath - data, err := os.ReadFile(path) if err != nil { return nil, err } - block, _ := pem.Decode(data) - if block == nil { - return nil, fmt.Errorf("failed to decode PEM") - } - keyAny, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { return nil, err @@ -67,12 +47,6 @@ func LoadKeyFromFilePublic() (crypto.PubKey, error) { if !ok { return nil, fmt.Errorf("not an ed25519 key") } - - // Convert Go ed25519 key to libp2p key - pubKey, err := crypto.UnmarshalEd25519PublicKey(edKey) - if err != nil { - return nil, err - } - - return pubKey, nil + // Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.) + return crypto.UnmarshalEd25519PublicKey(edKey) }