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
53{
54 return to_string(mptID_);
55}
56
57void
59{
60 jv[jss::mpt_issuance_id] = to_string(mptID_);
61}
62
64to_json(MPTIssue const& mptIssue)
65{
66 Json::Value jv;
67 mptIssue.setJson(jv);
68 return jv;
69}
70
72to_string(MPTIssue const& mptIssue)
73{
74 return to_string(mptIssue.getMptID());
75}
76
77MPTIssue
79{
80 if (!v.isObject())
81 {
82 Throw<std::runtime_error>(
83 "mptIssueFromJson can only be specified with an 'object' Json "
84 "value");
85 }
86
87 if (v.isMember(jss::currency) || v.isMember(jss::issuer))
88 {
89 Throw<std::runtime_error>(
90 "mptIssueFromJson, MPTIssue should not have currency or issuer");
91 }
92
93 Json::Value const& idStr = v[jss::mpt_issuance_id];
94
95 if (!idStr.isString())
96 {
97 Throw<Json::error>(
98 "mptIssueFromJson MPTID must be a string Json value");
99 }
100
101 MPTID id;
102 if (!id.parseHex(idStr.asString()))
103 {
104 Throw<Json::error>("mptIssueFromJson MPTID is invalid");
105 }
106
107 return MPTIssue{id};
108}
109
110} // namespace ripple
Represents a JSON value.
Definition: json_value.h:150
bool isString() const
bool isObject() const
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482
bool isMember(char const *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:962
std::string getText() const
Definition: MPTIssue.cpp:52
MPTIssue()=default
AccountID const & getIssuer() const
Definition: MPTIssue.cpp:40
void setJson(Json::Value &jv) const
Definition: MPTIssue.cpp:58
constexpr MPTID const & getMptID() const
Definition: MPTIssue.h:46
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:78
Json::Value to_json(Asset const &asset)
Definition: Asset.h:123
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630