Skip to content

Commit

Permalink
Improve error messages for APDU_CODE_BAD_KEY_HANDLE
Browse files Browse the repository at this point in the history
  • Loading branch information
jleni committed Mar 21, 2019
1 parent 6164d82 commit 6fa62e1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions user_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,19 @@ func (ledger *LedgerCosmos) sign(instruction byte, bip32Path []uint32, transacti

response, err := ledger.api.Exchange(message)
if err != nil {
if err.Error() == "[APDU_CODE_BAD_KEY_HANDLE] The parameters in the data field are incorrect" {
// In this special case, we can extract additional info
errorMsg := string(response)
switch errorMsg {
case "ERROR: JSMN_ERROR_NOMEM":
return nil, fmt.Errorf("Not enough tokens were provided");
case "PARSER ERROR: JSMN_ERROR_INVAL":
return nil, fmt.Errorf("Unexpected character in JSON string");
case "PARSER ERROR: JSMN_ERROR_PART":
return nil, fmt.Errorf("The JSON string is not a complete.");
}
return nil, fmt.Errorf(errorMsg)
}
return nil, err
}

Expand Down
19 changes: 19 additions & 0 deletions user_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,22 @@ func Test_UserSign(t *testing.T) {
return
}
}

func Test_UserSign_Fails(t *testing.T) {
userApp, err := FindLedgerCosmosUserApp()
if err != nil {
t.Fatalf(err.Error())
}
defer userApp.Close()

userApp.api.Logging = true

path := []uint32{44, 118, 0, 0, 5}

message := getDummyTx()
garbage := []byte{65}
message = append(garbage, message...)

_, err = userApp.SignSECP256K1(path, message)
assert.EqualError(t, err, "Unexpected character in JSON string")
}

0 comments on commit 6fa62e1

Please sign in to comment.