44 lines
897 B
Go
44 lines
897 B
Go
|
|
package infrastructure
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"oc-catalog/conf"
|
||
|
|
"os"
|
||
|
|
|
||
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
||
|
|
"github.com/libp2p/go-libp2p/core/pnet"
|
||
|
|
)
|
||
|
|
|
||
|
|
func LoadKeyFromFile(isPublic bool) (crypto.PrivKey, error) {
|
||
|
|
path := conf.GetConfig().PrivateKeyPath
|
||
|
|
if isPublic {
|
||
|
|
path = conf.GetConfig().PublicKeyPath
|
||
|
|
}
|
||
|
|
data, err := os.ReadFile(path)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.)
|
||
|
|
priv, err := crypto.UnmarshalPrivateKey(data)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return priv, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func LoadPSKFromFile() (pnet.PSK, error) {
|
||
|
|
path := conf.GetConfig().PSKPath
|
||
|
|
data, err := os.ReadFile(path)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.)
|
||
|
|
psk, err := pnet.DecodeV1PSK(bytes.NewReader(data))
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return psk, nil
|
||
|
|
}
|