xrpld
Loading...
Searching...
No Matches
UintTypes.cpp
1#include <xrpl/protocol/UintTypes.h>
2
3#include <xrpl/basics/strHex.h>
4#include <xrpl/beast/utility/Zero.h>
5#include <xrpl/protocol/SystemParameters.h>
6
7#include <algorithm>
8#include <cstddef>
9#include <string>
10#include <string_view>
11
12namespace xrpl {
13
14// For details on the protocol-level serialization please visit
15// https://xrpl.org/serialization.html#currency-codes
16
17namespace detail {
18
19// Characters we are willing to allow in the ASCII representation of a
20// three-letter currency code.
22 "abcdefghijklmnopqrstuvwxyz"
23 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24 "0123456789"
25 "<>(){}[]|?!@#$%^&*";
26
27// The location (in bytes) of the 3 digit currency inside a 160-bit value
29
30// The length of an ISO-4217 like code
32
33} // namespace detail
34
36to_string(Currency const& currency)
37{
38 if (currency == beast::kZero)
39 return systemCurrencyCode();
40
41 if (currency == noCurrency())
42 return "1";
43
44 static constexpr Currency kSIsoBits("FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF");
45
46 if ((currency & kSIsoBits).isZero())
47 {
48 std::string const iso(
49 currency.data() + detail::kIsoCodeOffset,
51
52 // Specifying the system currency code using ISO-style representation
53 // is not allowed.
54 if ((iso != systemCurrencyCode()) &&
55 (iso.find_first_not_of(detail::kIsoCharSet) == std::string::npos))
56 {
57 return iso;
58 }
59 }
60
61 return strHex(currency);
62}
63
64bool
65toCurrency(Currency& currency, std::string const& code)
66{
67 if (code.empty() || (code.compare(systemCurrencyCode()) == 0))
68 {
69 currency = beast::kZero;
70 return true;
71 }
72
73 // Handle ISO-4217-like 3-digit character codes.
74 if (code.size() == detail::kIsoCodeLength)
75 {
76 if (code.find_first_not_of(detail::kIsoCharSet) != std::string::npos)
77 return false;
78
79 currency = beast::kZero;
80
82
83 return true;
84 }
85
86 return currency.parseHex(code);
87}
88
91{
92 Currency currency;
93 if (!toCurrency(currency, code))
94 currency = noCurrency();
95 return currency;
96}
97
98Currency const&
100{
101 static Currency const kCurrency(beast::kZero);
102 return kCurrency;
103}
104
105Currency const&
107{
108 static Currency const kCurrency(1);
109 return kCurrency;
110}
111
112Currency const&
114{
115 static Currency const kCurrency(0x5852500000000000);
116 return kCurrency;
117}
118
119} // namespace xrpl
pointer data()
Definition base_uint.h:106
iterator begin()
Definition base_uint.h:117
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:507
T compare(T... args)
T copy(T... args)
T empty(T... args)
T find_first_not_of(T... args)
constexpr std::size_t kIsoCodeLength
Definition UintTypes.cpp:31
constexpr std::size_t kIsoCodeOffset
Definition UintTypes.cpp:28
constexpr std::string_view kIsoCharSet
Definition UintTypes.cpp:21
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:36
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
Currency const & xrpCurrency()
XRP currency.
Definition UintTypes.cpp:99
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
Currency const & noCurrency()
A placeholder for empty currencies.
static std::string const & systemCurrencyCode()
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
T size(T... args)