Add marshalling of private key

This commit is contained in:
mcrakhman 2023-03-27 16:03:58 +02:00
parent 3d017d1e0c
commit 2225aca40f
No known key found for this signature in database
GPG Key ID: DED12CFEF5B8396B
2 changed files with 23 additions and 0 deletions

View File

@ -55,6 +55,18 @@ func UnmarshalEd25519PublicKeyProto(bytes []byte) (PubKey, error) {
return UnmarshalEd25519PublicKey(msg.Data)
}
func UnmarshalEd25519PrivateKeyProto(bytes []byte) (PrivKey, error) {
msg := &cryptoproto.Key{}
err := proto.Unmarshal(bytes, msg)
if err != nil {
return nil, err
}
if msg.Type != cryptoproto.KeyType_Ed25519Private {
return nil, ErrIncorrectKeyType
}
return UnmarshalEd25519PrivateKey(msg.Data)
}
func NewSigningEd25519PubKeyFromBytes(bytes []byte) (PubKey, error) {
return UnmarshalEd25519PublicKey(bytes)
}
@ -114,6 +126,15 @@ func (k *Ed25519PrivKey) Sign(msg []byte) ([]byte, error) {
return ed25519.Sign(k.privKey, msg), nil
}
// Marshall marshalls the key into proto
func (k *Ed25519PrivKey) Marshall() ([]byte, error) {
msg := &cryptoproto.Key{
Type: cryptoproto.KeyType_Ed25519Public,
Data: k.privKey,
}
return msg.Marshal()
}
// Decrypt decrypts the message
func (k *Ed25519PrivKey) Decrypt(msg []byte) ([]byte, error) {
k.once.Do(func() {

View File

@ -27,6 +27,8 @@ type PrivKey interface {
Sign([]byte) ([]byte, error)
// GetPublic returns the associated public key
GetPublic() PubKey
// Marshall wraps key in proto encoding and marshalls it
Marshall() ([]byte, error)
// LibP2P returns libp2p model
LibP2P() (crypto.PrivKey, error)
}