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

     1  package tx
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"google.golang.org/protobuf/proto"
     8  	"google.golang.org/protobuf/types/known/anypb"
     9  
    10  	txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1"
    11  	txsigning "cosmossdk.io/x/tx/signing"
    12  	"cosmossdk.io/x/tx/signing/textual"
    13  
    14  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    15  	sdk "github.com/cosmos/cosmos-sdk/types"
    16  	signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
    17  	"github.com/cosmos/cosmos-sdk/x/auth/signing"
    18  )
    19  
    20  // signModeTextualHandler defines the SIGN_MODE_TEXTUAL SignModeHandler.
    21  // It is currently not enabled by default, but you can enable it manually
    22  // for TESTING purposes. It will be enabled once SIGN_MODE_TEXTUAL is fully
    23  // released, see https://github.com/cosmos/cosmos-sdk/issues/11970.
    24  type signModeTextualHandler struct {
    25  	t textual.SignModeHandler
    26  }
    27  
    28  var _ signing.SignModeHandler = signModeTextualHandler{}
    29  
    30  // DefaultMode implements SignModeHandler.DefaultMode
    31  func (signModeTextualHandler) DefaultMode() signingtypes.SignMode {
    32  	return signingtypes.SignMode_SIGN_MODE_TEXTUAL
    33  }
    34  
    35  // Modes implements SignModeHandler.Modes
    36  func (signModeTextualHandler) Modes() []signingtypes.SignMode {
    37  	return []signingtypes.SignMode{signingtypes.SignMode_SIGN_MODE_TEXTUAL}
    38  }
    39  
    40  // GetSignBytes implements SignModeHandler.GetSignBytes
    41  func (h signModeTextualHandler) GetSignBytes(mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) {
    42  	panic("SIGN_MODE_TEXTUAL needs GetSignBytesWithContext")
    43  }
    44  
    45  // GetSignBytesWithContext implements SignModeHandler.GetSignBytesWithContext
    46  func (h signModeTextualHandler) GetSignBytesWithContext(ctx context.Context, mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) {
    47  	if mode != signingtypes.SignMode_SIGN_MODE_TEXTUAL {
    48  		return nil, fmt.Errorf("expected %s, got %s", signingtypes.SignMode_SIGN_MODE_TEXTUAL, mode)
    49  	}
    50  
    51  	protoTx, ok := tx.(*wrapper)
    52  	if !ok {
    53  		return nil, fmt.Errorf("can only handle a protobuf Tx, got %T", tx)
    54  	}
    55  
    56  	pbAny, err := codectypes.NewAnyWithValue(data.PubKey)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	txBody := &txv1beta1.TxBody{}
    62  	txAuthInfo := &txv1beta1.AuthInfo{}
    63  	err = proto.Unmarshal(protoTx.getBodyBytes(), txBody)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	err = proto.Unmarshal(protoTx.getAuthInfoBytes(), txAuthInfo)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	txData := txsigning.TxData{
    73  		Body:          txBody,
    74  		AuthInfo:      txAuthInfo,
    75  		BodyBytes:     protoTx.getBodyBytes(),
    76  		AuthInfoBytes: protoTx.getAuthInfoBytes(),
    77  	}
    78  
    79  	return h.t.GetSignBytes(ctx, txsigning.SignerData{
    80  		Address:       data.Address,
    81  		ChainID:       data.ChainID,
    82  		AccountNumber: data.AccountNumber,
    83  		Sequence:      data.Sequence,
    84  		PubKey: &anypb.Any{
    85  			TypeUrl: pbAny.TypeUrl,
    86  			Value:   pbAny.Value,
    87  		},
    88  	}, txData)
    89  }