Skip to content

Commit

Permalink
Add more error types for message
Browse files Browse the repository at this point in the history
  • Loading branch information
coderofstuff committed Sep 16, 2023
1 parent 9d1ae89 commit ba170b7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
8 changes: 7 additions & 1 deletion doc/COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
16 changes: 10 additions & 6 deletions src/handler/sign_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions src/sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 25 additions & 19 deletions tests/application_client/kaspa_command_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit ba170b7

Please sign in to comment.