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

crypto/keys/mintkey: fix errors handling in UnarmorPubKeyBytes #5823

Merged
merged 1 commit into from
Mar 18, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ resulted in a panic when the tx execution mode was `CheckTx`.
* (x/distribution) [\#5620](https://github.com/cosmos/cosmos-sdk/pull/5620) Fix nil pointer deref in distribution tax/rewward validation helpers.
* (types) [\#5741](https://github.com/cosmos/cosmos-sdk/issues/5741) Prevent ChainAnteDecorators() from panicking when empty AnteDecorator slice is supplied.
* (modules) [\#5569](https://github.com/cosmos/cosmos-sdk/issues/5569) `InitGenesis`, for the relevant modules, now ensures module accounts exist.
* (crypto/keys/mintkey) [\#5823](https://github.com/cosmos/cosmos-sdk/pull/5823) fix errors handling in UnarmorPubKeyBytes (underlying armoring function's
return error was not being checked).

### State Machine Breaking

Expand Down
6 changes: 6 additions & 0 deletions crypto/keys/mintkey/mintkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func UnarmorInfoBytes(armorStr string) ([]byte, error) {
// UnarmorPubKeyBytes returns the pubkey byte slice, a string of the algo type, and an error
func UnarmorPubKeyBytes(armorStr string) (bz []byte, algo string, err error) {
bz, header, err := unarmorBytes(armorStr, blockTypePubKey)
if err != nil {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
return nil, "", fmt.Errorf("couldn't unarmor bytes: %v", err)
}

switch header[headerVersion] {
case "0.0.0":
return bz, defaultAlgo, err
Expand All @@ -91,6 +95,8 @@ func UnarmorPubKeyBytes(armorStr string) (bz []byte, algo string, err error) {
header[headerType] = defaultAlgo
}
return bz, header[headerType], err
case "":
return nil, "", fmt.Errorf("header's version field is empty")
default:
err = fmt.Errorf("unrecognized version: %v", header[headerVersion])
return nil, "", err
Expand Down
19 changes: 18 additions & 1 deletion crypto/keys/mintkey/mintkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func TestArmorUnarmorPubKey(t *testing.T) {
require.Equal(t, "unknown", algo)
require.True(t, pub.Equals(info.GetPubKey()))

armored, err = cstore.ExportPrivKey("Bob", "passphrase", "alessio")
require.NoError(t, err)
_, _, err = mintkey.UnarmorPubKeyBytes(armored)
require.Equal(t, `couldn't unarmor bytes: unrecognized armor type "TENDERMINT PRIVATE KEY", expected: "TENDERMINT PUBLIC KEY"`, err.Error())

// armor pubkey manually
header := map[string]string{
"version": "0.0.0",
Expand All @@ -108,7 +113,19 @@ func TestArmorUnarmorPubKey(t *testing.T) {
require.Nil(t, bz)
require.Empty(t, algo)
require.Error(t, err)
require.Contains(t, err.Error(), "unrecognized version")
require.Equal(t, "header's version field is empty", err.Error())

// unknown version header
header = map[string]string{
"type": "unknown",
"version": "unknown",
}
armored = armor.EncodeArmor("TENDERMINT PUBLIC KEY", header, pubBytes)
bz, algo, err = mintkey.UnarmorPubKeyBytes(armored)
require.Nil(t, bz)
require.Empty(t, algo)
require.Error(t, err)
require.Equal(t, "unrecognized version: unknown", err.Error())
}

func TestArmorInfoBytes(t *testing.T) {
Expand Down