Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Redundant Bech32 PubKey Functions #5495

Merged
merged 7 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ logic has been implemented for v0.38 target version. Applications can migrate vi

### API Breaking Changes

* (types) [\#5495](https://github.com/cosmos/cosmos-sdk/pull/5495) Remove redundant `(Must)Bech32ify*` and `(Must)Get*KeyBech32`
functions in favor of `(Must)Bech32ifyPubKey` and `(Must)GetPubKeyFromBech32` respectively, both of
which take a `Bech32PubKeyType` (string).
* (types) [\#5430](https://github.com/cosmos/cosmos-sdk/pull/5430) `DecCoins#Add` parameter changed from `DecCoins`
to `...DecCoin`, `Coins#Add` parameter changed from `Coins` to `...Coin`
* (baseapp/types) [\#5421](https://github.com/cosmos/cosmos-sdk/pull/5421) The `Error` interface (`types/errors.go`)
Expand Down
91 changes: 50 additions & 41 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,46 @@ func Cmd(cdc *codec.Codec) *cobra.Command {
return cmd
}

// getPubKeyFromString returns a Tendermint PubKey (PubKeyEd25519) by attempting
// to decode the pubkey string from hex, base64, and finally bech32. If all
// encodings fail, an error is returned.
func getPubKeyFromString(pkstr string) (crypto.PubKey, error) {
var pubKey ed25519.PubKeyEd25519

bz, err := hex.DecodeString(pkstr)
if err == nil {
copy(pubKey[:], bz)
return pubKey, nil
}

bz, err = base64.StdEncoding.DecodeString(pkstr)
if err == nil {
copy(pubKey[:], bz)
return pubKey, nil
}

pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, pkstr)
if err == nil {
return pk, nil
}

pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeValPub, pkstr)
if err == nil {
return pk, nil
}

pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkstr)
if err == nil {
return pk, nil
}

return nil, fmt.Errorf("pubkey '%s' invalid; expected hex, base64, or bech32", pubKey)
}

func PubkeyCmd(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "pubkey [pubkey]",
Short: "Decode a pubkey from hex, base64, or bech32",
Short: "Decode a ED25519 pubkey from hex, base64, or bech32",
Long: fmt.Sprintf(`Decode a pubkey from hex, base64, or bech32.

Example:
Expand All @@ -44,67 +80,40 @@ $ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
`, version.ClientName, version.ClientName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

pubkeyString := args[0]
var pubKeyI crypto.PubKey

// try hex, then base64, then bech32
pubkeyBytes, err := hex.DecodeString(pubkeyString)
pk, err := getPubKeyFromString(args[0])
if err != nil {
var err2 error
pubkeyBytes, err2 = base64.StdEncoding.DecodeString(pubkeyString)
if err2 != nil {
var err3 error
pubKeyI, err3 = sdk.GetAccPubKeyBech32(pubkeyString)
if err3 != nil {
var err4 error
pubKeyI, err4 = sdk.GetValPubKeyBech32(pubkeyString)

if err4 != nil {
var err5 error
pubKeyI, err5 = sdk.GetConsPubKeyBech32(pubkeyString)
if err5 != nil {
return fmt.Errorf("expected hex, base64, or bech32. Got errors: hex: %v, base64: %v, bech32 Acc: %v, bech32 Val: %v, bech32 Cons: %v",
err, err2, err3, err4, err5)
}

}
}

}
return err
}

var pubKey ed25519.PubKeyEd25519
if pubKeyI == nil {
copy(pubKey[:], pubkeyBytes)
} else {
pubKey = pubKeyI.(ed25519.PubKeyEd25519)
pubkeyBytes = pubKey[:]
edPK, ok := pk.(ed25519.PubKeyEd25519)
if !ok {
return fmt.Errorf("invalid pubkey type; expected ED25519")
}

pubKeyJSONBytes, err := cdc.MarshalJSON(pubKey)
pubKeyJSONBytes, err := cdc.MarshalJSON(edPK)
if err != nil {
return err
}
accPub, err := sdk.Bech32ifyAccPub(pubKey)
accPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, edPK)
if err != nil {
return err
}
valPub, err := sdk.Bech32ifyValPub(pubKey)
valPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, edPK)
if err != nil {
return err
}

consenusPub, err := sdk.Bech32ifyConsPub(pubKey)
consenusPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, edPK)
if err != nil {
return err
}
cmd.Println("Address:", pubKey.Address())
cmd.Printf("Hex: %X\n", pubkeyBytes)

