2020-03-25 08:20:36 -07:00
|
|
|
package keyring
|
2018-06-28 17:54:47 -07:00
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-11 07:30:42 -07:00
|
|
|
"strings"
|
2020-04-08 02:38:28 -07:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2018-06-28 17:54:47 -07:00
|
|
|
)
|
2020-03-25 08:20:36 -07:00
|
|
|
|
2020-06-11 07:30:42 -07:00
|
|
|
// SignatureAlgo defines the interface for a keyring supported algorithm.
|
2020-04-08 02:38:28 -07:00
|
|
|
type SignatureAlgo interface {
|
|
|
|
Name() hd.PubKeyType
|
|
|
|
Derive() hd.DeriveFn
|
|
|
|
Generate() hd.GenerateFn
|
|
|
|
}
|
|
|
|
|
2020-06-11 07:30:42 -07:00
|
|
|
// NewSigningAlgoFromString creates a supported SignatureAlgo.
|
|
|
|
func NewSigningAlgoFromString(str string, algoList SigningAlgoList) (SignatureAlgo, error) {
|
|
|
|
for _, algo := range algoList {
|
|
|
|
if str == string(algo.Name()) {
|
|
|
|
return algo, nil
|
|
|
|
}
|
2020-04-08 02:38:28 -07:00
|
|
|
}
|
2020-06-11 07:30:42 -07:00
|
|
|
return nil, fmt.Errorf("provided algorithm %q is not supported", str)
|
2020-04-08 02:38:28 -07:00
|
|
|
}
|
|
|
|
|
2020-06-11 07:30:42 -07:00
|
|
|
// SigningAlgoList is a slice of signature algorithms
|
2020-04-08 02:38:28 -07:00
|
|
|
type SigningAlgoList []SignatureAlgo
|
|
|
|
|
2020-06-11 07:30:42 -07:00
|
|
|
// Contains returns true if the SigningAlgoList the given SignatureAlgo.
|
|
|
|
func (sal SigningAlgoList) Contains(algo SignatureAlgo) bool {
|
|
|
|
for _, cAlgo := range sal {
|
2020-04-08 02:38:28 -07:00
|
|
|
if cAlgo.Name() == algo.Name() {
|
2020-03-25 08:20:36 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 02:38:28 -07:00
|
|
|
|
2020-03-25 08:20:36 -07:00
|
|
|
return false
|
|
|
|
}
|
2020-06-11 07:30:42 -07:00
|
|
|
|
|
|
|
// String returns a comma separated string of the signature algorithm names in the list.
|
|
|
|
func (sal SigningAlgoList) String() string {
|
|
|
|
names := make([]string, len(sal))
|
|
|
|
for i := range sal {
|
|
|
|
names[i] = string(sal[i].Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(names, ",")
|
|
|
|
}
|