rippled
Loading...
Searching...
No Matches
STXChainBridge.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2022 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/contract.h>
21#include <xrpl/json/json_value.h>
22#include <xrpl/protocol/AccountID.h>
23#include <xrpl/protocol/Issue.h>
24#include <xrpl/protocol/SField.h>
25#include <xrpl/protocol/STAccount.h>
26#include <xrpl/protocol/STBase.h>
27#include <xrpl/protocol/STObject.h>
28#include <xrpl/protocol/STXChainBridge.h>
29#include <xrpl/protocol/Serializer.h>
30#include <xrpl/protocol/jss.h>
31
32#include <boost/format/free_funcs.hpp>
33
34#include <cstddef>
35#include <memory>
36#include <stdexcept>
37#include <string>
38#include <utility>
39
40namespace ripple {
41
43{
44}
45
47{
48}
49
51 AccountID const& srcChainDoor,
52 Issue const& srcChainIssue,
53 AccountID const& dstChainDoor,
54 Issue const& dstChainIssue)
55 : STBase{sfXChainBridge}
56 , lockingChainDoor_{sfLockingChainDoor, srcChainDoor}
57 , lockingChainIssue_{sfLockingChainIssue, srcChainIssue}
58 , issuingChainDoor_{sfIssuingChainDoor, dstChainDoor}
59 , issuingChainIssue_{sfIssuingChainIssue, dstChainIssue}
60{
61}
62
64 : STBase{sfXChainBridge}
65 , lockingChainDoor_{sfLockingChainDoor, o[sfLockingChainDoor]}
66 , lockingChainIssue_{sfLockingChainIssue, o[sfLockingChainIssue]}
67 , issuingChainDoor_{sfIssuingChainDoor, o[sfIssuingChainDoor]}
68 , issuingChainIssue_{sfIssuingChainIssue, o[sfIssuingChainIssue]}
69{
70}
71
73 : STXChainBridge{sfXChainBridge, v}
74{
75}
76
78 : STBase{name}
79{
80 if (!v.isObject())
81 {
82 Throw<std::runtime_error>(
83 "STXChainBridge can only be specified with a 'object' Json value");
84 }
85
86 auto checkExtra = [](Json::Value const& v) {
87 static auto const jbridge =
89 for (auto it = v.begin(); it != v.end(); ++it)
90 {
91 std::string const name = it.memberName();
92 if (!jbridge.isMember(name))
93 {
94 Throw<std::runtime_error>(
95 "STXChainBridge extra field detected: " + name);
96 }
97 }
98 return true;
99 };
100 checkExtra(v);
101
102 Json::Value const& lockingChainDoorStr = v[jss::LockingChainDoor];
103 Json::Value const& lockingChainIssue = v[jss::LockingChainIssue];
104 Json::Value const& issuingChainDoorStr = v[jss::IssuingChainDoor];
105 Json::Value const& issuingChainIssue = v[jss::IssuingChainIssue];
106
107 if (!lockingChainDoorStr.isString())
108 {
109 Throw<std::runtime_error>(
110 "STXChainBridge LockingChainDoor must be a string Json value");
111 }
112 if (!issuingChainDoorStr.isString())
113 {
114 Throw<std::runtime_error>(
115 "STXChainBridge IssuingChainDoor must be a string Json value");
116 }
117
118 auto const lockingChainDoor =
119 parseBase58<AccountID>(lockingChainDoorStr.asString());
120 auto const issuingChainDoor =
121 parseBase58<AccountID>(issuingChainDoorStr.asString());
122 if (!lockingChainDoor)
123 {
124 Throw<std::runtime_error>(
125 "STXChainBridge LockingChainDoor must be a valid account");
126 }
127 if (!issuingChainDoor)
128 {
129 Throw<std::runtime_error>(
130 "STXChainBridge IssuingChainDoor must be a valid account");
131 }
132
133 lockingChainDoor_ = STAccount{sfLockingChainDoor, *lockingChainDoor};
135 STIssue{sfLockingChainIssue, issueFromJson(lockingChainIssue)};
136 issuingChainDoor_ = STAccount{sfIssuingChainDoor, *issuingChainDoor};
138 STIssue{sfIssuingChainIssue, issueFromJson(issuingChainIssue)};
139}
140
142 : STBase{name}
143 , lockingChainDoor_{sit, sfLockingChainDoor}
144 , lockingChainIssue_{sit, sfLockingChainIssue}
145 , issuingChainDoor_{sit, sfIssuingChainDoor}
146 , issuingChainIssue_{sit, sfIssuingChainIssue}
147{
148}
149
150void
158
161{
162 Json::Value v;
163 v[jss::LockingChainDoor] = lockingChainDoor_.getJson(jo);
164 v[jss::LockingChainIssue] = lockingChainIssue_.getJson(jo);
165 v[jss::IssuingChainDoor] = issuingChainDoor_.getJson(jo);
166 v[jss::IssuingChainIssue] = issuingChainIssue_.getJson(jo);
167 return v;
168}
169
172{
173 return str(
174 boost::format("{ %s = %s, %s = %s, %s = %s, %s = %s }") %
175 sfLockingChainDoor.getName() % lockingChainDoor_.getText() %
176 sfLockingChainIssue.getName() % lockingChainIssue_.getText() %
177 sfIssuingChainDoor.getName() % issuingChainDoor_.getText() %
178 sfIssuingChainIssue.getName() % issuingChainIssue_.getText());
179}
180
183{
184 STObject o{sfXChainBridge};
185 o[sfLockingChainDoor] = lockingChainDoor_;
186 o[sfLockingChainIssue] = lockingChainIssue_;
187 o[sfIssuingChainDoor] = issuingChainDoor_;
188 o[sfIssuingChainIssue] = issuingChainIssue_;
189 return o;
190}
191
194{
195 return STI_XCHAIN_BRIDGE;
196}
197
198bool
200{
201 STXChainBridge const* v = dynamic_cast<STXChainBridge const*>(&t);
202 return v && (*v == *this);
203}
204
205bool
211
214{
215 return std::make_unique<STXChainBridge>(sit, name);
216}
217
218STBase*
220{
221 return emplace(n, buf, *this);
222}
223
224STBase*
226{
227 return emplace(n, buf, std::move(*this));
228}
229} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
const_iterator begin() const
const_iterator end() const
bool isString() const
bool isObject() const
std::string asString() const
Returns the unquoted string value.
A currency issued by an account.
Definition Issue.h:33
Identifies fields.
Definition SField.h:146
void add(Serializer &s) const override
Definition STAccount.cpp:93
bool isDefault() const override
std::string getText() const override
A type which can be exported to a well known binary format.
Definition STBase.h:135
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:233
virtual Json::Value getJson(JsonOptions=JsonOptions::none) const
Definition STBase.cpp:106
void add(Serializer &s) const override
Definition STIssue.cpp:110
Json::Value getJson(JsonOptions) const override
Definition STIssue.cpp:102
bool isDefault() const override
Definition STIssue.cpp:138
std::string getText() const override
Definition STIssue.cpp:96
bool isEquivalent(STBase const &t) const override
STObject toSTObject() const
STBase * copy(std::size_t n, void *buf) const override
AccountID const & issuingChainDoor() const
Issue const & issuingChainIssue() const
std::string getText() const override
Json::Value getJson(JsonOptions) const override
void add(Serializer &s) const override
Issue const & lockingChainIssue() const
SerializedTypeID getSType() const override
bool isDefault() const override
STBase * move(std::size_t n, void *buf) override
static std::unique_ptr< STXChainBridge > construct(SerialIter &, SField const &name)
AccountID const & lockingChainDoor() const
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
SerializedTypeID
Definition SField.h:110
Issue issueFromJson(Json::Value const &v)
Definition Issue.cpp:95
Note, should be treated as flags that can be | and &.
Definition STBase.h:37