Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get printable amount #2

Open
wants to merge 1 commit into
base: swap/add_check_address
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions src/common/parse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*******************************************************************************
* Ledger App Aptos.
* (c) 2025 Ledger
*
* 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 <string.h>
#include "parse.h"

#define MAX_AMOUNT_STR_LEN 21 // 19 for u64 + 1 for '\0' +1 for '.'

bool adjustDecimals(const char *src,
uint32_t srcLength,
char *target,
uint32_t targetLength,
uint8_t decimals) {
uint32_t startOffset;
uint32_t lastZeroOffset = 0;
uint32_t offset = 0;

if (srcLength <= decimals) {
uint32_t delta = decimals - srcLength;
if (targetLength < srcLength + 1 + 2 + delta) {
return false;
}
target[offset++] = '0';
target[offset++] = '.';
for (uint32_t i = 0; i < delta; i++) {
target[offset++] = '0';
}
startOffset = offset;
for (uint32_t i = 0; i < srcLength; i++) {
target[offset++] = src[i];
}
target[offset] = '\0';
} else {
uint32_t sourceOffset = 0;
uint32_t delta = srcLength - decimals;
if (targetLength < srcLength + 1 + 1) {
return false;
}
while (offset < delta) {
target[offset++] = src[sourceOffset++];
}
if (decimals != 0) {
target[offset++] = '.';
}
startOffset = offset;
while (sourceOffset < srcLength) {
target[offset++] = src[sourceOffset++];
}
target[offset] = '\0';
}
for (uint32_t i = startOffset; i < offset; i++) {
if (target[i] == '0') {
if (lastZeroOffset == 0) {
lastZeroOffset = i;
}
} else {
lastZeroOffset = 0;
}
}
if (lastZeroOffset != 0) {
target[lastZeroOffset] = '\0';
if (target[lastZeroOffset - 1] == '.') {
target[lastZeroOffset - 1] = '\0';
}
}
return true;
}
unsigned short print_amount(uint64_t amount, char *out, uint32_t outlen, uint8_t decimals) {

if (amount == 0) {
if (outlen < 2) {
return 0;
}
out[0] = '0';
out[1] = '\0';

return strlen(out);
}

char tmp[MAX_AMOUNT_STR_LEN];
char tmp2[MAX_AMOUNT_STR_LEN];
uint32_t numDigits = 0, i;
uint64_t base = 1;

while (base <= amount) {
base *= 10;
numDigits++;
}
if (numDigits > sizeof(tmp) - 2) {
return 0;
}
base /= 10;
for (i = 0; i < numDigits; i++) {
tmp[i] = '0' + ((amount / base) % 10);
base /= 10;
}
tmp[i] = '\0';
out[0] = '\0';
if ( adjustDecimals(tmp, i, tmp2, MAX_AMOUNT_STR_LEN, decimals) ) {
if (strlen(tmp2) < outlen - 1) {
strlcpy(out, tmp2, outlen);
}
}
return strlen(out);
}
9 changes: 9 additions & 0 deletions src/common/parse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "os.h"


#ifndef PARSE_H
#define PARSE_H

unsigned short print_amount(uint64_t amount, char *out, uint32_t outlen, uint8_t sun);

#endif
6 changes: 6 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@
* Signature length (bytes).
*/
#define SIGNATURE_LEN 64

/**
* Aptos decimal precision.
*/
#define APT_DECIMAL_PRECISION 8

49 changes: 46 additions & 3 deletions src/swap/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
#ifdef HAVE_SWAP

#include <string.h> // memset, explicit_bzero
#include "handle_swap_sign_transaction.h"
#include "swap.h"
#include "os.h"
#include "constants.h"

#define MAX_TICKER_LEN 16

/* Set empty printable_amount on error, printable amount otherwise */
void swap_handle_get_printable_amount(get_printable_amount_parameters_t* params) {
PRINTF("TODO: swap_handle_get_printable_amount\n");

uint64_t amount;
uint8_t decimals;
char ticker[MAX_TICKER_LEN] = {0};

PRINTF("Inside Aptos swap_handle_get_printable_amount \n");

// If the amount is a fee, its value is nominated in APT
// If there is no coin_configuration, consider that we are doing a APT swap
if (params->is_fee || params->coin_configuration == NULL) {
memcpy(ticker, "APT", sizeof("APT"));
decimals = APT_DECIMAL_PRECISION;
} else {
if (!swap_parse_config(params->coin_configuration,
params->coin_configuration_length,
ticker,
sizeof(ticker),
&decimals)) {
PRINTF("Fail to parse coin_configuration\n");
goto error;
}
}

if (!swap_str_to_u64(params->amount, params->amount_length, &amount)) {
PRINTF("Amount is too big\n");
goto error;
}

if (print_amount(amount,
params->printable_amount,
sizeof(params->printable_amount),
decimals) == 0) {
PRINTF("print_amount failed\n");
goto error;
}

strlcat(params->printable_amount, " ", sizeof(params->printable_amount));
strlcat(params->printable_amount, ticker, sizeof(params->printable_amount));

PRINTF("Amount %s\n", params->printable_amount);
return;

error:
memset(params->printable_amount, '\0', sizeof(params->printable_amount));
}

#endif
Loading