github.com/cosmos/cosmos-sdk@v0.50.10/types/tx_msg.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	fmt "fmt"
     6  	strings "strings"
     7  
     8  	"github.com/cosmos/gogoproto/proto"
     9  	protov2 "google.golang.org/protobuf/proto"
    10  
    11  	"github.com/cosmos/cosmos-sdk/codec"
    12  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    13  	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
    14  )
    15  
    16  type (
    17  	// Msg defines the interface a transaction message needed to fulfill.
    18  	Msg = proto.Message
    19  
    20  	// LegacyMsg defines the interface a transaction message needed to fulfill up through
    21  	// v0.47.
    22  	LegacyMsg interface {
    23  		Msg
    24  
    25  		// GetSigners returns the addrs of signers that must sign.
    26  		// CONTRACT: All signatures must be present to be valid.
    27  		// CONTRACT: Returns addrs in some deterministic order.
    28  		GetSigners() []AccAddress
    29  	}
    30  
    31  	// Fee defines an interface for an application application-defined concrete
    32  	// transaction type to be able to set and return the transaction fee.
    33  	Fee interface {
    34  		GetGas() uint64
    35  		GetAmount() Coins
    36  	}
    37  
    38  	// Signature defines an interface for an application application-defined
    39  	// concrete transaction type to be able to set and return transaction signatures.
    40  	Signature interface {
    41  		GetPubKey() cryptotypes.PubKey
    42  		GetSignature() []byte
    43  	}
    44  
    45  	// HasMsgs defines an interface a transaction must fulfill.
    46  	HasMsgs interface {
    47  		// GetMsgs gets the all the transaction's messages.
    48  		GetMsgs() []Msg
    49  	}
    50  
    51  	// Tx defines an interface a transaction must fulfill.
    52  	Tx interface {
    53  		HasMsgs
    54  
    55  		// GetMsgsV2 gets the transaction's messages as google.golang.org/protobuf/proto.Message's.
    56  		GetMsgsV2() ([]protov2.Message, error)
    57  	}
    58  
    59  	// FeeTx defines the interface to be implemented by Tx to use the FeeDecorators
    60  	FeeTx interface {
    61  		Tx
    62  		GetGas() uint64
    63  		GetFee() Coins
    64  		FeePayer() []byte
    65  		FeeGranter() []byte
    66  	}
    67  
    68  	// TxWithMemo must have GetMemo() method to use ValidateMemoDecorator
    69  	TxWithMemo interface {
    70  		Tx
    71  		GetMemo() string
    72  	}
    73  
    74  	// TxWithTimeoutHeight extends the Tx interface by allowing a transaction to
    75  	// set a height timeout.
    76  	TxWithTimeoutHeight interface {
    77  		Tx
    78  
    79  		GetTimeoutHeight() uint64
    80  	}
    81  
    82  	// HasValidateBasic defines a type that has a ValidateBasic method.
    83  	// ValidateBasic is deprecated and now facultative.
    84  	// Prefer validating messages directly in the msg server.
    85  	HasValidateBasic interface {
    86  		// ValidateBasic does a simple validation check that
    87  		// doesn't require access to any other information.
    88  		ValidateBasic() error
    89  	}
    90  )
    91  
    92  // TxDecoder unmarshals transaction bytes
    93  type TxDecoder func(txBytes []byte) (Tx, error)
    94  
    95  // TxEncoder marshals transaction to bytes
    96  type TxEncoder func(tx Tx) ([]byte, error)
    97  
    98  // MsgTypeURL returns the TypeURL of a `sdk.Msg`.
    99  var MsgTypeURL = codectypes.MsgTypeURL
   100  
   101  // GetMsgFromTypeURL returns a `sdk.Msg` message type from a type URL
   102  func GetMsgFromTypeURL(cdc codec.Codec, input string) (Msg, error) {
   103  	var msg Msg
   104  	bz, err := json.Marshal(struct {
   105  		Type string `json:"@type"`
   106  	}{
   107  		Type: input,
   108  	})
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	if err := cdc.UnmarshalInterfaceJSON(bz, &msg); err != nil {
   114  		return nil, fmt.Errorf("failed to determine sdk.Msg for %s URL : %w", input, err)
   115  	}
   116  
   117  	return msg, nil
   118  }
   119  
   120  // GetModuleNameFromTypeURL assumes that module name is the second element of the msg type URL
   121  // e.g. "cosmos.bank.v1beta1.MsgSend" => "bank"
   122  // It returns an empty string if the input is not a valid type URL
   123  func GetModuleNameFromTypeURL(input string) string {
   124  	moduleName := strings.Split(input, ".")
   125  	if len(moduleName) > 1 {
   126  		return moduleName[1]
   127  	}
   128  
   129  	return ""
   130  }