2020-01-24 07:32:00 -08:00
|
|
|
package codec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-07-10 04:01:25 -07:00
|
|
|
"errors"
|
2020-01-24 07:32:00 -08:00
|
|
|
"fmt"
|
2020-06-04 03:38:24 -07:00
|
|
|
"io"
|
2020-01-24 07:32:00 -08:00
|
|
|
|
|
|
|
amino "github.com/tendermint/go-amino"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2020-05-19 13:17:29 -07:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
2020-01-24 07:32:00 -08:00
|
|
|
)
|
|
|
|
|
2021-02-15 07:32:51 -08:00
|
|
|
// LegacyAmino defines a wrapper for an Amino codec that properly
|
|
|
|
// handles protobuf types with Any's. Deprecated.
|
2020-08-10 12:41:21 -07:00
|
|
|
type LegacyAmino struct {
|
2020-05-19 13:17:29 -07:00
|
|
|
Amino *amino.Codec
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) Seal() {
|
2020-05-19 13:17:29 -07:00
|
|
|
cdc.Amino.Seal()
|
|
|
|
}
|
2020-01-24 07:32:00 -08:00
|
|
|
|
2020-09-07 07:47:12 -07:00
|
|
|
func NewLegacyAmino() *LegacyAmino {
|
2020-08-10 12:41:21 -07:00
|
|
|
return &LegacyAmino{amino.NewCodec()}
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterEvidences registers Tendermint evidence types with the provided Amino
|
|
|
|
// codec.
|
2020-08-10 12:41:21 -07:00
|
|
|
func RegisterEvidences(cdc *LegacyAmino) {
|
2020-08-14 10:58:53 -07:00
|
|
|
cdc.Amino.RegisterInterface((*tmtypes.Evidence)(nil), nil)
|
|
|
|
cdc.Amino.RegisterConcrete(&tmtypes.DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence", nil)
|
2020-01-24 07:32:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSONIndent provides a utility for indented JSON encoding of an object
|
|
|
|
// via an Amino codec. It returns an error if it cannot serialize or indent as
|
|
|
|
// JSON.
|
2020-08-26 02:39:38 -07:00
|
|
|
func MarshalJSONIndent(cdc *LegacyAmino, obj interface{}) ([]byte, error) {
|
|
|
|
bz, err := cdc.MarshalJSON(obj)
|
2020-01-24 07:32:00 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
if err = json.Indent(&out, bz, "", " "); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustMarshalJSONIndent executes MarshalJSONIndent except it panics upon failure.
|
2020-08-26 02:39:38 -07:00
|
|
|
func MustMarshalJSONIndent(cdc *LegacyAmino, obj interface{}) []byte {
|
|
|
|
bz, err := MarshalJSONIndent(cdc, obj)
|
2020-01-24 07:32:00 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to marshal JSON: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz
|
|
|
|
}
|
2020-05-19 13:17:29 -07:00
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) marshalAnys(o interface{}) error {
|
2020-05-19 13:17:29 -07:00
|
|
|
return types.UnpackInterfaces(o, types.AminoPacker{Cdc: cdc.Amino})
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) unmarshalAnys(o interface{}) error {
|
2020-05-19 13:17:29 -07:00
|
|
|
return types.UnpackInterfaces(o, types.AminoUnpacker{Cdc: cdc.Amino})
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) jsonMarshalAnys(o interface{}) error {
|
2020-05-19 13:17:29 -07:00
|
|
|
return types.UnpackInterfaces(o, types.AminoJSONPacker{Cdc: cdc.Amino})
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) jsonUnmarshalAnys(o interface{}) error {
|
2020-05-19 13:17:29 -07:00
|
|
|
return types.UnpackInterfaces(o, types.AminoJSONUnpacker{Cdc: cdc.Amino})
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
func (cdc *LegacyAmino) Marshal(o interface{}) ([]byte, error) {
|
2020-05-19 13:17:29 -07:00
|
|
|
err := cdc.marshalAnys(o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cdc.Amino.MarshalBinaryBare(o)
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
func (cdc *LegacyAmino) MustMarshal(o interface{}) []byte {
|
|
|
|
bz, err := cdc.Marshal(o)
|
2020-05-19 13:17:29 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bz
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
func (cdc *LegacyAmino) MarshalLengthPrefixed(o interface{}) ([]byte, error) {
|
2020-05-19 13:17:29 -07:00
|
|
|
err := cdc.marshalAnys(o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cdc.Amino.MarshalBinaryLengthPrefixed(o)
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
func (cdc *LegacyAmino) MustMarshalLengthPrefixed(o interface{}) []byte {
|
|
|
|
bz, err := cdc.MarshalLengthPrefixed(o)
|
2020-05-19 13:17:29 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bz
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
func (cdc *LegacyAmino) Unmarshal(bz []byte, ptr interface{}) error {
|
2020-05-19 13:17:29 -07:00
|
|
|
err := cdc.Amino.UnmarshalBinaryBare(bz, ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cdc.unmarshalAnys(ptr)
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
func (cdc *LegacyAmino) MustUnmarshal(bz []byte, ptr interface{}) {
|
|
|
|
err := cdc.Unmarshal(bz, ptr)
|
2020-05-19 13:17:29 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
func (cdc *LegacyAmino) UnmarshalLengthPrefixed(bz []byte, ptr interface{}) error {
|
2020-05-19 13:17:29 -07:00
|
|
|
err := cdc.Amino.UnmarshalBinaryLengthPrefixed(bz, ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cdc.unmarshalAnys(ptr)
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
func (cdc *LegacyAmino) MustUnmarshalLengthPrefixed(bz []byte, ptr interface{}) {
|
|
|
|
err := cdc.UnmarshalLengthPrefixed(bz, ptr)
|
2020-05-19 13:17:29 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// MarshalJSON implements codec.Codec interface
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) MarshalJSON(o interface{}) ([]byte, error) {
|
2020-05-19 13:17:29 -07:00
|
|
|
err := cdc.jsonMarshalAnys(o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cdc.Amino.MarshalJSON(o)
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) MustMarshalJSON(o interface{}) []byte {
|
2020-05-19 13:17:29 -07:00
|
|
|
bz, err := cdc.MarshalJSON(o)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bz
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:46:22 -07:00
|
|
|
// UnmarshalJSON implements codec.Codec interface
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) UnmarshalJSON(bz []byte, ptr interface{}) error {
|
2020-05-19 13:17:29 -07:00
|
|
|
err := cdc.Amino.UnmarshalJSON(bz, ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cdc.jsonUnmarshalAnys(ptr)
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) MustUnmarshalJSON(bz []byte, ptr interface{}) {
|
2020-05-19 13:17:29 -07:00
|
|
|
err := cdc.UnmarshalJSON(bz, ptr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (*LegacyAmino) UnpackAny(*types.Any, interface{}) error {
|
2020-07-10 04:01:25 -07:00
|
|
|
return errors.New("AminoCodec can't handle unpack protobuf Any's")
|
2020-05-19 13:17:29 -07:00
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) RegisterInterface(ptr interface{}, iopts *amino.InterfaceOptions) {
|
2020-05-19 13:17:29 -07:00
|
|
|
cdc.Amino.RegisterInterface(ptr, iopts)
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) RegisterConcrete(o interface{}, name string, copts *amino.ConcreteOptions) {
|
2020-05-19 13:17:29 -07:00
|
|
|
cdc.Amino.RegisterConcrete(o, name, copts)
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error) {
|
2020-05-19 13:17:29 -07:00
|
|
|
err := cdc.jsonMarshalAnys(o)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return cdc.Amino.MarshalJSONIndent(o, prefix, indent)
|
|
|
|
}
|
2020-06-04 03:38:24 -07:00
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func (cdc *LegacyAmino) PrintTypes(out io.Writer) error {
|
2020-06-04 03:38:24 -07:00
|
|
|
return cdc.Amino.PrintTypes(out)
|
|
|
|
}
|