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