2018-02-22 07:17:19 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-30 07:54:01 -07:00
|
|
|
"io"
|
2018-02-22 07:17:19 -08:00
|
|
|
|
2021-09-24 07:37:34 -07:00
|
|
|
"sigs.k8s.io/yaml"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2020-03-25 08:20:36 -07:00
|
|
|
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2018-02-22 07:17:19 -08:00
|
|
|
)
|
|
|
|
|
2019-02-11 17:18:58 -08:00
|
|
|
// available output formats.
|
2019-02-08 12:45:23 -08:00
|
|
|
const (
|
|
|
|
OutputFormatText = "text"
|
|
|
|
OutputFormatJSON = "json"
|
|
|
|
)
|
2018-02-28 06:36:04 -08:00
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
type bechKeyOutFn func(k *cryptokeyring.Record) (cryptokeyring.KeyOutput, error)
|
2018-03-05 08:41:50 -08:00
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKeyOutFn, output string) {
|
|
|
|
ko, err := bechKeyOut(k)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2020-07-06 12:50:09 -07:00
|
|
|
switch output {
|
2019-02-08 12:45:23 -08:00
|
|
|
case OutputFormatText:
|
2021-09-20 05:02:15 -07:00
|
|
|
printTextRecords(w, []cryptokeyring.KeyOutput{ko})
|
2019-03-01 13:29:33 -08:00
|
|
|
|
2019-03-09 13:00:08 -08:00
|
|
|
case OutputFormatJSON:
|
2020-06-30 13:59:21 -07:00
|
|
|
out, err := KeysCdc.MarshalJSON(ko)
|
2018-02-22 07:17:19 -08:00
|
|
|
if err != nil {
|
2018-04-20 09:51:47 -07:00
|
|
|
panic(err)
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
fmt.Fprintln(w, string(out))
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
func printKeyringRecords(w io.Writer, records []*cryptokeyring.Record, output string) {
|
|
|
|
kos, err := cryptokeyring.MkAccKeysOutput(records)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-03-01 13:29:33 -08:00
|
|
|
|
2020-07-06 12:50:09 -07:00
|
|
|
switch output {
|
2019-02-08 12:45:23 -08:00
|
|
|
case OutputFormatText:
|
2021-09-20 05:02:15 -07:00
|
|
|
printTextRecords(w, kos)
|
2019-03-01 13:29:33 -08:00
|
|
|
|
2019-02-08 12:45:23 -08:00
|
|
|
case OutputFormatJSON:
|
2021-09-20 05:02:15 -07:00
|
|
|
// TODO https://github.com/cosmos/cosmos-sdk/issues/8046
|
|
|
|
// Replace AminoCdc with Proto JSON
|
2020-06-30 13:59:21 -07:00
|
|
|
out, err := KeysCdc.MarshalJSON(kos)
|
2018-02-22 07:17:19 -08:00
|
|
|
if err != nil {
|
2018-04-20 09:51:47 -07:00
|
|
|
panic(err)
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
2020-06-30 13:59:21 -07:00
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
fmt.Fprintf(w, "%s", out)
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
}
|
2018-05-28 16:27:34 -07:00
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
func printTextRecords(w io.Writer, kos []cryptokeyring.KeyOutput) {
|
2019-05-31 06:14:35 -07:00
|
|
|
out, err := yaml.Marshal(&kos)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-03-01 13:29:33 -08:00
|
|
|
}
|
2021-09-20 05:02:15 -07:00
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
fmt.Fprintln(w, string(out))
|
2019-03-01 13:29:33 -08:00
|
|
|
}
|