From ebe344a40a1c6cfd3c424b7537cfa99d06a675eb Mon Sep 17 00:00:00 2001 From: austinabell Date: Tue, 5 Nov 2019 13:47:13 -0500 Subject: [PATCH] Exposes amino codec to be able to decode pk bytes in application --- crypto/encoding/amino/amino.go | 9 +++++---- crypto/encoding/amino/encode_test.go | 12 ++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/crypto/encoding/amino/amino.go b/crypto/encoding/amino/amino.go index f7be3a20366..cc710dc6fcc 100644 --- a/crypto/encoding/amino/amino.go +++ b/crypto/encoding/amino/amino.go @@ -10,7 +10,8 @@ import ( "github.com/tendermint/tendermint/crypto/secp256k1" ) -var cdc = amino.NewCodec() +// AminoCdc defines the codec to decode crypto keys +var AminoCdc = amino.NewCodec() // nameTable is used to map public key concrete types back // to their registered amino names. This should eventually be handled @@ -25,7 +26,7 @@ func init() { // TODO: Remove above note when // https://github.com/tendermint/go-amino/issues/9 // is resolved - RegisterAmino(cdc) + RegisterAmino(AminoCdc) // TODO: Have amino provide a way to go from concrete struct to route directly. // Its currently a private API @@ -61,11 +62,11 @@ func RegisterAmino(cdc *amino.Codec) { } func PrivKeyFromBytes(privKeyBytes []byte) (privKey crypto.PrivKey, err error) { - err = cdc.UnmarshalBinaryBare(privKeyBytes, &privKey) + err = AminoCdc.UnmarshalBinaryBare(privKeyBytes, &privKey) return } func PubKeyFromBytes(pubKeyBytes []byte) (pubKey crypto.PubKey, err error) { - err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) + err = AminoCdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) return } diff --git a/crypto/encoding/amino/encode_test.go b/crypto/encoding/amino/encode_test.go index d4e34945422..a8e2aa50340 100644 --- a/crypto/encoding/amino/encode_test.go +++ b/crypto/encoding/amino/encode_test.go @@ -18,7 +18,7 @@ type byter interface { func checkAminoBinary(t *testing.T, src, dst interface{}, size int) { // Marshal to binary bytes. - bz, err := cdc.MarshalBinaryBare(src) + bz, err := AminoCdc.MarshalBinaryBare(src) require.Nil(t, err, "%+v", err) if byterSrc, ok := src.(byter); ok { // Make sure this is compatible with current (Bytes()) encoding. @@ -28,13 +28,13 @@ func checkAminoBinary(t *testing.T, src, dst interface{}, size int) { assert.Equal(t, size, len(bz), "Amino binary size mismatch") // Unmarshal. - err = cdc.UnmarshalBinaryBare(bz, dst) + err = AminoCdc.UnmarshalBinaryBare(bz, dst) require.Nil(t, err, "%+v", err) } func checkAminoJSON(t *testing.T, src interface{}, dst interface{}, isNil bool) { // Marshal to JSON bytes. - js, err := cdc.MarshalJSON(src) + js, err := AminoCdc.MarshalJSON(src) require.Nil(t, err, "%+v", err) if isNil { assert.Equal(t, string(js), `null`) @@ -43,14 +43,14 @@ func checkAminoJSON(t *testing.T, src interface{}, dst interface{}, isNil bool) assert.Contains(t, string(js), `"value":`) } // Unmarshal. - err = cdc.UnmarshalJSON(js, dst) + err = AminoCdc.UnmarshalJSON(js, dst) require.Nil(t, err, "%+v", err) } // ExamplePrintRegisteredTypes refers to unknown identifier: PrintRegisteredTypes //nolint:govet func ExamplePrintRegisteredTypes() { - cdc.PrintTypes(os.Stdout) + AminoCdc.PrintTypes(os.Stdout) // Output: | Type | Name | Prefix | Length | Notes | //| ---- | ---- | ------ | ----- | ------ | //| PubKeyEd25519 | tendermint/PubKeyEd25519 | 0x1624DE64 | 0x20 | | @@ -140,7 +140,7 @@ func TestPubkeyAminoName(t *testing.T) { {multisig.PubKeyMultisigThreshold{}, multisig.PubKeyMultisigThresholdAminoRoute, true}, } for i, tc := range tests { - got, found := PubkeyAminoName(cdc, tc.key) + got, found := PubkeyAminoName(AminoCdc, tc.key) require.Equal(t, tc.found, found, "not equal on tc %d", i) if tc.found { require.Equal(t, tc.want, got, "not equal on tc %d", i)