Skip to content

Commit

Permalink
Exposes amino codec to be able to decode pk bytes in application
Browse files Browse the repository at this point in the history
  • Loading branch information
austinabell committed Nov 5, 2019
1 parent 274447e commit ebe344a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
9 changes: 5 additions & 4 deletions crypto/encoding/amino/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions crypto/encoding/amino/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`)
Expand All @@ -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 | |
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ebe344a

Please sign in to comment.