1#include <xrpl/tx/transactors/token/ConfidentialMPTConvertBack.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/core/ServiceRegistry.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/ledger/helpers/TokenHelpers.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/Protocol.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STTx.h>
15#include <xrpl/protocol/TER.h>
16#include <xrpl/protocol/XRPAmount.h>
17#include <xrpl/tx/Transactor.h>
78 if (!mptoken->isFieldPresent(sfHolderEncryptionKey))
81 auto const mptIssuanceID = tx[sfMPTokenIssuanceID];
82 auto const account = tx[sfAccount];
83 auto const amount = tx[sfMPTAmount];
84 auto const blindingFactor = tx[sfBlindingFactor];
85 auto const holderPubKey = (*mptoken)[sfHolderEncryptionKey];
91 (*mptoken)[~sfConfidentialBalanceVersion].value_or(0));
95 bool const hasAuditor = issuance->isFieldPresent(sfAuditorEncryptionKey);
100 .publicKey = (*issuance)[sfAuditorEncryptionKey],
101 .encryptedAmount = tx[sfAuditorEncryptedAmount],
111 Slice(blindingFactor.data(), blindingFactor.size()),
113 .publicKey = holderPubKey,
114 .encryptedAmount = tx[sfHolderEncryptedAmount],
117 .publicKey = (*issuance)[sfIssuerEncryptionKey],
118 .encryptedAmount = tx[sfIssuerEncryptedAmount],
129 (*mptoken)[sfConfidentialBalanceSpending],
130 tx[sfBalanceCommitment],
147 auto const mptIssuanceID = ctx.
tx[sfMPTokenIssuanceID];
148 auto const account = ctx.
tx[sfAccount];
149 auto const amount = ctx.
tx[sfMPTAmount];
156 if (!sleIssuance->isFlag(lsfMPTCanHoldConfidentialBalance) ||
157 !sleIssuance->isFieldPresent(sfIssuerEncryptionKey))
161 bool const requiresAuditor = sleIssuance->isFieldPresent(sfAuditorEncryptionKey);
165 if (requiresAuditor && !hasAuditor)
170 if (!requiresAuditor && hasAuditor)
175 if (sleIssuance->getAccountID(sfIssuer) == account)
182 if (!sleMptoken->isFieldPresent(sfHolderEncryptionKey) ||
183 !sleMptoken->isFieldPresent(sfConfidentialBalanceSpending) ||
184 !sleMptoken->isFieldPresent(sfIssuerEncryptedBalance))
191 if (requiresAuditor && !sleMptoken->isFieldPresent(sfAuditorEncryptedBalance))
197 if ((*sleIssuance)[~sfConfidentialOutstandingAmount].value_or(0) < amount)
201 MPTIssue const mptIssue(mptIssuanceID);
218 auto const mptIssuanceID =
ctx_.tx[sfMPTokenIssuanceID];
228 auto const amtToConvertBack =
ctx_.tx[sfMPTAmount];
229 auto const amt = (*sleMptoken)[~sfMPTAmount].valueOr(0);
235 (*sleMptoken)[sfMPTAmount] = amt + amtToConvertBack;
237 auto const coa = (*sleIssuance)[~sfConfidentialOutstandingAmount].valueOr(0);
238 if (coa < amtToConvertBack)
240 (*sleIssuance)[sfConfidentialOutstandingAmount] = coa - amtToConvertBack;
247 (*sleMptoken)[sfConfidentialBalanceSpending],
ctx_.tx[sfHolderEncryptedAmount]);
251 JLOG(
ctx_.journal.error())
252 <<
"ConfidentialMPTConvertBack failed homomorphic subtract for holder spending "
258 (*sleMptoken)[sfConfidentialBalanceSpending] = std::move(*res);
264 (*sleMptoken)[sfIssuerEncryptedBalance],
ctx_.tx[sfIssuerEncryptedAmount]);
268 JLOG(
ctx_.journal.error())
269 <<
"ConfidentialMPTConvertBack failed homomorphic subtract for issuer balance.";
274 (*sleMptoken)[sfIssuerEncryptedBalance] = std::move(*res);
280 (*sleMptoken)[sfAuditorEncryptedBalance],
ctx_.tx[sfAuditorEncryptedAmount]);
284 JLOG(
ctx_.journal.error())
285 <<
"ConfidentialMPTConvertBack failed homomorphic subtract for auditor balance.";
290 (*sleMptoken)[sfAuditorEncryptedBalance] = std::move(*res);
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.
void visitInvariantEntry(bool isDelete, std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after) override
static TER preclaim(PreclaimContext const &ctx)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
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.
static NotTEC preflight(PreflightContext const &ctx)
AccountID const & getIssuer() const
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.
bool isFieldPresent(SField const &field) const
SeqProxy getSeqProxy() const
constexpr std::uint32_t value() const
An immutable linear range of bytes.
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
AccountID const accountID_
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Keylet mptokenIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
NotTEC checkEncryptedAmountFormat(STObject const &object)
Validates the format of encrypted amount fields in a transaction.
constexpr std::uint32_t kConfidentialFeeMultiplier
Extra base fee multiplier charged to confidential MPT transactions.
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
bool isValidCompressedECPoint(Slice const &buffer)
Verifies that a buffer contains a valid, parsable compressed EC point.
TER verifyRevealedAmount(uint64_t const amount, Slice const &blindingFactor, ConfidentialRecipient const &holder, ConfidentialRecipient const &issuer, std::optional< ConfidentialRecipient > const &auditor)
Verifies revealed amount encryptions for all recipients.
constexpr std::size_t kEcConvertBackProofLength
128 bytes compact sigma proof + 688 bytes single bulletproof.
uint256 getConvertBackContextHash(AccountID const &account, uint192 const &issuanceID, std::uint32_t sequence, std::uint32_t version)
Generates the context hash for ConfidentialMPTConvertBack transactions.
TERSubset< CanCvtToNotTEC > NotTEC
std::optional< Buffer > homomorphicSubtract(Slice const &a, Slice const &b)
Homomorphically subtracts two ElGamal ciphertexts.
TER verifyConvertBackProof(Slice const &proof, Slice const &pubKeySlice, Slice const &spendingBalance, Slice const &balanceCommitment, uint64_t amount, uint256 const &contextHash)
Verifies all zero-knowledge proofs for a ConfidentialMPTConvertBack transaction.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
void incrementConfidentialVersion(STObject &mptoken)
Increments the confidential balance version counter on an MPToken.
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
static TER verifyProofs(STTx const &tx, std::shared_ptr< SLE const > const &issuance, std::shared_ptr< SLE const > const &mptoken)
Verifies the cryptographic proofs for a ConvertBack transaction.
Bundles an ElGamal public key with its associated encrypted amount.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.