Add support for hex / base64 / btc (b58) encoding of binary data

This commit is contained in:
Ethan Frey
2017-02-28 20:26:45 +01:00
parent c59e2d7d13
commit e1c717a048
2 changed files with 53 additions and 1 deletions

View File

@ -23,6 +23,8 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
data "github.com/tendermint/go-data"
"github.com/tendermint/go-data/base58"
keys "github.com/tendermint/go-keys"
"github.com/tendermint/go-keys/cryptostore"
"github.com/tendermint/go-keys/storage/filestorage"
@ -59,8 +61,9 @@ func Execute() {
func init() {
cobra.OnInitialize(initEnv)
RootCmd.PersistentFlags().StringP("root", "r", os.ExpandEnv("$HOME/.tlc"), "root directory for config and data")
RootCmd.PersistentFlags().StringP("output", "o", "text", "Output format (text|json)")
RootCmd.PersistentFlags().String("keydir", "keys", "Directory to store private keys (subdir of root)")
RootCmd.PersistentFlags().StringP("output", "o", "text", "Output format (text|json)")
RootCmd.PersistentFlags().StringP("encoding", "e", "hex", "Binary encoding (hex|b64|btc)")
}
// initEnv sets to use ENV variables if set.
@ -101,6 +104,19 @@ func validateFlags(cmd *cobra.Command) error {
return errors.Errorf("Unsupported output format: %s", output)
}
// validate and set encoding
enc := viper.GetString("encoding")
switch enc {
case "hex":
data.Encoder = data.HexEncoder
case "b64":
data.Encoder = data.B64Encoder
case "btc":
data.Encoder = base58.BTCEncoder
default:
return errors.Errorf("Unsupported encoding: %s", enc)
}
// store the keys directory
keyDir = viper.GetString("keydir")
if !filepath.IsAbs(keyDir) {