cmd.Println("Address:", edPK.Address())
cmd.Printf("Hex: %X\n", edPK[:])
cmd.Println("JSON (base64):", string(pubKeyJSONBytes))
cmd.Println("Bech32 Acc:", accPub)
cmd.Println("Bech32 Validator Operator:", valPub)
cmd.Println("Bech32 Validator Consensus:", consenusPub)

return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keys.Keybase, inBuf *bufio.
}

if viper.GetString(FlagPublicKey) != "" {
pk, err := sdk.GetAccPubKeyBech32(viper.GetString(FlagPublicKey))
pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, viper.GetString(FlagPublicKey))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
require.Equal(t, keys.TypeLedger, key1.GetType())
require.Equal(t,
"terrapub1addwnpepqvpg7r26nl2pvqqern00m6s9uaax3hauu2rzg8qpjzq9hy6xve7sw0d84m6",
sdk.MustBech32ifyAccPub(key1.GetPubKey()))
sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, key1.GetPubKey()))

config.SetCoinType(118)
config.SetFullFundraiserPath("44'/118'/0'/0/0")
Expand Down Expand Up @@ -116,5 +116,5 @@ func Test_runAddCmdLedger(t *testing.T) {
require.Equal(t, keys.TypeLedger, key1.GetType())
require.Equal(t,
"cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0",
sdk.MustBech32ifyAccPub(key1.GetPubKey()))
sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, key1.GetPubKey()))
}
2 changes: 1 addition & 1 deletion client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (rvo ResultValidatorsOutput) String() string {
}

func bech32ValidatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error) {
bechValPubkey, err := sdk.Bech32ifyConsPub(validator.PubKey)
bechValPubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, validator.PubKey)
if err != nil {
return ValidatorOutput{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/keys/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestCreateLedger(t *testing.T) {

// The mock is available, check that the address is correct
pubKey := ledger.GetPubKey()
pk, err := sdk.Bech32ifyAccPub(pubKey)
pk, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey)
assert.NoError(t, err)
assert.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk)

Expand All @@ -80,7 +80,7 @@ func TestCreateLedger(t *testing.T) {
assert.Equal(t, "some_account", restoredKey.GetName())
assert.Equal(t, TypeLedger, restoredKey.GetType())
pubKey = restoredKey.GetPubKey()
pk, err = sdk.Bech32ifyAccPub(pubKey)
pk, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey)
assert.NoError(t, err)
assert.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk)

