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