github.com/Finschia/finschia-sdk@v0.48.1/client/tx/legacy.go (about)

     1  package tx
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Finschia/finschia-sdk/client"
     7  	"github.com/Finschia/finschia-sdk/codec"
     8  	"github.com/Finschia/finschia-sdk/x/auth/legacy/legacytx"
     9  	"github.com/Finschia/finschia-sdk/x/auth/signing"
    10  )
    11  
    12  // ConvertTxToStdTx converts a transaction to the legacy StdTx format
    13  func ConvertTxToStdTx(codec *codec.LegacyAmino, tx signing.Tx) (legacytx.StdTx, error) {
    14  	if stdTx, ok := tx.(legacytx.StdTx); ok {
    15  		return stdTx, nil
    16  	}
    17  
    18  	aminoTxConfig := legacytx.StdTxConfig{Cdc: codec}
    19  	builder := aminoTxConfig.NewTxBuilder()
    20  
    21  	err := CopyTx(tx, builder, true)
    22  	if err != nil {
    23  		return legacytx.StdTx{}, err
    24  	}
    25  
    26  	stdTx, ok := builder.GetTx().(legacytx.StdTx)
    27  	if !ok {
    28  		return legacytx.StdTx{}, fmt.Errorf("expected %T, got %+v", legacytx.StdTx{}, builder.GetTx())
    29  	}
    30  
    31  	return stdTx, nil
    32  }
    33  
    34  // CopyTx copies a Tx to a new TxBuilder, allowing conversion between
    35  // different transaction formats. If ignoreSignatureError is true, copying will continue
    36  // tx even if the signature cannot be set in the target builder resulting in an unsigned tx.
    37  func CopyTx(tx signing.Tx, builder client.TxBuilder, ignoreSignatureError bool) error {
    38  	err := builder.SetMsgs(tx.GetMsgs()...)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	sigs, err := tx.GetSignaturesV2()
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	err = builder.SetSignatures(sigs...)
    49  	if err != nil {
    50  		if ignoreSignatureError {
    51  			// we call SetSignatures() agan with no args to clear any signatures in case the
    52  			// previous call to SetSignatures() had any partial side-effects
    53  			_ = builder.SetSignatures()
    54  		} else {
    55  			return err
    56  		}
    57  	}
    58  
    59  	builder.SetMemo(tx.GetMemo())
    60  	builder.SetFeeAmount(tx.GetFee())
    61  	builder.SetGasLimit(tx.GetGas())
    62  	builder.SetTimeoutHeight(tx.GetTimeoutHeight())
    63  
    64  	return nil
    65  }