cosmos-sdk/types/codec.go

66 lines
1.5 KiB
Go

package types
import (
"cosmossdk.io/collections"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
)
const (
// MsgInterfaceProtoName defines the protobuf name of the cosmos Msg interface
MsgInterfaceProtoName = "cosmos.base.v1beta1.Msg"
)
// RegisterLegacyAminoCodec registers the sdk message type.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterInterface((*Msg)(nil), nil)
cdc.RegisterInterface((*Tx)(nil), nil)
}
// RegisterInterfaces registers the sdk message type.
func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterInterface(MsgInterfaceProtoName, (*Msg)(nil))
}
// Collection Codecs
// IntValue represents a collections.ValueCodec to work with Int.
var IntValue collections.ValueCodec[math.Int] = intValueCodec{}
type intValueCodec struct{}
func (i intValueCodec) Encode(value math.Int) ([]byte, error) {
return value.Marshal()
}
func (i intValueCodec) Decode(b []byte) (math.Int, error) {
v := new(Int)
err := v.Unmarshal(b)
if err != nil {
return Int{}, err
}
return *v, nil
}
func (i intValueCodec) EncodeJSON(value math.Int) ([]byte, error) {
return value.MarshalJSON()
}
func (i intValueCodec) DecodeJSON(b []byte) (Int, error) {
v := new(Int)
err := v.UnmarshalJSON(b)
if err != nil {
return Int{}, err
}
return *v, nil
}
func (i intValueCodec) Stringify(value Int) string {
return value.String()
}
func (i intValueCodec) ValueType() string {
return "math.Int"
}