2021-04-17 10:00:07 +00:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/keys"
|
|
|
|
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
)
|
|
|
|
|
|
|
|
// available output formats.
|
|
|
|
const (
|
|
|
|
OutputFormatText = "text"
|
|
|
|
OutputFormatJSON = "json"
|
|
|
|
|
|
|
|
// defaultKeyDBName is the client's subdirectory where keys are stored.
|
|
|
|
defaultKeyDBName = "keys"
|
|
|
|
)
|
|
|
|
|
2022-04-11 08:09:39 +00:00
|
|
|
type bechKeyOutFn func(keyInfo *cryptokeyring.Record) (cryptokeyring.KeyOutput, error)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2022-04-11 08:09:39 +00:00
|
|
|
func printKeyInfo(w io.Writer, keyInfo *cryptokeyring.Record, bechKeyOut bechKeyOutFn, output string) {
|
2021-04-17 10:00:07 +00:00
|
|
|
ko, err := bechKeyOut(keyInfo)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch output {
|
|
|
|
case OutputFormatText:
|
|
|
|
printTextInfos(w, []cryptokeyring.KeyOutput{ko})
|
|
|
|
|
|
|
|
case OutputFormatJSON:
|
|
|
|
out, err := keys.KeysCdc.MarshalJSON(ko)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(w, string(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTextInfos(w io.Writer, kos []cryptokeyring.KeyOutput) {
|
|
|
|
out, err := yaml.Marshal(&kos)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, string(out))
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
func validateMultisigThreshold(k, nKeys int) error {
|
|
|
|
if k <= 0 {
|
|
|
|
return fmt.Errorf("threshold must be a positive integer")
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
2021-06-29 17:02:21 +00:00
|
|
|
if nKeys < k {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"threshold k of n multisignature: %d < %d", nKeys, k)
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
2021-06-29 17:02:21 +00:00
|
|
|
return nil
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|