cryptnox-pos 1.0.0
Standalone USDC payment terminal firmware (ESP32 + Cryptnox smart card)
Loading...
Searching...
No Matches
addr_check.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: LGPL-3.0-or-later
3 * Copyright (c) 2026 Cryptnox SA
4 */
5
21
22#ifndef ADDR_CHECK_H
23#define ADDR_CHECK_H
24
25#include <stddef.h>
26#include <string.h>
27
29#define ADDR_TRON_LEN 34U
30
32static inline bool addr_is_base58(const char *s)
33{
34 static const char *const B58 =
35 "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
36 if (s == NULL) { return false; }
37 /* An empty string has no character outside the alphabet, which would make a
38 * plain loop answer "yes". Callers check the length too, but this is the
39 * function whose name promises the answer, so it answers correctly. */
40 if (*s == '\0') { return false; }
41 for (const char *p = s; *p != '\0'; p++) {
42 if (strchr(B58, *p) == NULL) { return false; }
43 }
44 return true;
45}
46
53static inline bool addr_tron_plausible(const char *s)
54{
55 return (s != NULL) && (strlen(s) == ADDR_TRON_LEN) && (s[0] == 'T') &&
57}
58
66static inline bool addr_tron_decode(const char *s, unsigned char out[25])
67{
68 static const char *const B58 =
69 "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
70 if (!addr_tron_plausible(s)) { return false; }
71 memset(out, 0, 25U);
72 for (const char *p = s; *p != '\0'; p++) {
73 unsigned carry = static_cast<unsigned>(strchr(B58, *p) - B58);
74 for (int j = 24; j >= 0; j--) {
75 carry += 58U * out[j];
76 out[j] = static_cast<unsigned char>(carry & 0xFFU);
77 carry >>= 8;
78 }
79 if (carry != 0U) { return false; } /* more than 25 bytes */
80 }
81 return out[0] == 0x41U;
82}
83
84#endif /* ADDR_CHECK_H */
static bool addr_is_base58(const char *s)
true if every character of s is in the Bitcoin/Tron base58 set.
Definition addr_check.h:32
static bool addr_tron_decode(const char *s, unsigned char out[25])
Decode a base58 Tron address into its 25 bytes (0x41 || 20-byte key hash || 4-byte checksum).
Definition addr_check.h:66
#define ADDR_TRON_LEN
Length of a base58 Tron address, without the NUL.
Definition addr_check.h:29
static bool addr_tron_plausible(const char *s)
Structural check on a Tron address: 34 characters, 'T', base58.
Definition addr_check.h:53