xrpld
Loading...
Searching...
No Matches
ConfidentialMPTHolderKeyUpdate.cpp
1#include <xrpl/tx/transactors/token/ConfidentialMPTHolderKeyUpdate.h>
2
3#include <xrpl/basics/Slice.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/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/MPTIssue.h>
13#include <xrpl/protocol/Protocol.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/TER.h>
16#include <xrpl/protocol/TxFlags.h>
17#include <xrpl/protocol/XRPAmount.h>
18#include <xrpl/tx/Transactor.h>
19
20#include <bit>
21#include <cstdint>
22
23namespace xrpl {
24
25bool
27{
28 // Holder key update is only meaningful when confidential transfers are enabled.
29 return ctx.rules.enabled(featureConfidentialTransfer);
30}
31
34{
35 return tfConfidentialMPTHolderKeyUpdateMask;
36}
37
40{
41 bool const rotation = ctx.tx.isFlag(tfHolderKeyRotation);
42 bool const recovery = ctx.tx.isFlag(tfHolderKeyRecovery);
43 bool const cancel = ctx.tx.isFlag(tfCancelRecovery);
44
45 // Exactly one of the three mode flags must be set.
46 static constexpr auto modeFlags = tfHolderKeyRotation | tfHolderKeyRecovery | tfCancelRecovery;
47 if (std::popcount(ctx.tx.getFlags() & modeFlags) != 1)
48 return temINVALID_FLAG;
49
50 // The issuer cannot hold confidential balances.
51 if (ctx.tx[sfAccount] == MPTIssue(ctx.tx[sfMPTokenIssuanceID]).getIssuer())
52 return temMALFORMED;
53
54 bool const hasHolderKey = ctx.tx.isFieldPresent(sfHolderEncryptionKey);
55 bool const hasSpending = ctx.tx.isFieldPresent(sfConfidentialBalanceSpending);
56 bool const hasInbox = ctx.tx.isFieldPresent(sfConfidentialBalanceInbox);
57 bool const hasProof = ctx.tx.isFieldPresent(sfZKProof);
58
59 if (cancel)
60 {
61 // Cancel mode only revokes a pending recovery authorization; it
62 // carries no key material, balances, or proof.
63 if (hasHolderKey || hasSpending || hasInbox || hasProof)
64 return temMALFORMED;
65
66 return tesSUCCESS;
67 }
68
69 // Rotation and Recovery both require a holder key and a proof.
70 if (!hasHolderKey || !hasProof)
71 return temMALFORMED;
72
73 // Rotation mode requires re-encrypted balances; Recovery mode must not
74 // provide them since the holder cannot decrypt the current ones.
75 if (rotation && (!hasSpending || !hasInbox))
76 return temMALFORMED;
77
78 if (recovery && (hasSpending || hasInbox))
79 return temMALFORMED;
80
81 if (hasSpending &&
82 ctx.tx[sfConfidentialBalanceSpending].length() != kEcGamalEncryptedTotalLength)
83 return temBAD_CIPHERTEXT;
84
85 if (hasInbox && ctx.tx[sfConfidentialBalanceInbox].length() != kEcGamalEncryptedTotalLength)
86 return temBAD_CIPHERTEXT;
87
88 // TODO: Check the sfZKProof length for each mode once the proof sizes are available from
89 // mpt-crypto.
90
91 if (!isValidCompressedECPoint(ctx.tx[sfHolderEncryptionKey]))
92 return temMALFORMED;
93
94 if (hasSpending && !isValidCiphertext(ctx.tx[sfConfidentialBalanceSpending]))
95 return temBAD_CIPHERTEXT;
96
97 if (hasInbox && !isValidCiphertext(ctx.tx[sfConfidentialBalanceInbox]))
98 return temBAD_CIPHERTEXT;
99
100 return tesSUCCESS;
101}
102
108
109TER
111{
112 auto const account = ctx.tx[sfAccount];
113 auto const mptIssuanceID = ctx.tx[sfMPTokenIssuanceID];
114
115 auto const sleIssuance = ctx.view.read(keylet::mptokenIssuance(mptIssuanceID));
116 if (!sleIssuance)
117 return tecOBJECT_NOT_FOUND;
118
119 if (!sleIssuance->isFlag(lsfMPTCanHoldConfidentialBalance))
120 return tecNO_PERMISSION;
121
122 auto const sleMptoken = ctx.view.read(keylet::mptoken(mptIssuanceID, account));
123 if (!sleMptoken)
124 return tecOBJECT_NOT_FOUND;
125
126 if (!sleMptoken->isFieldPresent(sfHolderEncryptionKey) ||
127 !sleMptoken->isFieldPresent(sfConfidentialBalanceSpending) ||
128 !sleMptoken->isFieldPresent(sfConfidentialBalanceInbox))
129 {
130 return tecNO_PERMISSION;
131 }
132
133 if (ctx.tx.isFlag(tfCancelRecovery))
134 {
135 // Nothing to cancel if no recovery is pending.
136 if (!sleMptoken->isFieldPresent(sfRecoveryKey))
137 return tecNO_PERMISSION;
138
139 return tesSUCCESS;
140 }
141
142 if (ctx.tx[sfHolderEncryptionKey] == (*sleMptoken)[sfHolderEncryptionKey])
143 return tecDUPLICATE;
144
145 // Recovery mode: reject if a recovery is already pending
146 if (ctx.tx.isFlag(tfHolderKeyRecovery) && sleMptoken->isFieldPresent(sfRecoveryKey))
147 return tecNO_PERMISSION;
148
149 // TODO: Verify sfZKProof. Rotation mode proves the new spending and inbox ciphertexts hold the
150 // same amounts as the current ones under the new key; Recovery mode proves knowledge of the
151 // private key for the new sfHolderEncryptionKey.
152 return tesSUCCESS;
153}
154
155TER
157{
158 auto const mptIssuanceID = ctx_.tx[sfMPTokenIssuanceID];
159 auto const sleMptoken = view().peek(keylet::mptoken(mptIssuanceID, accountID_));
160 if (!sleMptoken)
161 {
162 // LCOV_EXCL_START
163 UNREACHABLE(
164 "xrpl::ConfidentialMPTHolderKeyUpdate::doApply : preclaim already validated the "
165 "MPToken exists");
166 return tecINTERNAL;
167 // LCOV_EXCL_STOP
168 }
169
170 if (ctx_.tx.isFlag(tfCancelRecovery))
171 {
172 // The holder revokes their pending recovery authorization; the
173 // current key and balances are left untouched.
174 sleMptoken->makeFieldAbsent(sfRecoveryKey);
175 view().update(sleMptoken);
176 return tesSUCCESS;
177 }
178
179 auto const newPubKey = ctx_.tx[sfHolderEncryptionKey];
180
181 if (ctx_.tx.isFlag(tfHolderKeyRotation))
182 {
183 // Replace the key and balances in rotation mode. Rotation proves the
184 // holder still has the old key, so any pending recovery is cancelled.
185 (*sleMptoken)[sfHolderEncryptionKey] = newPubKey;
186 (*sleMptoken)[sfConfidentialBalanceSpending] = ctx_.tx[sfConfidentialBalanceSpending];
187 (*sleMptoken)[sfConfidentialBalanceInbox] = ctx_.tx[sfConfidentialBalanceInbox];
188 sleMptoken->makeFieldAbsent(sfRecoveryKey);
189 incrementConfidentialVersion(*sleMptoken);
190 }
191 else if (ctx_.tx.isFlag(tfHolderKeyRecovery))
192 {
193 // Recovery mode: the holder cannot decrypt their current balances,
194 // so only the pending recovery key is recorded. The balances are
195 // rewritten separately by the issuer via ConfidentialMPTRecoverBalance.
196 (*sleMptoken)[sfRecoveryKey] = newPubKey;
197 }
198 else
199 {
200 // LCOV_EXCL_START
201 UNREACHABLE("xrpl::ConfidentialMPTHolderKeyUpdate::doApply : invalid mode");
202 return tecINTERNAL;
203 // LCOV_EXCL_STOP
204 }
205
206 view().update(sleMptoken);
207 return tesSUCCESS;
208}
209
210void
214
215bool
217 STTx const&,
218 TER,
219 XRPAmount,
220 ReadView const&,
221 beast::Journal const&)
222{
223 return true;
224}
225
226} // 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.
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
static bool checkExtraFeatures(PreflightContext const &ctx)
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
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)
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.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
std::shared_ptr< STLedgerEntry const > const & ConstRef
bool isFlag(std::uint32_t) const
Definition STObject.cpp:511
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
std::uint32_t getFlags() const
Definition STObject.cpp:517
ApplyView & view()
Definition Transactor.h:184
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
AccountID const accountID_
Definition Transactor.h:166
ApplyContext & ctx_
Definition Transactor.h:162
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:547
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:541
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr std::uint32_t kConfidentialFeeMultiplier
Extra base fee multiplier charged to confidential MPT transactions.
Definition Protocol.h:578
bool isValidCompressedECPoint(Slice const &buffer)
Verifies that a buffer contains a valid, parsable compressed EC point.
constexpr std::size_t kEcGamalEncryptedTotalLength
EC ElGamal ciphertext length: two compressed EC points concatenated.
Definition Protocol.h:507
bool isValidCiphertext(Slice const &buffer)
Verifies that a buffer contains two valid, parsable EC public keys.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
@ temBAD_CIPHERTEXT
Definition TER.h:134
@ temINVALID_FLAG
Definition TER.h:99
@ temMALFORMED
Definition TER.h:75
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecOBJECT_NOT_FOUND
Definition TER.h:337
@ tecINTERNAL
Definition TER.h:321
@ tecNO_PERMISSION
Definition TER.h:316
@ tecDUPLICATE
Definition TER.h:326
void incrementConfidentialVersion(STObject &mptoken)
Increments the confidential balance version counter on an MPToken.
@ tesSUCCESS
Definition TER.h:253
T popcount(T... args)
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:92
ReadView const & view
Definition Transactor.h:95
State information when preflighting a tx.
Definition Transactor.h:39