rippled
Loading...
Searching...
No Matches
MPTIssue.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 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/json/json_errors.h>
21#include <xrpl/protocol/MPTIssue.h>
22#include <xrpl/protocol/jss.h>
23
24namespace ripple {
25
26MPTIssue::MPTIssue(MPTID const& issuanceID) : mptID_(issuanceID)
27{
28}
29
30AccountID const&
32{
33 // MPTID is concatenation of sequence + account
34 static_assert(sizeof(MPTID) == (sizeof(std::uint32_t) + sizeof(AccountID)));
35 // copy from id skipping the sequence
36 AccountID const* account = reinterpret_cast<AccountID const*>(
37 mptID_.data() + sizeof(std::uint32_t));
38
39 return *account;
40}
41
42MPTID const&
44{
45 return mptID_;
46}
47
50{
51 return to_string(mptID_);
52}
53
54void
56{
57 jv[jss::mpt_issuance_id] = to_string(mptID_);
58}
59
61to_json(MPTIssue const& mptIssue)
62{
63 Json::Value jv;
64 mptIssue.setJson(jv);
65 return jv;
66}
67
69to_string(MPTIssue const& mptIssue)
70{
71 return to_string(mptIssue.getMptID());
72}
73
74MPTIssue
76{
77 if (!v.isObject())
78 {
79 Throw<std::runtime_error>(
80 "mptIssueFromJson can only be specified with an 'object' Json "
81 "value");
82 }
83
84 if (v.isMember(jss::currency) || v.isMember(jss::issuer))
85 {
86 Throw<std::runtime_error>(
87 "mptIssueFromJson, MPTIssue should not have currency or issuer");
88 }
89
90 Json::Value const& idStr = v[jss::mpt_issuance_id];
91
92 if (!idStr.isString())
93 {
94 Throw<Json::error>(
95 "mptIssueFromJson MPTID must be a string Json value");
96 }
97
98 MPTID id;
99 if (!id.parseHex(idStr.asString()))
100 {
101 Throw<Json::error>("mptIssueFromJson MPTID is invalid");
102 }
103
104 return MPTIssue{id};
105}
106
107} // 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 isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:943
std::string getText() const
Definition: MPTIssue.cpp:49
MPTIssue()=default
MPTID const & getMptID() const
Definition: MPTIssue.cpp:43
AccountID const & getIssuer() const
Definition: MPTIssue.cpp:31
void setJson(Json::Value &jv) const
Definition: MPTIssue.cpp:55
pointer data()
Definition: base_uint.h:124
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:49
base_uint< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition: UintTypes.h:64
MPTIssue mptIssueFromJson(Json::Value const &jv)
Definition: MPTIssue.cpp:75
Json::Value to_json(Asset const &asset)
Definition: Asset.cpp:74
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629