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/beast/utility/instrumentation.h>
6#include <xrpl/core/ServiceRegistry.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/ledger/helpers/TokenHelpers.h>
9#include <xrpl/protocol/ConfidentialTransfer.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 // issuer cannot merge
28 if (MPTIssue(ctx.tx[sfMPTokenIssuanceID]).getIssuer() == ctx.tx[sfAccount])
29 return temMALFORMED;
30
31 return tesSUCCESS;
32}
33
39
40TER
42{
43 auto const sleIssuance = ctx.view.read(keylet::mptokenIssuance(ctx.tx[sfMPTokenIssuanceID]));
44 if (!sleIssuance)
46
47 if (!sleIssuance->isFlag(lsfMPTCanHoldConfidentialBalance))
48 return tecNO_PERMISSION;
49
50 // already checked in preflight, but should also check that issuer on the
51 // issuance isn't the account either
52 if (sleIssuance->getAccountID(sfIssuer) == ctx.tx[sfAccount])
53 {
54 // LCOV_EXCL_START
55 UNREACHABLE(
56 "xrpl::ConfidentialMPTMergeInbox::preclaim : issuer derived from the MPT ID must "
57 "match the ledger's stored issuer");
58 return tefINTERNAL;
59 // LCOV_EXCL_STOP
60 }
61
62 auto const sleMptoken =
63 ctx.view.read(keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], ctx.tx[sfAccount]));
64 if (!sleMptoken)
66
67 if (!sleMptoken->isFieldPresent(sfConfidentialBalanceInbox) ||
68 !sleMptoken->isFieldPresent(sfConfidentialBalanceSpending) ||
69 !sleMptoken->isFieldPresent(sfHolderEncryptionKey))
70 {
71 return tecNO_PERMISSION;
72 }
73
74 // Check lock
75 auto const account = ctx.tx[sfAccount];
76 MPTIssue const mptIssue(ctx.tx[sfMPTokenIssuanceID]);
77 if (auto const ter = checkFrozen(ctx.view, account, mptIssue); !isTesSuccess(ter))
78 return ter;
79
80 // Check auth
81 if (auto const ter = requireAuth(ctx.view, mptIssue, account); !isTesSuccess(ter))
82 return ter;
83
84 return tesSUCCESS;
85}
86
87TER
89{
90 auto const mptIssuanceID = ctx_.tx[sfMPTokenIssuanceID];
91 auto sleMptoken = view().peek(keylet::mptoken(mptIssuanceID, accountID_));
92 if (!sleMptoken)
93 {
94 // LCOV_EXCL_START
95 UNREACHABLE(
96 "xrpl::ConfidentialMPTMergeInbox::doApply : preclaim already validated the "
97 "MPToken exists");
98 return tecINTERNAL;
99 // LCOV_EXCL_STOP
100 }
101
102 // sanity check
103 if (!sleMptoken->isFieldPresent(sfConfidentialBalanceSpending) ||
104 !sleMptoken->isFieldPresent(sfConfidentialBalanceInbox) ||
105 !sleMptoken->isFieldPresent(sfHolderEncryptionKey))
106 {
107 // LCOV_EXCL_START
108 UNREACHABLE(
109 "xrpl::ConfidentialMPTMergeInbox::doApply : preclaim already validated these "
110 "fields are present");
111 return tecINTERNAL;
112 // LCOV_EXCL_STOP
113 }
114
115 // Merge inbox into spending: spending = spending + inbox
116 // This allows holder to use received funds. Without merging, incoming
117 // transfers sit in inbox and cannot be spent or converted back.
118 auto sum = homomorphicAdd(
119 (*sleMptoken)[sfConfidentialBalanceSpending], (*sleMptoken)[sfConfidentialBalanceInbox]);
120 if (!sum)
121 {
122 // LCOV_EXCL_START
123 JLOG(ctx_.journal.error())
124 << "ConfidentialMPTMergeInbox failed homomorphic add for inbox merge.";
125 return tecINTERNAL;
126 // LCOV_EXCL_STOP
127 }
128
129 (*sleMptoken)[sfConfidentialBalanceSpending] = std::move(*sum);
130
131 // Reset inbox to encrypted zero. Must use canonical zero encryption
132 // (deterministic ciphertext) so the ledger state is reproducible.
133 auto zeroEncryption =
134 encryptCanonicalZeroAmount((*sleMptoken)[sfHolderEncryptionKey], accountID_, mptIssuanceID);
135
136 if (!zeroEncryption)
137 {
138 // LCOV_EXCL_START
139 UNREACHABLE(
140 "xrpl::ConfidentialMPTMergeInbox::doApply : canonical zero encryption cannot fail "
141 "for an already-valid holder public key");
142 return tecINTERNAL;
143 // LCOV_EXCL_STOP
144 }
145
146 (*sleMptoken)[sfConfidentialBalanceInbox] = std::move(*zeroEncryption);
147
148 incrementConfidentialVersion(*sleMptoken);
149
150 view().update(sleMptoken);
151 return tesSUCCESS;
152}
153
154void
161
162bool
164 STTx const&,
165 TER,
166 XRPAmount,
167 ReadView const&,
168 beast::Journal const&)
169{
170 return true;
171}
172
173} // 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)
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:41
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
ApplyView & view()
Definition Transactor.h:175
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
AccountID const accountID_
Definition Transactor.h:157
ApplyContext & ctx_
Definition Transactor.h:153
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:543
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
static auto sum(TCollection const &col)
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
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
@ tefINTERNAL
Definition TER.h:165
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:607
@ temMALFORMED
Definition TER.h:75
bool isTesSuccess(TER x) noexcept
Definition TER.h:676
TERSubset< CanCvtToTER > TER
Definition TER.h:647
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:329
@ tecINTERNAL
Definition TER.h:313
@ tecNO_PERMISSION
Definition TER.h:308
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:245
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