diff --git a/crypto/ed25519/ed25519.go b/crypto/ed25519/ed25519.go index 1ce6f7b11..4138eec99 100644 --- a/crypto/ed25519/ed25519.go +++ b/crypto/ed25519/ed25519.go @@ -37,12 +37,20 @@ func init() { PrivKeyAminoName, nil) } +var _, PubKeyPrefix = amino.NameToDisfix(PubKeyAminoName) +var _, privKeyPrefix = amino.NameToDisfix(PrivKeyAminoName) + // PrivKeyEd25519 implements crypto.PrivKey. type PrivKeyEd25519 [64]byte // Bytes marshals the privkey using amino encoding. func (privKey PrivKeyEd25519) Bytes() []byte { - return cdc.MustMarshalBinaryBare(privKey) + buf := bytes.NewBuffer(nil) + buf.Write(privKeyPrefix[:]) + if err := amino.EncodeByteSlice(buf, privKey[:]); err != nil { + panic(err.Error()) + } + return buf.Bytes() } // Sign produces a signature on the provided message. @@ -141,11 +149,12 @@ func (pubKey PubKeyEd25519) Address() crypto.Address { // Bytes marshals the PubKey using amino encoding. func (pubKey PubKeyEd25519) Bytes() []byte { - bz, err := cdc.MarshalBinaryBare(pubKey) - if err != nil { - panic(err) + buf := bytes.NewBuffer(nil) + buf.Write(PubKeyPrefix[:]) + if err := amino.EncodeByteSlice(buf, pubKey[:]); err != nil { + panic(err.Error()) } - return bz + return buf.Bytes() } func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig []byte) bool { diff --git a/crypto/ed25519/ed25519_test.go b/crypto/ed25519/ed25519_test.go index 6fe2c0946..b61a0d6d8 100644 --- a/crypto/ed25519/ed25519_test.go +++ b/crypto/ed25519/ed25519_test.go @@ -6,8 +6,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/tendermint/go-amino" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" + cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino" ) func TestSignAndValidateEd25519(t *testing.T) { @@ -28,3 +30,14 @@ func TestSignAndValidateEd25519(t *testing.T) { assert.False(t, pubKey.VerifyBytes(msg, sig)) } + +func TestMarshal(t *testing.T) { + cdc := amino.NewCodec() + cryptoamino.RegisterAmino(cdc) + + privKey := ed25519.GenPrivKey() + pubKey := privKey.PubKey() + + require.Equal(t, cdc.MustMarshalBinaryBare(privKey), privKey.Bytes()) + require.Equal(t, cdc.MustMarshalBinaryBare(pubKey), pubKey.Bytes()) +} diff --git a/crypto/multisig/threshold_pubkey.go b/crypto/multisig/threshold_pubkey.go index 36e2dc2dd..abff4c649 100644 --- a/crypto/multisig/threshold_pubkey.go +++ b/crypto/multisig/threshold_pubkey.go @@ -1,6 +1,7 @@ package multisig import ( + amino "github.com/tendermint/go-amino" "github.com/tendermint/tendermint/crypto" ) @@ -12,6 +13,8 @@ type PubKeyMultisigThreshold struct { var _ crypto.PubKey = PubKeyMultisigThreshold{} +var _, PubKeyPrefix = amino.NameToDisfix(PubKeyMultisigThresholdAminoRoute) + // NewPubKeyMultisigThreshold returns a new PubKeyMultisigThreshold. // Panics if len(pubkeys) < k or 0 >= k. func NewPubKeyMultisigThreshold(k int, pubkeys []crypto.PubKey) crypto.PubKey { @@ -69,6 +72,7 @@ func (pk PubKeyMultisigThreshold) VerifyBytes(msg []byte, marshalledSig []byte) // Bytes returns the amino encoded version of the PubKeyMultisigThreshold func (pk PubKeyMultisigThreshold) Bytes() []byte { + // TODO custom marshaller return cdc.MustMarshalBinaryBare(pk) } diff --git a/crypto/secp256k1/secp256k1.go b/crypto/secp256k1/secp256k1.go index 5338d10a5..1341e5d67 100644 --- a/crypto/secp256k1/secp256k1.go +++ b/crypto/secp256k1/secp256k1.go @@ -34,6 +34,9 @@ func init() { PrivKeyAminoName, nil) } +var _, PubKeyPrefix = amino.NameToDisfix(PubKeyAminoName) +var _, privKeyPrefix = amino.NameToDisfix(PrivKeyAminoName) + //------------------------------------- var _ crypto.PrivKey = PrivKeySecp256k1{} @@ -43,7 +46,12 @@ type PrivKeySecp256k1 [32]byte // Bytes marshalls the private key using amino encoding. func (privKey PrivKeySecp256k1) Bytes() []byte { - return cdc.MustMarshalBinaryBare(privKey) + buf := bytes.NewBuffer(nil) + buf.Write(privKeyPrefix[:]) + if err := amino.EncodeByteSlice(buf, privKey[:]); err != nil { + panic(err.Error()) + } + return buf.Bytes() } // PubKey performs the point-scalar multiplication from the privKey on the @@ -151,11 +159,12 @@ func (pubKey PubKeySecp256k1) Address() crypto.Address { // Bytes returns the pubkey marshalled with amino encoding. func (pubKey PubKeySecp256k1) Bytes() []byte { - bz, err := cdc.MarshalBinaryBare(pubKey) - if err != nil { - panic(err) + buf := bytes.NewBuffer(nil) + buf.Write(PubKeyPrefix[:]) + if err := amino.EncodeByteSlice(buf, pubKey[:]); err != nil { + panic(err.Error()) } - return bz + return buf.Bytes() } func (pubKey PubKeySecp256k1) String() string { diff --git a/crypto/secp256k1/secp256k1_test.go b/crypto/secp256k1/secp256k1_test.go index a83cd0f5f..f45ec13ef 100644 --- a/crypto/secp256k1/secp256k1_test.go +++ b/crypto/secp256k1/secp256k1_test.go @@ -9,7 +9,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/tendermint/go-amino" "github.com/tendermint/tendermint/crypto" + cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino" "github.com/tendermint/tendermint/crypto/secp256k1" underlyingSecp256k1 "github.com/btcsuite/btcd/btcec" @@ -115,3 +117,14 @@ func TestGenPrivKeySecp256k1(t *testing.T) { }) } } + +func TestMarshal(t *testing.T) { + cdc := amino.NewCodec() + cryptoamino.RegisterAmino(cdc) + + privKey := secp256k1.GenPrivKey() + pubKey := privKey.PubKey() + + require.Equal(t, cdc.MustMarshalBinaryBare(privKey), privKey.Bytes()) + require.Equal(t, cdc.MustMarshalBinaryBare(pubKey), pubKey.Bytes()) +} diff --git a/crypto/sr25519/privkey.go b/crypto/sr25519/privkey.go index 17d33ebf2..491f1b0ce 100644 --- a/crypto/sr25519/privkey.go +++ b/crypto/sr25519/privkey.go @@ -1,10 +1,12 @@ package sr25519 import ( + "bytes" "crypto/subtle" "fmt" "io" + amino "github.com/tendermint/go-amino" "github.com/tendermint/tendermint/crypto" schnorrkel "github.com/ChainSafe/go-schnorrkel" @@ -16,9 +18,16 @@ const PrivKeySr25519Size = 32 // PrivKeySr25519 implements crypto.PrivKey. type PrivKeySr25519 [PrivKeySr25519Size]byte +var _, privKeyPrefix = amino.NameToDisfix(PrivKeyAminoName) + // Bytes marshals the privkey using amino encoding. func (privKey PrivKeySr25519) Bytes() []byte { - return cdc.MustMarshalBinaryBare(privKey) + buf := bytes.NewBuffer(nil) + buf.Write(privKeyPrefix[:]) + if err := amino.EncodeByteSlice(buf, privKey[:]); err != nil { + panic(err.Error()) + } + return buf.Bytes() } // Sign produces a signature on the provided message. diff --git a/crypto/sr25519/pubkey.go b/crypto/sr25519/pubkey.go index a678806f2..d8360660d 100644 --- a/crypto/sr25519/pubkey.go +++ b/crypto/sr25519/pubkey.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" + amino "github.com/tendermint/go-amino" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/tmhash" @@ -18,6 +19,8 @@ const PubKeySr25519Size = 32 // PubKeySr25519 implements crypto.PubKey for the Sr25519 signature scheme. type PubKeySr25519 [PubKeySr25519Size]byte +var _, pubKeyPrefix = amino.NameToDisfix(PubKeyAminoName) + // Address is the SHA256-20 of the raw pubkey bytes. func (pubKey PubKeySr25519) Address() crypto.Address { return crypto.Address(tmhash.SumTruncated(pubKey[:])) @@ -25,11 +28,12 @@ func (pubKey PubKeySr25519) Address() crypto.Address { // Bytes marshals the PubKey using amino encoding. func (pubKey PubKeySr25519) Bytes() []byte { - bz, err := cdc.MarshalBinaryBare(pubKey) - if err != nil { - panic(err) + buf := bytes.NewBuffer(nil) + buf.Write(pubKeyPrefix[:]) + if err := amino.EncodeByteSlice(buf, pubKey[:]); err != nil { + panic(err.Error()) } - return bz + return buf.Bytes() } func (pubKey PubKeySr25519) VerifyBytes(msg []byte, sig []byte) bool { diff --git a/crypto/sr25519/sr25519_test.go b/crypto/sr25519/sr25519_test.go index 62cf564a9..d3c7835f1 100644 --- a/crypto/sr25519/sr25519_test.go +++ b/crypto/sr25519/sr25519_test.go @@ -6,7 +6,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/tendermint/go-amino" "github.com/tendermint/tendermint/crypto" + cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino" "github.com/tendermint/tendermint/crypto/sr25519" ) @@ -29,3 +31,14 @@ func TestSignAndValidateSr25519(t *testing.T) { assert.False(t, pubKey.VerifyBytes(msg, sig)) } + +func TestMarshal(t *testing.T) { + cdc := amino.NewCodec() + cryptoamino.RegisterAmino(cdc) + + privKey := sr25519.GenPrivKey() + pubKey := privKey.PubKey() + + require.Equal(t, cdc.MustMarshalBinaryBare(privKey), privKey.Bytes()) + require.Equal(t, cdc.MustMarshalBinaryBare(pubKey), pubKey.Bytes()) +}