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: " + object["LedgerEntryType"].asString()); // LCOV_EXCL_LINE
63 }
64 continue;
65 }
66
67 uint256 index;
68 std::ignore = index.parseHex(object[jss::index].asString());
69 ret[index] = object;
70 }
71
72 return ret;
73}
74
75// Check if ledger object is there
76bool
77objectExists(uint256 const& objID, Env& env)
78{
79 Json::Value params;
80 params[jss::index] = to_string(objID);
81
82 auto const result = env.rpc("json", "ledger_entry", to_string(params))["result"];
83
84 if ((result["status"] == "error") && (result["error"] == "entryNotFound"))
85 return false;
86
87 if ((result["node"]["LedgerEntryType"] != jss::PermissionedDomain))
88 return false;
89
90 if (result["status"] == "success")
91 return true;
92
93 throw std::runtime_error("Error getting ledger_entry RPC result.");
94}
95
96// Extract credentials from account_object object
99{
100 Credentials ret;
101 Json::Value credentials(Json::arrayValue);
102 credentials = object["AcceptedCredentials"];
103 for (auto const& credential : credentials)
104 {
106 obj = credential[jss::Credential];
107 auto const& issuer = obj[jss::Issuer];
108 auto const& credentialType = obj["CredentialType"];
109 auto blob = strUnHex(credentialType.asString()).value();
110 ret.push_back({human2Acc.at(issuer.asString()), std::string(blob.begin(), blob.end())});
111 }
112 return ret;
113}
114
115// Sort credentials the same way as PermissionedDomainSet. Silently
116// remove duplicates.
119{
120 std::set<Credential> credentialsSet;
121 for (auto const& credential : input)
122 credentialsSet.insert(credential);
123 return {credentialsSet.begin(), credentialsSet.end()};
124}
125
128{
129 uint256 ret;
130 auto metaJson = meta->getJson(JsonOptions::none);
132 a = metaJson["AffectedNodes"];
133
134 for (auto const& node : a)
135 {
136 if (!node.isMember("CreatedNode") || node["CreatedNode"]["LedgerEntryType"] != "PermissionedDomain")
137 {
138 continue;
139 }
140 std::ignore = ret.parseHex(node["CreatedNode"]["LedgerIndex"].asString());
141 break;
142 }
143
144 return ret;
145}
146
147} // namespace pdomain
148} // namespace jtx
149} // namespace test
150} // 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:471
Immutable cryptographic account descriptor.
Definition Account.h:19
A transaction testing environment.
Definition Env.h:119
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:792
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:597
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
T push_back(T... args)