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