github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/migrations/legacytx/stdtx.go (about)

     1  package legacytx
     2  
     3  import (
     4  	errorsmod "cosmossdk.io/errors"
     5  	"cosmossdk.io/math"
     6  
     7  	"github.com/cosmos/cosmos-sdk/codec/legacy"
     8  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
     9  	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
    10  	sdk "github.com/cosmos/cosmos-sdk/types"
    11  	"github.com/cosmos/cosmos-sdk/types/tx/signing"
    12  )
    13  
    14  // Interface implementation checks
    15  var (
    16  	_ codectypes.UnpackInterfacesMessage = (*StdTx)(nil)
    17  
    18  	_ codectypes.UnpackInterfacesMessage = (*StdSignature)(nil)
    19  )
    20  
    21  // StdFee includes the amount of coins paid in fees and the maximum
    22  // gas to be used by the transaction. The ratio yields an effective "gasprice",
    23  // which must be above some miminum to be accepted into the mempool.
    24  // [Deprecated]
    25  type StdFee struct {
    26  	Amount  sdk.Coins `json:"amount" yaml:"amount"`
    27  	Gas     uint64    `json:"gas" yaml:"gas"`
    28  	Payer   string    `json:"payer,omitempty" yaml:"payer"`
    29  	Granter string    `json:"granter,omitempty" yaml:"granter"`
    30  }
    31  
    32  // Deprecated: NewStdFee returns a new instance of StdFee
    33  func NewStdFee(gas uint64, amount sdk.Coins) StdFee {
    34  	return StdFee{
    35  		Amount: amount,
    36  		Gas:    gas,
    37  	}
    38  }
    39  
    40  // GetGas returns the fee's (wanted) gas.
    41  func (fee StdFee) GetGas() uint64 {
    42  	return fee.Gas
    43  }
    44  
    45  // GetAmount returns the fee's amount.
    46  func (fee StdFee) GetAmount() sdk.Coins {
    47  	return fee.Amount
    48  }
    49  
    50  // Bytes returns the encoded bytes of a StdFee.
    51  func (fee StdFee) Bytes() []byte {
    52  	if len(fee.Amount) == 0 {
    53  		fee.Amount = sdk.NewCoins()
    54  	}
    55  
    56  	bz, err := legacy.Cdc.MarshalJSON(fee)
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  
    61  	return bz
    62  }
    63  
    64  // GasPrices returns the gas prices for a StdFee.
    65  //
    66  // NOTE: The gas prices returned are not the true gas prices that were
    67  // originally part of the submitted transaction because the fee is computed
    68  // as fee = ceil(gasWanted * gasPrices).
    69  func (fee StdFee) GasPrices() sdk.DecCoins {
    70  	return sdk.NewDecCoinsFromCoins(fee.Amount...).QuoDec(math.LegacyNewDec(int64(fee.Gas)))
    71  }
    72  
    73  // StdTip is the tips used in a tipped transaction.
    74  type StdTip struct {
    75  	Amount sdk.Coins `json:"amount" yaml:"amount"`
    76  	Tipper string    `json:"tipper" yaml:"tipper"`
    77  }
    78  
    79  // StdTx is the legacy transaction format for wrapping a Msg with Fee and Signatures.
    80  // It only works with Amino, please prefer the new protobuf Tx in types/tx.
    81  // NOTE: the first signature is the fee payer (Signatures must not be nil).
    82  // Deprecated
    83  type StdTx struct {
    84  	Msgs          []sdk.Msg      `json:"msg" yaml:"msg"`
    85  	Fee           StdFee         `json:"fee" yaml:"fee"`
    86  	Signatures    []StdSignature `json:"signatures" yaml:"signatures"`
    87  	Memo          string         `json:"memo" yaml:"memo"`
    88  	TimeoutHeight uint64         `json:"timeout_height" yaml:"timeout_height"`
    89  }
    90  
    91  // Deprecated
    92  func NewStdTx(msgs []sdk.Msg, fee StdFee, sigs []StdSignature, memo string) StdTx {
    93  	return StdTx{
    94  		Msgs:       msgs,
    95  		Fee:        fee,
    96  		Signatures: sigs,
    97  		Memo:       memo,
    98  	}
    99  }
   100  
   101  // GetMsgs returns the all the transaction's messages.
   102  func (tx StdTx) GetMsgs() []sdk.Msg { return tx.Msgs }
   103  
   104  // Deprecated: AsAny implements intoAny. It doesn't work for protobuf serialization,
   105  // so it can't be saved into protobuf configured storage. We are using it only for API
   106  // compatibility.
   107  func (tx *StdTx) AsAny() *codectypes.Any {
   108  	return codectypes.UnsafePackAny(tx)
   109  }
   110  
   111  // GetMemo returns the memo
   112  func (tx StdTx) GetMemo() string { return tx.Memo }
   113  
   114  // GetTimeoutHeight returns the transaction's timeout height (if set).
   115  func (tx StdTx) GetTimeoutHeight() uint64 {
   116  	return tx.TimeoutHeight
   117  }
   118  
   119  // GetSignatures returns the signature of signers who signed the Msg.
   120  // CONTRACT: Length returned is same as length of
   121  // pubkeys returned from MsgKeySigners, and the order
   122  // matches.
   123  // CONTRACT: If the signature is missing (ie the Msg is
   124  // invalid), then the corresponding signature is
   125  // .Empty().
   126  func (tx StdTx) GetSignatures() [][]byte {
   127  	sigs := make([][]byte, len(tx.Signatures))
   128  	for i, stdSig := range tx.Signatures {
   129  		sigs[i] = stdSig.Signature
   130  	}
   131  	return sigs
   132  }
   133  
   134  // GetSignaturesV2 implements SigVerifiableTx.GetSignaturesV2
   135  func (tx StdTx) GetSignaturesV2() ([]signing.SignatureV2, error) {
   136  	res := make([]signing.SignatureV2, len(tx.Signatures))
   137  
   138  	for i, sig := range tx.Signatures {
   139  		var err error
   140  		res[i], err = StdSignatureToSignatureV2(legacy.Cdc, sig)
   141  		if err != nil {
   142  			return nil, errorsmod.Wrapf(err, "Unable to convert signature %v to V2", sig)
   143  		}
   144  	}
   145  
   146  	return res, nil
   147  }
   148  
   149  // GetPubkeys returns the pubkeys of signers if the pubkey is included in the signature
   150  // If pubkey is not included in the signature, then nil is in the slice instead
   151  func (tx StdTx) GetPubKeys() ([]cryptotypes.PubKey, error) {
   152  	pks := make([]cryptotypes.PubKey, len(tx.Signatures))
   153  
   154  	for i, stdSig := range tx.Signatures {
   155  		pks[i] = stdSig.GetPubKey()
   156  	}
   157  
   158  	return pks, nil
   159  }
   160  
   161  // GetGas returns the Gas in StdFee
   162  func (tx StdTx) GetGas() uint64 { return tx.Fee.Gas }
   163  
   164  // GetFee returns the FeeAmount in StdFee
   165  func (tx StdTx) GetFee() sdk.Coins { return tx.Fee.Amount }
   166  
   167  // FeeGranter always returns nil for StdTx
   168  func (tx StdTx) FeeGranter() sdk.AccAddress {
   169  	return nil
   170  }
   171  
   172  func (tx StdTx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
   173  	for _, m := range tx.Msgs {
   174  		err := codectypes.UnpackInterfaces(m, unpacker)
   175  		if err != nil {
   176  			return err
   177  		}
   178  	}
   179  
   180  	// Signatures contain PubKeys, which need to be unpacked.
   181  	for _, s := range tx.Signatures {
   182  		err := s.UnpackInterfaces(unpacker)
   183  		if err != nil {
   184  			return err
   185  		}
   186  	}
   187  
   188  	return nil
   189  }