1#include <xrpl/tx/transactors/token/ConfidentialMPTClawback.h>
3#include <xrpl/beast/utility/Journal.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/core/ServiceRegistry.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/protocol/ConfidentialTransfer.h>
8#include <xrpl/protocol/Feature.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/LedgerFormats.h>
11#include <xrpl/protocol/Protocol.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STTx.h>
14#include <xrpl/protocol/TER.h>
15#include <xrpl/protocol/XRPAmount.h>
16#include <xrpl/tx/Transactor.h>
26 auto const account = ctx.
tx[sfAccount];
33 if (account == ctx.
tx[sfHolder])
37 auto const clawAmount = ctx.
tx[sfMPTAmount];
58 auto const account = ctx.
tx[sfAccount];
63 auto const holder = ctx.
tx[sfHolder];
68 auto const mptIssuanceID = ctx.
tx[sfMPTokenIssuanceID];
74 if (sleIssuance->getAccountID(sfIssuer) != account)
78 "xrpl::ConfidentialMPTClawback::preclaim : preflight already validated the "
79 "submitter is the issuer");
85 if (!sleIssuance->isFieldPresent(sfIssuerEncryptionKey))
89 if (!sleIssuance->isFlag(lsfMPTCanClawback))
93 if (!sleIssuance->isFlag(lsfMPTCanHoldConfidentialBalance))
98 if (!sleHolderMPToken)
102 if (!sleHolderMPToken->isFieldPresent(sfIssuerEncryptedBalance))
106 if (!sleHolderMPToken->isFieldPresent(sfHolderEncryptionKey))
111 auto const amount = ctx.
tx[sfMPTAmount];
112 if (amount > (*sleIssuance)[~sfConfidentialOutstandingAmount].value_or(0) ||
113 amount > (*sleIssuance)[sfOutstandingAmount])
116 auto const contextHash =
124 (*sleIssuance)[sfIssuerEncryptionKey],
125 (*sleHolderMPToken)[sfIssuerEncryptedBalance],
132 auto const mptIssuanceID =
ctx_.tx[sfMPTokenIssuanceID];
133 auto const holder =
ctx_.tx[sfHolder];
138 if (!sleIssuance || !sleHolderMPToken)
142 "xrpl::ConfidentialMPTClawback::doApply : preclaim already validated these "
148 auto const clawAmount =
ctx_.tx[sfMPTAmount];
150 auto const holderPubKey = (*sleHolderMPToken)[sfHolderEncryptionKey];
151 auto const issuerPubKey = (*sleIssuance)[sfIssuerEncryptionKey];
155 if (!encZeroForHolder)
159 "xrpl::ConfidentialMPTClawback::doApply : canonical zero encryption cannot fail "
160 "for an already-valid holder public key");
166 if (!encZeroForIssuer)
170 "xrpl::ConfidentialMPTClawback::doApply : canonical zero encryption cannot fail "
171 "for an already-valid issuer public key");
177 (*sleHolderMPToken)[sfConfidentialBalanceInbox] = *encZeroForHolder;
178 (*sleHolderMPToken)[sfConfidentialBalanceSpending] = *encZeroForHolder;
179 (*sleHolderMPToken)[sfIssuerEncryptedBalance] = std::move(*encZeroForIssuer);
182 if (sleHolderMPToken->isFieldPresent(sfAuditorEncryptedBalance))
186 if (!sleIssuance->isFieldPresent(sfAuditorEncryptionKey))
190 "xrpl::ConfidentialMPTClawback::doApply : the holder's auditor balance implies "
191 "the issuance has an auditor public key");
196 auto const auditorPubKey = (*sleIssuance)[sfAuditorEncryptionKey];
200 if (!encZeroForAuditor)
204 "xrpl::ConfidentialMPTClawback::doApply : canonical zero encryption cannot "
205 "fail for an already-valid auditor public key");
210 (*sleHolderMPToken)[sfAuditorEncryptedBalance] = std::move(*encZeroForAuditor);
216 if (
view().rules().enabled(featureConfidentialMPTKeyRotation))
220 auto const oldCOA = (*sleIssuance)[sfConfidentialOutstandingAmount];
221 if (clawAmount > oldCOA)
223 (*sleIssuance)[sfConfidentialOutstandingAmount] = oldCOA - clawAmount;
226 auto const oldOA = (*sleIssuance)[sfOutstandingAmount];
227 if (clawAmount > oldOA)
229 (*sleIssuance)[sfOutstandingAmount] = oldOA - clawAmount;
A generic endpoint for log messages.
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 TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
void visitInvariantEntry(bool isDelete, std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after) override
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.
AccountID const & getIssuer() const
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.
SeqProxy getSeqProxy() const
constexpr std::uint32_t value() const
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void setMirrorEpochs(SLE const &issuance, SLE &mptoken)
Set the holder's MPToken mirror epochs to match the issuance's current key epochs.
std::optional< Buffer > encryptCanonicalZeroAmount(Slice const &pubKeySlice, AccountID const &account, MPTID const &mptId)
Generates the canonical zero encryption for a specific MPToken.
constexpr std::uint32_t kConfidentialFeeMultiplier
Extra base fee multiplier charged to confidential MPT transactions.
constexpr std::size_t kEcClawbackProofLength
Length of the ZKProof for ConfidentialMPTClawback.
TER verifyClawbackProof(uint64_t const amount, Slice const &proof, Slice const &pubKeySlice, Slice const &ciphertext, UInt256 const &contextHash)
Verifies a compact sigma clawback proof.
TERSubset< CanCvtToNotTEC > NotTEC
UInt256 getClawbackContextHash(AccountID const &account, UInt192 const &issuanceID, std::uint32_t sequence, AccountID const &holder)
Generates the context hash for ConfidentialMPTClawback transactions.
TERSubset< CanCvtToTER > TER
void incrementConfidentialVersion(STObject &mptoken)
Increments the confidential balance version counter on an MPToken.
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.