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