client/keys/parse: honor config changes (#6172)

`keys parse` uses the global configuration before
before client applications have had a chance to
apply their settings.

This change adds a `GetSealedConfig()` helper
that waits for the config to be sealed before
returning it.

fixes #5091
addresses #5283
This commit is contained in:
Adam Bozanich
2020-05-08 10:30:55 +02:00
committed by GitHub
parent c8c47786da
commit 4e328d75db
4 changed files with 69 additions and 28 deletions
+20 -12
View File
@@ -1,6 +1,7 @@
package keys
import (
"context"
"encoding/hex"
"errors"
"fmt"
@@ -18,14 +19,15 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
var config = sdk.GetConfig()
var bech32Prefixes = []string{
config.GetBech32AccountAddrPrefix(),
config.GetBech32AccountPubPrefix(),
config.GetBech32ValidatorAddrPrefix(),
config.GetBech32ValidatorPubPrefix(),
config.GetBech32ConsensusAddrPrefix(),
config.GetBech32ConsensusPubPrefix(),
func bech32Prefixes(config *sdk.Config) []string {
return []string{
config.GetBech32AccountAddrPrefix(),
config.GetBech32AccountPubPrefix(),
config.GetBech32ValidatorAddrPrefix(),
config.GetBech32ValidatorPubPrefix(),
config.GetBech32ConsensusAddrPrefix(),
config.GetBech32ConsensusPubPrefix(),
}
}
type hexOutput struct {
@@ -45,7 +47,8 @@ type bech32Output struct {
Formats []string `json:"formats"`
}
func newBech32Output(bs []byte) bech32Output {
func newBech32Output(config *sdk.Config, bs []byte) bech32Output {
bech32Prefixes := bech32Prefixes(config)
out := bech32Output{Formats: make([]string, len(bech32Prefixes))}
for i, prefix := range bech32Prefixes {
@@ -87,6 +90,11 @@ hexadecimal into bech32 cosmos prefixed format and vice versa.
}
func parseKey(cmd *cobra.Command, args []string) error {
config, _ := sdk.GetSealedConfig(context.Background())
return doParseKey(cmd, config, args)
}
func doParseKey(cmd *cobra.Command, config *sdk.Config, args []string) error {
addr := strings.TrimSpace(args[0])
outstream := cmd.OutOrStdout()
@@ -94,7 +102,7 @@ func parseKey(cmd *cobra.Command, args []string) error {
return errors.New("couldn't parse empty input")
}
if !(runFromBech32(outstream, addr) || runFromHex(outstream, addr)) {
if !(runFromBech32(outstream, addr) || runFromHex(config, outstream, addr)) {
return errors.New("couldn't find valid bech32 nor hex data")
}
@@ -114,13 +122,13 @@ func runFromBech32(w io.Writer, bech32str string) bool {
}
// print info from hex
func runFromHex(w io.Writer, hexstr string) bool {
func runFromHex(config *sdk.Config, w io.Writer, hexstr string) bool {
bz, err := hex.DecodeString(hexstr)
if err != nil {
return false
}
displayParseKeyInfo(w, newBech32Output(bz))
displayParseKeyInfo(w, newBech32Output(config, bz))
return true
}
+4 -1
View File
@@ -3,6 +3,7 @@ package keys
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)
@@ -10,6 +11,8 @@ func TestParseKey(t *testing.T) {
bech32str := "cosmos104ytdpvrx9284zd50v9ep8c6j7pua7dkk0x3ek"
hexstr := "EB5AE9872103497EC092EF901027049E4F39200C60040D3562CD7F104A39F62E6E5A39A818F4"
config := sdk.NewConfig()
tests := []struct {
name string
args []string
@@ -23,7 +26,7 @@ func TestParseKey(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.wantErr, parseKey(ParseKeyStringCommand(), tt.args) != nil)
require.Equal(t, tt.wantErr, doParseKey(ParseKeyStringCommand(), config, tt.args) != nil)
})
}
}