From f946b630a44c0d0ffc879c90c9cd019d07f0ac92 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 28 May 2018 19:27:34 -0400 Subject: [PATCH] fixes from review --- client/keys/utils.go | 24 ++++++++++------- types/account.go | 64 +++++++++++++++++++++----------------------- types/stake.go | 2 +- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/client/keys/utils.go b/client/keys/utils.go index eaf1e4c2f..a49ac63c0 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -74,15 +74,7 @@ func printInfo(info keys.Info) { switch viper.Get(cli.OutputFlag) { case "text": fmt.Printf("NAME:\tADDRESS:\t\t\t\t\tPUBKEY:\n") - bechAccount, err := sdk.Bech32CosmosifyAcc(ko.Address) - if err != nil { - panic(err) - } - bechPubKey, err := sdk.Bech32CosmosifyAccPub(ko.PubKey) - if err != nil { - panic(err) - } - fmt.Printf("%s\t%s\t%s\n", ko.Name, bechAccount, bechPubKey) + printKeyOutput(ko) case "json": out, err := json.MarshalIndent(ko, "", "\t") if err != nil { @@ -98,7 +90,7 @@ func printInfos(infos []keys.Info) { case "text": fmt.Printf("NAME:\tADDRESS:\t\t\t\t\tPUBKEY:\n") for _, ko := range kos { - fmt.Printf("%s\t%s\t%s\n", ko.Name, ko.Address, ko.PubKey) + printKeyOutput(ko) } case "json": out, err := json.MarshalIndent(kos, "", "\t") @@ -108,3 +100,15 @@ func printInfos(infos []keys.Info) { fmt.Println(string(out)) } } + +func printKeyOutput(ko KeyOutput) { + bechAccount, err := sdk.Bech32CosmosifyAcc(ko.Address) + if err != nil { + panic(err) + } + bechPubKey, err := sdk.Bech32CosmosifyAccPub(ko.PubKey) + if err != nil { + panic(err) + } + fmt.Printf("%s\t%s\t%s\n", ko.Name, bechAccount, bechPubKey) +} diff --git a/types/account.go b/types/account.go index 615fd30c1..b593dfb53 100644 --- a/types/account.go +++ b/types/account.go @@ -13,24 +13,32 @@ import ( //Address is a go crypto-style Address type Address = cmn.HexBytes +// Bech32 prefixes +const ( + Bech32PrefixAccAddr = "cosmosaccaddr" + Bech32PrefixAccPub = "cosmosaccpub" + Bech32PrefixValAddr = "cosmosvaladdr" + Bech32PrefixValPub = "cosmosvalpub" +) + // Bech32CosmosifyAcc takes Address and returns the Bech32Cosmos encoded string func Bech32CosmosifyAcc(addr Address) (string, error) { - return bech32cosmos.ConvertAndEncode("cosmosaccaddr", addr.Bytes()) + return bech32cosmos.ConvertAndEncode(Bech32PrefixAccAddr, addr.Bytes()) } // Bech32CosmosifyAccPub takes AccountPubKey and returns the Bech32Cosmos encoded string func Bech32CosmosifyAccPub(pub crypto.PubKey) (string, error) { - return bech32cosmos.ConvertAndEncode("cosmosaccpub", pub.Bytes()) + return bech32cosmos.ConvertAndEncode(Bech32PrefixAccPub, pub.Bytes()) } // Bech32CosmosifyVal returns the Bech32Cosmos encoded string for a validator address func Bech32CosmosifyVal(addr Address) (string, error) { - return bech32cosmos.ConvertAndEncode("cosmosvaladdr", addr.Bytes()) + return bech32cosmos.ConvertAndEncode(Bech32PrefixValAddr, addr.Bytes()) } // Bech32CosmosifyValPub returns the Bech32Cosmos encoded string for a validator pubkey func Bech32CosmosifyValPub(pub crypto.PubKey) (string, error) { - return bech32cosmos.ConvertAndEncode("cosmosvalpub", pub.Bytes()) + return bech32cosmos.ConvertAndEncode(Bech32PrefixValPub, pub.Bytes()) } // create an Address from a string @@ -47,16 +55,7 @@ func GetAccAddressHex(address string) (addr Address, err error) { // create an Address from a string func GetAccAddressBech32Cosmos(address string) (addr Address, err error) { - if len(address) == 0 { - return addr, errors.New("must use provide address") - } - - hrp, bz, err := bech32cosmos.DecodeAndConvert(address) - - if hrp != "cosmosaccaddr" { - return addr, fmt.Errorf("Invalid Address Prefix. Expected cosmosaccaddr, Got %s", hrp) - } - + bz, err := getFromBech32Cosmos(address, Bech32PrefixAccAddr) if err != nil { return nil, err } @@ -77,16 +76,7 @@ func GetValAddressHex(address string) (addr Address, err error) { // create an Address from a bech32cosmos string func GetValAddressBech32Cosmos(address string) (addr Address, err error) { - if len(address) == 0 { - return addr, errors.New("must use provide address") - } - - hrp, bz, err := bech32cosmos.DecodeAndConvert(address) - - if hrp != "cosmosvaladdr" { - return addr, fmt.Errorf("Invalid Address Prefix. Expected cosmosvaladdr, Got %s", hrp) - } - + bz, err := getFromBech32Cosmos(address, Bech32PrefixValAddr) if err != nil { return nil, err } @@ -95,15 +85,7 @@ func GetValAddressBech32Cosmos(address string) (addr Address, err error) { //Decode a validator publickey into a public key func GetValPubKeyBech32Cosmos(pubkey string) (pk crypto.PubKey, err error) { - if len(pubkey) == 0 { - return pk, errors.New("must use provide pubkey") - } - hrp, bz, err := bech32cosmos.DecodeAndConvert(pubkey) - - if hrp != "cosmosvalpub" { - return pk, fmt.Errorf("Invalid Validator Pubkey Prefix. Expected cosmosvalpub, Got %s", hrp) - } - + bz, err := getFromBech32Cosmos(pubkey, Bech32PrefixValPub) if err != nil { return nil, err } @@ -115,3 +97,19 @@ func GetValPubKeyBech32Cosmos(pubkey string) (pk crypto.PubKey, err error) { return pk, nil } + +func getFromBech32Cosmos(bech32, prefix string) ([]byte, error) { + if len(bech32) == 0 { + return nil, errors.New("must provide non-empty string") + } + hrp, bz, err := bech32cosmos.DecodeAndConvert(bech32) + if err != nil { + return nil, err + } + + if hrp != prefix { + return nil, fmt.Errorf("Invalid bech32 prefix. Expected %s, Got %s", prefix, hrp) + } + + return bz, nil +} diff --git a/types/stake.go b/types/stake.go index c640e98e7..6a1a3a95f 100644 --- a/types/stake.go +++ b/types/stake.go @@ -19,7 +19,7 @@ const ( func BondStatusToString(b BondStatus) string { switch b { case 0x00: - return "Ubbonded" + return "Unbonded" case 0x01: return "Unbonding" case 0x02: