Skip to content

Commit b9b1324

Browse files
mergify[bot]rootulpsontrinh16
authored
fix(crypto): error if incorrect ledger public key (backport cosmos#19691) (cosmos#19746)
Co-authored-by: Rootul P <rootulp@gmail.com> Co-authored-by: sontrinh16 <trinhleson2000@gmail.com>
1 parent 6b11b65 commit b9b1324

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4040
### Bug Fixes
4141

4242
* (x/gov) [#19725](https://github.com/cosmos/cosmos-sdk/pull/19725) Fetch a failed proposal tally from proposal.FinalTallyResult in the gprc query.
43+
* (crypto) [#19691](https://github.com/cosmos/cosmos-sdk/pull/19746) Fix tx sign doesn't throw an error when incorrect Ledger is used.
4344

4445
## [v0.47.10](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.47.10) - 2024-02-27
4546

crypto/keyring/keyring.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,14 @@ func SignWithLedger(k *Record, msg []byte) (sig []byte, pub types.PubKey, err er
641641
if err != nil {
642642
return nil, nil, err
643643
}
644+
ledgerPubKey := priv.PubKey()
645+
pubKey, err := k.GetPubKey()
646+
if err != nil {
647+
return nil, nil, err
648+
}
649+
if !pubKey.Equals(ledgerPubKey) {
650+
return nil, nil, fmt.Errorf("the public key that the user attempted to sign with does not match the public key on the ledger device. %v does not match %v", pubKey.String(), ledgerPubKey.String())
651+
}
644652

645653
if !priv.PubKey().VerifySignature(msg, sig) {
646654
return nil, nil, errors.New("Ledger generated an invalid signature. Perhaps you have multiple ledgers and need to try another one")

crypto/keyring/keyring_ledger_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213

1314
"github.com/cosmos/cosmos-sdk/crypto/hd"
15+
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
16+
"github.com/cosmos/cosmos-sdk/crypto/ledger"
17+
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
1418
"github.com/cosmos/cosmos-sdk/types"
1519
)
1620

@@ -133,3 +137,65 @@ func TestAltKeyring_SaveLedgerKey(t *testing.T) {
133137
path := ledgerInfo.GetPath()
134138
require.Equal(t, "m/44'/118'/3'/0/1", path.String())
135139
}
140+
141+
func TestSignWithLedger(t *testing.T) {
142+
// Create two distinct Ledger records: recordA and recordB.
143+
// RecordA is added to the Ledger but recordB is not added.
144+
pathA := hd.NewFundraiserParams(0, types.CoinType, 0)
145+
privA, _, err := ledger.NewPrivKeySecp256k1(*pathA, "cosmos")
146+
require.NoError(t, err)
147+
recordA, err := NewLedgerRecord("ledgerA", privA.PubKey(), pathA)
148+
require.NoError(t, err)
149+
pubA, err := recordA.GetPubKey()
150+
require.NoError(t, err)
151+
152+
pathB := hd.NewFundraiserParams(0, types.CoinType, 1)
153+
// privB won't be added to the Ledger because it doesn't use ledger.NewPrivKeySecp256k1
154+
privB := secp256k1.GenPrivKey()
155+
recordB, err := NewLedgerRecord("ledgerB", privB.PubKey(), pathB)
156+
require.NoError(t, err)
157+
pubB, err := recordB.GetPubKey()
158+
require.NoError(t, err)
159+
160+
require.NotEqual(t, pubA, pubB)
161+
type testCase struct {
162+
name string
163+
record *Record
164+
msg []byte
165+
wantSig []byte
166+
wantPub cryptotypes.PubKey
167+
wantErr bool
168+
wantErrContains string
169+
}
170+
testCases := []testCase{
171+
{
172+
name: "ordinary ledger tx",
173+
record: recordA,
174+
msg: []byte("msg"),
175+
wantSig: []byte{0xfb, 0x93, 0x1b, 0xb9, 0x75, 0x25, 0xe7, 0x99, 0x64, 0xc2, 0x78, 0xf7, 0x94, 0x9a, 0x63, 0x83, 0xe2, 0x59, 0x76, 0x48, 0x1d, 0x2, 0xbc, 0xc2, 0x83, 0x21, 0x24, 0x4b, 0x95, 0x99, 0x25, 0x8b, 0x30, 0x38, 0x6, 0x61, 0x79, 0x9a, 0x9e, 0x8, 0x98, 0xfd, 0x34, 0xc6, 0x7e, 0x47, 0x4d, 0x5f, 0xe, 0xf3, 0xc3, 0xe7, 0xdd, 0xe3, 0x89, 0x80, 0xda, 0x8b, 0x48, 0x15, 0x34, 0xce, 0xdf, 0x1c},
176+
wantPub: pubA,
177+
wantErr: false,
178+
},
179+
{
180+
name: "want error when the public key the user attempted to sign with doesn't match the public key on the ledger",
181+
record: recordB,
182+
msg: []byte("msg"),
183+
wantSig: []byte(nil),
184+
wantPub: nil,
185+
wantErr: true,
186+
wantErrContains: "the public key that the user attempted to sign with does not match the public key on the ledger device",
187+
},
188+
}
189+
190+
for _, tc := range testCases {
191+
t.Run(tc.name, func(t *testing.T) {
192+
sig, pub, err := SignWithLedger(tc.record, tc.msg)
193+
assert.Equal(t, tc.wantSig, sig)
194+
assert.Equal(t, tc.wantPub, pub)
195+
if tc.wantErr {
196+
assert.Error(t, err)
197+
assert.Contains(t, err.Error(), tc.wantErrContains)
198+
}
199+
})
200+
}
201+
}

0 commit comments

Comments
 (0)