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
31#include <boost/format/free_funcs.hpp>
32
33#include <cstddef>
34#include <memory>
35#include <stdexcept>
36#include <string>
37#include <utility>
38
39namespace ripple {
40
42{
43}
44
46{
47}
48
50 AccountID const& srcChainDoor,
51 Issue const& srcChainIssue,
52 AccountID const& dstChainDoor,
53 Issue const& dstChainIssue)
54 : STBase{sfXChainBridge}
55 , lockingChainDoor_{sfLockingChainDoor, srcChainDoor}
56 , lockingChainIssue_{sfLockingChainIssue, srcChainIssue}
57 , issuingChainDoor_{sfIssuingChainDoor, dstChainDoor}
58 , issuingChainIssue_{sfIssuingChainIssue, dstChainIssue}
59{
60}
61
63 : STBase{sfXChainBridge}
64 , lockingChainDoor_{sfLockingChainDoor, o[sfLockingChainDoor]}
65 , lockingChainIssue_{sfLockingChainIssue, o[sfLockingChainIssue]}
66 , issuingChainDoor_{sfIssuingChainDoor, o[sfIssuingChainDoor]}
67 , issuingChainIssue_{sfIssuingChainIssue, o[sfIssuingChainIssue]}
68{
69}
70
72 : STXChainBridge{sfXChainBridge, v}
73{
74}
75
77 : STBase{name}
78{
79 if (!v.isObject())
80 {
81 Throw<std::runtime_error>(
82 "STXChainBridge can only be specified with a 'object' Json value");
83 }
84
85 auto checkExtra = [](Json::Value const& v) {
86 static auto const jbridge =
88 for (auto it = v.begin(); it != v.end(); ++it)
89 {
90 std::string const name = it.memberName();
91 if (!jbridge.isMember(name))
92 {
93 Throw<std::runtime_error>(
94 "STXChainBridge extra field detected: " + name);
95 }
96 }
97 return true;
98 };
99 checkExtra(v);
100
101 Json::Value const& lockingChainDoorStr =
102 v[sfLockingChainDoor.getJsonName()];
103 Json::Value const& lockingChainIssue = v[sfLockingChainIssue.getJsonName()];
104 Json::Value const& issuingChainDoorStr =
105 v[sfIssuingChainDoor.getJsonName()];
106 Json::Value const& issuingChainIssue = v[sfIssuingChainIssue.getJsonName()];
107
108 if (!lockingChainDoorStr.isString())
109 {
110 Throw<std::runtime_error>(
111 "STXChainBridge LockingChainDoor must be a string Json value");
112 }
113 if (!issuingChainDoorStr.isString())
114 {
115 Throw<std::runtime_error>(
116 "STXChainBridge IssuingChainDoor must be a string Json value");
117 }
118
119 auto const lockingChainDoor =
120 parseBase58<AccountID>(lockingChainDoorStr.asString());
121 auto const issuingChainDoor =
122 parseBase58<AccountID>(issuingChainDoorStr.asString());
123 if (!lockingChainDoor)
124 {
125 Throw<std::runtime_error>(
126 "STXChainBridge LockingChainDoor must be a valid account");
127 }
128 if (!issuingChainDoor)
129 {
130 Throw<std::runtime_error>(
131 "STXChainBridge IssuingChainDoor must be a valid account");
132 }
133
134 lockingChainDoor_ = STAccount{sfLockingChainDoor, *lockingChainDoor};
136 STIssue{sfLockingChainIssue, issueFromJson(lockingChainIssue)};
137 issuingChainDoor_ = STAccount{sfIssuingChainDoor, *issuingChainDoor};
139 STIssue{sfIssuingChainIssue, issueFromJson(issuingChainIssue)};
140}
141
143 : STBase{name}
144 , lockingChainDoor_{sit, sfLockingChainDoor}
145 , lockingChainIssue_{sit, sfLockingChainIssue}
146 , issuingChainDoor_{sit, sfIssuingChainDoor}
147 , issuingChainIssue_{sit, sfIssuingChainIssue}
148{
149}
150
151void
153{
158}
159
162{
163 Json::Value v;
164 v[sfLockingChainDoor.getJsonName()] = lockingChainDoor_.getJson(jo);
165 v[sfLockingChainIssue.getJsonName()] = lockingChainIssue_.getJson(jo);
166 v[sfIssuingChainDoor.getJsonName()] = issuingChainDoor_.getJson(jo);
167 v[sfIssuingChainIssue.getJsonName()] = issuingChainIssue_.getJson(jo);
168 return v;
169}
170
173{
174 return str(
175 boost::format("{ %s = %s, %s = %s, %s = %s, %s = %s }") %
176 sfLockingChainDoor.getName() % lockingChainDoor_.getText() %
177 sfLockingChainIssue.getName() % lockingChainIssue_.getText() %
178 sfIssuingChainDoor.getName() % issuingChainDoor_.getText() %
179 sfIssuingChainIssue.getName() % issuingChainIssue_.getText());
180}
181
184{
185 STObject o{sfXChainBridge};
186 o[sfLockingChainDoor] = lockingChainDoor_;
187 o[sfLockingChainIssue] = lockingChainIssue_;
188 o[sfIssuingChainDoor] = issuingChainDoor_;
189 o[sfIssuingChainIssue] = issuingChainIssue_;
190 return o;
191}
192
195{
196 return STI_XCHAIN_BRIDGE;
197}
198
199bool
201{
202 STXChainBridge const* v = dynamic_cast<STXChainBridge const*>(&t);
203 return v && (*v == *this);
204}
205
206bool
208{
211}
212
215{
216 return std::make_unique<STXChainBridge>(sit, name);
217}
218
219STBase*
221{
222 return emplace(n, buf, *this);
223}
224
225STBase*
227{
228 return emplace(n, buf, std::move(*this));
229}
230} // 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.
Definition: json_value.cpp:482
A currency issued by an account.
Definition: Issue.h:33
Identifies fields.
Definition: SField.h:143
void add(Serializer &s) const override
Definition: STAccount.cpp:93
bool isDefault() const override
Definition: STAccount.cpp:116
std::string getText() const override
Definition: STAccount.cpp:122
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
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
SerializedTypeID
Definition: SField.h:107
Issue issueFromJson(Json::Value const &v)
Definition: Issue.cpp:95
Note, should be treated as flags that can be | and &.
Definition: STBase.h:37