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

     1  package ante
     2  
     3  import (
     4  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
     5  	sdk "github.com/cosmos/cosmos-sdk/types"
     6  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
     7  )
     8  
     9  type HasExtensionOptionsTx interface {
    10  	GetExtensionOptions() []*codectypes.Any
    11  	GetNonCriticalExtensionOptions() []*codectypes.Any
    12  }
    13  
    14  // ExtensionOptionChecker is a function that returns true if the extension option is accepted.
    15  type ExtensionOptionChecker func(*codectypes.Any) bool
    16  
    17  // rejectExtensionOption is the default extension check that reject all tx
    18  // extensions.
    19  func rejectExtensionOption(*codectypes.Any) bool {
    20  	return false
    21  }
    22  
    23  // RejectExtensionOptionsDecorator is an AnteDecorator that rejects all extension
    24  // options which can optionally be included in protobuf transactions. Users that
    25  // need extension options should create a custom AnteHandler chain that handles
    26  // needed extension options properly and rejects unknown ones.
    27  type RejectExtensionOptionsDecorator struct {
    28  	checker ExtensionOptionChecker
    29  }
    30  
    31  // NewExtensionOptionsDecorator creates a new antehandler that rejects all extension
    32  // options which can optionally be included in protobuf transactions that don't pass the checker.
    33  // Users that need extension options should pass a custom checker that returns true for the
    34  // needed extension options.
    35  func NewExtensionOptionsDecorator(checker ExtensionOptionChecker) sdk.AnteDecorator {
    36  	if checker == nil {
    37  		checker = rejectExtensionOption
    38  	}
    39  
    40  	return RejectExtensionOptionsDecorator{checker: checker}
    41  }
    42  
    43  var _ sdk.AnteDecorator = RejectExtensionOptionsDecorator{}
    44  
    45  // AnteHandle implements the AnteDecorator.AnteHandle method
    46  func (r RejectExtensionOptionsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
    47  	err = checkExtOpts(tx, r.checker)
    48  	if err != nil {
    49  		return ctx, err
    50  	}
    51  
    52  	return next(ctx, tx, simulate)
    53  }
    54  
    55  func checkExtOpts(tx sdk.Tx, checker ExtensionOptionChecker) error {
    56  	if hasExtOptsTx, ok := tx.(HasExtensionOptionsTx); ok {
    57  		for _, opt := range hasExtOptsTx.GetExtensionOptions() {
    58  			if !checker(opt) {
    59  				return sdkerrors.ErrUnknownExtensionOptions
    60  			}
    61  		}
    62  	}
    63  
    64  	return nil
    65  }