crypto
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user