Expand Down
8 changes: 4 additions & 4 deletions crypto/keys/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Bech32KeysOutput(infos []Info) ([]KeyOutput, error) {
func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) {
consAddr := sdk.ConsAddress(keyInfo.GetPubKey().Address().Bytes())

bechPubKey, err := sdk.Bech32ifyConsPub(keyInfo.GetPubKey())
bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, keyInfo.GetPubKey())
if err != nil {
return KeyOutput{}, err
}
Expand All @@ -64,7 +64,7 @@ func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) {
func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) {
valAddr := sdk.ValAddress(keyInfo.GetPubKey().Address().Bytes())

bechPubKey, err := sdk.Bech32ifyValPub(keyInfo.GetPubKey())
bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, keyInfo.GetPubKey())
if err != nil {
return KeyOutput{}, err
}
Expand All @@ -77,7 +77,7 @@ func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) {
// public keys will be added.
func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) {
accAddr := sdk.AccAddress(keyInfo.GetPubKey().Address().Bytes())
bechPubKey, err := sdk.Bech32ifyAccPub(keyInfo.GetPubKey())
bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, keyInfo.GetPubKey())
if err != nil {
return KeyOutput{}, err
}
Expand All @@ -90,7 +90,7 @@ func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) {
for i, pk := range mInfo.PubKeys {
accAddr := sdk.AccAddress(pk.PubKey.Address().Bytes())

bechPubKey, err := sdk.Bech32ifyAccPub(pk.PubKey)
bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk.PubKey)
if err != nil {
return KeyOutput{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/keys/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (

func TestBech32KeysOutput(t *testing.T) {
tmpKey := secp256k1.GenPrivKey().PubKey()
bechTmpKey := sdk.MustBech32ifyAccPub(tmpKey)
bechTmpKey := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, tmpKey)
tmpAddr := sdk.AccAddress(tmpKey.Address().Bytes())

multisigPks := multisig.NewPubKeyMultisigThreshold(1, []crypto.PubKey{tmpKey})
multiInfo := NewMultiInfo("multisig", multisigPks)
accAddr := sdk.AccAddress(multiInfo.GetPubKey().Address().Bytes())
bechPubKey := sdk.MustBech32ifyAccPub(multiInfo.GetPubKey())
bechPubKey := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, multiInfo.GetPubKey())

expectedOutput := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType().String(), accAddr.String(), bechPubKey)
expectedOutput.Threshold = 1
Expand Down
3 changes: 2 additions & 1 deletion crypto/keys/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_writeReadLedgerInfo(t *testing.T) {
Expand All @@ -24,7 +25,7 @@ func Test_writeReadLedgerInfo(t *testing.T) {
assert.Equal(t, "44'/118'/5'/0/1", path.String())
assert.Equal(t,
"cosmospub1addwnpepqddddqg2glc8x4fl7vxjlnr7p5a3czm5kcdp4239sg6yqdc4rc2r5wmxv8p",
types.MustBech32ifyAccPub(lInfo.GetPubKey()))
types.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, lInfo.GetPubKey()))

// Serialize and restore
serialized := marshalInfo(lInfo)
Expand Down
8 changes: 4 additions & 4 deletions crypto/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestPublicKeyUnsafe(t *testing.T) {
fmt.Sprintf("%x", priv.PubKey().Bytes()),
"Is your device using test mnemonic: %s ?", tests.TestMnemonic)

pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey())
pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey())
require.NoError(t, err)
require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0",
pubKeyAddr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic)
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestPublicKeyUnsafeHDPath(t *testing.T) {
tmp := priv.(PrivKeyLedgerSecp256k1)
(&tmp).AssertIsPrivKeyInner()

pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey())
pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey())
require.NoError(t, err)
require.Equal(t,
expectedAnswers[i], pubKeyAddr,
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestPublicKeySafe(t *testing.T) {
fmt.Sprintf("%x", priv.PubKey().Bytes()),
"Is your device using test mnemonic: %s ?", tests.TestMnemonic)

pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey())
pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey())
require.NoError(t, err)
require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0",
pubKeyAddr, "Is your device using test mnemonic: %s ?", tests.TestMnemonic)
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestPublicKeyHDPath(t *testing.T) {
tmp := priv.(PrivKeyLedgerSecp256k1)
(&tmp).AssertIsPrivKeyInner()

pubKeyAddr, err := sdk.Bech32ifyAccPub(priv.PubKey())
pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey())
require.NoError(t, err)
require.Equal(t,
expectedPubKeys[i], pubKeyAddr,
Expand Down
2 changes: 1 addition & 1 deletion server/tm_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ShowValidatorCmd(ctx *Context) *cobra.Command {
return printlnJSON(valPubKey)
}

pubkey, err := sdk.Bech32ifyConsPub(valPubKey)
pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey)
if err != nil {
return err
}
Expand Down
Loading