From ba170b71c18526aa13aaa6265f75de6672ebb10a Mon Sep 17 00:00:00 2001 From: coderofstuff <114628839+coderofstuff@users.noreply.github.com> Date: Fri, 15 Sep 2023 22:05:49 -0600 Subject: [PATCH] Add more error types for message --- doc/COMMANDS.md | 8 +++- src/handler/sign_msg.c | 16 ++++--- src/sw.h | 16 ++++--- .../kaspa_command_sender.py | 44 +++++++++++-------- 4 files changed, 53 insertions(+), 31 deletions(-) diff --git a/doc/COMMANDS.md b/doc/COMMANDS.md index f15e093..0aa991c 100644 --- a/doc/COMMANDS.md +++ b/doc/COMMANDS.md @@ -173,5 +173,11 @@ Transactions signed with ECDSA are currently not supported. | 0xB00A | `SW_WRONG_BIP32_COIN_TYPE` | `Coin Type` must be `111111'` | | 0xB00B | `SW_WRONG_BIP32_TYPE` | `Type` passed is not valid. Must be either `0` for `Receive` or `1` for `Change`| | 0xB00C | `SW_WRONG_BIP32_PATH_LEN` | Path length must be `5` | -| 0xB00D | `SW_MESSAGE_TOO_LONG` | Message len greater than max | +| 0xB010 | `SW_MESSAGE_PARSING_FAIL` | Unable to parse message data | +| 0xB011 | `SW_MESSAGE_TOO_LONG` | Message len greater than max | +| 0xB012 | `SW_MESSAGE_TOO_SHORT` | Message len is 0 | +| 0xB013 | `SW_MESSAGE_ADDRESS_TYPE_FAIL` | Address type could not be parsed or is not `0`/`1` | +| 0xB014 | `SW_MESSAGE_ADDRESS_INDEX_FAIL` | Address index could not be parsed | +| 0xB015 | `SW_MESSAGE_LEN_PARSING_FAIL` | Message length could not be parsed | +| 0xB016 | `SW_MESSAGE_UNEXPECTED` | Unexpected error while parsing message | | 0x9000 | `OK` | Success | diff --git a/src/handler/sign_msg.c b/src/handler/sign_msg.c index 880fdc9..5005842 100644 --- a/src/handler/sign_msg.c +++ b/src/handler/sign_msg.c @@ -50,20 +50,24 @@ int handler_sign_msg(buffer_t *cdata) { G_context.state = STATE_NONE; if (!buffer_read_u8(cdata, &G_context.msg_info.address_type)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_ADDRESS_TYPE_FAIL); + } + + if (G_context.msg_info.address_type != 0 && G_context.msg_info.address_type != 1) { + return io_send_sw(SW_MESSAGE_ADDRESS_TYPE_FAIL); } if (!buffer_read_u32(cdata, &G_context.msg_info.address_index, BE)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_ADDRESS_TYPE_FAIL); } uint8_t message_len = 0; if (!buffer_read_u8(cdata, &message_len)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_LEN_PARSING_FAIL); } if (message_len == 0) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_TOO_SHORT); } if (message_len > MAX_MESSAGE_LEN) { @@ -73,13 +77,13 @@ int handler_sign_msg(buffer_t *cdata) { G_context.msg_info.message_len = (size_t) message_len; if (!buffer_can_read(cdata, G_context.msg_info.message_len)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_PARSING_FAIL); } memcpy(G_context.msg_info.message, cdata->ptr + cdata->offset, G_context.msg_info.message_len); if (!buffer_seek_cur(cdata, G_context.msg_info.message_len)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_UNEXPECTED); } G_context.bip32_path[0] = 0x8000002C; diff --git a/src/sw.h b/src/sw.h index 2f81e8a..a60be47 100644 --- a/src/sw.h +++ b/src/sw.h @@ -84,8 +84,14 @@ */ #define SW_SIGNATURE_FAIL 0xB008 -#define SW_WRONG_BIP32_PURPOSE 0xB009 -#define SW_WRONG_BIP32_COIN_TYPE 0xB00A -#define SW_WRONG_BIP32_TYPE 0xB00B -#define SW_WRONG_BIP32_PATH_LEN 0xB00C -#define SW_MESSAGE_TOO_LONG 0xB00D +#define SW_WRONG_BIP32_PURPOSE 0xB009 +#define SW_WRONG_BIP32_COIN_TYPE 0xB00A +#define SW_WRONG_BIP32_TYPE 0xB00B +#define SW_WRONG_BIP32_PATH_LEN 0xB00C +#define SW_MESSAGE_PARSING_FAIL 0xB010 +#define SW_MESSAGE_TOO_LONG 0xB011 +#define SW_MESSAGE_TOO_SHORT 0xB012 +#define SW_MESSAGE_ADDRESS_TYPE_FAIL 0xB013 +#define SW_MESSAGE_ADDRESS_INDEX_FAIL 0xB014 +#define SW_MESSAGE_LEN_PARSING_FAIL 0xB015 +#define SW_MESSAGE_UNEXPECTED 0xB016 diff --git a/tests/application_client/kaspa_command_sender.py b/tests/application_client/kaspa_command_sender.py index 74b4530..e5af126 100644 --- a/tests/application_client/kaspa_command_sender.py +++ b/tests/application_client/kaspa_command_sender.py @@ -38,25 +38,31 @@ class InsType(IntEnum): SIGN_MESSAGE = 0x07 class Errors(IntEnum): - SW_DENY = 0x6985 - SW_WRONG_P1P2 = 0x6A86 - SW_WRONG_DATA_LENGTH = 0x6A87 - SW_INS_NOT_SUPPORTED = 0x6D00 - SW_CLA_NOT_SUPPORTED = 0x6E00 - SW_WRONG_RESPONSE_LENGTH = 0xB000 - SW_DISPLAY_BIP32_PATH_FAIL = 0xB001 - SW_DISPLAY_ADDRESS_FAIL = 0xB002 - SW_DISPLAY_AMOUNT_FAIL = 0xB003 - SW_WRONG_TX_LENGTH = 0xB004 - SW_TX_PARSING_FAIL = 0xB005 - SW_TX_HASH_FAIL = 0xB006 - SW_BAD_STATE = 0xB007 - SW_SIGNATURE_FAIL = 0xB008 - SW_WRONG_BIP32_PURPOSE = 0xB009 - SW_WRONG_BIP32_COIN_TYPE = 0xB00A - SW_WRONG_BIP32_TYPE = 0xB00B - SW_WRONG_BIP32_PATH_LEN = 0xB00C - SW_MESSAGE_TOO_LONG = 0xB00D + SW_DENY = 0x6985 + SW_WRONG_P1P2 = 0x6A86 + SW_WRONG_DATA_LENGTH = 0x6A87 + SW_INS_NOT_SUPPORTED = 0x6D00 + SW_CLA_NOT_SUPPORTED = 0x6E00 + SW_WRONG_RESPONSE_LENGTH = 0xB000 + SW_DISPLAY_BIP32_PATH_FAIL = 0xB001 + SW_DISPLAY_ADDRESS_FAIL = 0xB002 + SW_DISPLAY_AMOUNT_FAIL = 0xB003 + SW_WRONG_TX_LENGTH = 0xB004 + SW_TX_PARSING_FAIL = 0xB005 + SW_TX_HASH_FAIL = 0xB006 + SW_BAD_STATE = 0xB007 + SW_SIGNATURE_FAIL = 0xB008 + SW_WRONG_BIP32_PURPOSE = 0xB009 + SW_WRONG_BIP32_COIN_TYPE = 0xB00A + SW_WRONG_BIP32_TYPE = 0xB00B + SW_WRONG_BIP32_PATH_LEN = 0xB00C + SW_MESSAGE_PARSING_FAIL = 0xB010 + SW_MESSAGE_TOO_LONG = 0xB011 + SW_MESSAGE_TOO_SHORT = 0xB012 + SW_MESSAGE_ADDRESS_TYPE_FAIL = 0xB013 + SW_MESSAGE_ADDRESS_INDEX_FAIL = 0xB014 + SW_MESSAGE_LEN_PARSING_FAIL = 0xB015 + SW_MESSAGE_UNEXPECTED = 0xB016 def split_message(message: bytes, max_size: int) -> List[bytes]: return [message[x:x + max_size] for x in range(0, len(message), max_size)]