github.com/cosmos/cosmos-sdk@v0.50.1/crypto/keyring/signing_algorithms.go (about)

     1  package keyring
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/cockroachdb/errors"
     7  
     8  	"github.com/cosmos/cosmos-sdk/crypto/hd"
     9  )
    10  
    11  // SignatureAlgo defines the interface for a keyring supported algorithm.
    12  type SignatureAlgo interface {
    13  	Name() hd.PubKeyType
    14  	Derive() hd.DeriveFn
    15  	Generate() hd.GenerateFn
    16  }
    17  
    18  // NewSigningAlgoFromString creates a supported SignatureAlgo.
    19  func NewSigningAlgoFromString(str string, algoList SigningAlgoList) (SignatureAlgo, error) {
    20  	for _, algo := range algoList {
    21  		if str == string(algo.Name()) {
    22  			return algo, nil
    23  		}
    24  	}
    25  	return nil, errors.Wrap(ErrUnsupportedSigningAlgo, str)
    26  }
    27  
    28  // SigningAlgoList is a slice of signature algorithms
    29  type SigningAlgoList []SignatureAlgo
    30  
    31  // Contains returns true if the SigningAlgoList the given SignatureAlgo.
    32  func (sal SigningAlgoList) Contains(algo SignatureAlgo) bool {
    33  	for _, cAlgo := range sal {
    34  		if cAlgo.Name() == algo.Name() {
    35  			return true
    36  		}
    37  	}
    38  
    39  	return false
    40  }
    41  
    42  // String returns a comma separated string of the signature algorithm names in the list.
    43  func (sal SigningAlgoList) String() string {
    44  	names := make([]string, len(sal))
    45  	for i := range sal {
    46  		names[i] = string(sal[i].Name())
    47  	}
    48  
    49  	return strings.Join(names, ",")
    50  }