Skip to content

Commit

Permalink
Merge pull request #26 from ZondaX/jleni/errMsg
Browse files Browse the repository at this point in the history
Improve error messages in case of APDU_CODE_BAD_KEY_HANDLE
  • Loading branch information
jleni authored Mar 21, 2019
2 parents e2f595b + 6fa62e1 commit af771e4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[[constraint]]
name = "github.com/zondax/ledger-go"
version = "v0.8.0"
version = "v0.9.0"

[prune]
go-tests = true
Expand Down
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 af771e4

Please sign in to comment.