From ca28041e13f9dac39c9b2a8ea00f4abae2afa91b Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Thu, 22 Jul 2021 21:18:09 +0200 Subject: [PATCH] remove deprecated Bytes method from the Key interface (#204) --- core/crypto/ecdsa.go | 12 +----------- core/crypto/ecdsa_test.go | 4 ++-- core/crypto/ed25519.go | 10 ---------- core/crypto/ed25519_test.go | 2 -- core/crypto/fixture_test.go | 10 +++++----- core/crypto/key.go | 8 ++------ core/crypto/openssl_common.go | 18 +----------------- core/crypto/rsa_go.go | 20 +------------------- core/crypto/rsa_test.go | 4 ++-- core/crypto/secp256k1.go | 14 ++------------ core/crypto/secp256k1_test.go | 4 ++-- core/peer/peer.go | 2 +- core/peer/peer_test.go | 4 ++-- 13 files changed, 21 insertions(+), 91 deletions(-) diff --git a/core/crypto/ecdsa.go b/core/crypto/ecdsa.go index 3b7a425a5d..9f8c67e75a 100644 --- a/core/crypto/ecdsa.go +++ b/core/crypto/ecdsa.go @@ -12,7 +12,7 @@ import ( pb "github.com/libp2p/go-libp2p-core/crypto/pb" - sha256 "github.com/minio/sha256-simd" + "github.com/minio/sha256-simd" ) // ECDSAPrivateKey is an implementation of an ECDSA private key @@ -102,11 +102,6 @@ func UnmarshalECDSAPublicKey(data []byte) (PubKey, error) { return &ECDSAPublicKey{pub}, nil } -// Bytes returns the private key as protobuf bytes -func (ePriv *ECDSAPrivateKey) Bytes() ([]byte, error) { - return MarshalPrivateKey(ePriv) -} - // Type returns the key type func (ePriv *ECDSAPrivateKey) Type() pb.KeyType { return pb.KeyType_ECDSA @@ -141,11 +136,6 @@ func (ePriv *ECDSAPrivateKey) GetPublic() PubKey { return &ECDSAPublicKey{&ePriv.priv.PublicKey} } -// Bytes returns the public key as protobuf bytes -func (ePub *ECDSAPublicKey) Bytes() ([]byte, error) { - return MarshalPublicKey(ePub) -} - // Type returns the key type func (ePub *ECDSAPublicKey) Type() pb.KeyType { return pb.KeyType_ECDSA diff --git a/core/crypto/ecdsa_test.go b/core/crypto/ecdsa_test.go index 328b9a7c6c..eb4d181756 100644 --- a/core/crypto/ecdsa_test.go +++ b/core/crypto/ecdsa_test.go @@ -66,7 +66,7 @@ func TestECDSAMarshalLoop(t *testing.T) { t.Fatal(err) } - privB, err := priv.Bytes() + privB, err := MarshalPrivateKey(priv) if err != nil { t.Fatal(err) } @@ -80,7 +80,7 @@ func TestECDSAMarshalLoop(t *testing.T) { t.Fatal("keys are not equal") } - pubB, err := pub.Bytes() + pubB, err := MarshalPublicKey(pub) if err != nil { t.Fatal(err) } diff --git a/core/crypto/ed25519.go b/core/crypto/ed25519.go index 1707e7509f..b7e8addb8f 100644 --- a/core/crypto/ed25519.go +++ b/core/crypto/ed25519.go @@ -42,11 +42,6 @@ func (k *Ed25519PrivateKey) Type() pb.KeyType { return pb.KeyType_Ed25519 } -// Bytes marshals an ed25519 private key to protobuf bytes. -func (k *Ed25519PrivateKey) Bytes() ([]byte, error) { - return MarshalPrivateKey(k) -} - // Raw private key bytes. func (k *Ed25519PrivateKey) Raw() ([]byte, error) { // The Ed25519 private key contains two 32-bytes curve points, the private @@ -88,11 +83,6 @@ func (k *Ed25519PublicKey) Type() pb.KeyType { return pb.KeyType_Ed25519 } -// Bytes returns a ed25519 public key as protobuf bytes. -func (k *Ed25519PublicKey) Bytes() ([]byte, error) { - return MarshalPublicKey(k) -} - // Raw public key bytes. func (k *Ed25519PublicKey) Raw() ([]byte, error) { return k.k, nil diff --git a/core/crypto/ed25519_test.go b/core/crypto/ed25519_test.go index ae1bebe76e..ec606f760c 100644 --- a/core/crypto/ed25519_test.go +++ b/core/crypto/ed25519_test.go @@ -71,7 +71,6 @@ func TestMarshalLoop(t *testing.T) { t.Run("PrivateKey", func(t *testing.T) { for name, f := range map[string]func() ([]byte, error){ - "Bytes": priv.Bytes, "Marshal": func() ([]byte, error) { return MarshalPrivateKey(priv) }, @@ -126,7 +125,6 @@ func TestMarshalLoop(t *testing.T) { t.Run("PublicKey", func(t *testing.T) { for name, f := range map[string]func() ([]byte, error){ - "Bytes": pub.Bytes, "Marshal": func() ([]byte, error) { return MarshalPublicKey(pub) }, diff --git a/core/crypto/fixture_test.go b/core/crypto/fixture_test.go index 471e85472f..168a4f77ec 100644 --- a/core/crypto/fixture_test.go +++ b/core/crypto/fixture_test.go @@ -8,7 +8,7 @@ import ( "io/ioutil" "testing" - crypto "github.com/libp2p/go-libp2p-core/crypto" + "github.com/libp2p/go-libp2p-core/crypto" crypto_pb "github.com/libp2p/go-libp2p-core/crypto/pb" ) @@ -62,7 +62,7 @@ func TestFixtures(t *testing.T) { if err != nil { t.Fatal(err) } - pubBytes2, err := pub.Bytes() + pubBytes2, err := crypto.MarshalPublicKey(pub) if err != nil { t.Fatal(err) } @@ -73,7 +73,7 @@ func TestFixtures(t *testing.T) { if err != nil { t.Fatal(err) } - privBytes2, err := priv.Bytes() + privBytes2, err := crypto.MarshalPrivateKey(priv) if err != nil { t.Fatal(err) } @@ -113,11 +113,11 @@ func generate() { if err != nil { panic(err) } - pubb, err := pub.Bytes() + pubb, err := crypto.MarshalPublicKey(pub) if err != nil { panic(err) } - privb, err := priv.Bytes() + privb, err := crypto.MarshalPrivateKey(priv) if err != nil { panic(err) } diff --git a/core/crypto/key.go b/core/crypto/key.go index 090338810f..137a807fa6 100644 --- a/core/crypto/key.go +++ b/core/crypto/key.go @@ -19,7 +19,7 @@ import ( pb "github.com/libp2p/go-libp2p-core/crypto/pb" "github.com/gogo/protobuf/proto" - sha256 "github.com/minio/sha256-simd" + "github.com/minio/sha256-simd" ) const ( @@ -69,10 +69,6 @@ var PrivKeyUnmarshallers = map[pb.KeyType]PrivKeyUnmarshaller{ // Key represents a crypto key that can be compared to another key type Key interface { - // Bytes returns a serialized, storeable representation of this key - // DEPRECATED in favor of Marshal / Unmarshal - Bytes() ([]byte, error) - // Equals checks whether two PubKeys are the same Equals(Key) bool @@ -82,7 +78,7 @@ type Key interface { // This function is the inverse of {Priv,Pub}KeyUnmarshaler. Raw() ([]byte, error) - // Type returns the protobof key type. + // Type returns the protobuf key type. Type() pb.KeyType } diff --git a/core/crypto/openssl_common.go b/core/crypto/openssl_common.go index 88807cafd7..648a3bcefb 100644 --- a/core/crypto/openssl_common.go +++ b/core/crypto/openssl_common.go @@ -7,7 +7,7 @@ import ( pb "github.com/libp2p/go-libp2p-core/crypto/pb" - openssl "github.com/libp2p/go-openssl" + "github.com/libp2p/go-openssl" ) // define these as separate types so we can add more key types later and reuse @@ -55,17 +55,6 @@ func (pk *opensslPublicKey) Type() pb.KeyType { } } -// Bytes returns protobuf bytes of a public key -func (pk *opensslPublicKey) Bytes() ([]byte, error) { - pk.cacheLk.Lock() - var err error - if pk.cached == nil { - pk.cached, err = MarshalPublicKey(pk) - } - pk.cacheLk.Unlock() - return pk.cached, err -} - func (pk *opensslPublicKey) Raw() ([]byte, error) { return pk.key.MarshalPKIXPublicKeyDER() } @@ -99,11 +88,6 @@ func (sk *opensslPrivateKey) Type() pb.KeyType { } } -// Bytes returns protobuf bytes from a private key -func (sk *opensslPrivateKey) Bytes() ([]byte, error) { - return MarshalPrivateKey(sk) -} - func (sk *opensslPrivateKey) Raw() ([]byte, error) { return sk.key.MarshalPKCS1PrivateKeyDER() } diff --git a/core/crypto/rsa_go.go b/core/crypto/rsa_go.go index f28a327b83..72b4b7986c 100644 --- a/core/crypto/rsa_go.go +++ b/core/crypto/rsa_go.go @@ -9,7 +9,6 @@ import ( "crypto/x509" "errors" "io" - "sync" pb "github.com/libp2p/go-libp2p-core/crypto/pb" @@ -25,8 +24,7 @@ type RsaPrivateKey struct { type RsaPublicKey struct { k rsa.PublicKey - cacheLk sync.Mutex - cached []byte + cached []byte } // GenerateRSAKeyPair generates a new rsa private and public key @@ -56,17 +54,6 @@ func (pk *RsaPublicKey) Type() pb.KeyType { return pb.KeyType_RSA } -// Bytes returns protobuf bytes of a public key -func (pk *RsaPublicKey) Bytes() ([]byte, error) { - pk.cacheLk.Lock() - var err error - if pk.cached == nil { - pk.cached, err = MarshalPublicKey(pk) - } - pk.cacheLk.Unlock() - return pk.cached, err -} - func (pk *RsaPublicKey) Raw() ([]byte, error) { return x509.MarshalPKIXPublicKey(&pk.k) } @@ -97,11 +84,6 @@ func (sk *RsaPrivateKey) Type() pb.KeyType { return pb.KeyType_RSA } -// Bytes returns protobuf bytes from a private key -func (sk *RsaPrivateKey) Bytes() ([]byte, error) { - return MarshalPrivateKey(sk) -} - func (sk *RsaPrivateKey) Raw() ([]byte, error) { b := x509.MarshalPKCS1PrivateKey(&sk.sk) return b, nil diff --git a/core/crypto/rsa_test.go b/core/crypto/rsa_test.go index 9de03a1850..69151b86c9 100644 --- a/core/crypto/rsa_test.go +++ b/core/crypto/rsa_test.go @@ -95,7 +95,7 @@ func TestRSAMarshalLoop(t *testing.T) { t.Fatal(err) } - privB, err := priv.Bytes() + privB, err := MarshalPrivateKey(priv) if err != nil { t.Fatal(err) } @@ -109,7 +109,7 @@ func TestRSAMarshalLoop(t *testing.T) { t.Fatal("keys are not equal") } - pubB, err := pub.Bytes() + pubB, err := MarshalPublicKey(pub) if err != nil { t.Fatal(err) } diff --git a/core/crypto/secp256k1.go b/core/crypto/secp256k1.go index 6e98ea6bf7..7936a09e13 100644 --- a/core/crypto/secp256k1.go +++ b/core/crypto/secp256k1.go @@ -6,8 +6,8 @@ import ( pb "github.com/libp2p/go-libp2p-core/crypto/pb" - btcec "github.com/btcsuite/btcd/btcec" - sha256 "github.com/minio/sha256-simd" + "github.com/btcsuite/btcd/btcec" + "github.com/minio/sha256-simd" ) // Secp256k1PrivateKey is an Secp256k1 private key @@ -47,11 +47,6 @@ func UnmarshalSecp256k1PublicKey(data []byte) (PubKey, error) { return (*Secp256k1PublicKey)(k), nil } -// Bytes returns protobuf bytes from a private key -func (k *Secp256k1PrivateKey) Bytes() ([]byte, error) { - return MarshalPrivateKey(k) -} - // Type returns the private key type func (k *Secp256k1PrivateKey) Type() pb.KeyType { return pb.KeyType_Secp256k1 @@ -88,11 +83,6 @@ func (k *Secp256k1PrivateKey) GetPublic() PubKey { return (*Secp256k1PublicKey)((*btcec.PrivateKey)(k).PubKey()) } -// Bytes returns protobuf bytes from a public key -func (k *Secp256k1PublicKey) Bytes() ([]byte, error) { - return MarshalPublicKey(k) -} - // Type returns the public key type func (k *Secp256k1PublicKey) Type() pb.KeyType { return pb.KeyType_Secp256k1 diff --git a/core/crypto/secp256k1_test.go b/core/crypto/secp256k1_test.go index aff2bd9773..81f2f57884 100644 --- a/core/crypto/secp256k1_test.go +++ b/core/crypto/secp256k1_test.go @@ -66,7 +66,7 @@ func TestSecp256k1MarshalLoop(t *testing.T) { t.Fatal(err) } - privB, err := priv.Bytes() + privB, err := MarshalPrivateKey(priv) if err != nil { t.Fatal(err) } @@ -80,7 +80,7 @@ func TestSecp256k1MarshalLoop(t *testing.T) { t.Fatal("keys are not equal") } - pubB, err := pub.Bytes() + pubB, err := MarshalPublicKey(pub) if err != nil { t.Fatal(err) } diff --git a/core/peer/peer.go b/core/peer/peer.go index fd895c84be..b88ae056c8 100644 --- a/core/peer/peer.go +++ b/core/peer/peer.go @@ -186,7 +186,7 @@ func ToCid(id ID) cid.Cid { // IDFromPublicKey returns the Peer ID corresponding to the public key pk. func IDFromPublicKey(pk ic.PubKey) (ID, error) { - b, err := pk.Bytes() + b, err := ic.MarshalPublicKey(pk) if err != nil { return "", err } diff --git a/core/peer/peer_test.go b/core/peer/peer_test.go index fa6d81c873..bf5d46e046 100644 --- a/core/peer/peer_test.go +++ b/core/peer/peer_test.go @@ -52,7 +52,7 @@ func (ks *keyset) generate() error { return err } - bpk, err := ks.pk.Bytes() + bpk, err := ic.MarshalPublicKey(ks.pk) if err != nil { return err } @@ -74,7 +74,7 @@ func (ks *keyset) load(hpkp, skBytesStr string) error { } ks.pk = ks.sk.GetPublic() - bpk, err := ks.pk.Bytes() + bpk, err := ic.MarshalPublicKey(ks.pk) if err != nil { return err }