github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/types/codec.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
     7  	"github.com/tendermint/go-amino"
     8  )
     9  
    10  const (
    11  	// EthAccountName is the amino encoding name for EthAccount
    12  	EthAccountName = "fbexchain/EthAccount"
    13  )
    14  
    15  // RegisterCodec registers the account interfaces and concrete types on the
    16  // provided Amino codec.
    17  func RegisterCodec(cdc *codec.Codec) {
    18  	cdc.RegisterConcrete(&EthAccount{}, EthAccountName, nil)
    19  
    20  	cdc.RegisterConcreteUnmarshaller(EthAccountName, func(cdc *amino.Codec, data []byte) (interface{}, int, error) {
    21  		var cacc componentAccount
    22  		var acc = &cacc.ethAccount
    23  		acc.BaseAccount = &cacc.baseAccount
    24  		err := acc.UnmarshalFromAmino(cdc, data)
    25  		if err != nil {
    26  			return nil, 0, err
    27  		}
    28  		return acc, len(data), nil
    29  	})
    30  	cdc.RegisterConcreteMarshaller(EthAccountName, func(cdc *amino.Codec, v interface{}) ([]byte, error) {
    31  		if acc, ok := v.(*EthAccount); ok {
    32  			return acc.MarshalToAmino(cdc)
    33  		} else if acc, ok := v.(EthAccount); ok {
    34  			return acc.MarshalToAmino(cdc)
    35  		} else {
    36  			return nil, fmt.Errorf("%T is not an EthAccount", v)
    37  		}
    38  	})
    39  	cdc.EnableBufferMarshaler(EthAccount{})
    40  }