rippled
Loading...
Searching...
No Matches
permissioned_domains.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 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 <test/jtx/permissioned_domains.h>
21#include <exception>
22
23namespace ripple {
24namespace test {
25namespace jtx {
26namespace pdomain {
27
28// helpers
29// Make json for PermissionedDomainSet transaction
32 AccountID const& account,
33 Credentials const& credentials,
35{
36 Json::Value jv;
37 jv[sfTransactionType] = jss::PermissionedDomainSet;
38 jv[sfAccount] = to_string(account);
39 if (domain)
40 jv[sfDomainID] = to_string(*domain);
41
42 Json::Value acceptedCredentials(Json::arrayValue);
43 for (auto const& credential : credentials)
44 {
46 object[sfCredential] = credential.toJson();
47 acceptedCredentials.append(std::move(object));
48 }
49
50 jv[sfAcceptedCredentials] = acceptedCredentials;
51 return jv;
52}
53
54// Make json for PermissionedDomainDelete transaction
56deleteTx(AccountID const& account, uint256 const& domain)
57{
59 jv[sfTransactionType] = jss::PermissionedDomainDelete;
60 jv[sfAccount] = to_string(account);
61 jv[sfDomainID] = to_string(domain);
62 return jv;
63}
64
65// Get PermissionedDomain objects by type from account_objects rpc call
67getObjects(Account const& account, Env& env, bool withType)
68{
70 Json::Value params;
71 params[jss::account] = account.human();
72 if (withType)
73 params[jss::type] = jss::permissioned_domain;
74
75 auto const& resp = env.rpc("json", "account_objects", to_string(params));
77 objects = resp[jss::result][jss::account_objects];
78 for (auto const& object : objects)
79 {
80 if (object["LedgerEntryType"] != "PermissionedDomain")
81 {
82 if (withType)
83 { // impossible to get there
84 Throw<std::runtime_error>(
85 "Invalid object type: " +
86 object["LedgerEntryType"].asString()); // LCOV_EXCL_LINE
87 }
88 continue;
89 }
90
91 uint256 index;
92 std::ignore = index.parseHex(object[jss::index].asString());
93 ret[index] = object;
94 }
95
96 return ret;
97}
98
99// Check if ledger object is there
100bool
101objectExists(uint256 const& objID, Env& env)
102{
103 Json::Value params;
104 params[jss::index] = to_string(objID);
105
106 auto const result =
107 env.rpc("json", "ledger_entry", to_string(params))["result"];
108
109 if ((result["status"] == "error") && (result["error"] == "entryNotFound"))
110 return false;
111
112 if ((result["node"]["LedgerEntryType"] != jss::PermissionedDomain))
113 return false;
114
115 if (result["status"] == "success")
116 return true;
117
118 throw std::runtime_error("Error getting ledger_entry RPC result.");
119}
120
121// Extract credentials from account_object object
124 Json::Value const& object,
126{
127 Credentials ret;
128 Json::Value credentials(Json::arrayValue);
129 credentials = object["AcceptedCredentials"];
130 for (auto const& credential : credentials)
131 {
133 obj = credential[jss::Credential];
134 auto const& issuer = obj[jss::Issuer];
135 auto const& credentialType = obj["CredentialType"];
136 auto blob = strUnHex(credentialType.asString()).value();
137 ret.push_back(
138 {human2Acc.at(issuer.asString()),
139 std::string(blob.begin(), blob.end())});
140 }
141 return ret;
142}
143
144// Sort credentials the same way as PermissionedDomainSet. Silently
145// remove duplicates.
148{
149 std::set<Credential> credentialsSet;
150 for (auto const& credential : input)
151 credentialsSet.insert(credential);
152 return {credentialsSet.begin(), credentialsSet.end()};
153}
154
157{
158 uint256 ret;
159 auto metaJson = meta->getJson(JsonOptions::none);
161 a = metaJson["AffectedNodes"];
162
163 for (auto const& node : a)
164 {
165 if (!node.isMember("CreatedNode") ||
166 node["CreatedNode"]["LedgerEntryType"] != "PermissionedDomain")
167 {
168 continue;
169 }
170 std::ignore =
171 ret.parseHex(node["CreatedNode"]["LedgerIndex"].asString());
172 break;
173 }
174
175 return ret;
176}
177
178} // namespace pdomain
179} // namespace jtx
180} // namespace test
181} // namespace ripple
T at(T... args)
T begin(T... args)
Represents a JSON value.
Definition: json_value.h:147
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:891
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:502
Immutable cryptographic account descriptor.
Definition: Account.h:38
A transaction testing environment.
Definition: Env.h:117
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:764
T end(T... args)
T insert(T... args)
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
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:26
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:629
@ credential
Credentials signature.
T push_back(T... args)