rippled
Loading...
Searching...
No Matches
UintTypes.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2014 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/beast/utility/Zero.h>
21#include <xrpl/protocol/Serializer.h>
22#include <xrpl/protocol/SystemParameters.h>
23#include <xrpl/protocol/UintTypes.h>
24#include <string_view>
25
26namespace ripple {
27
28// For details on the protocol-level serialization please visit
29// https://xrpl.org/serialization.html#currency-codes
30
31namespace detail {
32
33// Characters we are willing to allow in the ASCII representation of a
34// three-letter currency code.
36 "abcdefghijklmnopqrstuvwxyz"
37 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
38 "0123456789"
39 "<>(){}[]|?!@#$%^&*";
40
41// The location (in bytes) of the 3 digit currency inside a 160-bit value
43
44// The length of an ISO-4217 like code
46
47} // namespace detail
48
50to_string(Currency const& currency)
51{
52 if (currency == beast::zero)
53 return systemCurrencyCode();
54
55 if (currency == noCurrency())
56 return "1";
57
58 static constexpr Currency sIsoBits(
59 "FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF");
60
61 if ((currency & sIsoBits).isZero())
62 {
63 std::string const iso(
64 currency.data() + detail::isoCodeOffset,
66
67 // Specifying the system currency code using ISO-style representation
68 // is not allowed.
69 if ((iso != systemCurrencyCode()) &&
70 (iso.find_first_not_of(detail::isoCharSet) == std::string::npos))
71 {
72 return iso;
73 }
74 }
75
76 return strHex(currency);
77}
78
79bool
80to_currency(Currency& currency, std::string const& code)
81{
82 if (code.empty() || !code.compare(systemCurrencyCode()))
83 {
84 currency = beast::zero;
85 return true;
86 }
87
88 // Handle ISO-4217-like 3-digit character codes.
89 if (code.size() == detail::isoCodeLength)
90 {
91 if (code.find_first_not_of(detail::isoCharSet) != std::string::npos)
92 return false;
93
94 currency = beast::zero;
95
97 code.begin(), code.end(), currency.begin() + detail::isoCodeOffset);
98
99 return true;
100 }
101
102 return currency.parseHex(code);
103}
104
107{
108 Currency currency;
109 if (!to_currency(currency, code))
110 currency = noCurrency();
111 return currency;
112}
113
114Currency const&
116{
117 static Currency const currency(beast::zero);
118 return currency;
119}
120
121Currency const&
123{
124 static Currency const currency(1);
125 return currency;
126}
127
128Currency const&
130{
131 static Currency const currency(0x5852500000000000);
132 return currency;
133}
134
135} // namespace ripple
T begin(T... args)
iterator begin()
Definition: base_uint.h:135
pointer data()
Definition: base_uint.h:124
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:502
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:35
constexpr std::size_t isoCodeOffset
Definition: UintTypes.cpp:42
constexpr std::size_t isoCodeLength
Definition: UintTypes.cpp:45
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:129
Currency const & noCurrency()
A placeholder for empty currencies.
Definition: UintTypes.cpp:122
static std::string const & systemCurrencyCode()
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition: UintTypes.h:56
Currency const & xrpCurrency()
XRP currency.
Definition: UintTypes.cpp:115
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition: UintTypes.cpp:80
T size(T... args)