From 85d87341e26f123ca4b0d36cb4ab159ca031d050 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Thu, 22 Jul 2021 20:58:14 +0200 Subject: [PATCH] remove deprecated Bytes method from the Key interface --- crypto/ecdsa.go | 12 +----------- crypto/ecdsa_test.go | 4 ++-- crypto/ed25519.go | 10 ---------- crypto/ed25519_test.go | 2 -- crypto/fixture_test.go | 10 +++++----- crypto/key.go | 8 ++------ crypto/openssl_common.go | 18 +----------------- crypto/rsa_go.go | 16 ---------------- crypto/rsa_test.go | 4 ++-- crypto/secp256k1.go | 14 ++------------ crypto/secp256k1_test.go | 4 ++-- peer/peer.go | 4 ++-- peer/peer_test.go | 4 ++-- 13 files changed, 21 insertions(+), 89 deletions(-) diff --git a/crypto/ecdsa.go b/crypto/ecdsa.go index 3b7a425a..9f8c67e7 100644 --- a/crypto/ecdsa.go +++ b/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/crypto/ecdsa_test.go b/crypto/ecdsa_test.go index 328b9a7c..eb4d1817 100644 --- a/crypto/ecdsa_test.go +++ b/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/crypto/ed25519.go b/crypto/ed25519.go index 1707e750..b7e8addb 100644 --- a/crypto/ed25519.go +++ b/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/crypto/ed25519_test.go b/crypto/ed25519_test.go index ae1bebe7..ec606f76 100644 --- a/crypto/ed25519_test.go +++ b/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/crypto/fixture_test.go b/crypto/fixture_test.go index 471e8547..168a4f77 100644 --- a/crypto/fixture_test.go +++ b/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/crypto/key.go b/crypto/key.go index 09033881..137a807f 100644 --- a/crypto/key.go +++ b/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/crypto/openssl_common.go b/crypto/openssl_common.go index 88807caf..648a3bce 100644 --- a/crypto/openssl_common.go +++ b/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/crypto/rsa_go.go b/crypto/rsa_go.go index f28a327b..bb57facb 100644 --- a/crypto/rsa_go.go +++ b/crypto/rsa_go.go @@ -56,17 +56,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 +86,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/crypto/rsa_test.go b/crypto/rsa_test.go index 9de03a18..69151b86 100644 --- a/crypto/rsa_test.go +++ b/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/crypto/secp256k1.go b/crypto/secp256k1.go index 6e98ea6b..7936a09e 100644 --- a/crypto/secp256k1.go +++ b/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/crypto/secp256k1_test.go b/crypto/secp256k1_test.go index aff2bd97..81f2f578 100644 --- a/crypto/secp256k1_test.go +++ b/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/peer/peer.go b/peer/peer.go index 81b88d9f..15057661 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -7,7 +7,7 @@ import ( "fmt" "strings" - cid "github.com/ipfs/go-cid" + "github.com/ipfs/go-cid" ic "github.com/libp2p/go-libp2p-core/crypto" b58 "github.com/mr-tron/base58/base58" mh "github.com/multiformats/go-multihash" @@ -220,7 +220,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/peer/peer_test.go b/peer/peer_test.go index b203a7ad..6fd1d7a7 100644 --- a/peer/peer_test.go +++ b/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 }