rippled
Loading...
Searching...
No Matches
permissioned_domains.cpp
1#include <test/jtx.h>
2
3namespace xrpl {
4namespace test {
5namespace jtx {
6namespace pdomain {
7
8// helpers
9// Make json for PermissionedDomainSet transaction
11setTx(AccountID const& account, Credentials const& credentials, std::optional<uint256> domain)
12{
13 Json::Value jv;
14 jv[sfTransactionType] = jss::PermissionedDomainSet;
15 jv[sfAccount] = to_string(account);
16 if (domain)
17 jv[sfDomainID] = to_string(*domain);
18
19 Json::Value acceptedCredentials(Json::arrayValue);
20 for (auto const& credential : credentials)
21 {
23 object[sfCredential] = credential.toJson();
24 acceptedCredentials.append(std::move(object));
25 }
26
27 jv[sfAcceptedCredentials] = acceptedCredentials;
28 return jv;
29}
30
31// Make json for PermissionedDomainDelete transaction
33deleteTx(AccountID const& account, uint256 const& domain)
34{
36 jv[sfTransactionType] = jss::PermissionedDomainDelete;
37 jv[sfAccount] = to_string(account);
38 jv[sfDomainID] = to_string(domain);
39 return jv;
40}
41
42// Get PermissionedDomain objects by type from account_objects rpc call
44getObjects(Account const& account, Env& env, bool withType)
45{
47 Json::Value params;
48 params[jss::account] = account.human();
49 if (withType)
50 params[jss::type] = jss::permissioned_domain;
51
52 auto const& resp = env.rpc("json", "account_objects", to_string(params));
54 objects = resp[jss::result][jss::account_objects];
55 for (auto const& object : objects)
56 {
57 if (object["LedgerEntryType"] != "PermissionedDomain")
58 {
59 if (withType)
60 { // impossible to get there
61 Throw<std::runtime_error>(
62 "Invalid object type: " +
63 object["LedgerEntryType"].asString()); // LCOV_EXCL_LINE
64 }
65 continue;
66 }
67
68 uint256 index;
69 std::ignore = index.parseHex(object[jss::index].asString());
70 ret[index] = object;
71 }
72
73 return ret;
74}
75
76// Check if ledger object is there
77bool
78objectExists(uint256 const& objID, Env& env)
79{
80 Json::Value params;
81 params[jss::index] = to_string(objID);
82
83 auto const result = env.rpc("json", "ledger_entry", to_string(params))["result"];
84
85 if ((result["status"] == "error") && (result["error"] == "entryNotFound"))
86 return false;
87
88 if ((result["node"]["LedgerEntryType"] != jss::PermissionedDomain))
89 return false;
90
91 if (result["status"] == "success")
92 return true;
93
94 throw std::runtime_error("Error getting ledger_entry RPC result.");
95}
96
97// Extract credentials from account_object object
100 Json::Value const& object,
102{
103 Credentials ret;
104 Json::Value credentials(Json::arrayValue);
105 credentials = object["AcceptedCredentials"];
106 for (auto const& credential : credentials)
107 {
109 obj = credential[jss::Credential];
110 auto const& issuer = obj[jss::Issuer];
111 auto const& credentialType = obj["CredentialType"];
112 // NOLINTNEXTLINE(bugprone-unchecked-optional-access): used only in tests
113 auto blob = strUnHex(credentialType.asString()).value();
114 ret.push_back({human2Acc.at(issuer.asString()), std::string(blob.begin(), blob.end())});
115 }
116 return ret;
117}
118
119// Sort credentials the same way as PermissionedDomainSet. Silently
120// remove duplicates.
123{
124 std::set<Credential> credentialsSet;
125 for (auto const& credential : input)
126 credentialsSet.insert(credential);
127 return {credentialsSet.begin(), credentialsSet.end()};
128}
129
132{
133 uint256 ret;
134 auto metaJson = meta->getJson(JsonOptions::none);
136 a = metaJson["AffectedNodes"];
137
138 for (auto const& node : a)
139 {
140 if (!node.isMember("CreatedNode") ||
141 node["CreatedNode"]["LedgerEntryType"] != "PermissionedDomain")
142 {
143 continue;
144 }
145 std::ignore = ret.parseHex(node["CreatedNode"]["LedgerIndex"].asString());
146 break;
147 }
148
149 return ret;
150}
151
152} // namespace pdomain
153} // namespace jtx
154} // namespace test
155} // namespace xrpl
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:476
Immutable cryptographic account descriptor.
Definition Account.h:19
A transaction testing environment.
Definition Env.h:122
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:847
Set the domain on a JTx.
Definition domain.h:11
T end(T... args)
T insert(T... args)
@ arrayValue
array value (ordered list)
Definition json_value.h:25
@ objectValue
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)
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)
std::map< uint256, Json::Value > getObjects(Account const &account, Env &env, bool withType)
bool objectExists(uint256 const &objID, Env &env)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
T push_back(T... args)