rippled
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 <ripple/protocol/Issue.h>
21 
22 #include <ripple/json/json_errors.h>
23 #include <ripple/protocol/AccountID.h>
24 #include <ripple/protocol/UintTypes.h>
25 #include <ripple/protocol/jss.h>
26 
27 namespace 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 
52 bool
53 isConsistent(Issue const& ac)
54 {
55  return isXRP(ac.currency) == isXRP(ac.account);
56 }
57 
59 to_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 
68 to_json(Issue const& is)
69 {
70  Json::Value jv;
71  jv[jss::currency] = to_string(is.currency);
72  if (!isXRP(is.currency))
73  jv[jss::issuer] = toBase58(is.account);
74  return jv;
75 }
76 
77 Issue
79 {
80  if (!v.isObject())
81  {
82  Throw<std::runtime_error>(
83  "issueFromJson can only be specified with an 'object' Json value");
84  }
85 
86  Json::Value const curStr = v[jss::currency];
87  Json::Value const issStr = v[jss::issuer];
88 
89  if (!curStr.isString())
90  {
91  Throw<Json::error>(
92  "issueFromJson currency must be a string Json value");
93  }
94 
95  auto const currency = to_currency(curStr.asString());
96  if (currency == badCurrency() || currency == noCurrency())
97  {
98  Throw<Json::error>("issueFromJson currency must be a valid currency");
99  }
100 
101  if (isXRP(currency))
102  {
103  if (!issStr.isNull())
104  {
105  Throw<Json::error>("Issue, XRP should not have issuer");
106  }
107  return xrpIssue();
108  }
109 
110  if (!issStr.isString())
111  {
112  Throw<Json::error>("issueFromJson issuer must be a string Json value");
113  }
114  auto const issuer = parseBase58<AccountID>(issStr.asString());
115 
116  if (!issuer)
117  {
118  Throw<Json::error>("issueFromJson issuer must be a valid account");
119  }
120 
121  return Issue{currency, *issuer};
122 }
123 
126 {
127  os << to_string(x);
128  return os;
129 }
130 
131 } // namespace ripple
ripple::badCurrency
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:129
ripple::to_currency
bool to_currency(Currency &currency, std::string const &code)
Tries to convert a string to a Currency, returns true on success.
Definition: UintTypes.cpp:80
ripple::Issue
A currency issued by an account.
Definition: Issue.h:35
Json::Value::isObject
bool isObject() const
Definition: json_value.cpp:1027
std::string
STL class.
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1009
ripple::noCurrency
Currency const & noCurrency()
A placeholder for empty currencies.
Definition: UintTypes.cpp:122
ripple::isConsistent
bool isConsistent(Book const &book)
Definition: Book.cpp:25
std::string::reserve
T reserve(T... args)
ripple::Issue::currency
Currency currency
Definition: Issue.h:38
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
Json::Value::isNull
bool isNull() const
isNull() tests to see if this field is null.
Definition: json_value.cpp:967
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:303
std::ostream
STL class.
ripple::Issue::getText
std::string getText() const
Definition: Issue.cpp:30
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:91
ripple::issueFromJson
Issue issueFromJson(Json::Value const &v)
Definition: Issue.cpp:78
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::to_json
Json::Value to_json(Issue const &is)
Definition: Issue.cpp:68
ripple::xrpIssue
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition: Issue.h:105
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::noAccount
AccountID const & noAccount()
A placeholder for empty accounts.
Definition: AccountID.cpp:175
ripple::Issue::account
AccountID account
Definition: Issue.h:39
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469