talerAmount.h (4723B)
1 /** 2 * @file talerAmount.h 3 * @author Adrian STEINER (steia19@bfh.ch) 4 * @author Sree Harsha Totakura <sreeharsha@totakura.in> 5 * @brief Amount representation and utility function to handle the data. 6 * This file consists of parts of the file taler_amount_lib.h 7 * from the git repository https://git.gnunet.org/exchange.git. 8 * This is used to generate the same hash. 9 * The library cannot be used directly due to memory optimizations and MCU 10 * runtime capability. 11 * @version 0.1 12 * @date 19-02-2025 13 * 14 * @copyright (C) 2025 Adrian STEINER 15 * This program is free software: you can redistribute it and/or modify 16 * it under the terms of the GNU General Public License as published by 17 * the Free Software Foundation, either version 3 of the License, or 18 * (at your option) any later version. 19 *pbm_renderImage 20 * This program is distributed in the hope that it will be useful, 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 * GNU General Public License for more details. 24 * 25 * You should have received a copy of the GNU General Public License 26 * along with this program. If not, see <https: //www.gnu.org/licenses/>. 27 * 28 */ 29 #ifndef TALER_AMOUNT_H 30 #define TALER_AMOUNT_H 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 #include "xtotpConfig.h" 37 38 #include <stdbool.h> 39 #include <stdint.h> 40 /** 41 * @brief Number of bytes to store a secret. 42 * 43 */ 44 #define TALER_SECRET_LEN (20) 45 46 /** 47 * @brief Max length of an base8 input of a secret 48 * 49 */ 50 #define TALER_SECRET_BASE8_LENGTH (((TALER_SECRET_LEN * 8) + 2) / 3) 51 52 /** 53 * @brief Number of characters (plus 1 for 0-termination) we use to 54 * represent currency names (i.e. EUR, USD, etc.). We use 8+4 for 55 * alignment in the `struct TALER_Amount`. The amount is typically an 56 * ISO 4217 currency code when an alphanumeric 3-digit code is used. 57 * For regional currencies, the first character should be a "*" followed 58 * by a region-specific name (i.e. "*BRETAGNEFR"). 59 */ 60 #define TALER_CURRENCY_LEN (12) 61 62 /** 63 * @brief The "fraction" value in a `struct TALER_Amount` represents which 64 * fraction of the "main" value. 65 * 66 * @note that we need sub-cent precision here as transaction fees might 67 * be that low, and as we want to support microdonations. 68 * 69 * An actual `struct Amount a` thus represents 70 * "a.value + (a.fraction / #TALER_AMOUNT_FRAC_BASE)" units of "a.currency". 71 */ 72 #define TALER_AMOUNT_FRAC_BASE (100000000) 73 74 /** 75 * @brief How many digits behind the comma are required to represent the 76 * fractional value in human readable decimal format? Must match 77 * lg(#TALER_AMOUNT_FRAC_BASE). 78 */ 79 #define TALER_AMOUNT_FRAC_LEN (8) 80 81 /** 82 * @brief Defines the maximum of digits to be calculated. 83 * 84 */ 85 #define TALER_MAX_PASSCODE_LENGTH (8) 86 87 /** 88 * @brief Defines a packed attribute in a structure. 89 * Packed avoids padding bytes. 90 * 91 */ 92 #define PACKED __attribute__((packed)) 93 94 /** 95 * @brief Amount, encoded for network transmission. 96 */ 97 typedef struct { 98 uint64_t value PACKED; ///< Value in the main currency, in NBO. */ 99 uint32_t fraction PACKED; ///< Fraction (integer multiples of 100 ///< #TALER_AMOUNT_FRAC_BASE), in NBO. 101 uint8_t 102 currency[TALER_CURRENCY_LEN]; ///< Type of the currency being represented. 103 } TALER_AmountNBO; 104 105 /** 106 * @brief Taler extended data for xTOTP and autopay 107 * 108 */ 109 typedef struct { 110 uint8_t currency[TALER_CURRENCY_LEN]; ///< Currency 111 uint32_t fraction; ///< Fraction of currency 112 uint8_t merchantTemplateName[XTOTP_MERCHANT_TEMPLATE_SIZE]; ///< Merchant 113 ///< template name 114 } TALER_xData; 115 116 /** 117 * @brief Convert the amount to the needed @ref TALER_AmountNBO to calculate the 118 * xTOTP passcode. 119 * 120 * @param target The target @ref TALER_AmountNBO structure 121 * @param amountInfo xData reference to get the currency and the fraction of it 122 * @param enteredAmount The entered amount to convert 123 */ 124 void TALER_amount2AmountNBO(TALER_AmountNBO *target, 125 const TALER_xData *amountInfo, 126 const uint64_t enteredAmount); 127 128 /** 129 * @brief Initialise a @ref TALER_xDATA structure 130 * 131 * @param dataHandler The data structure to initlialise 132 * @param currency The currency to add 133 * @param fraction The fraction of the currency (e.g. 100 for Euro/CHF/Dollar) 134 * @param templateName Linked template for auto paying, can be set or not(use 135 * NULL or empty string) 136 */ 137 void TALER_xDataInit(TALER_xData *dataHandler, 138 const uint8_t *currency, 139 uint32_t fraction, 140 const uint8_t *templateName); 141 #ifdef __cplusplus 142 } 143 #endif 144 145 #endif /* TALER_AMOUNT_H*/