github.com/lino-network/lino@v0.6.11/types/txencoder.go (about)

     1  package types
     2  
     3  import (
     4  	wire "github.com/cosmos/cosmos-sdk/codec"
     5  	sdk "github.com/cosmos/cosmos-sdk/types"
     6  	cauth "github.com/cosmos/cosmos-sdk/x/auth"
     7  )
     8  
     9  // Theses two functions must be paired, that if txEncoder use JSON format,
    10  // the txDecoder must use JSON format as well.
    11  // All libraries use use the TxEncoder below.
    12  
    13  // TxDecoder - default tx decoder, decode tx before authenticate handler
    14  func TxDecoder(cdc *wire.Codec) sdk.TxDecoder {
    15  	return func(txBytes []byte) (tx sdk.Tx, err sdk.Error) {
    16  		defer func() {
    17  			if r := recover(); r != nil {
    18  				err = sdk.ErrTxDecode("tx decode panic")
    19  			}
    20  		}()
    21  		tx = cauth.StdTx{}
    22  
    23  		if len(txBytes) == 0 {
    24  			return nil, sdk.ErrTxDecode("txBytes are empty")
    25  		}
    26  
    27  		// StdTx.Msg is an interface. The concrete types
    28  		// are registered by MakeTxCodec
    29  		unmarshalErr := cdc.UnmarshalJSON(txBytes, &tx)
    30  		if unmarshalErr != nil {
    31  			return nil, sdk.ErrTxDecode("")
    32  		}
    33  		return tx, nil
    34  	}
    35  }
    36  
    37  // TxDecoder - default tx decoder, decode tx before authenticate handler
    38  func TxEncoder(cdc *wire.Codec) sdk.TxEncoder {
    39  	return func(tx sdk.Tx) ([]byte, error) {
    40  		return cdc.MarshalJSON(tx)
    41  	}
    42  }