xrpld
Loading...
Searching...
No Matches
permissioned_domains.cpp
1#include <test/jtx/permissioned_domains.h>
2
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5
6#include <xrpl/basics/StringUtilities.h>
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/basics/contract.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/json/to_string.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STObject.h>
14#include <xrpl/protocol/jss.h>
15
16#include <map>
17#include <memory>
18#include <optional>
19#include <set>
20#include <stdexcept>
21#include <tuple>
22#include <unordered_map>
23#include <utility>
24
26
27// helpers
28// Make json for PermissionedDomainSet transaction
31{
32 json::Value jv;
33 jv[sfTransactionType] = jss::PermissionedDomainSet;
34 jv[sfAccount] = to_string(account);
35 if (domain)
36 jv[sfDomainID] = to_string(*domain);
37
38 json::Value acceptedCredentials(json::ValueType::Array);
39 for (auto const& credential : credentials)
40 {
42 object[sfCredential] = credential.toJson();
43 acceptedCredentials.append(std::move(object));
44 }
45
46 jv[sfAcceptedCredentials] = acceptedCredentials;
47 return jv;
48}
49
50// Make json for PermissionedDomainDelete transaction
52deleteTx(AccountID const& account, uint256 const& domain)
53{
55 jv[sfTransactionType] = jss::PermissionedDomainDelete;
56 jv[sfAccount] = to_string(account);
57 jv[sfDomainID] = to_string(domain);
58 return jv;
59}
60
61// Get PermissionedDomain objects by type from account_objects rpc call
63getObjects(Account const& account, Env& env, bool withType)
64{
66 json::Value params;
67 params[jss::account] = account.human();
68 if (withType)
69 params[jss::type] = jss::permissioned_domain;
70
71 auto const& resp = env.rpc("json", "account_objects", to_string(params));
73 objects = resp[jss::result][jss::account_objects];
74 for (auto const& object : objects)
75 {
76 if (object["LedgerEntryType"] != "PermissionedDomain")
77 {
78 if (withType)
79 { // impossible to get there
81 "Invalid object type: " +
82 object["LedgerEntryType"].asString()); // LCOV_EXCL_LINE
83 }
84 continue;
85 }
86
87 uint256 index;
88 std::ignore = index.parseHex(object[jss::index].asString());
89 ret[index] = object;
90 }
91
92 return ret;
93}
94
95// Check if ledger object is there
96bool
97objectExists(uint256 const& objID, Env& env)
98{
99 json::Value params;
100 params[jss::index] = to_string(objID);
101
102 auto const result = env.rpc("json", "ledger_entry", to_string(params))["result"];
103
104 if ((result["status"] == "error") && (result["error"] == "entryNotFound"))
105 return false;
106
107 if ((result["node"]["LedgerEntryType"] != jss::PermissionedDomain))
108 return false;
109
110 if (result["status"] == "success")
111 return true;
112
113 throw std::runtime_error("Error getting ledger_entry RPC result.");
114}
115
116// Extract credentials from account_object object
119 json::Value const& object,
121{
122 Credentials ret;
124 credentials = object["AcceptedCredentials"];
125 for (auto const& credential : credentials)
126 {
128 obj = credential[jss::Credential];
129 auto const& issuer = obj[jss::Issuer];
130 auto const& credentialType = obj["CredentialType"];
131 // NOLINTNEXTLINE(bugprone-unchecked-optional-access): used only in tests
132 auto blob = strUnHex(credentialType.asString()).value();
133 ret.push_back(
134 {.issuer = human2Acc.at(issuer.asString()),
135 .credType = std::string(blob.begin(), blob.end())});
136 }
137 return ret;
138}
139
140// Sort credentials the same way as PermissionedDomainSet. Silently
141// remove duplicates.
144{
145 std::set<Credential> credentialsSet;
146 for (auto const& credential : input)
147 credentialsSet.insert(credential);
148 return {credentialsSet.begin(), credentialsSet.end()};
149}
150
153{
154 uint256 ret;
155 auto metaJson = meta->getJson(JsonOptions::Values::None);
157 a = metaJson["AffectedNodes"];
158
159 for (auto const& node : a)
160 {
161 if (!node.isMember("CreatedNode") ||
162 node["CreatedNode"]["LedgerEntryType"] != "PermissionedDomain")
163 {
164 continue;
165 }
166 std::ignore = ret.parseHex(node["CreatedNode"]["LedgerIndex"].asString());
167 break;
168 }
169
170 return ret;
171}
172
173} // namespace xrpl::test::jtx::pdomain
T at(T... args)
T begin(T... args)
Represents a JSON value.
Definition json_value.h:130
Value & append(Value const &value)
Append value to array at the end.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:507
Immutable cryptographic account descriptor.
Definition jtx/Account.h:17
A transaction testing environment.
Definition Env.h:143
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:864
T end(T... args)
T insert(T... args)
@ Array
array value (ordered list)
Definition json_value.h:25
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
std::vector< Credential > Credentials
Credentials credentialsFromJson(json::Value const &object, std::unordered_map< std::string, Account > const &human2Acc)
std::map< uint256, json::Value > getObjects(Account const &account, Env &env, bool withType)
json::Value setTx(AccountID const &account, Credentials const &credentials, std::optional< uint256 > domain)
json::Value deleteTx(AccountID const &account, uint256 const &domain)
uint256 getNewDomain(std::shared_ptr< STObject const > const &meta)
Credentials sortCredentials(Credentials const &input)
bool objectExists(uint256 const &objID, Env &env)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
BaseUInt< 256 > uint256
Definition base_uint.h:562
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
T push_back(T... args)