rippled
Loading...
Searching...
No Matches
MPTokenAuthorize.cpp
1#include <xrpl/ledger/View.h>
2#include <xrpl/ledger/helpers/AccountRootHelpers.h>
3#include <xrpl/ledger/helpers/DirectoryHelpers.h>
4#include <xrpl/ledger/helpers/MPTokenHelpers.h>
5#include <xrpl/protocol/Feature.h>
6#include <xrpl/protocol/TxFlags.h>
7#include <xrpl/protocol/st.h>
8#include <xrpl/tx/transactors/token/MPTokenAuthorize.h>
9
10namespace xrpl {
11
14{
15 return tfMPTokenAuthorizeMask;
16}
17
20{
21 if (ctx.tx[sfAccount] == ctx.tx[~sfHolder])
22 return temMALFORMED;
23
24 return tesSUCCESS;
25}
26
27TER
29{
30 auto const accountID = ctx.tx[sfAccount];
31 auto const holderID = ctx.tx[~sfHolder];
32
33 // if non-issuer account submits this tx, then they are trying either:
34 // 1. Unauthorize/delete MPToken
35 // 2. Use/create MPToken
36 //
37 // Note: `accountID` is holder's account
38 // `holderID` is NOT used
39 if (!holderID)
40 {
41 std::shared_ptr<SLE const> const sleMpt =
42 ctx.view.read(keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], accountID));
43
44 // There is an edge case where all holders have zero balance, issuance
45 // is legally destroyed, then outstanding MPT(s) are deleted afterwards.
46 // Thus, there is no need to check for the existence of the issuance if
47 // the MPT is being deleted with a zero balance. Check for unauthorize
48 // before fetching the MPTIssuance object.
49
50 // if holder wants to delete/unauthorize a mpt
51 if ((ctx.tx.getFlags() & tfMPTUnauthorize) != 0u)
52 {
53 if (!sleMpt)
55
56 if ((*sleMpt)[sfMPTAmount] != 0)
57 {
58 auto const sleMptIssuance =
59 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
60 if (!sleMptIssuance)
61 return tefINTERNAL; // LCOV_EXCL_LINE
62
63 return tecHAS_OBLIGATIONS;
64 }
65
66 if ((*sleMpt)[~sfLockedAmount].value_or(0) != 0)
67 {
68 auto const sleMptIssuance =
69 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
70 if (!sleMptIssuance)
71 return tefINTERNAL; // LCOV_EXCL_LINE
72
73 return tecHAS_OBLIGATIONS;
74 }
75 if (ctx.view.rules().enabled(featureSingleAssetVault) && sleMpt->isFlag(lsfMPTLocked))
76 return tecNO_PERMISSION;
77
78 return tesSUCCESS;
79 }
80
81 // Now test when the holder wants to hold/create/authorize a new MPT
82 auto const sleMptIssuance = 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 auto const sleHolder = ctx.view.read(keylet::account(*holderID));
98 if (!sleHolder)
99 return tecNO_DST;
100
101 auto const sleMptIssuance = 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) == 0u)
120 return tecNO_AUTH;
121
122 // The holder must create the MPT before the issuer can authorize it.
123 if (!ctx.view.exists(keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
124 return tecOBJECT_NOT_FOUND;
125
126 // Can't unauthorize the pseudo-accounts because they are implicitly
127 // always authorized. No need to amendment gate since Vault and LoanBroker
128 // can only be created if the Vault amendment is enabled.
129 if (isPseudoAccount(ctx.view, *holderID, {&sfVaultID, &sfLoanBrokerID}))
130 return tecNO_PERMISSION;
131
132 return tesSUCCESS;
133}
134
135TER
137 ApplyView& view,
138 MPTID const& mptIssuanceID,
139 AccountID const& account,
140 std::uint32_t const flags)
141{
142 auto const mptokenKey = keylet::mptoken(mptIssuanceID, account);
143
144 auto const ownerNode =
145 view.dirInsert(keylet::ownerDir(account), mptokenKey, describeOwnerDir(account));
146
147 if (!ownerNode)
148 return tecDIR_FULL; // LCOV_EXCL_LINE
149
150 auto mptoken = std::make_shared<SLE>(mptokenKey);
151 (*mptoken)[sfAccount] = account;
152 (*mptoken)[sfMPTokenIssuanceID] = mptIssuanceID;
153 (*mptoken)[sfFlags] = flags;
154 (*mptoken)[sfOwnerNode] = *ownerNode;
155
156 view.insert(mptoken);
157
158 return tesSUCCESS;
159}
160
161TER
163{
164 auto const& tx = ctx_.tx;
165 return authorizeMPToken(
166 ctx_.view(),
168 tx[sfMPTokenIssuanceID],
169 account_,
171 tx.getFlags(),
172 tx[~sfHolder]);
173}
174
175} // namespace xrpl
STTx const & tx
beast::Journal const journal
ApplyView & view()
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:116
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:289
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TER createMPToken(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, std::uint32_t const flags)
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:120
std::uint32_t getFlags() const
Definition STObject.cpp:509
AccountID const account_
Definition Transactor.h:116
ApplyView & view()
Definition Transactor.h:132
XRPAmount preFeeBalance_
Definition Transactor.h:117
ApplyContext & ctx_
Definition Transactor.h:112
T is_same_v
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:336
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:474
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:486
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ tefINTERNAL
Definition TER.h:153
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)
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
@ temMALFORMED
Definition TER.h:67
@ tecDIR_FULL
Definition TER.h:268
@ tecOBJECT_NOT_FOUND
Definition TER.h:307
@ tecNO_AUTH
Definition TER.h:281
@ tecNO_PERMISSION
Definition TER.h:286
@ tecDUPLICATE
Definition TER.h:296
@ tecHAS_OBLIGATIONS
Definition TER.h:298
@ tecNO_DST
Definition TER.h:271
@ tesSUCCESS
Definition TER.h:225
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:57
ReadView const & view
Definition Transactor.h:60
State information when preflighting a tx.
Definition Transactor.h:14