xrpld
Loading...
Searching...
No Matches
ConfidentialMPTClawback.cpp
1#include <xrpl/tx/transactors/token/ConfidentialMPTClawback.h>
2
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>
16
17#include <memory>
18#include <utility>
19
20namespace xrpl {
21
24{
25 auto const account = ctx.tx[sfAccount];
26
27 // Only issuer can clawback
28 if (account != MPTIssue(ctx.tx[sfMPTokenIssuanceID]).getIssuer())
29 return temMALFORMED;
30
31 // Cannot clawback from self
32 if (account == ctx.tx[sfHolder])
33 return temMALFORMED;
34
35 // Check invalid claw amount
36 auto const clawAmount = ctx.tx[sfMPTAmount];
37 if (clawAmount == 0 || clawAmount > kMaxMpTokenAmount)
38 return temBAD_AMOUNT;
39
40 // Verify proof length
41 if (ctx.tx[sfZKProof].length() != kEcClawbackProofLength)
42 return temMALFORMED;
43
44 return tesSUCCESS;
45}
46
52
53TER
55{
56 // Check if sender account exists
57 auto const account = ctx.tx[sfAccount];
58 if (!ctx.view.exists(keylet::account(account)))
59 return terNO_ACCOUNT;
60
61 // Check if holder account exists
62 auto const holder = ctx.tx[sfHolder];
63 if (!ctx.view.exists(keylet::account(holder)))
64 return tecNO_TARGET;
65
66 // Check if MPT issuance exists
67 auto const mptIssuanceID = ctx.tx[sfMPTokenIssuanceID];
68 auto const sleIssuance = ctx.view.read(keylet::mptokenIssuance(mptIssuanceID));
69 if (!sleIssuance)
71
72 // Sanity check: account must be the same as issuer
73 if (sleIssuance->getAccountID(sfIssuer) != account)
74 {
75 // LCOV_EXCL_START
76 UNREACHABLE(
77 "xrpl::ConfidentialMPTClawback::preclaim : preflight already validated the "
78 "submitter is the issuer");
79 return tefINTERNAL;
80 // LCOV_EXCL_STOP
81 }
82
83 // Check if issuance has issuer ElGamal public key
84 if (!sleIssuance->isFieldPresent(sfIssuerEncryptionKey))
85 return tecNO_PERMISSION;
86
87 // Check if clawback is allowed
88 if (!sleIssuance->isFlag(lsfMPTCanClawback))
89 return tecNO_PERMISSION;
90
91 // Check if issuance allows confidential transfer
92 if (!sleIssuance->isFlag(lsfMPTCanHoldConfidentialBalance))
93 return tecNO_PERMISSION;
94
95 // Check holder's MPToken
96 auto const sleHolderMPToken = ctx.view.read(keylet::mptoken(mptIssuanceID, holder));
97 if (!sleHolderMPToken)
99
100 // Check if holder has confidential balances to claw back
101 if (!sleHolderMPToken->isFieldPresent(sfIssuerEncryptedBalance))
102 return tecNO_PERMISSION;
103
104 // Check if Holder has ElGamal public Key
105 if (!sleHolderMPToken->isFieldPresent(sfHolderEncryptionKey))
106 return tecNO_PERMISSION;
107
108 // Sanity check: claw amount can not exceed confidential outstanding amount
109 // or total outstanding amount (prevents underflow in doApply)
110 auto const amount = ctx.tx[sfMPTAmount];
111 if (amount > (*sleIssuance)[~sfConfidentialOutstandingAmount].value_or(0) ||
112 amount > (*sleIssuance)[sfOutstandingAmount])
114
115 auto const contextHash =
116 getClawbackContextHash(account, mptIssuanceID, ctx.tx.getSeqProxy().value(), holder);
117
118 // Verify the revealed confidential amount by the issuer matches the exact
119 // confidential balance of the holder.
120 return verifyClawbackProof(
121 amount,
122 ctx.tx[sfZKProof],
123 (*sleIssuance)[sfIssuerEncryptionKey],
124 (*sleHolderMPToken)[sfIssuerEncryptedBalance],
125 contextHash);
126}
127
128TER
130{
131 auto const mptIssuanceID = ctx_.tx[sfMPTokenIssuanceID];
132 auto const holder = ctx_.tx[sfHolder];
133
134 auto sleIssuance = view().peek(keylet::mptokenIssuance(mptIssuanceID));
135 auto sleHolderMPToken = view().peek(keylet::mptoken(mptIssuanceID, holder));
136
137 if (!sleIssuance || !sleHolderMPToken)
138 {
139 // LCOV_EXCL_START
140 UNREACHABLE(
141 "xrpl::ConfidentialMPTClawback::doApply : preclaim already validated these "
142 "objects exist");
143 return tecINTERNAL;
144 // LCOV_EXCL_STOP
145 }
146
147 auto const clawAmount = ctx_.tx[sfMPTAmount];
148
149 auto const holderPubKey = (*sleHolderMPToken)[sfHolderEncryptionKey];
150 auto const issuerPubKey = (*sleIssuance)[sfIssuerEncryptionKey];
151
152 // After clawback, the balance should be encrypted zero.
153 auto const encZeroForHolder = encryptCanonicalZeroAmount(holderPubKey, holder, mptIssuanceID);
154 if (!encZeroForHolder)
155 {
156 // LCOV_EXCL_START
157 UNREACHABLE(
158 "xrpl::ConfidentialMPTClawback::doApply : canonical zero encryption cannot fail "
159 "for an already-valid holder public key");
160 return tecINTERNAL;
161 // LCOV_EXCL_STOP
162 }
163
164 auto encZeroForIssuer = encryptCanonicalZeroAmount(issuerPubKey, holder, mptIssuanceID);
165 if (!encZeroForIssuer)
166 {
167 // LCOV_EXCL_START
168 UNREACHABLE(
169 "xrpl::ConfidentialMPTClawback::doApply : canonical zero encryption cannot fail "
170 "for an already-valid issuer public key");
171 return tecINTERNAL;
172 // LCOV_EXCL_STOP
173 }
174
175 // Set holder's confidential balances to encrypted zero
176 (*sleHolderMPToken)[sfConfidentialBalanceInbox] = *encZeroForHolder;
177 (*sleHolderMPToken)[sfConfidentialBalanceSpending] = *encZeroForHolder;
178 (*sleHolderMPToken)[sfIssuerEncryptedBalance] = std::move(*encZeroForIssuer);
179 incrementConfidentialVersion(*sleHolderMPToken);
180
181 if (sleHolderMPToken->isFieldPresent(sfAuditorEncryptedBalance))
182 {
183 // Sanity check: the issuance must have an auditor public key if
184 // auditing is enabled.
185 if (!sleIssuance->isFieldPresent(sfAuditorEncryptionKey))
186 {
187 // LCOV_EXCL_START
188 UNREACHABLE(
189 "xrpl::ConfidentialMPTClawback::doApply : the holder's auditor balance implies "
190 "the issuance has an auditor public key");
191 return tecINTERNAL;
192 // LCOV_EXCL_STOP
193 }
194
195 auto const auditorPubKey = (*sleIssuance)[sfAuditorEncryptionKey];
196
197 auto encZeroForAuditor = encryptCanonicalZeroAmount(auditorPubKey, holder, mptIssuanceID);
198
199 if (!encZeroForAuditor)
200 {
201 // LCOV_EXCL_START
202 UNREACHABLE(
203 "xrpl::ConfidentialMPTClawback::doApply : canonical zero encryption cannot "
204 "fail for an already-valid auditor public key");
205 return tecINTERNAL;
206 // LCOV_EXCL_STOP
207 }
208
209 (*sleHolderMPToken)[sfAuditorEncryptedBalance] = std::move(*encZeroForAuditor);
210 }
211
212 // Decrease Global Confidential Outstanding Amount
213 auto const oldCOA = (*sleIssuance)[sfConfidentialOutstandingAmount];
214 if (clawAmount > oldCOA)
215 return tecINTERNAL; // LCOV_EXCL_LINE
216 (*sleIssuance)[sfConfidentialOutstandingAmount] = oldCOA - clawAmount;
217
218 // Decrease Global Total Outstanding Amount
219 auto const oldOA = (*sleIssuance)[sfOutstandingAmount];
220 if (clawAmount > oldOA)
221 return tecINTERNAL; // LCOV_EXCL_LINE
222 (*sleIssuance)[sfOutstandingAmount] = oldOA - clawAmount;
223
224 view().update(sleHolderMPToken);
225 view().update(sleIssuance);
226
227 return tesSUCCESS;
228}
229
230void
237
238bool
240 STTx const&,
241 TER,
242 XRPAmount,
243 ReadView const&,
244 beast::Journal const&)
245{
246 return true;
247}
248
249} // namespace xrpl
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 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
Definition MPTIssue.cpp:29
A view into a ledger.
Definition ReadView.h:41
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
Definition STTx.cpp:199
constexpr std::uint32_t value() const
Definition SeqProxy.h:80
ApplyView & view()
Definition Transactor.h:175
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
ApplyContext & ctx_
Definition Transactor.h:153
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
@ terNO_ACCOUNT
Definition TER.h:213
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:534
@ tefINTERNAL
Definition TER.h:165
constexpr std::size_t kEcClawbackProofLength
Length of the ZKProof for ConfidentialMPTClawback.
Definition Protocol.h:529
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:607
uint256 getClawbackContextHash(AccountID const &account, uint192 const &issuanceID, std::uint32_t sequence, AccountID const &holder)
Generates the context hash for ConfidentialMPTClawback transactions.
@ temMALFORMED
Definition TER.h:75
@ temBAD_AMOUNT
Definition TER.h:77
TERSubset< CanCvtToTER > TER
Definition TER.h:647
@ tecNO_TARGET
Definition TER.h:307
@ tecOBJECT_NOT_FOUND
Definition TER.h:329
@ tecINTERNAL
Definition TER.h:313
@ tecINSUFFICIENT_FUNDS
Definition TER.h:328
@ tecNO_PERMISSION
Definition TER.h:308
void incrementConfidentialVersion(STObject &mptoken)
Increments the confidential balance version counter on an MPToken.
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:296
@ tesSUCCESS
Definition TER.h:245
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.
Definition Transactor.h:83
ReadView const & view
Definition Transactor.h:86
State information when preflighting a tx.
Definition Transactor.h:38