rippled
Loading...
Searching...
No Matches
Permissions.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2025 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 <xrpl/beast/utility/instrumentation.h>
21#include <xrpl/protocol/Permissions.h>
22#include <xrpl/protocol/jss.h>
23
24namespace ripple {
25
27{
29#pragma push_macro("TRANSACTION")
30#undef TRANSACTION
31
32#define TRANSACTION(tag, value, name, delegatable, fields) {value, delegatable},
33
34#include <xrpl/protocol/detail/transactions.macro>
35
36#undef TRANSACTION
37#pragma pop_macro("TRANSACTION")
38 };
39
41#pragma push_macro("PERMISSION")
42#undef PERMISSION
43
44#define PERMISSION(type, txType, value) {#type, type},
45
46#include <xrpl/protocol/detail/permissions.macro>
47
48#undef PERMISSION
49#pragma pop_macro("PERMISSION")
50 };
51
53#pragma push_macro("PERMISSION")
54#undef PERMISSION
55
56#define PERMISSION(type, txType, value) {type, #type},
57
58#include <xrpl/protocol/detail/permissions.macro>
59
60#undef PERMISSION
61#pragma pop_macro("PERMISSION")
62 };
63
65#pragma push_macro("PERMISSION")
66#undef PERMISSION
67
68#define PERMISSION(type, txType, value) {type, txType},
69
70#include <xrpl/protocol/detail/permissions.macro>
71
72#undef PERMISSION
73#pragma pop_macro("PERMISSION")
74 };
75
76 for ([[maybe_unused]] auto const& permission : granularPermissionMap_)
77 XRPL_ASSERT(
78 permission.second > UINT16_MAX,
79 "ripple::Permission::granularPermissionMap_ : granular permission "
80 "value must not exceed the maximum uint16_t value.");
81}
82
83Permission const&
85{
86 static Permission const instance;
87 return instance;
88}
89
92{
93 auto const it = granularPermissionMap_.find(name);
94 if (it != granularPermissionMap_.end())
95 return static_cast<uint32_t>(it->second);
96
97 return std::nullopt;
98}
99
102{
103 auto const it = granularNameMap_.find(value);
104 if (it != granularNameMap_.end())
105 return it->second;
106
107 return std::nullopt;
108}
109
112{
113 auto const it = granularTxTypeMap_.find(gpType);
114 if (it != granularTxTypeMap_.end())
115 return it->second;
116
117 return std::nullopt;
118}
119
120bool
121Permission::isDelegatable(std::uint32_t const& permissionValue) const
122{
123 auto const granularPermission =
124 getGranularName(static_cast<GranularPermissionType>(permissionValue));
125 if (granularPermission)
126 // granular permissions are always allowed to be delegated
127 return true;
128
129 auto const it = delegatableTx_.find(permissionValue - 1);
130 if (it != delegatableTx_.end() && it->second == Delegation::notDelegatable)
131 return false;
132
133 return true;
134}
135
136uint32_t
138{
139 return static_cast<uint32_t>(type) + 1;
140}
141
142TxType
143Permission::permissionToTxType(uint32_t const& value) const
144{
145 return static_cast<TxType>(value - 1);
146}
147
148} // namespace ripple
std::unordered_map< std::uint16_t, Delegation > delegatableTx_
Definition: Permissions.h:57
TxType permissionToTxType(uint32_t const &value) const
bool isDelegatable(std::uint32_t const &permissionValue) const
std::optional< std::string > getGranularName(GranularPermissionType const &value) const
std::unordered_map< GranularPermissionType, std::string > granularNameMap_
Definition: Permissions.h:62
std::unordered_map< std::string, GranularPermissionType > granularPermissionMap_
Definition: Permissions.h:60
std::optional< std::uint32_t > getGranularValue(std::string const &name) const
Definition: Permissions.cpp:91
std::optional< TxType > getGranularTxType(GranularPermissionType const &gpType) const
uint32_t txToPermissionType(TxType const &type) const
static Permission const & getInstance()
Definition: Permissions.cpp:84
std::unordered_map< GranularPermissionType, TxType > granularTxTypeMap_
Definition: Permissions.h:64
T end(T... args)
T find(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
TxType
Transaction type identifiers.
Definition: TxFormats.h:57
@ notDelegatable
Definition: Permissions.h:50
GranularPermissionType
We have both transaction type permissions and granular type permissions.
Definition: Permissions.h:38