github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/tx/adapter.go (about)

     1  package tx
     2  
     3  import (
     4  	"google.golang.org/protobuf/types/known/anypb"
     5  
     6  	basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
     7  	multisigv1beta1 "cosmossdk.io/api/cosmos/crypto/multisig/v1beta1"
     8  	signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
     9  	txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1"
    10  	txsigning "cosmossdk.io/x/tx/signing"
    11  
    12  	"github.com/cosmos/cosmos-sdk/types/tx"
    13  )
    14  
    15  // GetSigningTxData returns an x/tx/signing.TxData representation of a transaction for use in the signing
    16  // API defined in x/tx.  The reason for all of this conversion is that x/tx depends on the protoreflect API
    17  // defined in google.golang.org/protobuf while x/auth/tx depends on the legacy proto API defined in
    18  // github.com/gogo/protobuf and the downstream SDK fork of that library, github.com/cosmos/gogoproto.
    19  // Therefore we need to convert between the two APIs.
    20  func (w *wrapper) GetSigningTxData() txsigning.TxData {
    21  	body := w.tx.Body
    22  	authInfo := w.tx.AuthInfo
    23  
    24  	msgs := make([]*anypb.Any, len(body.Messages))
    25  	for i, msg := range body.Messages {
    26  		msgs[i] = &anypb.Any{
    27  			TypeUrl: msg.TypeUrl,
    28  			Value:   msg.Value,
    29  		}
    30  	}
    31  
    32  	extOptions := make([]*anypb.Any, len(body.ExtensionOptions))
    33  	for i, extOption := range body.ExtensionOptions {
    34  		extOptions[i] = &anypb.Any{
    35  			TypeUrl: extOption.TypeUrl,
    36  			Value:   extOption.Value,
    37  		}
    38  	}
    39  
    40  	nonCriticalExtOptions := make([]*anypb.Any, len(body.NonCriticalExtensionOptions))
    41  	for i, extOption := range body.NonCriticalExtensionOptions {
    42  		nonCriticalExtOptions[i] = &anypb.Any{
    43  			TypeUrl: extOption.TypeUrl,
    44  			Value:   extOption.Value,
    45  		}
    46  	}
    47  
    48  	feeCoins := authInfo.Fee.Amount
    49  	feeAmount := make([]*basev1beta1.Coin, len(feeCoins))
    50  	for i, coin := range feeCoins {
    51  		feeAmount[i] = &basev1beta1.Coin{
    52  			Denom:  coin.Denom,
    53  			Amount: coin.Amount.String(),
    54  		}
    55  	}
    56  
    57  	txSignerInfos := make([]*txv1beta1.SignerInfo, len(authInfo.SignerInfos))
    58  	for i, signerInfo := range authInfo.SignerInfos {
    59  		modeInfo := &txv1beta1.ModeInfo{}
    60  		adaptModeInfo(signerInfo.ModeInfo, modeInfo)
    61  		txSignerInfo := &txv1beta1.SignerInfo{
    62  			PublicKey: &anypb.Any{
    63  				TypeUrl: signerInfo.PublicKey.TypeUrl,
    64  				Value:   signerInfo.PublicKey.Value,
    65  			},
    66  			Sequence: signerInfo.Sequence,
    67  			ModeInfo: modeInfo,
    68  		}
    69  		txSignerInfos[i] = txSignerInfo
    70  	}
    71  
    72  	txAuthInfo := &txv1beta1.AuthInfo{
    73  		SignerInfos: txSignerInfos,
    74  		Fee: &txv1beta1.Fee{
    75  			Amount:   feeAmount,
    76  			GasLimit: authInfo.Fee.GasLimit,
    77  			Payer:    authInfo.Fee.Payer,
    78  			Granter:  authInfo.Fee.Granter,
    79  		},
    80  	}
    81  
    82  	txBody := &txv1beta1.TxBody{
    83  		Messages:                    msgs,
    84  		Memo:                        body.Memo,
    85  		TimeoutHeight:               body.TimeoutHeight,
    86  		ExtensionOptions:            extOptions,
    87  		NonCriticalExtensionOptions: nonCriticalExtOptions,
    88  	}
    89  	txData := txsigning.TxData{
    90  		AuthInfo:      txAuthInfo,
    91  		AuthInfoBytes: w.getAuthInfoBytes(),
    92  		Body:          txBody,
    93  		BodyBytes:     w.getBodyBytes(),
    94  	}
    95  	return txData
    96  }
    97  
    98  func adaptModeInfo(legacy *tx.ModeInfo, res *txv1beta1.ModeInfo) {
    99  	// handle nil modeInfo. this is permissible through the code path:
   100  	// https://github.com/cosmos/cosmos-sdk/blob/4a6a1e3cb8de459891cb0495052589673d14ef51/x/auth/tx/builder.go#L295
   101  	// -> https://github.com/cosmos/cosmos-sdk/blob/b7841e3a76a38d069c1b9cb3d48368f7a67e9c26/x/auth/tx/sigs.go#L15-L17
   102  	// when signature.Data is nil.
   103  	if legacy == nil {
   104  		return
   105  	}
   106  
   107  	switch mi := legacy.Sum.(type) {
   108  	case *tx.ModeInfo_Single_:
   109  		res.Sum = &txv1beta1.ModeInfo_Single_{
   110  			Single: &txv1beta1.ModeInfo_Single{
   111  				Mode: signingv1beta1.SignMode(legacy.GetSingle().Mode),
   112  			},
   113  		}
   114  	case *tx.ModeInfo_Multi_:
   115  		multiModeInfos := legacy.GetMulti().ModeInfos
   116  		modeInfos := make([]*txv1beta1.ModeInfo, len(multiModeInfos))
   117  		for _, modeInfo := range multiModeInfos {
   118  			adaptModeInfo(modeInfo, &txv1beta1.ModeInfo{})
   119  		}
   120  		res.Sum = &txv1beta1.ModeInfo_Multi_{
   121  			Multi: &txv1beta1.ModeInfo_Multi{
   122  				Bitarray: &multisigv1beta1.CompactBitArray{
   123  					Elems:           mi.Multi.Bitarray.Elems,
   124  					ExtraBitsStored: mi.Multi.Bitarray.ExtraBitsStored,
   125  				},
   126  				ModeInfos: modeInfos,
   127  			},
   128  		}
   129  	}
   130  }