xrpld
Loading...
Searching...
No Matches
ConfidentialMPTMergeInbox.cpp
1#include <xrpl/tx/transactors/token/ConfidentialMPTMergeInbox.h>
2
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>
18
19#include <memory>
20#include <utility>
21
22namespace xrpl {
23
26{
27 if (!ctx.rules.enabled(featureConfidentialTransfer))
28 return temDISABLED;
29
30 // issuer cannot merge
31 if (MPTIssue(ctx.tx[sfMPTokenIssuanceID]).getIssuer() == ctx.tx[sfAccount])
32 return temMALFORMED;
33
34 return tesSUCCESS;
35}
36
42
43TER
45{
46 auto const sleIssuance = ctx.view.read(keylet::mptokenIssuance(ctx.tx[sfMPTokenIssuanceID]));
47 if (!sleIssuance)
49
50 if (!sleIssuance->isFlag(lsfMPTCanHoldConfidentialBalance))
51 return tecNO_PERMISSION;
52
53 // already checked in preflight, but should also check that issuer on the
54 // issuance isn't the account either
55 if (sleIssuance->getAccountID(sfIssuer) == ctx.tx[sfAccount])
56 return tefINTERNAL; // LCOV_EXCL_LINE
57
58 auto const sleMptoken =
59 ctx.view.read(keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], ctx.tx[sfAccount]));
60 if (!sleMptoken)
62
63 if (!sleMptoken->isFieldPresent(sfConfidentialBalanceInbox) ||
64 !sleMptoken->isFieldPresent(sfConfidentialBalanceSpending) ||
65 !sleMptoken->isFieldPresent(sfHolderEncryptionKey))
66 {
67 return tecNO_PERMISSION;
68 }
69
70 // Check lock
71 auto const account = ctx.tx[sfAccount];
72 MPTIssue const mptIssue(ctx.tx[sfMPTokenIssuanceID]);
73 if (auto const ter = checkFrozen(ctx.view, account, mptIssue); !isTesSuccess(ter))
74 return ter;
75
76 // Check auth
77 if (auto const ter = requireAuth(ctx.view, mptIssue, account); !isTesSuccess(ter))
78 return ter;
79
80 return tesSUCCESS;
81}
82
83TER
85{
86 auto const mptIssuanceID = ctx_.tx[sfMPTokenIssuanceID];
87 auto sleMptoken = view().peek(keylet::mptoken(mptIssuanceID, accountID_));
88 if (!sleMptoken)
89 return tecINTERNAL; // LCOV_EXCL_LINE
90
91 // sanity check
92 if (!sleMptoken->isFieldPresent(sfConfidentialBalanceSpending) ||
93 !sleMptoken->isFieldPresent(sfConfidentialBalanceInbox) ||
94 !sleMptoken->isFieldPresent(sfHolderEncryptionKey))
95 {
96 return tecINTERNAL; // LCOV_EXCL_LINE
97 }
98
99 // Merge inbox into spending: spending = spending + inbox
100 // This allows holder to use received funds. Without merging, incoming
101 // transfers sit in inbox and cannot be spent or converted back.
102 auto sum = homomorphicAdd(
103 (*sleMptoken)[sfConfidentialBalanceSpending], (*sleMptoken)[sfConfidentialBalanceInbox]);
104 if (!sum)
105 {
106 // LCOV_EXCL_START
107 JLOG(ctx_.journal.error())
108 << "ConfidentialMPTMergeInbox failed homomorphic add for inbox merge.";
109 return tecINTERNAL;
110 // LCOV_EXCL_STOP
111 }
112
113 (*sleMptoken)[sfConfidentialBalanceSpending] = std::move(*sum);
114
115 // Reset inbox to encrypted zero. Must use canonical zero encryption
116 // (deterministic ciphertext) so the ledger state is reproducible.
117 auto zeroEncryption =
118 encryptCanonicalZeroAmount((*sleMptoken)[sfHolderEncryptionKey], accountID_, mptIssuanceID);
119
120 if (!zeroEncryption)
121 return tecINTERNAL; // LCOV_EXCL_LINE
122
123 (*sleMptoken)[sfConfidentialBalanceInbox] = std::move(*zeroEncryption);
124
125 incrementConfidentialVersion(*sleMptoken);
126
127 view().update(sleMptoken);
128 return tesSUCCESS;
129}
130
131void
138
139bool
141 STTx const&,
142 TER,
143 XRPAmount,
144 ReadView const&,
145 beast::Journal const&)
146{
147 return true;
148}
149
150} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
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)
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 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
static NotTEC preflight(PreflightContext const &ctx)
AccountID const & getIssuer() const
Definition MPTIssue.cpp:29
A view into a ledger.
Definition ReadView.h:31
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:171
ApplyView & view()
Definition Transactor.h:136
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
AccountID const accountID_
Definition Transactor.h:120
ApplyContext & ctx_
Definition Transactor.h:116
Keylet mptokenIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:521
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:533
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static auto sum(TCollection const &col)
Definition BookStep.cpp:993
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.
Definition Protocol.h:364
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
@ tefINTERNAL
Definition TER.h:163
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
@ temMALFORMED
Definition TER.h:73
@ temDISABLED
Definition TER.h:100
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
TERSubset< CanCvtToTER > TER
Definition TER.h:634
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.
@ tecOBJECT_NOT_FOUND
Definition TER.h:324
@ tecINTERNAL
Definition TER.h:308
@ tecNO_PERMISSION
Definition TER.h:303
void incrementConfidentialVersion(STObject &mptoken)
Increments the confidential balance version counter on an MPToken.
std::optional< Buffer > homomorphicAdd(Slice const &a, Slice const &b)
Homomorphically adds two ElGamal ciphertexts.
@ tesSUCCESS
Definition TER.h:240
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
State information when preflighting a tx.
Definition Transactor.h:18