cosmos-sdk/x/gov/types/codec.go

52 lines
1.7 KiB
Go

package types
import (
"github.com/cosmos/cosmos-sdk/codec"
)
// Codec defines the interface required to serialize custom x/gov types.
type Codec interface {
codec.Marshaler
MarshalProposal(Proposal) ([]byte, error)
UnmarshalProposal([]byte) (Proposal, error)
}
// RegisterCodec registers all the necessary types and interfaces for the
// governance module.
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterInterface((*Content)(nil), nil)
cdc.RegisterConcrete(MsgSubmitProposalBase{}, "cosmos-sdk/MsgSubmitProposalBase", nil)
cdc.RegisterConcrete(MsgSubmitProposal{}, "cosmos-sdk/MsgSubmitProposal", nil)
cdc.RegisterConcrete(MsgDeposit{}, "cosmos-sdk/MsgDeposit", nil)
cdc.RegisterConcrete(MsgVote{}, "cosmos-sdk/MsgVote", nil)
cdc.RegisterConcrete(TextProposal{}, "cosmos-sdk/TextProposal", nil)
}
// RegisterProposalTypeCodec registers an external proposal content type defined
// in another module for the internal ModuleCdc. This allows the MsgSubmitProposal
// to be correctly Amino encoded and decoded.
//
// NOTE: This should only be used for applications that are still using a concrete
// Amino codec for serialization.
func RegisterProposalTypeCodec(o interface{}, name string) {
amino.RegisterConcrete(o, name, nil)
}
var (
amino = codec.New()
// ModuleCdc references the global x/gov module codec. Note, the codec should
// ONLY be used in certain instances of tests and for JSON encoding as Amino is
// still used for that purpose.
//
// The actual codec used for serialization should be provided to x/gov and
// defined at the application level.
ModuleCdc = codec.NewHybridCodec(amino)
)
func init() {
RegisterCodec(amino)
codec.RegisterCrypto(amino)
}