diff --git a/tools/crypto.go b/tools/crypto.go index caf0fdb..fb712f2 100644 --- a/tools/crypto.go +++ b/tools/crypto.go @@ -1,6 +1,7 @@ package tools import ( + "bytes" "crypto/ed25519" "crypto/x509" "encoding/pem" @@ -17,7 +18,12 @@ func LoadKeyFromFilePrivate() (crypto.PrivKey, error) { 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 @@ -27,16 +33,31 @@ func LoadKeyFromFilePrivate() (crypto.PrivKey, error) { if !ok { return nil, fmt.Errorf("not an ed25519 key") } - return crypto.UnmarshalEd25519PrivateKey(edKey) + + // Convert properly to libp2p key + privKey, _, err := crypto.GenerateEd25519Key( + bytes.NewReader(edKey.Seed()), + ) + if err != nil { + return nil, err + } + + return privKey, nil } 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 @@ -46,6 +67,12 @@ func LoadKeyFromFilePublic() (crypto.PubKey, error) { if !ok { return nil, fmt.Errorf("not an ed25519 key") } - // Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.) - return crypto.UnmarshalEd25519PublicKey(edKey) + + // Convert Go ed25519 key to libp2p key + pubKey, err := crypto.UnmarshalEd25519PublicKey(edKey) + if err != nil { + return nil, err + } + + return pubKey, nil }