Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Sep 18, 2020
1 parent 56f5ec3 commit fe58118
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
61 changes: 60 additions & 1 deletion crypto/keys/ed25519/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import (

"golang.org/x/crypto/ed25519"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/tendermint/tendermint/crypto"
tmed25519 "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/tmhash"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)

//-------------------------------------

var _ crypto.PrivKey = &PrivKey{}
var _ cryptotypes.PrivKey = &PrivKey{}
var _ codec.AminoMarshaler = &PubKey{}

const (
PrivKeyName = "tendermint/PrivKeyEd25519"
Expand Down Expand Up @@ -87,6 +91,32 @@ func (privKey *PrivKey) Type() string {
return keyType
}

// MarshalAmino overrides Amino binary marshalling.
func (privKey PrivKey) MarshalAmino() ([]byte, error) {
return privKey.Key, nil
}

// UnmarshalAmino overrides Amino binary marshalling.
func (privKey *PrivKey) UnmarshalAmino(bz []byte) error {
*privKey = PrivKey{
Key: bz,
}

return nil
}

// MarshalAminoJSON overrides Amino JSON marshalling.
func (privKey PrivKey) MarshalAminoJSON() ([]byte, error) {
// When we marshal to Amino JSON, we don't marshal the "key" field itself,
// just its contents (i.e. the key bytes).
return privKey.MarshalAmino()
}

// UnmarshalAminoJSON overrides Amino JSON marshalling.
func (privKey *PrivKey) UnmarshalAminoJSON(bz []byte) error {
return privKey.UnmarshalAmino(bz)
}

// GenPrivKey generates a new ed25519 private key.
// It uses OS randomness in conjunction with the current global random seed
// in tendermint/libs/common to generate the private key.
Expand Down Expand Up @@ -118,6 +148,9 @@ func GenPrivKeyFromSecret(secret []byte) *PrivKey {

//-------------------------------------

var _ cryptotypes.PubKey = &PubKey{}
var _ codec.AminoMarshaler = &PubKey{}

// IntoTmPubKey allows our own PubKey types be converted into Tendermint's
// pubkey types.
type IntoTmPubKey interface {
Expand Down Expand Up @@ -165,6 +198,32 @@ func (pubKey *PubKey) Equals(other crypto.PubKey) bool {
return subtle.ConstantTimeCompare(pubKey.Bytes(), other.Bytes()) == 1
}

// MarshalAmino overrides Amino binary marshalling.
func (pubKey PubKey) MarshalAmino() ([]byte, error) {
return pubKey.Key, nil
}

// UnmarshalAmino overrides Amino binary marshalling.
func (pubKey *PubKey) UnmarshalAmino(bz []byte) error {
*pubKey = PubKey{
Key: bz,
}

return nil
}

// MarshalAminoJSON overrides Amino JSON marshalling.
func (pubKey PubKey) MarshalAminoJSON() ([]byte, error) {
// When we marshal to Amino JSON, we don't marshal the "key" field itself,
// just its contents (i.e. the key bytes).
return pubKey.MarshalAmino()
}

// UnmarshalAminoJSON overrides Amino JSON marshalling.
func (pubKey *PubKey) UnmarshalAminoJSON(bz []byte) error {
return pubKey.UnmarshalAmino(bz)
}

// AsTmPubKey converts our own PubKey into a Tendermint ED25519 pubkey.
func (pubKey *PubKey) AsTmPubKey() crypto.PubKey {
return tmed25519.PubKey(pubKey.Key)
Expand Down
4 changes: 2 additions & 2 deletions x/staking/types/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestABCIValidatorUpdate(t *testing.T) {
validator := NewValidator(valAddr1, pk1, Description{})

abciVal := validator.ABCIValidatorUpdate()
pk, err := encoding.PubKeyToProto(validator.GetConsPubKey())
pk, err := encoding.PubKeyToProto(validator.GetConsPubKey().(ed25519.IntoTmPubKey).AsTmPubKey())
require.NoError(t, err)
require.Equal(t, pk, abciVal.PubKey)
require.Equal(t, validator.BondedTokens().Int64(), abciVal.Power)
Expand All @@ -72,7 +72,7 @@ func TestABCIValidatorUpdateZero(t *testing.T) {
validator := NewValidator(valAddr1, pk1, Description{})

abciVal := validator.ABCIValidatorUpdateZero()
pk, err := encoding.PubKeyToProto(validator.GetConsPubKey())
pk, err := encoding.PubKeyToProto(validator.GetConsPubKey().(ed25519.IntoTmPubKey).AsTmPubKey())
require.NoError(t, err)
require.Equal(t, pk, abciVal.PubKey)
require.Equal(t, int64(0), abciVal.Power)
Expand Down

0 comments on commit fe58118

Please sign in to comment.