diff --git a/util/crypto/ed25519.go b/util/crypto/ed25519.go index 608da2f5..b520bfcd 100644 --- a/util/crypto/ed25519.go +++ b/util/crypto/ed25519.go @@ -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() { diff --git a/util/crypto/key.go b/util/crypto/key.go index 10f08875..91390999 100644 --- a/util/crypto/key.go +++ b/util/crypto/key.go @@ -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) }