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/basics/base_uint.h>
21#include <xrpl/basics/contract.h>
22#include <xrpl/json/json_errors.h>
23#include <xrpl/json/json_value.h>
24#include <xrpl/protocol/AccountID.h>
25#include <xrpl/protocol/MPTIssue.h>
26#include <xrpl/protocol/UintTypes.h>
27#include <xrpl/protocol/jss.h>
28
29#include <cstdint>
30#include <stdexcept>
31#include <string>
32
33namespace ripple {
34
35MPTIssue::MPTIssue(MPTID const& issuanceID) : mptID_(issuanceID)
36{
37}
38
39AccountID const&
41{
42 // MPTID is concatenation of sequence + account
43 static_assert(sizeof(MPTID) == (sizeof(std::uint32_t) + sizeof(AccountID)));
44 // copy from id skipping the sequence
45 AccountID const* account = reinterpret_cast<AccountID const*>(
46 mptID_.data() + sizeof(std::uint32_t));
47
48 return *account;
49}
50
51MPTID const&
53{
54 return mptID_;
55}
56
59{
60 return to_string(mptID_);
61}
62
63void
65{
66 jv[jss::mpt_issuance_id] = to_string(mptID_);
67}
68
70to_json(MPTIssue const& mptIssue)
71{
72 Json::Value jv;
73 mptIssue.setJson(jv);
74 return jv;
75}
76
78to_string(MPTIssue const& mptIssue)
79{
80 return to_string(mptIssue.getMptID());
81}
82
83MPTIssue
85{
86 if (!v.isObject())
87 {
88 Throw<std::runtime_error>(
89 "mptIssueFromJson can only be specified with an 'object' Json "
90 "value");
91 }
92
93 if (v.isMember(jss::currency) || v.isMember(jss::issuer))
94 {
95 Throw<std::runtime_error>(
96 "mptIssueFromJson, MPTIssue should not have currency or issuer");
97 }
98
99 Json::Value const& idStr = v[jss::mpt_issuance_id];
100
101 if (!idStr.isString())
102 {
103 Throw<Json::error>(
104 "mptIssueFromJson MPTID must be a string Json value");
105 }
106
107 MPTID id;
108 if (!id.parseHex(idStr.asString()))
109 {
110 Throw<Json::error>("mptIssueFromJson MPTID is invalid");
111 }
112
113 return MPTIssue{id};
114}
115
116} // 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 isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:949
std::string getText() const
Definition: MPTIssue.cpp:58
MPTIssue()=default
MPTID const & getMptID() const
Definition: MPTIssue.cpp:52
AccountID const & getIssuer() const
Definition: MPTIssue.cpp:40
void setJson(Json::Value &jv) const
Definition: MPTIssue.cpp:64
pointer data()
Definition: base_uint.h:125
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:84
Json::Value to_json(Asset const &asset)
Definition: Asset.cpp:82
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630