xrpld
Loading...
Searching...
No Matches
Issue.cpp
1#include <xrpl/protocol/Issue.h>
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/json/json_errors.h>
5#include <xrpl/json/json_value.h>
6#include <xrpl/protocol/AccountID.h>
7#include <xrpl/protocol/UintTypes.h>
8#include <xrpl/protocol/jss.h>
9
10#include <ostream>
11#include <stdexcept>
12#include <string>
13
14namespace xrpl {
15
16std::string
18{
19 std::string ret;
20
21 ret.reserve(64);
22 ret = to_string(currency);
23
24 if (!isXRP(currency))
25 {
26 ret += "/";
27
28 if (isXRP(account))
29 {
30 ret += "0";
31 }
32 else if (account == noAccount())
33 {
34 ret += "1";
35 }
36 else
37 {
38 ret += to_string(account);
39 }
40 }
41
42 return ret;
43}
44
45void
47{
48 jv[jss::currency] = to_string(currency);
49 if (!isXRP(currency))
50 jv[jss::issuer] = toBase58(account);
51}
52
53bool
55{
56 return *this == xrpIssue();
57}
58
59bool
61{
62 return native();
63}
64
65bool
67{
68 return isXRP(ac.currency) == isXRP(ac.account);
69}
70
72to_string(Issue const& ac)
73{
74 if (isXRP(ac.account))
75 return to_string(ac.currency);
76
77 return to_string(ac.account) + "/" + to_string(ac.currency);
78}
79
81toJson(Issue const& is)
82{
83 json::Value jv;
84 is.setJson(jv);
85 return jv;
86}
87
88Issue
90{
91 if (!v.isObject())
92 {
94 "issueFromJson can only be specified with an 'object' Json value");
95 }
96
97 if (v.isMember(jss::mpt_issuance_id))
98 {
99 Throw<std::runtime_error>("issueFromJson, Issue should not have mpt_issuance_id");
100 }
101
102 json::Value const curStr = v[jss::currency];
103 json::Value const issStr = v[jss::issuer];
104
105 if (!curStr.isString())
106 {
107 Throw<json::Error>("issueFromJson currency must be a string Json value");
108 }
109
110 auto const currency = toCurrency(curStr.asString());
111 if (currency == badCurrency() || currency == noCurrency())
112 {
113 Throw<json::Error>("issueFromJson currency must be a valid currency");
114 }
115
116 if (isXRP(currency))
117 {
118 if (!issStr.isNull())
119 {
120 Throw<json::Error>("Issue, XRP should not have issuer");
121 }
122 return xrpIssue();
123 }
124
125 if (!issStr.isString())
126 {
127 Throw<json::Error>("issueFromJson issuer must be a string Json value");
128 }
129 auto const issuer = parseBase58<AccountID>(issStr.asString());
130
131 if (!issuer || *issuer == noAccount() || *issuer == xrpAccount())
132 {
133 Throw<json::Error>("issueFromJson issuer must be a valid account");
134 }
135
136 return Issue{currency, *issuer};
137}
138
140operator<<(std::ostream& os, Issue const& x)
141{
142 os << to_string(x);
143 return os;
144}
145
146} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
bool isNull() const
isNull() tests to see if this field is null.
bool isObject() const
bool isString() const
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
AccountID account
Definition Issue.h:16
void setJson(json::Value &jv) const
Definition Issue.cpp:46
bool native() const
Definition Issue.cpp:54
bool integral() const
Definition Issue.cpp:60
std::string getText() const
Definition Issue.cpp:17
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:97
Issue issueFromJson(json::Value const &v)
Definition Issue.cpp:89
bool isXRP(AccountID const &c)
Definition AccountID.h:70
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition base_uint.h:648
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
json::Value toJson(Asset const &asset)
Definition Asset.h:157
AccountID const & noAccount()
A placeholder for empty accounts.
bool isConsistent(Asset const &asset)
Definition Asset.h:312
Currency const & noCurrency()
A placeholder for empty currencies.
AccountID const & xrpAccount()
Compute AccountID from public key.
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
T reserve(T... args)