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(
48 currency.data() + detail::isoCodeOffset,
50
51 // Specifying the system currency code using ISO-style representation
52 // is not allowed.
53 if ((iso != systemCurrencyCode()) &&
54 (iso.find_first_not_of(detail::isoCharSet) == std::string::npos))
55 {
56 return iso;
57 }
58 }
59
60 return strHex(currency);
61}
62
63bool
64to_currency(Currency& currency, std::string const& code)
65{
66 if (code.empty() || !code.compare(systemCurrencyCode()))
67 {
68 currency = beast::zero;
69 return true;
70 }
71
72 // Handle ISO-4217-like 3-digit character codes.
73 if (code.size() == detail::isoCodeLength)
74 {
75 if (code.find_first_not_of(detail::isoCharSet) != std::string::npos)
76 return false;
77
78 currency = beast::zero;
79
80 std::copy(code.begin(), code.end(), currency.begin() + detail::isoCodeOffset);
81
82 return true;
83 }
84
85 return currency.parseHex(code);
86}
87
90{
91 Currency currency;
92 if (!to_currency(currency, code))
93 currency = noCurrency();
94 return currency;
95}
96
97Currency const&
99{
100 static Currency const currency(beast::zero);
101 return currency;
102}
103
104Currency const&
106{
107 static Currency const currency(1);
108 return currency;
109}
110
111Currency const&
113{
114 static Currency const currency(0x5852500000000000);
115 return currency;
116}
117
118} // namespace xrpl
T begin(T... args)
iterator begin()
Definition base_uint.h:112
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:474
pointer data()
Definition base_uint.h:101
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:5
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:36
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:600
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
Currency const & xrpCurrency()
XRP currency.
Definition UintTypes.cpp:98
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:64
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
T size(T... args)