xrpld
Loading...
Searching...
No Matches
MPTokenIssuanceSet.cpp
1#include <xrpl/tx/transactors/token/MPTokenIssuanceSet.h>
2
3#include <xrpl/beast/utility/Journal.h>
4#include <xrpl/beast/utility/Zero.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/core/ServiceRegistry.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/protocol/ConfidentialTransfer.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/Indexes.h>
11#include <xrpl/protocol/LedgerFormats.h>
12#include <xrpl/protocol/Permissions.h>
13#include <xrpl/protocol/Protocol.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STTx.h>
17#include <xrpl/protocol/TER.h>
18#include <xrpl/protocol/TxFlags.h>
19#include <xrpl/protocol/XRPAmount.h>
20#include <xrpl/tx/Transactor.h>
21
22#include <algorithm>
23#include <cstdint>
24
25namespace xrpl {
26
27bool
29{
30 return !ctx.tx.isFieldPresent(sfDomainID) ||
31 (ctx.rules.enabled(featurePermissionedDomains) &&
32 ctx.rules.enabled(featureSingleAssetVault));
33}
34
37{
38 return tfMPTokenIssuanceSetMask;
39}
40
43{
44 auto const txFlags = ctx.tx.getFlags();
45 auto const enableFlags = txFlags & tfMPTokenIssuanceSetEnableFlagMask;
46 auto const metadata = ctx.tx[~sfMPTokenMetadata];
47 auto const transferFee = ctx.tx[~sfTransferFee];
48 auto const immutableFlags = ctx.tx[~sfImmutableFlags];
49 auto const isMutate = (enableFlags != 0u) || metadata || transferFee || immutableFlags;
50 auto const hasIssuerElGamalKey = ctx.tx.isFieldPresent(sfIssuerEncryptionKey);
51 auto const hasAuditorElGamalKey = ctx.tx.isFieldPresent(sfAuditorEncryptionKey);
52
53 bool const enablePrivacy = (enableFlags & tfMPTSetCanHoldConfidentialBalance) != 0u;
54 auto const hasDomain = ctx.tx.isFieldPresent(sfDomainID);
55 auto const hasHolder = ctx.tx.isFieldPresent(sfHolder);
56
57 if (isMutate && !ctx.rules.enabled(featureDynamicMPT))
58 return temDISABLED;
59
60 bool const setConfidentialBalanceImmutable =
61 immutableFlags && (*immutableFlags & tifMPTCanHoldConfidentialBalance) != 0u;
62 if ((hasIssuerElGamalKey || hasAuditorElGamalKey || enablePrivacy ||
63 setConfidentialBalanceImmutable) &&
64 !ctx.rules.enabled(featureConfidentialTransfer))
65 return temDISABLED;
66
67 if (hasDomain && hasHolder)
68 return temMALFORMED;
69
70 if (enablePrivacy && hasHolder)
71 return temMALFORMED;
72
73 // fails if both flags are set
74 if (ctx.tx.isFlag(tfMPTLock) && ctx.tx.isFlag(tfMPTUnlock))
75 return temINVALID_FLAG;
76
77 auto const accountID = ctx.tx[sfAccount];
78 auto const holderID = ctx.tx[~sfHolder];
79 if (holderID && accountID == holderID)
80 return temMALFORMED;
81
82 if (ctx.rules.enabled(featureSingleAssetVault) || ctx.rules.enabled(featureDynamicMPT) ||
83 ctx.rules.enabled(featureConfidentialTransfer))
84 {
85 // Is this transaction actually changing anything ?
86 if (txFlags == 0 && !hasDomain && !hasIssuerElGamalKey && !hasAuditorElGamalKey &&
87 !isMutate)
88 return temMALFORMED;
89 }
90
91 if (ctx.rules.enabled(featureDynamicMPT))
92 {
93 // Holder field is not allowed when mutating MPTokenIssuance
94 if (isMutate && holderID)
95 return temMALFORMED;
96
97 // A single transaction may either lock/unlock or mutate capability
98 // flags, but not both.
99 if (isMutate && (ctx.tx.isFlag(tfMPTLock) || ctx.tx.isFlag(tfMPTUnlock)))
100 return temMALFORMED;
101
102 if (transferFee && *transferFee > kMaxTransferFee)
103 return temBAD_TRANSFER_FEE;
104
105 if (transferFee && *transferFee > 0u && enablePrivacy)
106 return temBAD_TRANSFER_FEE;
107
108 if (metadata && metadata->length() > kMaxMpTokenMetadataLength)
109 return temMALFORMED;
110
111 // If the immutable flags field is included, at least one flag must be
112 // specified, and undefined flags must not be specified.
113 if (immutableFlags &&
114 ((*immutableFlags == 0u) ||
115 ((*immutableFlags & tifMPTokenIssuanceImmutableMask) != 0u)))
116 return temINVALID_FLAG;
117 }
118
119 if (hasHolder && (hasIssuerElGamalKey || hasAuditorElGamalKey))
120 return temMALFORMED;
121
122 if (hasAuditorElGamalKey && !hasIssuerElGamalKey)
123 return temMALFORMED;
124
125 if (hasIssuerElGamalKey && !isValidCompressedECPoint(ctx.tx[sfIssuerEncryptionKey]))
126 return temMALFORMED;
127
128 if (hasAuditorElGamalKey && !isValidCompressedECPoint(ctx.tx[sfAuditorEncryptionKey]))
129 return temMALFORMED;
130
131 return tesSUCCESS;
132}
133
134TER
136{
137 // ensure that issuance exists
138 auto const sleMptIssuance = ctx.view.read(keylet::mptokenIssuance(ctx.tx[sfMPTokenIssuanceID]));
139 if (!sleMptIssuance)
140 return tecOBJECT_NOT_FOUND;
141
142 if (!sleMptIssuance->isFlag(lsfMPTCanLock))
143 {
144 // For readability two separate `if` rather than `||` of two conditions
145 if (!ctx.view.rules().enabled(featureSingleAssetVault) &&
146 !ctx.view.rules().enabled(featureDynamicMPT))
147 {
148 return tecNO_PERMISSION;
149 }
150 if (ctx.tx.isFlag(tfMPTLock) || ctx.tx.isFlag(tfMPTUnlock))
151 {
152 return tecNO_PERMISSION;
153 }
154 }
155
156 // ensure it is issued by the tx submitter
157 if ((*sleMptIssuance)[sfIssuer] != ctx.tx[sfAccount])
158 return tecNO_PERMISSION;
159
160 if (auto const holderID = ctx.tx[~sfHolder])
161 {
162 // make sure holder account exists
163 if (!ctx.view.exists(keylet::account(*holderID)))
164 return tecNO_DST;
165
166 // the mptoken must exist
167 if (!ctx.view.exists(keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
168 return tecOBJECT_NOT_FOUND;
169 }
170
171 if (auto const domain = ctx.tx[~sfDomainID])
172 {
173 if (not sleMptIssuance->isFlag(lsfMPTRequireAuth))
174 return tecNO_PERMISSION;
175
176 if (*domain != beast::kZero)
177 {
178 auto const sleDomain = ctx.view.read(keylet::permissionedDomain(*domain));
179 if (!sleDomain)
180 return tecOBJECT_NOT_FOUND;
181 }
182 }
183
184 // sfImmutableFlags is soeDEFAULT, defaulting to 0 if not specified on
185 // the ledger.
186 auto const currentImmutableFlags = sleMptIssuance->getFieldU32(sfImmutableFlags);
187
188 auto isImmutable = [&](std::uint32_t flag) -> bool { return currentImmutableFlags & flag; };
189
190 auto const enableFlags = ctx.tx.getFlags() & tfMPTokenIssuanceSetEnableFlagMask;
191 if (enableFlags != 0u)
192 {
193 // If any of the flags to be set is immutable, return tecNO_PERMISSION.
194 if (std::ranges::any_of(flagMapping, [&](auto const& f) {
195 return isImmutable(f.immutableFlag) && ctx.tx.isFlag(f.setFlag);
196 }))
197 return tecNO_PERMISSION;
198 }
199
200 if (isImmutable(lsifMPTMetadata) && ctx.tx.isFieldPresent(sfMPTokenMetadata))
201 return tecNO_PERMISSION;
202
203 if (auto const fee = ctx.tx[~sfTransferFee])
204 {
205 // A non-zero TransferFee is only valid if the lsfMPTCanTransfer flag
206 // is already set on the ledger object, or is being enabled by this
207 // same transaction. The Immutability of lsfMPTCanTransfer is checked above.
208 if (fee > 0u && !sleMptIssuance->isFlag(lsfMPTCanTransfer) &&
209 (enableFlags & tfMPTSetCanTransfer) == 0u)
210 return tecNO_PERMISSION;
211
212 // Cannot set a non-zero TransferFee on an issuance that has confidential
213 // transfer enabled
214 if (fee > 0u && sleMptIssuance->isFlag(lsfMPTCanHoldConfidentialBalance))
215 return tecNO_PERMISSION;
216
217 // Cannot set TransferFee if it is immutable
218 if (isImmutable(lsifMPTTransferFee))
219 return tecNO_PERMISSION;
220 }
221
222 // cannot update issuer public key
223 if (ctx.tx.isFieldPresent(sfIssuerEncryptionKey) &&
224 sleMptIssuance->isFieldPresent(sfIssuerEncryptionKey))
225 {
226 return tecNO_PERMISSION;
227 }
228
229 // cannot update auditor public key
230 if (ctx.tx.isFieldPresent(sfAuditorEncryptionKey) &&
231 sleMptIssuance->isFieldPresent(sfAuditorEncryptionKey))
232 {
233 return tecNO_PERMISSION; // LCOV_EXCL_LINE
234 }
235
236 auto const enablesConfidentialBalance =
237 (enableFlags & tfMPTSetCanHoldConfidentialBalance) != 0u;
238 if (enablesConfidentialBalance && sleMptIssuance->isFieldPresent(sfTransferFee) &&
239 (*sleMptIssuance)[sfTransferFee] > 0u)
240 return tecNO_PERMISSION;
241
242 // Encryption keys can only be set if confidential amounts are already
243 // enabled on the issuance OR if the transaction is enabling it
244 if (ctx.tx.isFieldPresent(sfIssuerEncryptionKey) &&
245 !sleMptIssuance->isFlag(lsfMPTCanHoldConfidentialBalance) && !enablesConfidentialBalance)
246 {
247 return tecNO_PERMISSION;
248 }
249
250 if (ctx.tx.isFieldPresent(sfAuditorEncryptionKey) &&
251 !sleMptIssuance->isFlag(lsfMPTCanHoldConfidentialBalance) && !enablesConfidentialBalance)
252 {
253 return tecNO_PERMISSION;
254 }
255
256 // cannot upload key if there's circulating supply of COA
257 if ((ctx.tx.isFieldPresent(sfIssuerEncryptionKey) ||
258 ctx.tx.isFieldPresent(sfAuditorEncryptionKey) || enablesConfidentialBalance) &&
259 (*sleMptIssuance)[~sfConfidentialOutstandingAmount].value_or(0) > 0)
260 {
261 return tecNO_PERMISSION; // LCOV_EXCL_LINE
262 }
263
264 return tesSUCCESS;
265}
266
267TER
269{
270 auto const mptIssuanceID = ctx_.tx[sfMPTokenIssuanceID];
271 auto const holderID = ctx_.tx[~sfHolder];
272 auto const domainID = ctx_.tx[~sfDomainID];
273 SLE::pointer sle;
274
275 if (holderID)
276 {
277 sle = view().peek(keylet::mptoken(mptIssuanceID, *holderID));
278 }
279 else
280 {
281 sle = view().peek(keylet::mptokenIssuance(mptIssuanceID));
282 }
283
284 if (!sle)
285 return tecINTERNAL; // LCOV_EXCL_LINE
286
287 std::uint32_t const flagsIn = sle->getFieldU32(sfFlags);
288 std::uint32_t flagsOut = flagsIn;
289
290 if (ctx_.tx.isFlag(tfMPTLock))
291 {
292 flagsOut |= lsfMPTLocked;
293 }
294 else if (ctx_.tx.isFlag(tfMPTUnlock))
295 {
296 flagsOut &= ~lsfMPTLocked;
297 }
298
299 if (auto const enableFlags = (ctx_.tx.getFlags() & tfMPTokenIssuanceSetEnableFlagMask);
300 enableFlags != 0u)
301 {
302 for (auto const& f : flagMapping)
303 {
304 if (ctx_.tx.isFlag(f.setFlag))
305 {
306 flagsOut |= f.ledgerFlag;
307 }
308 }
309 }
310
311 if (flagsIn != flagsOut)
312 sle->setFieldU32(sfFlags, flagsOut);
313
314 if (auto const immutableFlags = ctx_.tx[~sfImmutableFlags])
315 {
316 // sle is guaranteed to be an ltMPTOKEN_ISSUANCE rather than an ltMPTOKEN.
317 // Preflight verification ensures that sfHolder and sfImmutableFlags can
318 // never both be present in the same transaction. Therefore, if
319 // sfImmutableFlags is present, sfHolder must be absent.
320 //
321 // In doApply, the absence of sfHolder causes the MPTokenIssuance keylet
322 // to be peeked. The runtime check below is a defensive fallback in case
323 // this invariant is ever broken by a future change.
324 XRPL_ASSERT(
325 sle->getType() == ltMPTOKEN_ISSUANCE,
326 "MPTokenIssuanceSet::doApply : modifying MPTokenIssuance");
327
328 if (sle->getType() != ltMPTOKEN_ISSUANCE)
329 return tecINTERNAL; // LCOV_EXCL_LINE
330
331 (*sle)[sfImmutableFlags] = (*sle)[sfImmutableFlags] | *immutableFlags;
332 }
333
334 if (auto const transferFee = ctx_.tx[~sfTransferFee])
335 {
336 // TransferFee uses soeDEFAULT style:
337 // - If the field is absent, it is interpreted as 0.
338 // - If the field is present, it must be non-zero.
339 // Therefore, when TransferFee is 0, the field should be removed.
340 if (transferFee == 0)
341 {
342 sle->makeFieldAbsent(sfTransferFee);
343 }
344 else
345 {
346 sle->setFieldU16(sfTransferFee, *transferFee);
347 }
348 }
349
350 if (auto const metadata = ctx_.tx[~sfMPTokenMetadata])
351 {
352 if (metadata->empty())
353 {
354 sle->makeFieldAbsent(sfMPTokenMetadata);
355 }
356 else
357 {
358 sle->setFieldVL(sfMPTokenMetadata, *metadata);
359 }
360 }
361
362 if (domainID)
363 {
364 // This is enforced in preflight.
365 XRPL_ASSERT(
366 sle->getType() == ltMPTOKEN_ISSUANCE,
367 "MPTokenIssuanceSet::doApply : modifying MPTokenIssuance");
368
369 if (*domainID != beast::kZero)
370 {
371 sle->setFieldH256(sfDomainID, *domainID);
372 }
373 else
374 {
375 if (sle->isFieldPresent(sfDomainID))
376 sle->makeFieldAbsent(sfDomainID);
377 }
378 }
379
380 if (auto const pubKey = ctx_.tx[~sfIssuerEncryptionKey])
381 {
382 // This is enforced in preflight.
383 XRPL_ASSERT(
384 sle->getType() == ltMPTOKEN_ISSUANCE,
385 "MPTokenIssuanceSet::doApply : modifying MPTokenIssuance");
386
387 sle->setFieldVL(sfIssuerEncryptionKey, *pubKey);
388 }
389
390 if (auto const pubKey = ctx_.tx[~sfAuditorEncryptionKey])
391 {
392 // This is enforced in preflight.
393 XRPL_ASSERT(
394 sle->getType() == ltMPTOKEN_ISSUANCE,
395 "MPTokenIssuanceSet::doApply : modifying MPTokenIssuance");
396
397 sle->setFieldVL(sfAuditorEncryptionKey, *pubKey);
398 }
399
400 view().update(sle);
401
402 return tesSUCCESS;
403}
404
405void
407{
408 // No transaction-specific invariants yet (future work).
409}
410
411bool
413 STTx const&,
414 TER,
415 XRPAmount,
416 ReadView const&,
417 beast::Journal const&)
418{
419 // No transaction-specific invariants yet (future work).
420 return true;
421}
422
423} // namespace xrpl
T any_of(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
static NotTEC preflight(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static constexpr std::array< FlagMapping, 7 > flagMapping
A view into a ledger.
Definition ReadView.h:41
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 SLE::const_pointer 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:180
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry const > const & const_ref
bool isFlag(std::uint32_t) const
Definition STObject.cpp:511
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
std::uint32_t getFlags() const
Definition STObject.cpp:517
ApplyView & view()
Definition Transactor.h:175
ApplyContext & ctx_
Definition Transactor.h:153
constexpr Zero kZero
Definition Zero.h:30
Keylet permissionedDomain(AccountID const &account, SeqProxy const &seq) noexcept
Definition Indexes.cpp:579
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:543
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:198
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:537
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isValidCompressedECPoint(Slice const &buffer)
Verifies that a buffer contains a valid, parsable compressed EC point.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:607
constexpr FlagValue tfMPTokenIssuanceSetEnableFlagMask
Definition TxFlags.h:383
constexpr std::size_t kMaxMpTokenMetadataLength
The maximum length of MPTokenMetadata.
Definition Protocol.h:291
constexpr FlagValue tifMPTokenIssuanceImmutableMask
Definition TxFlags.h:377
constexpr std::uint16_t kMaxTransferFee
The maximum token transfer fee allowed.
Definition Protocol.h:96
constexpr FlagValue tifMPTCanHoldConfidentialBalance
Definition TxFlags.h:376
constexpr std::uint32_t lsifMPTMetadata
@ temINVALID_FLAG
Definition TER.h:99
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
@ temBAD_TRANSFER_FEE
Definition TER.h:130
TERSubset< CanCvtToTER > TER
Definition TER.h:647
@ tecOBJECT_NOT_FOUND
Definition TER.h:329
@ tecINTERNAL
Definition TER.h:313
@ tecNO_PERMISSION
Definition TER.h:308
@ tecNO_DST
Definition TER.h:293
@ tesSUCCESS
Definition TER.h:245
constexpr std::uint32_t lsifMPTTransferFee
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:83
ReadView const & view
Definition Transactor.h:86
State information when preflighting a tx.
Definition Transactor.h:38