github.com/cosmos/cosmos-sdk@v0.50.10/types/mempool/signer_extraction_adapter.go (about)

     1  package mempool
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/cosmos/cosmos-sdk/types"
     7  	"github.com/cosmos/cosmos-sdk/x/auth/signing"
     8  )
     9  
    10  // SignerData contains canonical useful information about the signer of a transaction
    11  type SignerData struct {
    12  	Signer   sdk.AccAddress
    13  	Sequence uint64
    14  }
    15  
    16  // NewSignerData returns a new SignerData instance.
    17  func NewSignerData(signer sdk.AccAddress, sequence uint64) SignerData {
    18  	return SignerData{
    19  		Signer:   signer,
    20  		Sequence: sequence,
    21  	}
    22  }
    23  
    24  // String implements the fmt.Stringer interface.
    25  func (s SignerData) String() string {
    26  	return fmt.Sprintf("SignerData{Signer: %s, Sequence: %d}", s.Signer, s.Sequence)
    27  }
    28  
    29  // SignerExtractionAdapter is an interface used to determine how the signers of a transaction should be extracted
    30  // from the transaction.
    31  type SignerExtractionAdapter interface {
    32  	GetSigners(sdk.Tx) ([]SignerData, error)
    33  }
    34  
    35  var _ SignerExtractionAdapter = DefaultSignerExtractionAdapter{}
    36  
    37  // DefaultSignerExtractionAdapter is the default implementation of SignerExtractionAdapter. It extracts the signers
    38  // from a cosmos-sdk tx via GetSignaturesV2.
    39  type DefaultSignerExtractionAdapter struct{}
    40  
    41  // NewDefaultSignerExtractionAdapter constructs a new DefaultSignerExtractionAdapter instance
    42  func NewDefaultSignerExtractionAdapter() DefaultSignerExtractionAdapter {
    43  	return DefaultSignerExtractionAdapter{}
    44  }
    45  
    46  // GetSigners implements the Adapter interface
    47  func (DefaultSignerExtractionAdapter) GetSigners(tx sdk.Tx) ([]SignerData, error) {
    48  	sigTx, ok := tx.(signing.SigVerifiableTx)
    49  	if !ok {
    50  		return nil, fmt.Errorf("tx of type %T does not implement SigVerifiableTx", tx)
    51  	}
    52  
    53  	sigs, err := sigTx.GetSignaturesV2()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	signers := make([]SignerData, len(sigs))
    59  	for i, sig := range sigs {
    60  		signers[i] = NewSignerData(
    61  			sig.PubKey.Address().Bytes(),
    62  			sig.Sequence,
    63  		)
    64  	}
    65  
    66  	return signers, nil
    67  }