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