xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
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
14
namespace
xrpl
{
15
16
std::string
17
Issue::getText
()
const
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
45
void
46
Issue::setJson
(
json::Value
& jv)
const
47
{
48
jv[jss::currency] =
to_string
(
currency
);
49
if
(!
isXRP
(
currency
))
50
jv[jss::issuer] =
toBase58
(
account
);
51
}
52
53
bool
54
Issue::native
()
const
55
{
56
return
*
this
==
xrpIssue
();
57
}
58
59
bool
60
Issue::integral
()
const
61
{
62
return
native
();
63
}
64
65
bool
66
isConsistent
(
Issue
const
& ac)
67
{
68
return
isXRP
(ac.
currency
) ==
isXRP
(ac.
account
);
69
}
70
71
std::string
72
to_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
80
json::Value
81
toJson
(
Issue
const
& is)
82
{
83
json::Value
jv;
84
is.
setJson
(jv);
85
return
jv;
86
}
87
88
Issue
89
issueFromJson
(
json::Value
const
& v)
90
{
91
if
(!v.
isObject
())
92
{
93
Throw<std::runtime_error>
(
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
139
std::ostream
&
140
operator<<
(
std::ostream
& os,
Issue
const
& x)
141
{
142
os <<
to_string
(x);
143
return
os;
144
}
145
146
}
// namespace xrpl
std::ostream
std::string
json::Value
Represents a JSON value.
Definition
json_value.h:117
json::Value::isNull
bool isNull() const
isNull() tests to see if this field is null.
Definition
json_value.cpp:1092
json::Value::isObject
bool isObject() const
Definition
json_value.cpp:1152
json::Value::isString
bool isString() const
Definition
json_value.cpp:1134
json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition
json_value.cpp:494
json::Value::isMember
bool isMember(char const *key) const
Return true if the object has a member named key.
Definition
json_value.cpp:1049
xrpl::Issue
A currency issued by an account.
Definition
Issue.h:18
xrpl::Issue::currency
Currency currency
Definition
Issue.h:20
xrpl::Issue::account
AccountID account
Definition
Issue.h:21
xrpl::Issue::setJson
void setJson(json::Value &jv) const
Definition
Issue.cpp:46
xrpl::Issue::native
bool native() const
Definition
Issue.cpp:54
xrpl::Issue::integral
bool integral() const
Definition
Issue.cpp:60
xrpl::Issue::getText
std::string getText() const
Definition
Issue.cpp:17
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::xrpIssue
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition
Issue.h:108
xrpl::issueFromJson
Issue issueFromJson(json::Value const &v)
Definition
Issue.cpp:89
xrpl::isXRP
bool isXRP(AccountID const &c)
Definition
AccountID.h:84
xrpl::parseBase58
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition
AccountID.cpp:105
xrpl::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition
AccountID.cpp:95
xrpl::toCurrency
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition
UintTypes.cpp:65
xrpl::operator<<
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition
base_uint.h:672
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::toJson
json::Value toJson(Asset const &asset)
Definition
Asset.h:168
xrpl::noAccount
AccountID const & noAccount()
A placeholder for empty accounts.
Definition
AccountID.cpp:166
xrpl::isConsistent
bool isConsistent(Asset const &asset)
Definition
Asset.h:323
xrpl::noCurrency
Currency const & noCurrency()
A placeholder for empty currencies.
Definition
UintTypes.cpp:106
xrpl::xrpAccount
AccountID const & xrpAccount()
Compute AccountID from public key.
Definition
AccountID.cpp:159
xrpl::badCurrency
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition
UintTypes.cpp:113
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
ostream
std::string::reserve
T reserve(T... args)
stdexcept
string
Generated by
1.17.0