Skip to content

Commit

Permalink
Merge pull request #1979 from libp2p/google-protobuf
Browse files Browse the repository at this point in the history
switch to Google's Protobuf library, make protobufs compile with go generate
  • Loading branch information
marten-seemann authored Jan 12, 2023
2 parents 4ad3734 + f1109c5 commit 58c4cb8
Show file tree
Hide file tree
Showing 58 changed files with 3,238 additions and 9,519 deletions.
45 changes: 21 additions & 24 deletions core/crypto/ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"crypto/rand"
"testing"

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

"google.golang.org/protobuf/proto"
)

func TestBasicSignAndVerify(t *testing.T) {
Expand Down Expand Up @@ -79,15 +81,15 @@ func TestMarshalLoop(t *testing.T) {
// Ed25519 private keys used to contain the public key twice.
// For backwards-compatibility, we need to continue supporting
// that scenario.
pbmes := new(pb.PrivateKey)
pbmes.Type = priv.Type()
data, err := priv.Raw()
if err != nil {
t.Fatal(err)
}

pbmes.Data = append(data, data[len(data)-ed25519.PublicKeySize:]...)
return pbmes.Marshal()
data = append(data, data[len(data)-ed25519.PublicKeySize:]...)
return proto.Marshal(&pb.PrivateKey{
Type: priv.Type().Enum(),
Data: data,
})
},
} {
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -150,18 +152,14 @@ func TestMarshalLoop(t *testing.T) {
func TestUnmarshalErrors(t *testing.T) {
t.Run("PublicKey", func(t *testing.T) {
t.Run("Invalid data length", func(t *testing.T) {
pbmes := &pb.PublicKey{
Type: pb.KeyType_Ed25519,
data, err := proto.Marshal(&pb.PublicKey{
Type: pb.KeyType_Ed25519.Enum(),
Data: []byte{42},
}

data, err := pbmes.Marshal()
})
if err != nil {
t.Fatal(err)
}

_, err = UnmarshalPublicKey(data)
if err == nil {
if _, err := UnmarshalPublicKey(data); err == nil {
t.Fatal("expected an error")
}
})
Expand All @@ -174,16 +172,17 @@ func TestUnmarshalErrors(t *testing.T) {
t.Fatal(err)
}

pbmes := new(pb.PrivateKey)
pbmes.Type = priv.Type()
data, err := priv.Raw()
if err != nil {
t.Fatal(err)
}

// Append the private key instead of the public key.
pbmes.Data = append(data, data[:ed25519.PublicKeySize]...)
b, err := pbmes.Marshal()
data = append(data, data[:ed25519.PublicKeySize]...)

b, err := proto.Marshal(&pb.PrivateKey{
Type: priv.Type().Enum(),
Data: data,
})
if err != nil {
t.Fatal(err)
}
Expand All @@ -198,12 +197,10 @@ func TestUnmarshalErrors(t *testing.T) {
})

t.Run("Invalid data length", func(t *testing.T) {
pbmes := &pb.PrivateKey{
Type: pb.KeyType_Ed25519,
data, err := proto.Marshal(&pb.PrivateKey{
Type: pb.KeyType_Ed25519.Enum(),
Data: []byte{42},
}

data, err := pbmes.Marshal()
})
if err != nil {
t.Fatal(err)
}
Expand Down
25 changes: 13 additions & 12 deletions core/crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"fmt"
"io"

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

"github.com/gogo/protobuf/proto"
"google.golang.org/protobuf/proto"
)

//go:generate protoc --go_out=. --go_opt=Mpb/crypto.proto=./pb pb/crypto.proto

const (
// RSA is an enum for the supported RSA key type
RSA = iota
Expand Down Expand Up @@ -194,7 +196,7 @@ func PublicKeyFromProto(pmes *pb.PublicKey) (PubKey, error) {

switch tpk := pk.(type) {
case *RsaPublicKey:
tpk.cached, _ = pmes.Marshal()
tpk.cached, _ = proto.Marshal(pmes)
}

return pk, nil
Expand All @@ -214,14 +216,14 @@ func MarshalPublicKey(k PubKey) ([]byte, error) {
// PublicKeyToProto converts a public key object into an unserialized
// protobuf PublicKey message.
func PublicKeyToProto(k PubKey) (*pb.PublicKey, error) {
pbmes := new(pb.PublicKey)
pbmes.Type = k.Type()
data, err := k.Raw()
if err != nil {
return nil, err
}
pbmes.Data = data
return pbmes, nil
return &pb.PublicKey{
Type: k.Type().Enum(),
Data: data,
}, nil
}

// UnmarshalPrivateKey converts a protobuf serialized private key into its
Expand All @@ -243,15 +245,14 @@ func UnmarshalPrivateKey(data []byte) (PrivKey, error) {

// MarshalPrivateKey converts a key object into its protobuf serialized form.
func MarshalPrivateKey(k PrivKey) ([]byte, error) {
pbmes := new(pb.PrivateKey)
pbmes.Type = k.Type()
data, err := k.Raw()
if err != nil {
return nil, err
}

pbmes.Data = data
return proto.Marshal(pbmes)
return proto.Marshal(&pb.PrivateKey{
Type: k.Type().Enum(),
Data: data,
})
}

// ConfigDecodeKey decodes from b64 (for config file) to a byte array that can be unmarshalled.
Expand Down
11 changes: 0 additions & 11 deletions core/crypto/pb/Makefile

This file was deleted.

Loading

0 comments on commit 58c4cb8

Please sign in to comment.