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 xrpl {
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("FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF");
44
45 if ((currency & sIsoBits).isZero())
46 {
47 std::string const iso(
49
50 // Specifying the system currency code using ISO-style representation
51 // is not allowed.
52 if ((iso != systemCurrencyCode()) && (iso.find_first_not_of(detail::isoCharSet) == std::string::npos))
53 {
54 return iso;
55 }
56 }
57
58 return strHex(currency);
59}
60
61bool
62to_currency(Currency& currency, std::string const& code)
63{
64 if (code.empty() || !code.compare(systemCurrencyCode()))
65 {
66 currency = beast::zero;
67 return true;
68 }
69
70 // Handle ISO-4217-like 3-digit character codes.
71 if (code.size() == detail::isoCodeLength)
72 {
73 if (code.find_first_not_of(detail::isoCharSet) != std::string::npos)
74 return false;
75
76 currency = beast::zero;
77
78 std::copy(code.begin(), code.end(), currency.begin() + detail::isoCodeOffset);
79
80 return true;
81 }
82
83 return currency.parseHex(code);
84}
85
88{
89 Currency currency;
90 if (!to_currency(currency, code))
91 currency = noCurrency();
92 return currency;
93}
94
95Currency const&
97{
98 static Currency const currency(beast::zero);
99 return currency;
100}
101
102Currency const&
104{
105 static Currency const currency(1);
106 return currency;
107}
108
109Currency const&
111{
112 static Currency const currency(0x5852500000000000);
113 return currency;
114}
115
116} // namespace xrpl
T begin(T... args)
iterator begin()
Definition base_uint.h:113
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:472
pointer data()
Definition base_uint.h:102
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 isoCodeLength
Definition UintTypes.cpp:30
constexpr std::size_t isoCodeOffset
Definition UintTypes.cpp:27
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:37
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:11
Currency const & xrpCurrency()
XRP currency.
Definition UintTypes.cpp:96
Currency const & noCurrency()
A placeholder for empty currencies.
static std::string const & systemCurrencyCode()
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:62
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
T size(T... args)