diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fb83798fc6a..e46eb7bdf95c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Bug Fixes + +* [\#9766](https://github.com/cosmos/cosmos-sdk/pull/9766) Fix hardcoded ledger signing algorithm on `keys add` command. + ### Features * [\#9750](https://github.com/cosmos/cosmos-sdk/pull/9750) Emit events for tx signature and sequence, so clients can now query txs by signature (`tx.signature=''`) or by address and sequence combo (`tx.acc_seq='/'`). diff --git a/client/keys/add.go b/client/keys/add.go index ee006231ace2..22cc8a61f755 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -278,8 +278,9 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf // If we're using ledger, only thing we need is the path and the bech32 prefix. if useLedger { - bech32PrefixAccAddr := ctx.AddressPrefix - k, err := kb.SaveLedgerKey(name, hd.Secp256k1, bech32PrefixAccAddr, coinType, account, index) + bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix() + + info, err := kb.SaveLedgerKey(name, algo, bech32PrefixAccAddr, coinType, account, index) if err != nil { return err } diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index ef3b0a16df08..cb56990564da 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -420,14 +420,17 @@ func (ks keystore) SignByAddress(address, msg []byte, signMode signing.SignMode) func (ks keystore) SaveLedgerKey(uid string, algo SignatureAlgo, hrp string, coinType, account, index uint32) (*Record, error) { if !ks.options.SupportedAlgosLedger.Contains(algo) { - return nil, errorsmod.Wrap(ErrUnsupportedSigningAlgo, fmt.Sprintf("signature algo %s is not defined in the keyring options", algo.Name())) + return nil, fmt.Errorf( + "%w: signature algo %s is not defined in the keyring options", + ErrUnsupportedSigningAlgo, algo.Name(), + ) } hdPath := hd.NewFundraiserParams(account, coinType, index) priv, _, err := ledger.NewPrivKeySecp256k1(*hdPath, hrp) if err != nil { - return nil, errorsmod.Wrap(ErrLedgerGenerateKey, err.Error()) + return nil, fmt.Errorf("failed to generate ledger key: %w", err) } return ks.writeLedgerKey(uid, priv.PubKey(), hdPath) diff --git a/crypto/keyring/keyring_ledger_test.go b/crypto/keyring/keyring_ledger_test.go index 244e877f1747..614017c65101 100644 --- a/crypto/keyring/keyring_ledger_test.go +++ b/crypto/keyring/keyring_ledger_test.go @@ -104,8 +104,9 @@ func TestAltKeyring_SaveLedgerKey(t *testing.T) { require.NoError(t, err) // Test unsupported Algo - _, err = kr.SaveLedgerKey("key", notSupportedAlgo{}, "cosmos", 118, 0, 0) - require.True(t, errors.Is(err, ErrUnsupportedSigningAlgo)) + _, err = keyring.SaveLedgerKey("key", notSupportedAlgo{}, "cosmos", 118, 0, 0) + require.Error(t, err) + require.Contains(t, err.Error(), ErrUnsupportedSigningAlgo.Error()) k, err := kr.SaveLedgerKey("some_account", hd.Secp256k1, "cosmos", 118, 3, 1) if err != nil { diff --git a/crypto/ledger/ledger_secp256k1.go b/crypto/ledger/ledger_secp256k1.go index 67dcc73981ab..b5efe2bf9b5b 100644 --- a/crypto/ledger/ledger_secp256k1.go +++ b/crypto/ledger/ledger_secp256k1.go @@ -341,7 +341,7 @@ func getPubKeyUnsafe(device SECP256K1, path hd.BIP44Params) (types.PubKey, error func getPubKeyAddrSafe(device SECP256K1, path hd.BIP44Params, hrp string) (types.PubKey, string, error) { publicKey, addr, err := device.GetAddressPubKeySECP256K1(path.DerivationPath(), hrp) if err != nil { - return nil, "", fmt.Errorf("%w: address rejected for path %s", err, path) + return nil, "", fmt.Errorf("%w: address rejected for path %s", err, path.String()) } // re-serialize in the 33-byte compressed format