Skip to content

Commit

Permalink
Add general method to print addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
neithanmo committed Nov 25, 2024
1 parent e17a549 commit ed74da7
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 25 deletions.
30 changes: 5 additions & 25 deletions app/src/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);

Expand Down
20 changes: 20 additions & 0 deletions app/src/keys_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions app/src/ui_utils.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

#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;
}
19 changes: 19 additions & 0 deletions app/src/ui_utils.h
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit ed74da7

Please sign in to comment.