## Description Closes: #XXXX --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
197 lines
4.8 KiB
Go
197 lines
4.8 KiB
Go
package codec
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
amino "github.com/tendermint/go-amino"
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
|
)
|
|
|
|
// LegacyAmino defines a wrapper for an Amino codec that properly
|
|
// handles protobuf types with Any's. Deprecated.
|
|
type LegacyAmino struct {
|
|
Amino *amino.Codec
|
|
}
|
|
|
|
func (cdc *LegacyAmino) Seal() {
|
|
cdc.Amino.Seal()
|
|
}
|
|
|
|
func NewLegacyAmino() *LegacyAmino {
|
|
return &LegacyAmino{amino.NewCodec()}
|
|
}
|
|
|
|
// RegisterEvidences registers Tendermint evidence types with the provided Amino
|
|
// codec.
|
|
func RegisterEvidences(cdc *LegacyAmino) {
|
|
cdc.Amino.RegisterInterface((*tmtypes.Evidence)(nil), nil)
|
|
cdc.Amino.RegisterConcrete(&tmtypes.DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence", nil)
|
|
}
|
|
|
|
// 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.
|
|
func MarshalJSONIndent(cdc *LegacyAmino, obj interface{}) ([]byte, error) {
|
|
bz, err := cdc.MarshalJSON(obj)
|
|
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.
|
|
func MustMarshalJSONIndent(cdc *LegacyAmino, obj interface{}) []byte {
|
|
bz, err := MarshalJSONIndent(cdc, obj)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("failed to marshal JSON: %s", err))
|
|
}
|
|
|
|
return bz
|
|
}
|
|
|
|
func (cdc *LegacyAmino) marshalAnys(o interface{}) error {
|
|
return types.UnpackInterfaces(o, types.AminoPacker{Cdc: cdc.Amino})
|
|
}
|
|
|
|
func (cdc *LegacyAmino) unmarshalAnys(o interface{}) error {
|
|
return types.UnpackInterfaces(o, types.AminoUnpacker{Cdc: cdc.Amino})
|
|
}
|
|
|
|
func (cdc *LegacyAmino) jsonMarshalAnys(o interface{}) error {
|
|
return types.UnpackInterfaces(o, types.AminoJSONPacker{Cdc: cdc.Amino})
|
|
}
|
|
|
|
func (cdc *LegacyAmino) jsonUnmarshalAnys(o interface{}) error {
|
|
return types.UnpackInterfaces(o, types.AminoJSONUnpacker{Cdc: cdc.Amino})
|
|
}
|
|
|
|
func (cdc *LegacyAmino) Marshal(o interface{}) ([]byte, error) {
|
|
err := cdc.marshalAnys(o)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cdc.Amino.MarshalBinaryBare(o)
|
|
}
|
|
|
|
func (cdc *LegacyAmino) MustMarshal(o interface{}) []byte {
|
|
bz, err := cdc.Marshal(o)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return bz
|
|
}
|
|
|
|
func (cdc *LegacyAmino) MarshalLengthPrefixed(o interface{}) ([]byte, error) {
|
|
err := cdc.marshalAnys(o)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cdc.Amino.MarshalBinaryLengthPrefixed(o)
|
|
}
|
|
|
|
func (cdc *LegacyAmino) MustMarshalLengthPrefixed(o interface{}) []byte {
|
|
bz, err := cdc.MarshalLengthPrefixed(o)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return bz
|
|
}
|
|
|
|
func (cdc *LegacyAmino) Unmarshal(bz []byte, ptr interface{}) error {
|
|
err := cdc.Amino.UnmarshalBinaryBare(bz, ptr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return cdc.unmarshalAnys(ptr)
|
|
}
|
|
|
|
func (cdc *LegacyAmino) MustUnmarshal(bz []byte, ptr interface{}) {
|
|
err := cdc.Unmarshal(bz, ptr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (cdc *LegacyAmino) UnmarshalLengthPrefixed(bz []byte, ptr interface{}) error {
|
|
err := cdc.Amino.UnmarshalBinaryLengthPrefixed(bz, ptr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return cdc.unmarshalAnys(ptr)
|
|
}
|
|
|
|
func (cdc *LegacyAmino) MustUnmarshalLengthPrefixed(bz []byte, ptr interface{}) {
|
|
err := cdc.UnmarshalLengthPrefixed(bz, ptr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// MarshalJSON implements codec.Codec interface
|
|
func (cdc *LegacyAmino) MarshalJSON(o interface{}) ([]byte, error) {
|
|
err := cdc.jsonMarshalAnys(o)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cdc.Amino.MarshalJSON(o)
|
|
}
|
|
|
|
func (cdc *LegacyAmino) MustMarshalJSON(o interface{}) []byte {
|
|
bz, err := cdc.MarshalJSON(o)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return bz
|
|
}
|
|
|
|
// UnmarshalJSON implements codec.Codec interface
|
|
func (cdc *LegacyAmino) UnmarshalJSON(bz []byte, ptr interface{}) error {
|
|
err := cdc.Amino.UnmarshalJSON(bz, ptr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return cdc.jsonUnmarshalAnys(ptr)
|
|
}
|
|
|
|
func (cdc *LegacyAmino) MustUnmarshalJSON(bz []byte, ptr interface{}) {
|
|
err := cdc.UnmarshalJSON(bz, ptr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (*LegacyAmino) UnpackAny(*types.Any, interface{}) error {
|
|
return errors.New("AminoCodec can't handle unpack protobuf Any's")
|
|
}
|
|
|
|
func (cdc *LegacyAmino) RegisterInterface(ptr interface{}, iopts *amino.InterfaceOptions) {
|
|
cdc.Amino.RegisterInterface(ptr, iopts)
|
|
}
|
|
|
|
func (cdc *LegacyAmino) RegisterConcrete(o interface{}, name string, copts *amino.ConcreteOptions) {
|
|
cdc.Amino.RegisterConcrete(o, name, copts)
|
|
}
|
|
|
|
func (cdc *LegacyAmino) MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error) {
|
|
err := cdc.jsonMarshalAnys(o)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return cdc.Amino.MarshalJSONIndent(o, prefix, indent)
|
|
}
|
|
|
|
func (cdc *LegacyAmino) PrintTypes(out io.Writer) error {
|
|
return cdc.Amino.PrintTypes(out)
|
|
}
|