github.com/Finschia/finschia-sdk@v0.48.1/crypto/keyring/signing_algorithms.go (about)

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