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/Indexes.h>
9#include <xrpl/protocol/LedgerFormats.h>
10#include <xrpl/protocol/Protocol.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/STTx.h>
13#include <xrpl/protocol/TER.h>
14#include <xrpl/protocol/XRPAmount.h>
15#include <xrpl/tx/Transactor.h>
25 auto const account = ctx.
tx[sfAccount];
32 if (account == ctx.
tx[sfHolder])
36 auto const clawAmount = ctx.
tx[sfMPTAmount];
57 auto const account = ctx.
tx[sfAccount];
62 auto const holder = ctx.
tx[sfHolder];
67 auto const mptIssuanceID = ctx.
tx[sfMPTokenIssuanceID];
73 if (sleIssuance->getAccountID(sfIssuer) != account)
77 "xrpl::ConfidentialMPTClawback::preclaim : preflight already validated the "
78 "submitter is the issuer");
84 if (!sleIssuance->isFieldPresent(sfIssuerEncryptionKey))
88 if (!sleIssuance->isFlag(lsfMPTCanClawback))
92 if (!sleIssuance->isFlag(lsfMPTCanHoldConfidentialBalance))
97 if (!sleHolderMPToken)
101 if (!sleHolderMPToken->isFieldPresent(sfIssuerEncryptedBalance))
105 if (!sleHolderMPToken->isFieldPresent(sfHolderEncryptionKey))
110 auto const amount = ctx.
tx[sfMPTAmount];
111 if (amount > (*sleIssuance)[~sfConfidentialOutstandingAmount].value_or(0) ||
112 amount > (*sleIssuance)[sfOutstandingAmount])
115 auto const contextHash =
123 (*sleIssuance)[sfIssuerEncryptionKey],
124 (*sleHolderMPToken)[sfIssuerEncryptedBalance],
131 auto const mptIssuanceID =
ctx_.tx[sfMPTokenIssuanceID];
132 auto const holder =
ctx_.tx[sfHolder];
137 if (!sleIssuance || !sleHolderMPToken)
141 "xrpl::ConfidentialMPTClawback::doApply : preclaim already validated these "
147 auto const clawAmount =
ctx_.tx[sfMPTAmount];
149 auto const holderPubKey = (*sleHolderMPToken)[sfHolderEncryptionKey];
150 auto const issuerPubKey = (*sleIssuance)[sfIssuerEncryptionKey];
154 if (!encZeroForHolder)
158 "xrpl::ConfidentialMPTClawback::doApply : canonical zero encryption cannot fail "
159 "for an already-valid holder public key");
165 if (!encZeroForIssuer)
169 "xrpl::ConfidentialMPTClawback::doApply : canonical zero encryption cannot fail "
170 "for an already-valid issuer public key");
176 (*sleHolderMPToken)[sfConfidentialBalanceInbox] = *encZeroForHolder;
177 (*sleHolderMPToken)[sfConfidentialBalanceSpending] = *encZeroForHolder;
178 (*sleHolderMPToken)[sfIssuerEncryptedBalance] = std::move(*encZeroForIssuer);
181 if (sleHolderMPToken->isFieldPresent(sfAuditorEncryptedBalance))
185 if (!sleIssuance->isFieldPresent(sfAuditorEncryptionKey))
189 "xrpl::ConfidentialMPTClawback::doApply : the holder's auditor balance implies "
190 "the issuance has an auditor public key");
195 auto const auditorPubKey = (*sleIssuance)[sfAuditorEncryptionKey];
199 if (!encZeroForAuditor)
203 "xrpl::ConfidentialMPTClawback::doApply : canonical zero encryption cannot "
204 "fail for an already-valid auditor public key");
209 (*sleHolderMPToken)[sfAuditorEncryptedBalance] = std::move(*encZeroForAuditor);
213 auto const oldCOA = (*sleIssuance)[sfConfidentialOutstandingAmount];
214 if (clawAmount > oldCOA)
216 (*sleIssuance)[sfConfidentialOutstandingAmount] = oldCOA - clawAmount;
219 auto const oldOA = (*sleIssuance)[sfOutstandingAmount];
220 if (clawAmount > oldOA)
222 (*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.
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.
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.
TER verifyClawbackProof(uint64_t const amount, Slice const &proof, Slice const &pubKeySlice, Slice const &ciphertext, uint256 const &contextHash)
Verifies a compact sigma clawback proof.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.