Skip to content

Commit

Permalink
remove deprecated Bytes method from the Key interface (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann authored Jul 22, 2021
1 parent 0e10da7 commit ca28041
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 91 deletions.
12 changes: 1 addition & 11 deletions core/crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/crypto/ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
10 changes: 0 additions & 10 deletions core/crypto/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions core/crypto/ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down Expand Up @@ -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)
},
Expand Down
10 changes: 5 additions & 5 deletions core/crypto/fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 2 additions & 6 deletions core/crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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

Expand All @@ -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
}

Expand Down
18 changes: 1 addition & 17 deletions core/crypto/openssl_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down
20 changes: 1 addition & 19 deletions core/crypto/rsa_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/x509"
"errors"
"io"
"sync"

pb "github.com/libp2p/go-libp2p-core/crypto/pb"

Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/crypto/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
14 changes: 2 additions & 12 deletions core/crypto/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/crypto/secp256k1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions core/peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit ca28041

Please sign in to comment.