mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 06:42:16 +00:00
crypto/amino: Address anton's comment on PubkeyAminoRoute (#2592)
This commit is contained in:
parent
6ec52a9233
commit
ee7b3d260e
@ -1,8 +1,6 @@
|
|||||||
package cryptoAmino
|
package cryptoAmino
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
amino "github.com/tendermint/go-amino"
|
amino "github.com/tendermint/go-amino"
|
||||||
@ -13,6 +11,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var cdc = amino.NewCodec()
|
var cdc = amino.NewCodec()
|
||||||
|
|
||||||
|
// routeTable is used to map public key concrete types back
|
||||||
|
// to their amino routes. This should eventually be handled
|
||||||
|
// by amino. Example usage:
|
||||||
|
// routeTable[reflect.TypeOf(ed25519.PubKeyEd25519{})] = ed25519.PubKeyAminoRoute
|
||||||
var routeTable = make(map[reflect.Type]string, 3)
|
var routeTable = make(map[reflect.Type]string, 3)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -34,12 +37,9 @@ func init() {
|
|||||||
// PubkeyAminoRoute returns the amino route of a pubkey
|
// PubkeyAminoRoute returns the amino route of a pubkey
|
||||||
// cdc is currently passed in, as eventually this will not be using
|
// cdc is currently passed in, as eventually this will not be using
|
||||||
// a package level codec.
|
// a package level codec.
|
||||||
func PubkeyAminoRoute(cdc *amino.Codec, key crypto.PubKey) (string, error) {
|
func PubkeyAminoRoute(cdc *amino.Codec, key crypto.PubKey) (string, bool) {
|
||||||
route, ok := routeTable[reflect.TypeOf(key)]
|
route, found := routeTable[reflect.TypeOf(key)]
|
||||||
if !ok {
|
return route, found
|
||||||
return "", errors.New("Pubkey type not known")
|
|
||||||
}
|
|
||||||
return route, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterAmino registers all crypto related types in the given (amino) codec.
|
// RegisterAmino registers all crypto related types in the given (amino) codec.
|
||||||
|
@ -120,32 +120,29 @@ func TestNilEncodings(t *testing.T) {
|
|||||||
var e, f crypto.PrivKey
|
var e, f crypto.PrivKey
|
||||||
checkAminoJSON(t, &e, &f, true)
|
checkAminoJSON(t, &e, &f, true)
|
||||||
assert.EqualValues(t, e, f)
|
assert.EqualValues(t, e, f)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
|
func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
|
||||||
pk, err := PubKeyFromBytes([]byte("foo"))
|
pk, err := PubKeyFromBytes([]byte("foo"))
|
||||||
require.NotNil(t, err, "expecting a non-nil error")
|
require.NotNil(t, err)
|
||||||
require.Nil(t, pk, "expecting an empty public key on error")
|
require.Nil(t, pk)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPubkeyAminoRoute(t *testing.T) {
|
func TestPubkeyAminoRoute(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
key crypto.PubKey
|
key crypto.PubKey
|
||||||
want string
|
want string
|
||||||
wantErr bool
|
found bool
|
||||||
}{
|
}{
|
||||||
{ed25519.PubKeyEd25519{}, ed25519.PubKeyAminoRoute, false},
|
{ed25519.PubKeyEd25519{}, ed25519.PubKeyAminoRoute, true},
|
||||||
{secp256k1.PubKeySecp256k1{}, secp256k1.PubKeyAminoRoute, false},
|
{secp256k1.PubKeySecp256k1{}, secp256k1.PubKeyAminoRoute, true},
|
||||||
{&multisig.PubKeyMultisigThreshold{}, multisig.PubKeyMultisigThresholdAminoRoute, false},
|
{&multisig.PubKeyMultisigThreshold{}, multisig.PubKeyMultisigThresholdAminoRoute, true},
|
||||||
}
|
}
|
||||||
for i, tc := range tests {
|
for i, tc := range tests {
|
||||||
got, err := PubkeyAminoRoute(cdc, tc.key)
|
got, found := PubkeyAminoRoute(cdc, tc.key)
|
||||||
if tc.wantErr {
|
require.Equal(t, tc.found, found, "not equal on tc %d", i)
|
||||||
require.Error(t, err, "tc %d", i)
|
if tc.found {
|
||||||
} else {
|
require.Equal(t, tc.want, got, "not equal on tc %d", i)
|
||||||
require.NoError(t, err, "tc %d", i)
|
|
||||||
require.Equal(t, tc.want, got, "tc %d", i)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user