diff --git a/app/src/addr.c b/app/src/addr.c index c23120f..09d7457 100644 --- a/app/src/addr.c +++ b/app/src/addr.c @@ -26,29 +26,10 @@ #include "zxerror.h" #include "zxformat.h" #include "zxmacros.h" +#include "ui_utils.h" bool is_randomized = false; -#define BECH32_PREFIX "penumbra" - -#define FIXED_PREFIX BECH32_PREFIX "1" -#define ADDRESS_NUM_CHARS_SHORT_FORM 24 -#define NUM_CHARS_TO_DISPLAY 33 - -#define HRP_LENGTH (sizeof(FIXED_PREFIX) - 1) -#define RAW_ADDRESS_LENGTH 80 -#define CHECKSUM_LENGTH 8 -#define SEPARATOR_LENGTH 1 - -#define BECH32_BITS_PER_CHAR 5 -#define BITS_PER_BYTE 8 - -#define ENCODED_DATA_LENGTH \ - (((RAW_ADDRESS_LENGTH + CHECKSUM_LENGTH) * BITS_PER_BYTE + BECH32_BITS_PER_CHAR - 1) / BECH32_BITS_PER_CHAR) - -#define ENCODED_ADDR_LEN (HRP_LENGTH + SEPARATOR_LENGTH + ENCODED_DATA_LENGTH) - -#define ENCODED_ADDR_BUFFER_SIZE (ENCODED_ADDR_LEN + 2) zxerr_t addr_getNumItems(uint8_t *num_items) { zemu_log_stack("addr_getNumItems"); @@ -65,16 +46,15 @@ zxerr_t addr_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char * uint8_t *pageCount) { ZEMU_LOGF(50, "[addr_getItem] %d/%d\n", displayIdx, pageIdx) - char encoded_addr[ENCODED_ADDR_BUFFER_SIZE] = {'\0'}; + char encoded_addr[ENCODED_ADDR_BUFFER_SIZE + 1] = {'\0'}; switch (displayIdx) { case 0: snprintf(outKey, outKeyLen, "Address"); - int32_t ret = rs_bech32_encode((const uint8_t *)BECH32_PREFIX, sizeof(BECH32_PREFIX) - 1, G_io_apdu_buffer, - ADDRESS_LEN_BYTES, (uint8_t *)encoded_addr, ENCODED_ADDR_BUFFER_SIZE); - - if (ret < 0) return zxerr_unknown; + if (printAddress(G_io_apdu_buffer, ADDRESS_LEN_BYTES, encoded_addr, ENCODED_ADDR_BUFFER_SIZE) != parser_ok) { + return zxerr_unknown; + } pageString(outVal, outValLen, encoded_addr, pageIdx, pageCount); diff --git a/app/src/keys_def.h b/app/src/keys_def.h index 2ade8f8..7c5bf7d 100644 --- a/app/src/keys_def.h +++ b/app/src/keys_def.h @@ -42,6 +42,26 @@ extern "C" { #define ADDR_RANDOMIZER_LEN 12 +#define BECH32_PREFIX "penumbra" + +#define FIXED_PREFIX BECH32_PREFIX "1" +#define ADDRESS_NUM_CHARS_SHORT_FORM 24 +#define NUM_CHARS_TO_DISPLAY 33 + +#define HRP_LENGTH (sizeof(FIXED_PREFIX) - 1) +#define CHECKSUM_LENGTH 8 +#define SEPARATOR_LENGTH 1 + +#define BECH32_BITS_PER_CHAR 5 +#define BITS_PER_BYTE 8 + +#define ENCODED_DATA_LENGTH \ + (((ADDRESS_LEN_BYTES + CHECKSUM_LENGTH) * BITS_PER_BYTE + BECH32_BITS_PER_CHAR - 1) / BECH32_BITS_PER_CHAR) + +#define ENCODED_ADDR_LEN (HRP_LENGTH + SEPARATOR_LENGTH + ENCODED_DATA_LENGTH) + +#define ENCODED_ADDR_BUFFER_SIZE (ENCODED_ADDR_LEN + 2) + // raw keys in bytes /** The spending key consists of spend_key_bytes and ask. diff --git a/app/src/ui_utils.c b/app/src/ui_utils.c new file mode 100644 index 0000000..15b2724 --- /dev/null +++ b/app/src/ui_utils.c @@ -0,0 +1,46 @@ +/******************************************************************************* + * (c) 2018 - 2023 Zondax AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ + +#include + +#include "parser_common.h" +#include "keys_def.h" +#include "rslib.h" +#include "zxerror.h" +#include "zxformat.h" +#include "zxmacros.h" + +parser_error_t printAddress(uint8_t *address, uint16_t address_len, char *out, uint16_t out_len) { + if (address_len != ADDRESS_LEN_BYTES) { + return parser_invalid_address; + } + + // check we have space for the null terminator + if (out_len < ENCODED_ADDR_BUFFER_SIZE + 1) { + return parser_display_idx_out_of_range; + } + + MEMZERO(out, out_len); + + int32_t ret = rs_bech32_encode((const uint8_t *)BECH32_PREFIX, sizeof(BECH32_PREFIX) - 1, address, + address_len, (uint8_t *)out, out_len); + + if (ret < 0) { + return parser_unexpected_error; + } + + return parser_ok; +} diff --git a/app/src/ui_utils.h b/app/src/ui_utils.h new file mode 100644 index 0000000..13eb7a6 --- /dev/null +++ b/app/src/ui_utils.h @@ -0,0 +1,19 @@ +/******************************************************************************* + * (c) 2018 - 2023 Zondax AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ +#pragma once + + +parser_error_t printAddress(uint8_t *address, uint16_t address_len, char *out, uint16_t out_len);