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