Skip to content

Commit

Permalink
Replaced address truncation with even segmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrik Sletmo authored and TamtamHero committed Apr 29, 2020
1 parent 71eb5dc commit 142daf3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/limitations.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
#define MAX_FIELD_COUNT 60
#define MAX_FIELD_LEN 1024
#define MAX_RAW_TX 10000
#define FULL_ADDR_FORMAT true
#define DISPLAY_SEGMENTED_ADDR false

#elif defined(TARGET_NANOS)

#define MAX_FIELD_COUNT 24
#define MAX_FIELD_LEN 128
#define MAX_RAW_TX 800
#define FULL_ADDR_FORMAT false
#define DISPLAY_SEGMENTED_ADDR true

#endif

Expand Down
44 changes: 38 additions & 6 deletions src/xrp/format/general.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "percentage.h"
#include <string.h>

#define PAGE_W 16
#define ADDR_DST_OFFSET (PAGE_W * 3 + 2)

void uint8Formatter(field_t* field, char *dst) {
uint8_t value = readUnsigned8(field->data);
SNPRINTF(dst, "%u", value);
Expand Down Expand Up @@ -84,13 +87,42 @@ void blobFormatter(field_t* field, char *dst) {

void accountFormatter(field_t* field, char *dst) {
if (field->data != NULL) {
xrp_public_key_to_encoded_base58(field->data, field->length, dst, MAX_FIELD_LEN, 0, 1);
// Write full address to dst + ADDR_DST_OFFSET
uint16_t addrLength = xrp_public_key_to_encoded_base58(
field->data, field->length,
dst + ADDR_DST_OFFSET, MAX_FIELD_LEN - ADDR_DST_OFFSET,
0, 1
);

if (DISPLAY_SEGMENTED_ADDR && addrLength <= PAGE_W * 3) {
// If the application is configured to split addresses on the target
// device we segment the address into three parts of roughly the
// same length and display them each in the middle of their page
//
// The segments are positioned so that the longest segment comes first:
// long segment, base segment, base segment

// 1. Calculate how long every segment is
uint16_t baseSegmentLen = addrLength / 3;
uint16_t longSegmentLen = addrLength - 2 * baseSegmentLen;

// 2. Calculate the left padding required to center each segment
uint16_t basePadding = (PAGE_W - baseSegmentLen) / 2;
uint16_t longPadding = (PAGE_W - longSegmentLen) / 2;

// 3. Fill all three pages with spaces and copy the every segment
// to their corresponding position
os_memset(dst, ' ', PAGE_W * 3);
os_memmove(dst + PAGE_W * 0 + longPadding, dst + ADDR_DST_OFFSET, longSegmentLen);
os_memmove(dst + PAGE_W * 1 + basePadding, dst + ADDR_DST_OFFSET + longSegmentLen, baseSegmentLen);
os_memmove(dst + PAGE_W * 2 + basePadding, dst + ADDR_DST_OFFSET + longSegmentLen + baseSegmentLen, baseSegmentLen);

uint16_t addrLength = strlen(dst);
if (!FULL_ADDR_FORMAT && addrLength > 16) {
os_memmove(dst + 7, "..", 2);
os_memmove(dst + 9, dst + addrLength - 7, 7);
dst[16] = 0;
dst[48] = 0;
} else {
// Application is configured with normal address formatting, make sure
// that the address is moved to the beginning of our dst
os_memmove(dst, dst + 50, addrLength);
dst[addrLength] = 0;
}
} else {
strcpy(dst, "[empty]");
Expand Down

0 comments on commit 142daf3

Please sign in to comment.