rippled
Loading...
Searching...
No Matches
MPTokenAuthorize.cpp
1#include <xrpld/app/tx/detail/MPTokenAuthorize.h>
2
3#include <xrpl/ledger/View.h>
4#include <xrpl/protocol/Feature.h>
5#include <xrpl/protocol/TxFlags.h>
6#include <xrpl/protocol/st.h>
7
8namespace ripple {
9
15
18{
19 if (ctx.tx[sfAccount] == ctx.tx[~sfHolder])
20 return temMALFORMED;
21
22 return tesSUCCESS;
23}
24
25TER
27{
28 auto const accountID = ctx.tx[sfAccount];
29 auto const holderID = ctx.tx[~sfHolder];
30
31 // if non-issuer account submits this tx, then they are trying either:
32 // 1. Unauthorize/delete MPToken
33 // 2. Use/create MPToken
34 //
35 // Note: `accountID` is holder's account
36 // `holderID` is NOT used
37 if (!holderID)
38 {
40 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], accountID));
41
42 // There is an edge case where all holders have zero balance, issuance
43 // is legally destroyed, then outstanding MPT(s) are deleted afterwards.
44 // Thus, there is no need to check for the existence of the issuance if
45 // the MPT is being deleted with a zero balance. Check for unauthorize
46 // before fetching the MPTIssuance object.
47
48 // if holder wants to delete/unauthorize a mpt
49 if (ctx.tx.getFlags() & tfMPTUnauthorize)
50 {
51 if (!sleMpt)
53
54 if ((*sleMpt)[sfMPTAmount] != 0)
55 {
56 auto const sleMptIssuance = ctx.view.read(
57 keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
58 if (!sleMptIssuance)
59 return tefINTERNAL; // LCOV_EXCL_LINE
60
61 return tecHAS_OBLIGATIONS;
62 }
63
64 if ((*sleMpt)[~sfLockedAmount].value_or(0) != 0)
65 {
66 auto const sleMptIssuance = ctx.view.read(
67 keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
68 if (!sleMptIssuance)
69 return tefINTERNAL; // LCOV_EXCL_LINE
70
71 return tecHAS_OBLIGATIONS;
72 }
73 if (ctx.view.rules().enabled(featureSingleAssetVault) &&
74 sleMpt->isFlag(lsfMPTLocked))
75 return tecNO_PERMISSION;
76
77 return tesSUCCESS;
78 }
79
80 // Now test when the holder wants to hold/create/authorize a new MPT
81 auto const sleMptIssuance =
82 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
83
84 if (!sleMptIssuance)
86
87 if (accountID == (*sleMptIssuance)[sfIssuer])
88 return tecNO_PERMISSION;
89
90 // if holder wants to use and create a mpt
91 if (sleMpt)
92 return tecDUPLICATE;
93
94 return tesSUCCESS;
95 }
96
97 if (!ctx.view.exists(keylet::account(*holderID)))
98 return tecNO_DST;
99
100 auto const sleMptIssuance =
101 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
102 if (!sleMptIssuance)
103 return tecOBJECT_NOT_FOUND;
104
105 std::uint32_t const mptIssuanceFlags = sleMptIssuance->getFieldU32(sfFlags);
106
107 // If tx is submitted by issuer, they would either try to do the following
108 // for allowlisting:
109 // 1. authorize an account
110 // 2. unauthorize an account
111 //
112 // Note: `accountID` is issuer's account
113 // `holderID` is holder's account
114 if (accountID != (*sleMptIssuance)[sfIssuer])
115 return tecNO_PERMISSION;
116
117 // If tx is submitted by issuer, it only applies for MPT with
118 // lsfMPTRequireAuth set
119 if (!(mptIssuanceFlags & lsfMPTRequireAuth))
120 return tecNO_AUTH;
121
122 // The holder must create the MPT before the issuer can authorize it.
123 if (!ctx.view.exists(
124 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
125 return tecOBJECT_NOT_FOUND;
126
127 return tesSUCCESS;
128}
129
130TER
132 ApplyView& view,
133 MPTID const& mptIssuanceID,
134 AccountID const& account,
135 std::uint32_t const flags)
136{
137 auto const mptokenKey = keylet::mptoken(mptIssuanceID, account);
138
139 auto const ownerNode = view.dirInsert(
140 keylet::ownerDir(account), mptokenKey, describeOwnerDir(account));
141
142 if (!ownerNode)
143 return tecDIR_FULL; // LCOV_EXCL_LINE
144
145 auto mptoken = std::make_shared<SLE>(mptokenKey);
146 (*mptoken)[sfAccount] = account;
147 (*mptoken)[sfMPTokenIssuanceID] = mptIssuanceID;
148 (*mptoken)[sfFlags] = flags;
149 (*mptoken)[sfOwnerNode] = *ownerNode;
150
151 view.insert(mptoken);
152
153 return tesSUCCESS;
154}
155
156TER
158{
159 auto const& tx = ctx_.tx;
160 return authorizeMPToken(
161 ctx_.view(),
163 tx[sfMPTokenIssuanceID],
164 account_,
166 tx.getFlags(),
167 tx[~sfHolder]);
168}
169
170} // namespace ripple
ApplyView & view()
beast::Journal const journal
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:124
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
Definition ApplyView.h:300
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static TER createMPToken(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, std::uint32_t const flags)
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:111
std::uint32_t getFlags() const
Definition STObject.cpp:518
AccountID const account_
Definition Transactor.h:128
ApplyView & view()
Definition Transactor.h:144
XRPAmount mPriorBalance
Definition Transactor.h:129
ApplyContext & ctx_
Definition Transactor.h:124
T is_same_v
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:521
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:507
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:355
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ lsfMPTRequireAuth
constexpr std::uint32_t const tfMPTokenAuthorizeMask
Definition TxFlags.h:154
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:1031
@ tefINTERNAL
Definition TER.h:154
constexpr std::uint32_t const tfMPTUnauthorize
Definition TxFlags.h:153
@ tecNO_DST
Definition TER.h:272
@ tecOBJECT_NOT_FOUND
Definition TER.h:308
@ tecDIR_FULL
Definition TER.h:269
@ tecDUPLICATE
Definition TER.h:297
@ tecNO_PERMISSION
Definition TER.h:287
@ tecHAS_OBLIGATIONS
Definition TER.h:299
@ tecNO_AUTH
Definition TER.h:282
@ tesSUCCESS
Definition TER.h:226
TER authorizeMPToken(ApplyView &view, XRPAmount const &priorBalance, MPTID const &mptIssuanceID, AccountID const &account, beast::Journal journal, std::uint32_t flags=0, std::optional< AccountID > holderID=std::nullopt)
Definition View.cpp:1271
@ temMALFORMED
Definition TER.h:68
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
State information when preflighting a tx.
Definition Transactor.h:16