xrpld
Loading...
Searching...
No Matches
ConfidentialTransferExtended_test.cpp
1#include <test/jtx/AMM.h>
2#include <test/jtx/Account.h>
3#include <test/jtx/ConfidentialTransfer.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/TestHelpers.h>
6#include <test/jtx/amount.h>
7#include <test/jtx/batch.h>
8#include <test/jtx/credentials.h>
9#include <test/jtx/delegate.h>
10#include <test/jtx/deposit.h>
11#include <test/jtx/flags.h>
12#include <test/jtx/mpt.h>
13#include <test/jtx/owners.h>
14#include <test/jtx/ter.h>
15#include <test/jtx/ticket.h>
16
17#include <xrpl/basics/Buffer.h>
18#include <xrpl/basics/base_uint.h>
19#include <xrpl/basics/strHex.h>
20#include <xrpl/beast/unit_test/suite.h>
21#include <xrpl/json/json_value.h>
22#include <xrpl/protocol/ConfidentialTransfer.h>
23#include <xrpl/protocol/ErrorCodes.h>
24#include <xrpl/protocol/Feature.h>
25#include <xrpl/protocol/Indexes.h>
26#include <xrpl/protocol/Protocol.h>
27#include <xrpl/protocol/SField.h>
28#include <xrpl/protocol/TER.h>
29#include <xrpl/protocol/TxFlags.h>
30#include <xrpl/protocol/jss.h>
31
32#include <chrono>
33#include <cstdint>
34#include <optional>
35#include <string>
36#include <vector>
37
38namespace xrpl {
39
41{
42 void
44 {
45 testcase("Send deposit preauth");
46 using namespace test::jtx;
47
48 // When an account enables lsfDepositAuth (via asfDepositAuth flag),
49 // it requires explicit authorization before accepting incoming payments.
50 //
51 // There are two authorization mechanisms:
52 //
53 // 1. DIRECT ACCOUNT AUTHORIZATION (deposit::auth)
54 // - Bob directly authorizes Carol: deposit::auth(bob, carol)
55 // - Simple 1-to-1 trust relationship
56 // - Carol can send to Bob without credentials
57 //
58 // 2. CREDENTIAL-BASED AUTHORIZATION (deposit::authCredentials)
59 // - A trusted third party (dpIssuer) issues credentials
60 // - Bob authorizes a credential TYPE from an issuer
61 // - Anyone holding that credential can send to Bob
62 // - Requires sender to include credential ID in transaction
63
64 Account const alice("alice");
65 Account const bob("bob");
66 Account const carol("carol");
67 Account const dpIssuer("dpIssuer");
68 char const credType[] = "KYC_VERIFIED";
69
70 // Create and accept credential for an account
71 auto const createCredential = [&](Env& env, Account const& subject) -> std::string {
72 env(credentials::create(subject, dpIssuer, credType));
73 env.close();
74 env(credentials::accept(subject, dpIssuer, credType));
75 env.close();
76 auto const jv = credentials::ledgerEntry(env, subject, dpIssuer, credType);
77 return jv[jss::result][jss::index].asString();
78 };
79
80 // TEST 1: Direct Account Authorization
81 {
82 Env env(*this, features);
83 ConfidentialEnv confEnv{
84 env,
85 alice,
86 {
87 {.account = bob, .payAmount = 100, .convertAmount = 50},
88 {.account = carol, .payAmount = 100, .convertAmount = 50},
89 }};
90 auto& mpt = confEnv.mpt;
91 env(fset(bob, asfDepositAuth));
92 env.close();
93
94 // Carol cannot send to Bob without authorization
95 mpt.send({
96 .account = carol,
97 .dest = bob,
98 .amt = 10,
99 .err = tecNO_PERMISSION,
100 });
101
102 // Bob directly authorizes Carol
103 env(deposit::auth(bob, carol));
104 env.close();
105
106 // Now Carol can send to Bob
107 mpt.send({
108 .account = carol,
109 .dest = bob,
110 .amt = 10,
111 });
112 mpt.mergeInbox({
113 .account = bob,
114 });
115
116 // Bob revokes Carol's authorization
117 env(deposit::unauth(bob, carol));
118 env.close();
119
120 // Carol can no longer send to Bob
121 mpt.send({
122 .account = carol,
123 .dest = bob,
124 .amt = 10,
125 .err = tecNO_PERMISSION,
126 });
127 }
128
129 // TEST 2: Credential-Based Authorization
130 {
131 Env env(*this, features);
132 env.fund(XRP(50000), dpIssuer);
133 env.close();
134
135 ConfidentialEnv confEnv{
136 env,
137 alice,
138 {
139 {.account = bob, .payAmount = 100, .convertAmount = 50},
140 {.account = carol, .payAmount = 100, .convertAmount = 50},
141 }};
142 auto& mpt = confEnv.mpt;
143 env(fset(bob, asfDepositAuth));
144 env.close();
145
146 auto const credIdx = createCredential(env, carol);
147
148 // Carol cannot send yet - Bob hasn't authorized this credential type
149 mpt.send({
150 .account = carol,
151 .dest = bob,
152 .amt = 10,
153 .credentials = {{credIdx}},
154 .err = tecNO_PERMISSION,
155 });
156
157 // Bob authorizes the credential type from dpIssuer
158 env(deposit::authCredentials(bob, {{.issuer = dpIssuer, .credType = credType}}));
159 env.close();
160
161 // Carol still cannot send without including credential
162 mpt.send({
163 .account = carol,
164 .dest = bob,
165 .amt = 10,
166 .err = tecNO_PERMISSION,
167 });
168
169 // Carol CAN send when including her credential
170 mpt.send({.account = carol, .dest = bob, .amt = 10, .credentials = {{credIdx}}});
171 mpt.mergeInbox({
172 .account = bob,
173 });
174 }
175
176 // TEST 3: Direct Auth Takes Precedence Over Credentials
177 {
178 Env env(*this, features);
179 env.fund(XRP(50000), dpIssuer);
180 env.close();
181
182 ConfidentialEnv confEnv{
183 env,
184 alice,
185 {
186 {.account = bob, .payAmount = 100, .convertAmount = 50},
187 {.account = carol, .payAmount = 100, .convertAmount = 50},
188 }};
189 auto& mpt = confEnv.mpt;
190 env(fset(bob, asfDepositAuth));
191 env.close();
192
193 auto const credIdx = createCredential(env, carol);
194
195 // Bob directly authorizes Carol (no credential needed)
196 env(deposit::auth(bob, carol));
197 env.close();
198
199 // Carol can send without credentials (direct auth)
200 mpt.send({
201 .account = carol,
202 .dest = bob,
203 .amt = 10,
204 });
205 mpt.mergeInbox({
206 .account = bob,
207 });
208
209 // Carol can also send WITH credentials (still works)
210 mpt.send({.account = carol, .dest = bob, .amt = 10, .credentials = {{credIdx}}});
211 mpt.mergeInbox({
212 .account = bob,
213 });
214
215 // Bob revokes direct authorization
216 env(deposit::unauth(bob, carol));
217 env.close();
218
219 // Carol cannot send without credentials anymore
220 mpt.send({
221 .account = carol,
222 .dest = bob,
223 .amt = 10,
224 .err = tecNO_PERMISSION,
225 });
226
227 // But credential-based auth not set up, so this also fails
228 mpt.send({
229 .account = carol,
230 .dest = bob,
231 .amt = 10,
232 .credentials = {{credIdx}},
233 .err = tecNO_PERMISSION,
234 });
235
236 // Bob authorizes the credential type
237 env(deposit::authCredentials(bob, {{.issuer = dpIssuer, .credType = credType}}));
238 env.close();
239
240 // Now Carol can send with credentials
241 mpt.send({.account = carol, .dest = bob, .amt = 10, .credentials = {{credIdx}}});
242 }
243
244 auto const expireTime = 30;
245
246 // Lambda function that returns the credential index after creating a
247 // credential that expires shortly after the current ledger time.
248 auto const createExpiringCredential = [&](Env& env, Account const& subject) -> std::string {
249 auto jv = credentials::create(subject, dpIssuer, credType);
250 auto const expiry =
251 env.current()->header().parentCloseTime.time_since_epoch().count() + expireTime;
252 jv[sfExpiration.jsonName] = expiry;
253 env(jv);
254 env.close();
255 env(credentials::accept(subject, dpIssuer, credType));
256 env.close();
257 auto const credentials = credentials::ledgerEntry(env, subject, dpIssuer, credType);
258 return credentials[jss::result][jss::index].asString();
259 };
260
261 auto const credentialDeleted = [&](Env& env, Account const& subject) -> bool {
262 auto const credentials = credentials::ledgerEntry(env, subject, dpIssuer, credType);
263 return credentials[jss::result][jss::error] == "entryNotFound" &&
264 credentials[jss::result][jss::error_code] == RpcEntryNotFound &&
265 credentials[jss::result][jss::error_message] == "Entry not found.";
266 };
267
268 // TEST 4: Expired credential with matching depositPreauth entry.
269 // checkDepositPreauth in preclaim returns tesSUCCESS (the expired
270 // credential still exists and matches the depositPreauth key), so ZK
271 // proofs run. cleanupExpiredCredentials in doApply then removes the
272 // expired credential and returns tecEXPIRED.
273 {
274 Env env(*this, features);
275 env.fund(XRP(50000), dpIssuer);
276 env.close();
277
278 ConfidentialEnv confEnv{
279 env,
280 alice,
281 {
282 {.account = bob, .payAmount = 100, .convertAmount = 50},
283 {.account = carol, .payAmount = 100, .convertAmount = 50},
284 }};
285 auto& mpt = confEnv.mpt;
286 env(fset(bob, asfDepositAuth));
287 env.close();
288
289 auto const credIdx = createExpiringCredential(env, carol);
290
291 // Bob authorizes carol's credential type
292 env(deposit::authCredentials(bob, {{.issuer = dpIssuer, .credType = credType}}));
293 env.close();
294
295 // Advance ledger past credential expiration
296 env.close(std::chrono::seconds(expireTime));
297
298 // Send fails with tecEXPIRED; the expired credential is cleaned up
299 mpt.send({
300 .account = carol,
301 .dest = bob,
302 .amt = 10,
303 .credentials = {{credIdx}},
304 .err = tecEXPIRED,
305 });
306 env.close();
307
308 BEAST_EXPECT(credentialDeleted(env, carol));
309 }
310
311 // TEST 5: Expired credential, destination has no depositAuth.
312 // checkDepositPreauth in preclaim returns tesSUCCESS even with expired credentials,
313 // because we want to keep the checkDepositPreauth part before the expensive proof
314 // verification. cleanupExpiredCredentials in doApply removes the expired credential and
315 // returns tecEXPIRED.
316 {
317 Env env(*this, features);
318 env.fund(XRP(50000), dpIssuer);
319 env.close();
320
321 ConfidentialEnv confEnv{
322 env,
323 alice,
324 {
325 {.account = bob, .payAmount = 100, .convertAmount = 50},
326 {.account = carol, .payAmount = 100, .convertAmount = 50},
327 }};
328 auto& mpt = confEnv.mpt;
329
330 auto const credIdx = createExpiringCredential(env, carol);
331
332 // Advance ledger past credential expiration
333 env.close(std::chrono::seconds(expireTime));
334
335 // Send fails with tecEXPIRED; the expired credential is cleaned up
336 mpt.send({
337 .account = carol,
338 .dest = bob,
339 .amt = 10,
340 .credentials = {{credIdx}},
341 .err = tecEXPIRED,
342 });
343 env.close();
344
345 BEAST_EXPECT(credentialDeleted(env, carol));
346 }
347
348 // TEST 6: Expired credential, depositAuth enabled but credential
349 // not authorized by bob.
350 // checkDepositPreauth in preclaim calls checkDepositPreauth which
351 // finds no match and returns tecNO_PERMISSION. doApply never runs, so
352 // the expired credential is not cleaned up by this transaction. This is
353 // a deliberate tradeoff: allowing doApply to run solely for cleanup
354 // would require bypassing the preclaim short-circuit, forcing every
355 // validator to run the expensive ZK proof verification before
356 // discovering the authorization failure. Expired credentials here will
357 // be cleaned up opportunistically by a future transaction that
358 // references them.
359 {
360 Env env(*this, features);
361 env.fund(XRP(50000), dpIssuer);
362 env.close();
363
364 ConfidentialEnv confEnv{
365 env,
366 alice,
367 {
368 {.account = bob, .payAmount = 100, .convertAmount = 50},
369 {.account = carol, .payAmount = 100, .convertAmount = 50},
370 }};
371 auto& mpt = confEnv.mpt;
372 env(fset(bob, asfDepositAuth));
373 env.close();
374
375 auto const credIdx = createExpiringCredential(env, carol);
376
377 // Advance ledger past credential expiration
378 env.close(std::chrono::seconds(expireTime));
379
380 // Fails with tecNO_PERMISSION.
381 mpt.send({
382 .account = carol,
383 .dest = bob,
384 .amt = 10,
385 .credentials = {{credIdx}},
386 .err = tecNO_PERMISSION,
387 });
388 env.close();
389
390 // Expired credential is not deleted
391 BEAST_EXPECT(!credentialDeleted(env, carol));
392 }
393 }
394
395 void
397 {
398 testcase("Send credential validation");
399 using namespace test::jtx;
400
401 // Tests for credentials::checkFields (preflight) and
402 // credentials::valid (preclaim) validation.
403 //
404 // Preflight checks (temMALFORMED):
405 // - Empty credentials array
406 // - Array size exceeds maxCredentialsArraySize (8)
407 // - Duplicate credential IDs in array
408 //
409 // Preclaim checks (tecBAD_CREDENTIALS):
410 // - Credential doesn't exist
411 // - Credential doesn't belong to source account
412 // - Credential not accepted (lsfAccepted flag not set)
413
414 Account const alice("alice");
415 Account const bob("bob");
416 Account const carol("carol");
417 Account const dpIssuer("dpIssuer");
418 char const credType[] = "KYC";
419
420 // TEST 1: Preflight - Empty Credentials Array
421 {
422 Env env(*this, features);
423 ConfidentialEnv confEnv{
424 env,
425 alice,
426 {
427 {.account = bob, .payAmount = 100, .convertAmount = 50},
428 {.account = carol, .payAmount = 100, .convertAmount = 50},
429 }};
430 auto& mpt = confEnv.mpt;
431
432 mpt.send({
433 .account = carol,
434 .dest = bob,
435 .amt = 10,
436 .credentials = std::vector<std::string>{},
437 .err = temMALFORMED,
438 });
439 }
440
441 // TEST 2: Preflight - Credentials Array Too Large
442 {
443 Env env(*this, features);
444 ConfidentialEnv confEnv{
445 env,
446 alice,
447 {
448 {.account = bob, .payAmount = 100, .convertAmount = 50},
449 {.account = carol, .payAmount = 100, .convertAmount = 50},
450 }};
451 auto& mpt = confEnv.mpt;
452
453 std::vector<std::string> tooManyCredentials;
454 tooManyCredentials.reserve(9);
455 for (int i = 0; i < 9; ++i)
456 tooManyCredentials.push_back(to_string(UInt256(i)));
457
458 mpt.send({
459 .account = carol,
460 .dest = bob,
461 .amt = 10,
462 .credentials = tooManyCredentials,
463 .err = temMALFORMED,
464 });
465 }
466
467 // TEST 3: Preflight - Duplicate Credentials
468 {
469 Env env(*this, features);
470 env.fund(XRP(50000), dpIssuer);
471 env.close();
472 ConfidentialEnv confEnv{
473 env,
474 alice,
475 {
476 {.account = bob, .payAmount = 100, .convertAmount = 50},
477 {.account = carol, .payAmount = 100, .convertAmount = 50},
478 }};
479 auto& mpt = confEnv.mpt;
480
481 env(credentials::create(carol, dpIssuer, credType));
482 env.close();
483 env(credentials::accept(carol, dpIssuer, credType));
484 env.close();
485
486 auto const jv = credentials::ledgerEntry(env, carol, dpIssuer, credType);
487 std::string const credIdx = jv[jss::result][jss::index].asString();
488
489 mpt.send({
490 .account = carol,
491 .dest = bob,
492 .amt = 10,
493 .credentials = {{credIdx, credIdx}},
494 .err = temMALFORMED,
495 });
496 }
497
498 // TEST 4: Preclaim - Credential Doesn't Exist
499 {
500 Env env(*this, features);
501 ConfidentialEnv confEnv{
502 env,
503 alice,
504 {
505 {.account = bob, .payAmount = 100, .convertAmount = 50},
506 {.account = carol, .payAmount = 100, .convertAmount = 50},
507 }};
508 auto& mpt = confEnv.mpt;
509
510 std::string const fakeCredIdx = to_string(UInt256(999));
511 mpt.send({
512 .account = carol,
513 .dest = bob,
514 .amt = 10,
515 .credentials = {{fakeCredIdx}},
516 .err = tecBAD_CREDENTIALS,
517 });
518 }
519
520 // TEST 5: Preclaim - Credential Doesn't Belong to Source Account
521 {
522 Env env(*this, features);
523 env.fund(XRP(50000), dpIssuer);
524 env.close();
525 ConfidentialEnv confEnv{
526 env,
527 alice,
528 {
529 {.account = bob, .payAmount = 100, .convertAmount = 50},
530 {.account = carol, .payAmount = 100, .convertAmount = 50},
531 }};
532 auto& mpt = confEnv.mpt;
533
534 // Create credential for BOB (not carol)
535 env(credentials::create(bob, dpIssuer, credType));
536 env.close();
537 env(credentials::accept(bob, dpIssuer, credType));
538 env.close();
539
540 auto const jv = credentials::ledgerEntry(env, bob, dpIssuer, credType);
541 std::string const credIdx = jv[jss::result][jss::index].asString();
542
543 mpt.send({
544 .account = carol,
545 .dest = bob,
546 .amt = 10,
547 .credentials = {{credIdx}},
548 .err = tecBAD_CREDENTIALS,
549 });
550 }
551
552 // TEST 6: Preclaim - Credential Not Accepted
553 {
554 Env env(*this, features);
555 env.fund(XRP(50000), dpIssuer);
556 env.close();
557 ConfidentialEnv confEnv{
558 env,
559 alice,
560 {
561 {.account = bob, .payAmount = 100, .convertAmount = 50},
562 {.account = carol, .payAmount = 100, .convertAmount = 50},
563 }};
564 auto& mpt = confEnv.mpt;
565
566 // Create credential but DON'T accept it
567 env(credentials::create(carol, dpIssuer, credType));
568 env.close();
569
570 auto const jv = credentials::ledgerEntry(env, carol, dpIssuer, credType);
571 std::string const credIdx = jv[jss::result][jss::index].asString();
572
573 mpt.send({
574 .account = carol,
575 .dest = bob,
576 .amt = 10,
577 .credentials = {{credIdx}},
578 .err = tecBAD_CREDENTIALS,
579 });
580 }
581
582 // TEST 7: Preflight - sfCredentialIDs requires featureCredentials.
583 // Even with featureConfidentialTransfer enabled, supplying
584 // CredentialIDs while featureCredentials is disabled must be
585 // rejected in preflight via checkExtraFeatures.
586 {
587 Env env(*this, features - featureCredentials);
588 ConfidentialEnv confEnv{
589 env,
590 alice,
591 {
592 {.account = bob, .payAmount = 100, .convertAmount = 50},
593 {.account = carol, .payAmount = 100, .convertAmount = 50},
594 }};
595 auto& mpt = confEnv.mpt;
596
597 auto constexpr kCredIdx =
598 "48004829F915654A81B11C4AB8218D96FED67F209B58328A72314FB6EA288BE4";
599
600 mpt.send({
601 .account = carol,
602 .dest = bob,
603 .amt = 10,
604 .credentials = {{kCredIdx}},
605 .err = temDISABLED,
606 });
607 }
608 }
609
610 // Bob creates the AMM, but Bob is not the MPT holder checked below.
611 // The AMM has its own pseudo-account (`ammHolder`) that can hold the
612 // public MPT pool balance. That pseudo-account cannot normally
613 // initialize confidential state because the confidential txn's must be
614 // signed by sfAccount, and the AMM pseudo-account has no signing key.
615 // So this is a construction/impossibility test: public AMM MPT state exists
616 // but the corresponding confidential AMM clawback flow is not normally reachable.
617 void
619 {
620 testcase("AMM holder cannot have confidential state");
621 using namespace test::jtx;
622
623 Account const alice("alice");
624 Account const bob("bob");
625
626 for (bool const enablePseudoAccount : {false, true})
627 {
628 Env env{
629 *this,
630 enablePseudoAccount ? features | featureSingleAssetVault
631 : features - featureSingleAssetVault};
632
633 MPTTester mptAlice(env, alice, {.holders = {bob}});
634
635 mptAlice.create({
636 .flags = kMptDexFlags | tfMPTCanClawback | tfMPTCanHoldConfidentialBalance,
637 });
638 mptAlice.authorize({.account = bob});
639 mptAlice.pay(alice, bob, 1'000);
640
641 mptAlice.generateKeyPair(alice);
642 mptAlice.set({.account = alice, .issuerPubKey = mptAlice.getPubKey(alice)});
643
644 AMM const amm(env, bob, XRP(100), mptAlice(100));
645 Account const ammHolder("amm", amm.ammAccount());
646 auto const ammSle = env.le(keylet::account(ammHolder.id()));
647
648 BEAST_EXPECT(ammSle && ammSle->isFieldPresent(sfAMMID));
649 BEAST_EXPECT(mptAlice.getBalance(ammHolder) == 100);
650
651 BEAST_EXPECT(!mptAlice.getEncryptedBalance(ammHolder, MPTTester::holderEncryptedInbox));
652 BEAST_EXPECT(
653 !mptAlice.getEncryptedBalance(ammHolder, MPTTester::holderEncryptedSpending));
654 BEAST_EXPECT(
655 !mptAlice.getEncryptedBalance(ammHolder, MPTTester::issuerEncryptedBalance));
656 BEAST_EXPECT(
657 !mptAlice.getEncryptedBalance(ammHolder, MPTTester::auditorEncryptedBalance));
658
659 mptAlice.confidentialClaw({
660 .account = alice,
661 .holder = ammHolder,
662 .amt = 100,
663 .proof = strHex(gMakeZeroBuffer(kEcClawbackProofLength)),
664 .err = tecNO_PERMISSION,
665 });
666 }
667 }
668
669 // A full AMMWithdraw must succeed even when an unrelated third party
670 // holds a confidential balance. Pre-fixCleanup3_5_0 the erase of the
671 // AMM pseudo-account's MPToken was rejected in this case, permanently
672 // stranding the last LP's position.
673 void
675 {
676 testcase("Full AMMWithdraw with an unrelated holder's COA");
677 using namespace test::jtx;
678
679 Account const alice("alice");
680 Account const bob("bob");
681 Account const carol("carol");
682
683 for (bool const withFix : {true, false})
684 {
685 for (bool const carolConverts : {false, true})
686 {
687 Env env{*this, withFix ? features | fixCleanup3_5_0 : features - fixCleanup3_5_0};
688
689 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
690 mptAlice.create({.flags = kMptDexFlags | tfMPTCanHoldConfidentialBalance});
691 mptAlice.authorize({.account = bob});
692 mptAlice.authorize({.account = carol});
693 mptAlice.pay(alice, bob, 1'000);
694 mptAlice.pay(alice, carol, 100);
695
696 mptAlice.generateKeyPair(alice);
697 mptAlice.set({.account = alice, .issuerPubKey = mptAlice.getPubKey(alice)});
698
699 // bob is the sole LP, so withdrawing all of his LPTokens zeroes
700 // LPTokenBalance and deletes the AMM in that same transaction.
701 AMM amm(env, bob, XRP(100), mptAlice(100));
702 Account const ammHolder("amm", amm.ammAccount());
703 BEAST_EXPECT(amm.ammExists());
704 BEAST_EXPECT(mptAlice.getBalance(ammHolder) == 100);
705
706 // The AMM pseudo-account cannot initialize confidential state:
707 // confidential transactions must be signed by sfAccount and it
708 // has no signing key.
709 BEAST_EXPECT(
710 !mptAlice.getEncryptedBalance(ammHolder, MPTTester::holderEncryptedInbox));
711 BEAST_EXPECT(
712 !mptAlice.getEncryptedBalance(ammHolder, MPTTester::holderEncryptedSpending));
713 BEAST_EXPECT(
714 !mptAlice.getEncryptedBalance(ammHolder, MPTTester::issuerEncryptedBalance));
715 BEAST_EXPECT(
716 !mptAlice.getEncryptedBalance(ammHolder, MPTTester::auditorEncryptedBalance));
717
718 if (carolConverts)
719 {
720 mptAlice.generateKeyPair(carol);
721 mptAlice.convert(
722 {.account = carol, .amt = 1, .holderPubKey = mptAlice.getPubKey(carol)});
723 }
724
725 BEAST_EXPECT(mptAlice.getIssuanceConfidentialBalance() == (carolConverts ? 1 : 0));
726
727 if (carolConverts && !withFix)
728 {
729 amm.withdrawAll(bob, std::nullopt, Ter(tecINVARIANT_FAILED));
730 env.close();
731
732 // bob cannot close his position: the pool, his LPTokens and
733 // the AMM object all survive the failed withdrawal.
734 BEAST_EXPECT(amm.ammExists());
735 BEAST_EXPECT(mptAlice.getBalance(ammHolder) == 100);
736
737 // A partial withdrawal still succeeds, because it never
738 // erases the MPToken -- bob can drain the pool down to a
739 // residual amount but can never close his position. Withdraw
740 // half of his actual LPToken balance; a fixed token count
741 // would round the MPT side of the pool to zero and be
742 // rejected as a one-sided withdrawal.
743 auto const bobLPTokens = amm.getLPTokensBalance(bob.id());
744 amm.withdraw(
745 bob, IOUAmount{bobLPTokens.mantissa() / 2, bobLPTokens.exponent()});
746 env.close();
747 BEAST_EXPECT(amm.ammExists());
748 BEAST_EXPECT(mptAlice.getBalance(ammHolder) > 0);
749 }
750 else
751 {
752 // With the fix, carol's confidential balance is irrelevant to
753 // an AMM she has no stake in: bob closes his position and the
754 // AMM and its MPToken are erased.
755 amm.withdrawAll(bob);
756 env.close();
757
758 BEAST_EXPECT(!amm.ammExists());
759 BEAST_EXPECT(
760 env.le(keylet::mptoken(mptAlice.issuanceID(), amm.ammAccount())) ==
761 nullptr);
762 }
763 }
764 }
765 }
766
767 // Exercises every Confidential Transfer transaction type (MPTokenIssuanceSet,
768 // Convert, MergeInbox, Send, ConvertBack) using tickets instead of regular account
769 // sequence numbers.
770 void
772 {
773 testcase("Confidential transfer with tickets");
774 using namespace test::jtx;
775
776 Env env{*this, features};
777 Account const alice("alice");
778 Account const bob("bob");
779 Account const carol("carol");
780 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
781
782 mptAlice.create({
783 .ownerCount = 1,
784 .flags = tfMPTCanTransfer | tfMPTCanLock | tfMPTCanHoldConfidentialBalance,
785 });
786 mptAlice.authorize({.account = bob});
787 mptAlice.authorize({.account = carol});
788 mptAlice.pay(alice, bob, 100);
789 mptAlice.pay(alice, carol, 100);
790
791 mptAlice.generateKeyPair(alice);
792 mptAlice.generateKeyPair(bob);
793 mptAlice.generateKeyPair(carol);
794
795 // MPTokenIssuanceSet with ticket, registers alice's issuer key.
796 {
797 std::uint32_t const ticketSeq = env.seq(alice) + 1;
798 env(ticket::create(alice, 1));
799 mptAlice.set({.issuerPubKey = mptAlice.getPubKey(alice), .ticketSeq = ticketSeq});
800 }
801
802 // ConfidentialMPTConvert with ticket, first convert registers bob's key.
803 {
804 std::uint32_t const ticketSeq = env.seq(bob) + 1;
805 env(ticket::create(bob, 1));
806 mptAlice.convert({
807 .account = bob,
808 .amt = 50,
809 .holderPubKey = mptAlice.getPubKey(bob),
810 .ticketSeq = ticketSeq,
811 });
812 env.require(MptBalance(mptAlice, bob, 50));
813 }
814
815 // ConfidentialMPTConvert with ticket
816 {
817 std::uint32_t const ticketSeq = env.seq(bob) + 1;
818 env(ticket::create(bob, 1));
819 mptAlice.convert({.account = bob, .amt = 20, .ticketSeq = ticketSeq});
820 env.require(MptBalance(mptAlice, bob, 30));
821 }
822
823 // ConfidentialMPTMergeInbox with ticket.
824 {
825 std::uint32_t const ticketSeq = env.seq(bob) + 1;
826 env(ticket::create(bob, 1));
827 mptAlice.mergeInbox({.account = bob, .ticketSeq = ticketSeq});
828 }
829
830 mptAlice.convert({.account = carol, .amt = 50, .holderPubKey = mptAlice.getPubKey(carol)});
831 mptAlice.mergeInbox({.account = carol});
832
833 // ConfidentialMPTSend with ticket.
834 {
835 std::uint32_t const ticketSeq = env.seq(bob) + 1;
836 env(ticket::create(bob, 1));
837 mptAlice.send({.account = bob, .dest = carol, .amt = 10, .ticketSeq = ticketSeq});
838 }
839
840 // Merge carol's inbox so her spending balance includes the received send.
841 mptAlice.mergeInbox({.account = carol});
842
843 // ConfidentialMPTConvertBack with ticket.
844 // The convertBack proof context hash must use the ticket sequence.
845 {
846 std::uint32_t const ticketSeq = env.seq(carol) + 1;
847 env(ticket::create(carol, 1));
848 mptAlice.convertBack({.account = carol, .amt = 10, .ticketSeq = ticketSeq});
849 // carol converted 50, received 10 from bob, then converted back 10 → public 60
850 env.require(MptBalance(mptAlice, carol, 60));
851 }
852 }
853
854 // Verifies that cryptographic proofs in Convert transactions are bound to
855 // the ticket sequence rather than the account sequence.
856 // A proof built with the ticket sequence passes.
857 void
859 {
860 testcase("Convert proof binds to ticket sequence");
861 using namespace test::jtx;
862
863 Env env{*this, features};
864 Account const alice("alice");
865 Account const bob("bob");
866 MPTTester mptAlice(env, alice, {.holders = {bob}});
867
868 mptAlice.create({
869 .ownerCount = 1,
870 .holderCount = 0,
871 .flags = tfMPTCanTransfer | tfMPTCanLock | tfMPTCanHoldConfidentialBalance,
872 });
873 mptAlice.authorize({.account = bob});
874 mptAlice.pay(alice, bob, 100);
875
876 mptAlice.generateKeyPair(alice);
877 mptAlice.set({.account = alice, .issuerPubKey = mptAlice.getPubKey(alice)});
878 mptAlice.generateKeyPair(bob);
879
880 uint64_t const amt = 30;
881 Buffer const bf = generateBlindingFactor();
882 Buffer const holderCt = mptAlice.encryptAmount(bob, amt, bf);
883 Buffer const issuerCt = mptAlice.encryptAmount(alice, amt, bf);
884
885 std::uint32_t const ticketSeq1 = env.seq(bob) + 1;
886 env(ticket::create(bob, 1));
887
888 // Invalid: Schnorr proof built with the account seq (env.seq(bob)) rather
889 // than the ticket seq (ticketSeq1).
890 {
891 BEAST_EXPECT(env.seq(bob) != ticketSeq1);
892 UInt256 const badCtxHash =
893 getConvertContextHash(bob, mptAlice.issuanceID(), env.seq(bob));
894 auto const badProof = requireOptional(
895 mptAlice.getSchnorrProof(bob, badCtxHash), "Missing Schnorr Proof.");
896
897 mptAlice.convert({
898 .account = bob,
899 .amt = amt,
900 .proof = strHex(badProof),
901 .holderPubKey = mptAlice.getPubKey(bob),
902 .holderEncryptedAmt = holderCt,
903 .issuerEncryptedAmt = issuerCt,
904 .blindingFactor = bf,
905 .ticketSeq = ticketSeq1,
906 .err = tecBAD_PROOF,
907 });
908 }
909
910 std::uint32_t const ticketSeq2 = env.seq(bob) + 1;
911 env(ticket::create(bob, 1));
912
913 // Valid: proof auto-generated by convert() using ticketSeq2; context hashes match.
914 mptAlice.convert({
915 .account = bob,
916 .amt = amt,
917 .holderPubKey = mptAlice.getPubKey(bob),
918 .holderEncryptedAmt = holderCt,
919 .issuerEncryptedAmt = issuerCt,
920 .blindingFactor = bf,
921 .ticketSeq = ticketSeq2,
922 });
923 env.require(MptBalance(mptAlice, bob, 70));
924 }
925
926 // Exercises ticket-specific error codes for confidential transfer transactions:
927 void
929 {
930 testcase("test Destination Tag");
931
932 using namespace test::jtx;
933 Env env{*this, features};
934 Account const alice("alice"), bob("bob"), carol("carol");
935 ConfidentialEnv confEnv{
936 env,
937 alice,
938 {{.account = bob}, {.account = carol, .payAmount = 1000, .convertAmount = 50}},
939 tfMPTCanTransfer | tfMPTCanHoldConfidentialBalance};
940 auto& mptAlice = confEnv.mpt;
941
942 // Set RequireDest on carol
943 env(fset(carol, asfRequireDest));
944 env.close();
945
946 // Send without destination tag — rejected
947 mptAlice.send({
948 .account = bob,
949 .dest = carol,
950 .amt = 10,
951 .proof = getTrivialSendProofHex(),
952 .senderEncryptedAmt = getTrivialCiphertext(),
953 .destEncryptedAmt = getTrivialCiphertext(),
954 .issuerEncryptedAmt = getTrivialCiphertext(),
955 .amountCommitment = getTrivialCommitment(),
956 .balanceCommitment = getTrivialCommitment(),
957 .err = tecDST_TAG_NEEDED,
958 });
959
960 // Send with destination tag — succeeds (passes preclaim,
961 // reaches ZKP verification with the real proof)
962 mptAlice.send({.account = bob, .dest = carol, .amt = 10, .destinationTag = 42});
963
964 // Verify the destination tag is in the confirmed transaction
965 auto const tx = env.tx();
966 BEAST_EXPECT(tx);
967 BEAST_EXPECT(tx->isFieldPresent(sfDestinationTag));
968 BEAST_EXPECT((*tx)[sfDestinationTag] == 42);
969
970 env(fclear(carol, asfRequireDest));
971 env.close();
972
973 // Send without destination tag when not required — succeeds
974 mptAlice.mergeInbox({.account = carol});
975 mptAlice.send({.account = bob, .dest = carol, .amt = 10});
976 }
977
978 // terPRE_TICKET when the ticket doesn't exist yet, and tefNO_TICKET when
979 // the ticket has already been consumed or was never created.
980 void
982 {
983 testcase("Confidential transfer ticket errors");
984 using namespace test::jtx;
985
986 Env env{*this, features};
987 Account const alice("alice");
988 Account const bob("bob");
989 MPTTester mptAlice(env, alice, {.holders = {bob}});
990
991 mptAlice.create({
992 .ownerCount = 1,
993 .holderCount = 0,
994 .flags = tfMPTCanTransfer | tfMPTCanLock | tfMPTCanHoldConfidentialBalance,
995 });
996 mptAlice.authorize({.account = bob});
997 mptAlice.pay(alice, bob, 100);
998
999 mptAlice.generateKeyPair(alice);
1000 mptAlice.set({.account = alice, .issuerPubKey = mptAlice.getPubKey(alice)});
1001 mptAlice.generateKeyPair(bob);
1002
1003 // Give bob an inbox balance so MergeInbox has something to merge.
1004 mptAlice.convert({.account = bob, .amt = 10, .holderPubKey = mptAlice.getPubKey(bob)});
1005
1006 // Use MergeInbox as the confidential transfer transaction under test
1007 // so that ticket errors are isolated from cryptographic verification.
1008
1009 // terPRE_TICKET: ticket sequence is far in the future and hasn't been created.
1010 mptAlice.mergeInbox(
1011 {.account = bob, .ticketSeq = env.seq(bob) + 100, .err = terPRE_TICKET});
1012
1013 // Create one ticket and use it successfully.
1014 std::uint32_t const ticketSeq = env.seq(bob) + 1;
1015 env(ticket::create(bob, 1));
1016 mptAlice.mergeInbox({.account = bob, .ticketSeq = ticketSeq});
1017
1018 // tefNO_TICKET: attempt to reuse the same (already-consumed) ticket.
1019 mptAlice.mergeInbox({.account = bob, .ticketSeq = ticketSeq, .err = tefNO_TICKET});
1020
1021 // tefNO_TICKET: ticket sequence is in the past but was never created.
1022 mptAlice.mergeInbox({.account = bob, .ticketSeq = 1, .err = tefNO_TICKET});
1023 }
1024
1025 // Bob sends 100 MPT to Carol. Carol Merge Inbox. Carol sends 50 MPT to Dave.
1026 // Inner 3rd txn (Carol sends to Dave) fails because the proof is built with
1027 // when Carols's spending balance is 0. (before she received funds from Bob)
1028 //
1029 // Also tests Bob sending to two recipients (Carol and Dave) in a single
1030 // batch. Even though Bob has enough balance for both, the second send's
1031 // balance-linkage proof becomes incorrect once inner 1 updates Bob's encrypted
1032 // spending, so fails
1033 void
1035 {
1036 testcase("Batch confidential send - merge inbox dependency");
1037 using namespace test::jtx;
1038
1039 {
1040 Env env{*this, features};
1041 Account const alice("alice");
1042 Account const bob("bob");
1043 Account const carol("carol");
1044 Account const dave("dave");
1045
1046 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1047 // bob = A (100 spending), carol = B (0), dave = C (0)
1048 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
1049
1050 // Build the batch:
1051 // Batch Txn 1 bob -> carol 100 : valid proof, bob spending=100
1052 // Batch Txn 2 carol -> mergeInbox : valid JV
1053 // Batch Txn 3 carol->dave 50 : Invalid
1054 auto const bobSeq = env.seq(bob);
1055 auto const carolSeq = env.seq(carol);
1056 // 3 signers, Bob, Carol, Dave
1057 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 3);
1058
1059 auto const jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 100}, bobSeq + 1);
1060 auto const jv2 = mpt.mergeInboxJV({.account = carol});
1061 auto const jv3 = mpt.sendJV({.account = carol, .dest = dave, .amt = 50}, carolSeq + 1);
1062
1063 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1064 batch::Inner(jv1, bobSeq + 1),
1065 batch::Inner(jv2, carolSeq),
1066 batch::Inner(jv3, carolSeq + 1),
1067 batch::Sig(carol),
1068 Ter(tesSUCCESS));
1069 env.close();
1070
1071 // AllOrNothing: inner 3 fails
1072 // bob's spending must remain 100; carol's inbox must remain 0.
1073 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
1074 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 0);
1075 }
1076
1077 // Bob sends to two recipients (Carol and Dave) in one batch.
1078 // Bob has 150, enough for both sends individually. However, batch txn 1
1079 // changes Bob's encrypted spending on the ledger; batch txn 2 was built
1080 // against the old enc(150) so its balance-linkage proof is stale.
1081 {
1082 Env env{*this, features};
1083 Account const alice("alice");
1084 Account const bob("bob");
1085 Account const carol("carol");
1086 Account const dave("dave");
1087
1088 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1089 setupBatchEnv(mpt, alice, bob, carol, dave, 150, 0);
1090
1091 // tfAllOrNothing — rejects the whole batch as 2nd txn proof is incorrect
1092 {
1093 auto const bobSeq = env.seq(bob);
1094 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 2);
1095
1096 auto const jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 50}, bobSeq + 1);
1097 auto const jv2 = mpt.sendJV({.account = bob, .dest = dave, .amt = 60}, bobSeq + 2);
1098
1099 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1100 batch::Inner(jv1, bobSeq + 1),
1101 batch::Inner(jv2, bobSeq + 2),
1102 Ter(tesSUCCESS));
1103 env.close();
1104
1105 // Nothing applied: bob stays 150, carol and dave inbox stay 0.
1106 BEAST_EXPECT(
1107 mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 150);
1108 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 0);
1109 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 0);
1110 }
1111
1112 // If we change batch mode to be tfIndependent — txn 1 applies, inner 2 fails.
1113 {
1114 auto const bobSeq = env.seq(bob);
1115 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 2);
1116
1117 auto const jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 50}, bobSeq + 1);
1118 auto const jv2 = mpt.sendJV({.account = bob, .dest = dave, .amt = 60}, bobSeq + 2);
1119
1120 env(batch::outer(bob, bobSeq, batchFee, tfIndependent),
1121 batch::Inner(jv1, bobSeq + 1),
1122 batch::Inner(jv2, bobSeq + 2),
1123 Ter(tesSUCCESS));
1124 env.close();
1125
1126 // bob 150→100, carol inbox 0→50
1127 BEAST_EXPECT(
1128 mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
1129 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 50);
1130 // dave gets nothing
1131 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 0);
1132 }
1133 }
1134
1135 // Now, Bob sends Confidential MPT to 2 accounts in one batch.
1136 // However this time, the second txn proof is calculated using the
1137 // correct encrypted(spending) proof, so it should pass.
1138 {
1139 // bob has exactly enough for both sends.
1140 Env env{*this, features};
1141 Account const alice("alice");
1142 Account const bob("bob");
1143 Account const carol("carol");
1144 Account const dave("dave");
1145
1146 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1147 setupBatchEnv(mpt, alice, bob, carol, dave, 200, 0);
1148
1149 {
1150 auto const bobSeq = env.seq(bob);
1151 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 2);
1152
1153 // jv1 is built against the current ledger state (spending=200).
1154 auto const jv1 =
1155 mpt.sendJV({.account = bob, .dest = carol, .amt = 100}, bobSeq + 1);
1156
1157 // Compute post-jv1 state without touching the ledger.
1158 auto const chain1 = mpt.chainAfterSend(bob, 100, jv1);
1159
1160 // jv2 proof is built against predicted spending=100, version=N+1.
1161 auto const jv2 =
1162 mpt.sendJV({.account = bob, .dest = dave, .amt = 100}, bobSeq + 2, chain1);
1163
1164 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1165 batch::Inner(jv1, bobSeq + 1),
1166 batch::Inner(jv2, bobSeq + 2),
1167 Ter(tesSUCCESS));
1168 env.close();
1169
1170 // Both txns applied: bob 200→0, carol inbox=100, dave inbox=100.
1171 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 0);
1172 BEAST_EXPECT(
1173 mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 100);
1174 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 100);
1175 }
1176
1177 // Now Bob has 150, but tries to send two 100 in one batch.
1178 // This fails because Bob doesn't have enough MPT balance.
1179 {
1180 Env env2{*this, features};
1181 Account const alice2("alice");
1182 Account const bob2("bob");
1183 Account const carol2("carol");
1184 Account const dave2("dave");
1185
1186 MPTTester mpt2(env2, alice2, {.holders = {bob2, carol2, dave2}});
1187 setupBatchEnv(mpt2, alice2, bob2, carol2, dave2, 150, 0);
1188
1189 auto const bobSeq = env2.seq(bob2);
1190 auto const batchFee = batch::calcConfidentialBatchFee(env2, 0, 2);
1191
1192 auto const jv1 =
1193 mpt2.sendJV({.account = bob2, .dest = carol2, .amt = 100}, bobSeq + 1);
1194 auto const chain1 = mpt2.chainAfterSend(bob2, 100, jv1);
1195
1196 auto const jv2 =
1197 mpt2.sendJV({.account = bob2, .dest = dave2, .amt = 100}, bobSeq + 2, chain1);
1198
1199 env2(
1200 batch::outer(bob2, bobSeq, batchFee, tfAllOrNothing),
1201 batch::Inner(jv1, bobSeq + 1),
1202 batch::Inner(jv2, bobSeq + 2),
1203 Ter(tesSUCCESS));
1204 env2.close();
1205
1206 // AllOrNothing: inner 2 fails → nothing applied.
1207 BEAST_EXPECT(
1208 mpt2.getDecryptedBalance(bob2, MPTTester::holderEncryptedSpending) == 150);
1209 BEAST_EXPECT(
1210 mpt2.getDecryptedBalance(carol2, MPTTester::holderEncryptedInbox) == 0);
1211 BEAST_EXPECT(mpt2.getDecryptedBalance(dave2, MPTTester::holderEncryptedInbox) == 0);
1212 }
1213 }
1214 }
1215 void
1217 {
1218 testcase("Batch confidential convert and convertBack");
1219 using namespace test::jtx;
1220
1221 // convert + convertBack in one AllOrNothing batch, both valid.
1222 //
1223 // Bob has regular=50, spending=100.
1224 // jv1: convert 50 regular → inbox (Schnorr proof; does NOT touch spending/version)
1225 // jv2: convertBack 30 spending → regular (proof against spending=100, version=V)
1226 //
1227 // Since jv1 leaves spending and version unchanged, jv2's proof is still
1228 // valid when it executes, so both inner txns succeed.
1229 {
1230 Env env{*this, features};
1231 Account const alice("alice");
1232 Account const bob("bob");
1233 Account const carol("carol");
1234 Account const dave("dave");
1235
1236 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1237 // bob: spending=100, regular=0 after setupBatchEnv;
1238 // pay 50 more to give bob regular MPT to convert in the batch.
1239 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
1240 mpt.pay(alice, bob, 50);
1241
1242 auto const bobSeq = env.seq(bob);
1243 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 2);
1244
1245 // jv1: convert 50 regular MPT into confidential inbox
1246 auto const jv1 = mpt.convertJV({.account = bob, .amt = 50}, bobSeq + 1);
1247 // jv2: convert 30 spending back to regular MPT
1248 auto const jv2 = mpt.convertBackJV({.account = bob, .amt = 30}, bobSeq + 2);
1249
1250 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1251 batch::Inner(jv1, bobSeq + 1),
1252 batch::Inner(jv2, bobSeq + 2),
1253 Ter(tesSUCCESS));
1254 env.close();
1255
1256 // regular (mptAmount): 50 (pre) - 50 (convert) + 30 (convertBack) = 30
1257 // spending balance: 100 - 30 = 70
1258 // inbox: 0 + 50 (from convert) = 50
1259 env.require(MptBalance(mpt, bob, 30));
1260 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 70);
1261 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedInbox) == 50);
1262 }
1263
1264 // convert + mergeInbox + convertBack, stale convertBack proof.
1265 //
1266 // jv1: convert 50 regular → inbox
1267 // jv2: mergeInbox (inbox 50 → spending, version V → V+1)
1268 // jv3: convertBack 30 (proof built against spending=100, version=V)
1269 //
1270 // After jv2 applies, spending=150 and version=V+1, so jv3's
1271 // proof is stale. AllOrNothing rejects the whole batch.
1272 {
1273 Env env{*this, features};
1274 Account const alice("alice");
1275 Account const bob("bob");
1276 Account const carol("carol");
1277 Account const dave("dave");
1278
1279 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1280 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
1281 mpt.pay(alice, bob, 50);
1282
1283 auto const bobSeq = env.seq(bob);
1284 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 3);
1285
1286 auto const jv1 = mpt.convertJV({.account = bob, .amt = 50}, bobSeq + 1);
1287 auto const jv2 = mpt.mergeInboxJV({.account = bob});
1288 // jv3 proof is built against spending=100, version=V (pre-batch)
1289 auto const jv3 = mpt.convertBackJV({.account = bob, .amt = 30}, bobSeq + 3);
1290
1291 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1292 batch::Inner(jv1, bobSeq + 1),
1293 batch::Inner(jv2, bobSeq + 2),
1294 batch::Inner(jv3, bobSeq + 3),
1295 Ter(tesSUCCESS));
1296 env.close();
1297
1298 // jv3 fails so nothing is applied.
1299 env.require(MptBalance(mpt, bob, 50));
1300 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
1301 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedInbox) == 0);
1302 }
1303 }
1304
1305 // Tests a batch containing all four confidential MPT operations, Send,
1306 // Convert, ConvertBack, and MergeInbox in a single AllOrNothing batch.
1307 void
1309 {
1310 testcase("Batch confidential mixed operations");
1311 using namespace test::jtx;
1312
1313 // send(bob→carol) + convert(carol) + convertBack(dave)
1314 // + mergeInbox(carol) in one AllOrNothing batch.
1315 //
1316 // Setup:
1317 // bob: spending=100, regular=0
1318 // carol: spending=0, regular=50
1319 // dave: spending=50, regular=0
1320 //
1321 // After the batch:
1322 // bob spending: 100 -> 70 (sent 30 to carol)
1323 // carol inbox: 0+30(send)+50(convert)=80 -> merged -> spending=80, inbox=0
1324 // dave spending: 50 -> 30; regular: 0 -> 20
1325 {
1326 Env env{*this, features};
1327 Account const alice("alice");
1328 Account const bob("bob");
1329 Account const carol("carol");
1330 Account const dave("dave");
1331
1332 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1333 // bob: spending=100. carol: key registered, spending=0.
1334 // dave: key registered, spending=0 initially.
1335 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
1336 // Give carol 50 regular MPT to convert in the batch.
1337 mpt.pay(alice, carol, 50);
1338 // Give dave 50 regular MPT then convert to confidential spending.
1339 mpt.pay(alice, dave, 50);
1340 mpt.convert({.account = dave, .amt = 50});
1341 mpt.mergeInbox({.account = dave});
1342
1343 auto const bobSeq = env.seq(bob);
1344 auto const carolSeq = env.seq(carol);
1345 auto const daveSeq = env.seq(dave);
1346 // 2 extra signers (carol, dave), 4 inner txns
1347 auto const batchFee = batch::calcConfidentialBatchFee(env, 2, 4);
1348
1349 // jv1: bob sends 30 to carol
1350 auto const jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 30}, bobSeq + 1);
1351 // jv2: carol converts her 50 regular MPT to confidential
1352 auto const jv2 = mpt.convertJV({.account = carol, .amt = 50}, carolSeq);
1353 // jv3: dave converts 20 spending back to regular MPT
1354 auto const jv3 = mpt.convertBackJV({.account = dave, .amt = 20}, daveSeq);
1355 // jv4: carol merges inbox into spending
1356 // (inbox = 30 from jv1 + 50 from jv2 = 80 at execution time)
1357 auto const jv4 = mpt.mergeInboxJV({.account = carol});
1358
1359 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1360 batch::Inner(jv1, bobSeq + 1),
1361 batch::Inner(jv2, carolSeq),
1362 batch::Inner(jv3, daveSeq),
1363 batch::Inner(jv4, carolSeq + 1),
1364 batch::Sig(carol, dave),
1365 Ter(tesSUCCESS));
1366 env.close();
1367
1368 // All four applied:
1369 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 70);
1370 // carol's inbox was merged: spending=80, inbox=0
1371 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 80);
1372 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 0);
1373 // dave: spending=30, regular=20
1374 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedSpending) == 30);
1375 env.require(MptBalance(mpt, dave, 20));
1376 }
1377
1378 // bob send + bob convertBack in one AllOrNothing batch.
1379 //
1380 // The Send applies first and increments Bob's version counter.
1381 // The ConvertBack proof was built against the pre-Send (spending=100,
1382 // version=V), so batch txn is rejected.
1383 {
1384 Env env{*this, features};
1385 Account const alice("alice");
1386 Account const bob("bob");
1387 Account const carol("carol");
1388 Account const dave("dave");
1389
1390 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1391 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
1392
1393 auto const bobSeq = env.seq(bob);
1394 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 2);
1395
1396 // jv1: bob sends 30 to carol (spending 100->70, version V->V+1)
1397 auto const jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 30}, bobSeq + 1);
1398 // jv2: bob convertBack 40 , proof built against spending=100, version=V
1399 auto const jv2 = mpt.convertBackJV({.account = bob, .amt = 40}, bobSeq + 2);
1400
1401 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1402 batch::Inner(jv1, bobSeq + 1),
1403 batch::Inner(jv2, bobSeq + 2),
1404 Ter(tesSUCCESS));
1405 env.close();
1406
1407 // AllOrNothing: jv2 fails (stale proof) → nothing applied.
1408 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
1409 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 0);
1410 }
1411 }
1412
1413 // Verifies that batch transactions work correctly when tickets are used instead
1414 // of sequence numbers
1415 void
1417 {
1418 testcase("Batch confidential MPT - all or nothing");
1419 using namespace test::jtx;
1420
1421 Env env{*this, features};
1422 Account const alice("alice");
1423 Account const bob("bob");
1424 Account const carol("carol");
1425 Account const dave("dave");
1426
1427 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1428 // bob=100 spending, carol=60 spending, dave=0
1429 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 60);
1430
1431 // bob sends dave 10, carol sends dave 5, independent, both valid.
1432 {
1433 auto const bobSeq = env.seq(bob);
1434 auto const carolSeq = env.seq(carol);
1435 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 2);
1436
1437 auto const jv1 = mpt.sendJV({.account = bob, .dest = dave, .amt = 10}, bobSeq + 1);
1438 auto const jv2 = mpt.sendJV({.account = carol, .dest = dave, .amt = 5}, carolSeq);
1439
1440 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1441 batch::Inner(jv1, bobSeq + 1),
1442 batch::Inner(jv2, carolSeq),
1443 batch::Sig(carol),
1444 Ter(tesSUCCESS));
1445 env.close();
1446
1447 // Both txn applied: bob's balance 100→90, carol 60→55, dave inbox 0→15
1448 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 90);
1449 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 55);
1450 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 15);
1451 }
1452 }
1453
1454 void
1456 {
1457 testcase("Batch confidential MPT - only one");
1458 using namespace test::jtx;
1459
1460 Env env{*this, features};
1461 Account const alice("alice");
1462 Account const bob("bob");
1463 Account const carol("carol");
1464 Account const dave("dave");
1465
1466 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1467 // bob=100 spending, carol=60 spending, dave=0
1468 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 60);
1469
1470 // bob sends dave 200 (invalid), carol sends dave 300 (invalid)
1471 {
1472 auto const bobSeq = env.seq(bob);
1473 auto const carolSeq = env.seq(carol);
1474 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 2);
1475
1476 // Both proofs fail range check (amount > balance)
1477 auto const jv1 = mpt.sendJV({.account = bob, .dest = dave, .amt = 200}, bobSeq + 1);
1478 auto const jv2 = mpt.sendJV({.account = carol, .dest = dave, .amt = 300}, carolSeq);
1479
1480 env(batch::outer(bob, bobSeq, batchFee, tfOnlyOne),
1481 batch::Inner(jv1, bobSeq + 1),
1482 batch::Inner(jv2, carolSeq),
1483 batch::Sig(carol),
1484 Ter(tesSUCCESS));
1485 env.close();
1486
1487 // No success found → nothing applied; balances unchanged
1488 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
1489 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 60);
1490 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 0);
1491 }
1492
1493 // bob sends dave 200 (invalid), carol sends dave 5 (valid)
1494 {
1495 auto const bobSeq = env.seq(bob);
1496 auto const carolSeq = env.seq(carol);
1497 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 2);
1498
1499 auto const jv1 = mpt.sendJV({.account = bob, .dest = dave, .amt = 200}, bobSeq + 1);
1500 auto const jv2 = mpt.sendJV({.account = carol, .dest = dave, .amt = 5}, carolSeq);
1501
1502 env(batch::outer(bob, bobSeq, batchFee, tfOnlyOne),
1503 batch::Inner(jv1, bobSeq + 1),
1504 batch::Inner(jv2, carolSeq),
1505 batch::Sig(carol),
1506 Ter(tesSUCCESS));
1507 env.close();
1508
1509 // Only carol's send applied: carol 60→55, dave inbox 0→5, bob unchanged
1510 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
1511 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 55);
1512 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 5);
1513 }
1514 }
1515
1516 void
1518 {
1519 testcase("Batch confidential MPT - until failure");
1520 using namespace test::jtx;
1521
1522 Env env{*this, features};
1523 Account const alice("alice");
1524 Account const bob("bob");
1525 Account const carol("carol");
1526 Account const dave("dave");
1527
1528 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1529 // bob=100 spending, carol=60 spending, dave=0
1530 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 60);
1531
1532 // first fails → none applied
1533 // Bob sends Dave 200 (invalid — stops immediately)
1534 {
1535 auto const bobSeq = env.seq(bob);
1536 auto const carolSeq = env.seq(carol);
1537 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 2);
1538
1539 auto const jv1 = mpt.sendJV({.account = bob, .dest = dave, .amt = 200}, bobSeq + 1);
1540 auto const jv2 = mpt.sendJV({.account = carol, .dest = dave, .amt = 5}, carolSeq);
1541
1542 env(batch::outer(bob, bobSeq, batchFee, tfUntilFailure),
1543 batch::Inner(jv1, bobSeq + 1),
1544 batch::Inner(jv2, carolSeq),
1545 batch::Sig(carol),
1546 Ter(tesSUCCESS));
1547 env.close();
1548
1549 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
1550 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 60);
1551 }
1552
1553 // Bob sends dave 10, Carol sends dave 5 — both valid and independent
1554 {
1555 auto const bobSeq = env.seq(bob);
1556 auto const carolSeq = env.seq(carol);
1557 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 2);
1558
1559 auto const jv1 = mpt.sendJV({.account = bob, .dest = dave, .amt = 10}, bobSeq + 1);
1560 auto const jv2 = mpt.sendJV({.account = carol, .dest = dave, .amt = 5}, carolSeq);
1561
1562 env(batch::outer(bob, bobSeq, batchFee, tfUntilFailure),
1563 batch::Inner(jv1, bobSeq + 1),
1564 batch::Inner(jv2, carolSeq),
1565 batch::Sig(carol),
1566 Ter(tesSUCCESS));
1567 env.close();
1568
1569 // Both applied: bob 100→90, carol 60→55, dave inbox 0→15
1570 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 90);
1571 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 55);
1572 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 15);
1573 }
1574 }
1575
1576 void
1578 {
1579 testcase("Batch confidential MPT - independent");
1580 using namespace test::jtx;
1581
1582 Env env{*this, features};
1583 Account const alice("alice");
1584 Account const bob("bob");
1585 Account const carol("carol");
1586 Account const dave("dave");
1587
1588 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1589 // bob=100 spending, carol=60 spending, dave=0
1590 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 60);
1591
1592 // Bob sends dave 10 (valid), Carol sends dave 300
1593 // (invalid), Carol sends Dave 5 (valid). Carol's
1594 // balance is still 60 because the preceding send failed).
1595 {
1596 auto const bobSeq = env.seq(bob);
1597 auto const carolSeq = env.seq(carol);
1598 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 3);
1599
1600 auto const jv1 = mpt.sendJV({.account = bob, .dest = dave, .amt = 10}, bobSeq + 1);
1601
1602 // Carol trying to send dave 300 but own balance only 60
1603 auto const jv2 = mpt.sendJV({.account = carol, .dest = dave, .amt = 300}, carolSeq);
1604 auto const jv3 = mpt.sendJV({.account = carol, .dest = dave, .amt = 5}, carolSeq + 1);
1605
1606 env(batch::outer(bob, bobSeq, batchFee, tfIndependent),
1607 batch::Inner(jv1, bobSeq + 1),
1608 batch::Inner(jv2, carolSeq),
1609 batch::Inner(jv3, carolSeq + 1),
1610 batch::Sig(carol),
1611 Ter(tesSUCCESS));
1612 env.close();
1613
1614 // inner 1 (bob→dave 10) applied: bob 100→90
1615 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 90);
1616 // inner 2 failed (carol not changed), inner 3 applied: carol 60→55
1617 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 55);
1618 // dave inbox: 10 (from bob) + 5 (from carol inner 3) = 15
1619 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 15);
1620 }
1621 }
1622
1623 // Tests batching ConfidentialMPTConvert and a ConfidentialMPTConvertBack
1624 // in the same batch transaction. Because Convert only modifies the inbox
1625 // (never the spending balance or the version counter), a ConvertBack proof
1626 // built against the pre-batch spending balance is still valid when both
1627 // appear in the same batch.
1628 void
1630 {
1631 testcase("Batch confidential MPT with tickets");
1632 using namespace test::jtx;
1633
1634 // outer batch uses a ticket.
1635 // The inner send proofs are still bound to regular account sequences.
1636 {
1637 Env env{*this, features};
1638 Account const alice("alice");
1639 Account const bob("bob");
1640 Account const carol("carol");
1641 Account const dave("dave");
1642
1643 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1644 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
1645
1646 // Bob creates one ticket to use for the outer batch.
1647 std::uint32_t const outerTicketSeq = env.seq(bob) + 1;
1648 env(ticket::create(bob, 1));
1649 env.close();
1650
1651 auto const bobSeq = env.seq(bob);
1652 // 0 extra signers: all inner txns are from bob;
1653 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 2);
1654
1655 // When the outer uses a ticket (seq=0), inner txns start from bobSeq, bobSeq+1.
1656 // jv2 must use chain state predicted after jv1 since both sends are from bob.
1657 auto const jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 40}, bobSeq);
1658 auto const chain1 = mpt.chainAfterSend(bob, 40, jv1);
1659 auto const jv2 =
1660 mpt.sendJV({.account = bob, .dest = dave, .amt = 20}, bobSeq + 1, chain1);
1661
1662 env(batch::outer(bob, 0, batchFee, tfAllOrNothing),
1663 batch::Inner(jv1, bobSeq),
1664 batch::Inner(jv2, bobSeq + 1),
1665 ticket::Use(outerTicketSeq),
1666 Ter(tesSUCCESS));
1667 env.close();
1668
1669 // Both sends applied: bob 100→40, carol inbox=40, dave inbox=20.
1670 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 40);
1671 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 40);
1672 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 20);
1673 }
1674
1675 // inner transactions each consume their own ticket.
1676 // The send proof context hash must be bound to the ticket sequence, not the
1677 // account sequence. sendJV receives the ticket seq as its `seq` parameter.
1678 {
1679 Env env{*this, features};
1680 Account const alice("alice");
1681 Account const bob("bob");
1682 Account const carol("carol");
1683 Account const dave("dave");
1684
1685 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1686 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
1687
1688 // Bob creates two tickets for the two inner sends.
1689 std::uint32_t const ticketSeq1 = env.seq(bob) + 1;
1690 std::uint32_t const ticketSeq2 = env.seq(bob) + 2;
1691 env(ticket::create(bob, 2));
1692 env.close();
1693
1694 auto const bobSeq = env.seq(bob);
1695 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 2);
1696
1697 // jv1: proof bound to ticketSeq1.
1698 auto const jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 40}, ticketSeq1);
1699 // jv2: proof bound to ticketSeq2, spending state predicted after jv1.
1700 auto const chain1 = mpt.chainAfterSend(bob, 40, jv1);
1701 auto const jv2 =
1702 mpt.sendJV({.account = bob, .dest = dave, .amt = 30}, ticketSeq2, chain1);
1703
1704 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1705 batch::Inner(jv1, 0, ticketSeq1),
1706 batch::Inner(jv2, 0, ticketSeq2),
1707 Ter(tesSUCCESS));
1708 env.close();
1709
1710 // Both sends applied: bob 100→30, carol inbox=40, dave inbox=30.
1711 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 30);
1712 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 40);
1713 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 30);
1714 }
1715
1716 // inner send uses wrong sequence (account seq instead of ticket seq)
1717 {
1718 Env env{*this, features};
1719 Account const alice("alice");
1720 Account const bob("bob");
1721 Account const carol("carol");
1722 Account const dave("dave");
1723
1724 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
1725 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
1726
1727 std::uint32_t const ticketSeq = env.seq(bob) + 1;
1728 env(ticket::create(bob, 1));
1729 env.close();
1730
1731 auto const bobSeq = env.seq(bob);
1732 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 2);
1733
1734 // Proof intentionally built with account seq (bobSeq+1) instead of ticketSeq.
1735 auto const badJV = mpt.sendJV({.account = bob, .dest = carol, .amt = 40}, bobSeq + 1);
1736 auto const jv2 = mpt.mergeInboxJV({.account = bob});
1737
1738 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
1739 batch::Inner(badJV, 0, ticketSeq),
1740 batch::Inner(jv2, bobSeq + 1),
1741 Ter(tesSUCCESS));
1742 env.close();
1743
1744 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
1745 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 0);
1746 }
1747 }
1748
1749 // Basic tests of confidential transfer through delegation. Verifies that a delegated account
1750 // with the appropriate permissions can execute confidential transfer transactions
1751 // on behalf of the delegator.
1752 void
1754 {
1755 testcase("Confidential transfers through delegation");
1756 using namespace test::jtx;
1757
1758 Env env{*this, features};
1759 Account const alice{"alice"};
1760 Account const bob{"bob"};
1761 Account const carol{"carol"};
1762 Account const dave{"dave"};
1763
1764 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1765 env.fund(XRP(10000), dave);
1766 env.close();
1767
1768 mptAlice.create({
1769 .ownerCount = 1,
1770 .flags = tfMPTCanTransfer | tfMPTCanLock | tfMPTCanClawback |
1771 tfMPTCanHoldConfidentialBalance,
1772 });
1773 mptAlice.authorize({.account = bob});
1774 mptAlice.authorize({.account = carol});
1775 mptAlice.pay(alice, bob, 200);
1776 mptAlice.pay(alice, carol, 100);
1777
1778 mptAlice.generateKeyPair(alice);
1779 mptAlice.generateKeyPair(bob);
1780 mptAlice.generateKeyPair(carol);
1781 mptAlice.set({.issuerPubKey = mptAlice.getPubKey(alice)});
1782
1783 // ConfidentialMPTConvert is not delegable: attempting to grant it as a
1784 // delegated permission is rejected at preflight of DelegateSet.
1785 env(delegate::set(bob, dave, {"ConfidentialMPTConvert"}), Ter(temMALFORMED));
1786 env.close();
1787
1788 // Bob delegates MergeInbox to dave.
1789 env(delegate::set(bob, dave, {"ConfidentialMPTMergeInbox"}));
1790 env.close();
1791
1792 // A Convert carrying a Delegate is rejected at preflight because the
1793 // transaction type is not delegable at all.
1794 mptAlice.convert({
1795 .account = bob,
1796 .amt = 10,
1797 .holderPubKey = mptAlice.getPubKey(bob),
1798 .delegate = dave,
1799 .err = temINVALID,
1800 });
1801
1802 // Bob converts, registering bob's key.
1803 mptAlice.convert({
1804 .account = bob,
1805 .amt = 100,
1806 .holderPubKey = mptAlice.getPubKey(bob),
1807 });
1808 env.require(MptBalance(mptAlice, bob, 100));
1809
1810 // Bob converts again (no key registration).
1811 mptAlice.convert({.account = bob, .amt = 50});
1812
1813 // Dave executes MergeInbox on behalf of bob.
1814 mptAlice.mergeInbox({.account = bob, .delegate = dave});
1815
1816 // Carol converts and merge inbox.
1817 mptAlice.convert({
1818 .account = carol,
1819 .amt = 100,
1820 .holderPubKey = mptAlice.getPubKey(carol),
1821 });
1822 mptAlice.mergeInbox({.account = carol});
1823
1824 // Dave does not have permission to send on behalf of bob.
1825 mptAlice.send({
1826 .account = bob,
1827 .dest = carol,
1828 .amt = 10,
1829 .delegate = dave,
1831 });
1832
1833 // Bob delegates ConfidentialMPTSend to dave.
1834 env(delegate::set(bob, dave, {"ConfidentialMPTMergeInbox", "ConfidentialMPTSend"}));
1835 env.close();
1836
1837 // Dave executes Send on behalf of bob.
1838 mptAlice.send({.account = bob, .dest = carol, .amt = 10, .delegate = dave});
1839 mptAlice.mergeInbox({.account = carol});
1840
1841 // Dave does not have permission to convert back on behalf of bob.
1842 mptAlice.convertBack(
1843 {.account = bob, .amt = 10, .delegate = dave, .err = terNO_DELEGATE_PERMISSION});
1844
1845 // Bob delegates ConfidentialMPTConvertBack to dave.
1846 env(delegate::set(
1847 bob,
1848 dave,
1849 {"ConfidentialMPTMergeInbox", "ConfidentialMPTSend", "ConfidentialMPTConvertBack"}));
1850 env.close();
1851
1852 // Dave executes ConvertBack on behalf of bob.
1853 mptAlice.convertBack({.account = bob, .amt = 10, .delegate = dave});
1854
1855 // Dave does not have permission to clawback on behalf of alice.
1856 mptAlice.confidentialClaw(
1857 {.holder = bob, .amt = 130, .delegate = dave, .err = terNO_DELEGATE_PERMISSION});
1858
1859 // Alice delegates ConfidentialMPTClawback to dave.
1860 env(delegate::set(alice, dave, {"ConfidentialMPTClawback"}));
1861 env.close();
1862
1863 // Dave executes Clawback on behalf of alice.
1864 mptAlice.confidentialClaw({.holder = bob, .amt = 130, .delegate = dave});
1865 }
1866
1867 // Verifies that revoking delegation prevents further delegated operations.
1868 void
1870 {
1871 testcase("Confidential delegation revocation");
1872 using namespace test::jtx;
1873
1874 Env env{*this, features};
1875 Account const alice{"alice"};
1876 Account const bob{"bob"};
1877 Account const carol{"carol"};
1878
1879 MPTTester mptAlice(env, alice, {.holders = {bob}});
1880 env.fund(XRP(10000), carol);
1881 env.close();
1882
1883 mptAlice.create({
1884 .ownerCount = 1,
1885 .flags = tfMPTCanTransfer | tfMPTCanHoldConfidentialBalance,
1886 });
1887 mptAlice.authorize({.account = bob});
1888 mptAlice.pay(alice, bob, 100);
1889
1890 mptAlice.generateKeyPair(alice);
1891 mptAlice.generateKeyPair(bob);
1892 mptAlice.set({.issuerPubKey = mptAlice.getPubKey(alice)});
1893
1894 // Creating the Delegate SLE consumes one owner reserve slot for bob.
1895 auto const bobOwnersBefore = ownerCount(env, bob);
1896 env(delegate::set(bob, carol, {"ConfidentialMPTMergeInbox"}));
1897 env.close();
1898 env.require(Owners(bob, bobOwnersBefore + 1));
1899
1900 // Bob converts; carol merges inbox on behalf of bob.
1901 mptAlice.convert({
1902 .account = bob,
1903 .amt = 50,
1904 .holderPubKey = mptAlice.getPubKey(bob),
1905 });
1906 mptAlice.mergeInbox({.account = bob, .delegate = carol});
1907
1908 // Bob revokes all permissions, deletes the Delegate SLE, releasing the reserve.
1909 env(delegate::set(bob, carol, std::vector<std::string>{}));
1910 env.close();
1911 env.require(Owners(bob, bobOwnersBefore));
1912
1913 // Bob converts again to populate a fresh inbox.
1914 mptAlice.convert({.account = bob, .amt = 30});
1915
1916 // Carol can no longer merge inbox on behalf of bob.
1917 mptAlice.mergeInbox({
1918 .account = bob,
1919 .delegate = carol,
1921 });
1922
1923 // Bob can still merge his inbox.
1924 mptAlice.mergeInbox({.account = bob});
1925 }
1926
1927 // Verifies that a delegated confidential transfer works correctly when an
1928 // auditor is configured on the issuance.
1929 void
1931 {
1932 testcase("Confidential delegation with auditor");
1933 using namespace test::jtx;
1934
1935 Env env{*this, features};
1936 Account const alice{"alice"};
1937 Account const bob{"bob"};
1938 Account const carol{"carol"};
1939 Account const dave{"dave"};
1940 Account const auditor{"auditor"};
1941
1942 MPTTester mptAlice(env, alice, {.holders = {bob, carol}, .auditor = auditor});
1943 env.fund(XRP(10000), dave);
1944 env.close();
1945
1946 mptAlice.create({
1947 .ownerCount = 1,
1948 .flags = tfMPTCanTransfer | tfMPTCanHoldConfidentialBalance,
1949 });
1950 mptAlice.authorize({.account = bob});
1951 mptAlice.authorize({.account = carol});
1952 mptAlice.pay(alice, bob, 100);
1953 mptAlice.pay(alice, carol, 100);
1954
1955 mptAlice.generateKeyPair(alice);
1956 mptAlice.generateKeyPair(bob);
1957 mptAlice.generateKeyPair(carol);
1958 mptAlice.generateKeyPair(auditor);
1959 mptAlice.set({
1960 .issuerPubKey = mptAlice.getPubKey(alice),
1961 .auditorPubKey = mptAlice.getPubKey(auditor),
1962 });
1963
1964 // Bob delegates Send permission to dave (Convert is not delegable).
1965 env(delegate::set(bob, dave, {"ConfidentialMPTSend"}));
1966 env.close();
1967
1968 // Bob converts.
1969 mptAlice.convert({
1970 .account = bob,
1971 .amt = 50,
1972 .holderPubKey = mptAlice.getPubKey(bob),
1973 });
1974 mptAlice.mergeInbox({.account = bob});
1975
1976 mptAlice.convert({
1977 .account = carol,
1978 .amt = 50,
1979 .holderPubKey = mptAlice.getPubKey(carol),
1980 });
1981 mptAlice.mergeInbox({.account = carol});
1982
1983 // Dave sends on behalf of bob.
1984 mptAlice.send({.account = bob, .dest = carol, .amt = 20, .delegate = dave});
1985 mptAlice.send({.account = bob, .dest = carol, .amt = 10, .delegate = dave});
1986
1987 // Bob delegates ConvertBack and Send permissions to auditor.
1988 env(delegate::set(bob, auditor, {"ConfidentialMPTSend", "ConfidentialMPTConvertBack"}));
1989 env.close();
1990
1991 // auditor can send and convert back on behalf of bob as well.
1992 mptAlice.send({.account = bob, .dest = carol, .amt = 10, .delegate = auditor});
1993 mptAlice.convertBack({.account = bob, .amt = 10, .delegate = auditor});
1994 }
1995
1996 // Verifies that a non-issuer delegating clawback to a third party does not
1997 // allow that party to execute clawback, since clawback is issuer-only.
1998 void
2000 {
2001 testcase("Confidential clawback delegation requires issuer");
2002 using namespace test::jtx;
2003
2004 Env env{*this, features};
2005 Account const alice{"alice"};
2006 Account const bob{"bob"};
2007 Account const carol{"carol"};
2008 Account const dave{"dave"};
2009
2010 ConfidentialEnv const confEnv{
2011 env,
2012 alice,
2013 {
2014 {.account = bob, .payAmount = 100, .convertAmount = 50},
2015 {.account = carol, .payAmount = 100, .convertAmount = 100},
2016 },
2017 tfMPTCanTransfer | tfMPTCanClawback | tfMPTCanHoldConfidentialBalance};
2018 auto& mptAlice = confEnv.mpt;
2019 env.fund(XRP(10000), dave);
2020 env.close();
2021
2022 // Bob delegates Clawback permission to dave.
2023 env(delegate::set(bob, dave, {"ConfidentialMPTClawback"}));
2024 env.close();
2025
2026 // Dave attempts clawback on behalf of bob targetting bob, but since bob is not the issuer,
2027 // the transaction should be rejected.
2028 {
2029 json::Value jv;
2030 jv[jss::Account] = bob.human();
2031 jv[jss::TransactionType] = jss::ConfidentialMPTClawback;
2032 jv[sfMPTokenIssuanceID] = to_string(mptAlice.issuanceID());
2033 jv[sfHolder] = bob.human();
2034 jv[sfMPTAmount.jsonName] = "50";
2035 jv[sfZKProof.jsonName] = std::string(kEcClawbackProofLength * 2, '0');
2036 env(jv, delegate::As(dave), Ter(temMALFORMED));
2037 }
2038
2039 // Dave attempts clawback on behalf of bob targeting carol, but since bob is not the issuer,
2040 // the transaction should be rejected.
2041 {
2042 json::Value jv;
2043 jv[jss::Account] = bob.human();
2044 jv[jss::TransactionType] = jss::ConfidentialMPTClawback;
2045 jv[sfMPTokenIssuanceID] = to_string(mptAlice.issuanceID());
2046 jv[sfHolder] = carol.human();
2047 jv[sfMPTAmount.jsonName] = "100";
2048 jv[sfZKProof.jsonName] = std::string(kEcClawbackProofLength * 2, '0');
2049 env(jv, delegate::As(dave), Ter(temMALFORMED));
2050 }
2051 }
2052
2053 // Batch with delegated ConfidentialMPTSend txs, covering stale and updated inner
2054 // send proofs.
2055 void
2057 {
2058 testcase("Batch ConfidentialMPTSend with delegation");
2059 using namespace test::jtx;
2060
2061 // AllOrNothing: two delegated sends from bob via dave, second proof is
2062 // stale once the first send updates bob's spending, whole batch rolls back.
2063 {
2064 Env env{*this, features};
2065 Account const alice("alice");
2066 Account const bob("bob");
2067 Account const carol("carol");
2068 Account const dave("dave");
2069
2070 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
2071 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
2072
2073 env(delegate::set(bob, dave, {"ConfidentialMPTSend"}));
2074 env.close();
2075
2076 auto const bobSeq = env.seq(bob);
2077 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 2);
2078
2079 // jv1: proof against spending balance 100
2080 auto jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 60}, bobSeq + 1);
2081 jv1[jss::Delegate] = dave.human();
2082 // jv2: proof also against spending balance 100, which is stale once jv1 applies
2083 auto jv2 = mpt.sendJV({.account = bob, .dest = dave, .amt = 60}, bobSeq + 2);
2084 jv2[jss::Delegate] = dave.human();
2085
2086 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
2087 batch::Inner(jv1, bobSeq + 1),
2088 batch::Inner(jv2, bobSeq + 2),
2089 batch::Sig(dave),
2090 Ter(tesSUCCESS));
2091 env.close();
2092
2093 // Stale proof on jv2, AllOrNothing rolls back everything.
2094 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
2095 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 0);
2096 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 0);
2097 }
2098
2099 // AllOrNothing: two delegated sends with correctly chained proofs both apply.
2100 {
2101 Env env{*this, features};
2102 Account const alice("alice");
2103 Account const bob("bob");
2104 Account const carol("carol");
2105 Account const dave("dave");
2106
2107 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
2108 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
2109
2110 env(delegate::set(bob, dave, {"ConfidentialMPTSend"}));
2111 env.close();
2112
2113 auto const bobSeq = env.seq(bob);
2114 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 2);
2115
2116 // jv1: proof against spending balance 100.
2117 auto jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 40}, bobSeq + 1);
2118 jv1[jss::Delegate] = dave.human();
2119 auto const chain1 = mpt.chainAfterSend(bob, 40, jv1);
2120 // jv2: proof against predicted spending balance 60.
2121 auto jv2 = mpt.sendJV({.account = bob, .dest = dave, .amt = 40}, bobSeq + 2, chain1);
2122 jv2[jss::Delegate] = dave.human();
2123
2124 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
2125 batch::Inner(jv1, bobSeq + 1),
2126 batch::Inner(jv2, bobSeq + 2),
2127 batch::Sig(dave),
2128 Ter(tesSUCCESS));
2129 env.close();
2130
2131 // Both inner tx applied
2132 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 20);
2133 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 40);
2134 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 40);
2135 }
2136 }
2137
2138 // Test missing delegation permission inside a batch.
2139 void
2141 {
2142 testcase("Batch delegation missing permission");
2143 using namespace test::jtx;
2144
2145 // AllOrNothing: dave has no Send permission from bob, so the delegated
2146 // inner send fails. The whole batch rolls back.
2147 {
2148 Env env{*this, features};
2149 Account const alice("alice");
2150 Account const bob("bob");
2151 Account const carol("carol");
2152 Account const dave("dave");
2153
2154 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
2155 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 60);
2156
2157 // Bob grants dave only MergeInbox, not Send.
2158 env(delegate::set(bob, dave, {"ConfidentialMPTMergeInbox"}));
2159 env.close();
2160
2161 auto const bobSeq = env.seq(bob);
2162 auto const carolSeq = env.seq(carol);
2163 auto const batchFee = batch::calcConfidentialBatchFee(env, 2, 2);
2164
2165 // jv1: direct send from carol (valid proof).
2166 auto const jv1 = mpt.sendJV({.account = carol, .dest = dave, .amt = 30}, carolSeq);
2167 // jv2: delegated send, fails because dave has no Send permission.
2168 auto jv2 = mpt.sendJV({.account = bob, .dest = carol, .amt = 50}, bobSeq + 1);
2169 jv2[jss::Delegate] = dave.human();
2170
2171 env(batch::outer(bob, bobSeq, batchFee, tfAllOrNothing),
2172 batch::Inner(jv1, carolSeq),
2173 batch::Inner(jv2, bobSeq + 1),
2174 batch::Sig(carol, dave),
2175 Ter(tesSUCCESS));
2176 env.close();
2177
2178 // jv1 applied in the batch view, then jv2 failed, so
2179 // AllOrNothing discards both inner effects.
2180 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
2181 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 60);
2182 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 0);
2183 }
2184
2185 // Independent: the delegated confidential send is skipped because lack of permission. The
2186 // send from carol still applies.
2187 {
2188 Env env{*this, features};
2189 Account const alice("alice");
2190 Account const bob("bob");
2191 Account const carol("carol");
2192 Account const dave("dave");
2193
2194 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
2195 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 60);
2196
2197 // Bob does not grant dave any permissions.
2198 auto const bobSeq = env.seq(bob);
2199 auto const carolSeq = env.seq(carol);
2200 auto const batchFee = batch::calcConfidentialBatchFee(env, 2, 2);
2201
2202 auto jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 50}, bobSeq + 1);
2203 jv1[jss::Delegate] = dave.human();
2204 auto const jv2 = mpt.sendJV({.account = carol, .dest = dave, .amt = 30}, carolSeq);
2205
2206 env(batch::outer(bob, bobSeq, batchFee, tfIndependent),
2207 batch::Inner(jv1, bobSeq + 1),
2208 batch::Inner(jv2, carolSeq),
2209 batch::Sig(carol, dave),
2210 Ter(tesSUCCESS));
2211 env.close();
2212
2213 // jv1 failed and jv2 applied.
2214 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
2215 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 30);
2216 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 30);
2217 }
2218 }
2219
2220 // Test batch outer signer is the delegated account.
2221 void
2223 {
2224 testcase("Test batch delegated send with delegate as outer account");
2225 using namespace test::jtx;
2226
2227 // Dave holds bob's ConfidentialMPTSend delegation and is the outer batch
2228 // signer, so dave's outer signature consents to the delegated inner.
2229 // The batch applies.
2230 {
2231 Env env{*this, features};
2232 Account const alice("alice");
2233 Account const bob("bob");
2234 Account const carol("carol");
2235 Account const dave("dave");
2236
2237 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
2238 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
2239
2240 env(delegate::set(bob, dave, {"ConfidentialMPTSend"}));
2241 env.close();
2242
2243 auto const daveSeq = env.seq(dave);
2244 auto const bobSeq = env.seq(bob);
2245 auto const batchFee = batch::calcConfidentialBatchFee(env, 0, 2);
2246
2247 auto jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 40}, bobSeq);
2248 jv1[jss::Delegate] = dave.human();
2249 auto const jv2 = mpt.mergeInboxJV({.account = dave});
2250
2251 env(batch::outer(dave, daveSeq, batchFee, tfAllOrNothing),
2252 batch::Inner(jv1, bobSeq),
2253 batch::Inner(jv2, daveSeq + 1),
2254 Ter(tesSUCCESS));
2255 env.close();
2256
2257 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 60);
2258 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 40);
2259 }
2260
2261 // Dave submits a mixed batch: bob signs inner tx1, and
2262 // dave is the Delegate account signing for inner tx2.
2263 {
2264 Env env{*this, features};
2265 Account const alice("alice");
2266 Account const bob("bob");
2267 Account const carol("carol");
2268 Account const dave("dave");
2269
2270 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
2271 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 0);
2272
2273 env(delegate::set(bob, dave, {"ConfidentialMPTSend"}));
2274 env.close();
2275
2276 auto const daveSeq = env.seq(dave);
2277 auto const bobSeq = env.seq(bob);
2278 auto const batchFee = batch::calcConfidentialBatchFee(env, 1, 2);
2279
2280 auto const jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 40}, bobSeq);
2281 auto const chain1 = mpt.chainAfterSend(bob, 40, jv1);
2282 auto jv2 = mpt.sendJV({.account = bob, .dest = carol, .amt = 30}, bobSeq + 1, chain1);
2283 jv2[jss::Delegate] = dave.human();
2284
2285 // Dave is outer; bob signs because his account appears in inner txns.
2286 env(batch::outer(dave, daveSeq, batchFee, tfAllOrNothing),
2287 batch::Inner(jv1, bobSeq),
2288 batch::Inner(jv2, bobSeq + 1),
2289 batch::Sig(bob),
2290 Ter(tesSUCCESS));
2291 env.close();
2292
2293 // Both sends applied: bob 100→30, carol inbox=70.
2294 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 30);
2295 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 70);
2296 }
2297
2298 // The delegated inner's required signer is the delegate (dave), not bob.
2299 // Bob signs but is not a required signer, so the batch is rejected as an
2300 // extra signer. The delegator's signature cannot stand in for the
2301 // delegate's.
2302 {
2303 Env env{*this, features};
2304 Account const alice("alice");
2305 Account const bob("bob");
2306 Account const carol("carol");
2307 Account const dave("dave");
2308
2309 MPTTester mpt(env, alice, {.holders = {bob, carol, dave}});
2310 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 60);
2311
2312 // Bob does not grant dave any permissions.
2313 auto const daveSeq = env.seq(dave);
2314 auto const bobSeq = env.seq(bob);
2315 auto const carolSeq = env.seq(carol);
2316 auto const batchFee = batch::calcConfidentialBatchFee(env, 2, 2);
2317
2318 auto jv1 = mpt.sendJV({.account = bob, .dest = carol, .amt = 50}, bobSeq);
2319 jv1[jss::Delegate] = dave.human();
2320 auto const jv2 = mpt.sendJV({.account = carol, .dest = dave, .amt = 30}, carolSeq);
2321
2322 env(batch::outer(dave, daveSeq, batchFee, tfAllOrNothing),
2323 batch::Inner(jv1, bobSeq),
2324 batch::Inner(jv2, carolSeq),
2325 batch::Sig(bob, carol),
2326 Ter(temBAD_SIGNER));
2327 env.close();
2328
2329 // jv1 fails before jv2 is attempted.
2330 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 100);
2331 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 60);
2332 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 0);
2333 }
2334 }
2335
2336 // Mixed batch with delegated and non-delegated inner confidential MPT transactions.
2337 void
2339 {
2340 testcase("Batch delegated confidential multiple operations");
2341 using namespace test::jtx;
2342
2343 Env env{*this, features};
2344 Account const alice("alice");
2345 Account const bob("bob");
2346 Account const carol("carol");
2347 Account const dave("dave");
2348 Account const erin("erin");
2349 Account const frank("frank");
2350
2351 MPTTester mpt(env, alice, {.holders = {bob, carol, dave, frank}});
2352 setupBatchEnv(mpt, alice, bob, carol, dave, 100, 60);
2353 mpt.pay(alice, bob, 50);
2354 env.fund(XRP(10000), erin);
2355 env.close();
2356
2357 mpt.authorize({.account = frank});
2358 mpt.pay(alice, frank, 40);
2359 mpt.generateKeyPair(frank);
2360
2361 env(delegate::set(bob, dave, {"ConfidentialMPTConvertBack"}));
2362 env(delegate::set(carol, erin, {"ConfidentialMPTSend"}));
2363 env(delegate::set(bob, erin, {"ConfidentialMPTMergeInbox"}));
2364 env.close();
2365
2366 auto const daveSeq = env.seq(dave);
2367 auto const bobSeq = env.seq(bob);
2368 auto const carolSeq = env.seq(carol);
2369 auto const frankSeq = env.seq(frank);
2370 auto const batchFee = batch::calcConfidentialBatchFee(env, 4, 6);
2371
2372 // Dave submits the batch. Bob's convertback uses Dave as Delegate;
2373 // Convert is not delegable, so Bob signs his own convert inner tx.
2374 // Carol's send and Bob's mergeInbox use Erin as Delegate. Frank's
2375 // convert and mergeInbox are non-delegated.
2376 auto jv1 = mpt.convertBackJV({.account = bob, .amt = 30}, bobSeq);
2377 jv1[jss::Delegate] = dave.human();
2378 auto const jv2 = mpt.convertJV({.account = bob, .amt = 20}, bobSeq + 1);
2379 auto jv3 = mpt.sendJV({.account = carol, .dest = bob, .amt = 15}, carolSeq);
2380 jv3[jss::Delegate] = erin.human();
2381 auto const jv4 = mpt.convertJV(
2382 {.account = frank, .amt = 25, .holderPubKey = mpt.getPubKey(frank)}, frankSeq);
2383 auto const jv5 = mpt.mergeInboxJV({.account = frank});
2384 auto jv6 = mpt.mergeInboxJV({.account = bob});
2385 jv6[jss::Delegate] = erin.human();
2386
2387 env(batch::outer(dave, daveSeq, batchFee, tfAllOrNothing),
2388 batch::Inner(jv1, bobSeq),
2389 batch::Inner(jv2, bobSeq + 1),
2390 batch::Inner(jv3, carolSeq),
2391 batch::Inner(jv4, frankSeq),
2392 batch::Inner(jv5, frankSeq + 1),
2393 batch::Inner(jv6, bobSeq + 2),
2394 batch::Sig(erin, frank, bob),
2395 Ter(tesSUCCESS));
2396 env.close();
2397
2398 env.require(MptBalance(mpt, bob, 60));
2399 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending) == 105);
2400 BEAST_EXPECT(mpt.getDecryptedBalance(bob, MPTTester::holderEncryptedInbox) == 0);
2401 env.require(MptBalance(mpt, carol, 0));
2402 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedSpending) == 45);
2403 BEAST_EXPECT(mpt.getDecryptedBalance(carol, MPTTester::holderEncryptedInbox) == 0);
2404 env.require(MptBalance(mpt, frank, 15));
2405 BEAST_EXPECT(mpt.getDecryptedBalance(frank, MPTTester::holderEncryptedSpending) == 25);
2406 BEAST_EXPECT(mpt.getDecryptedBalance(frank, MPTTester::holderEncryptedInbox) == 0);
2407 env.require(MptBalance(mpt, dave, 0));
2408 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedSpending) == 0);
2409 BEAST_EXPECT(mpt.getDecryptedBalance(dave, MPTTester::holderEncryptedInbox) == 0);
2410 auto const outstandingBalance = mpt.getIssuanceOutstandingBalance();
2411 BEAST_EXPECT(outstandingBalance && *outstandingBalance == 250);
2412 BEAST_EXPECT(mpt.getIssuanceConfidentialBalance() == 175);
2413 }
2414
2415 // Test invalid scenarios for delegation with tickets. ConfidentialMPTConvert
2416 // is not delegable, so ConfidentialMPTConvertBack (which is delegable and
2417 // whose ZK proof also binds to the transaction/ticket sequence) is used as
2418 // the delegated operation. Carol acts as bob's delegate throughout.
2419 void
2421 {
2422 testcase("Invalid cases for delegation with tickets");
2423 using namespace test::jtx;
2424
2425 Env env{*this, features};
2426 Account const alice("alice");
2427 Account const bob("bob");
2428 Account const carol("carol");
2429 MPTTester mptAlice(env, alice, {.holders = {bob}});
2430 env.fund(XRP(10000), carol);
2431 env.close();
2432
2433 mptAlice.create({
2434 .ownerCount = 1,
2435 .flags = tfMPTCanTransfer | tfMPTCanHoldConfidentialBalance | tfMPTCanClawback,
2436 });
2437 mptAlice.authorize({.account = bob});
2438 mptAlice.pay(alice, bob, 200);
2439
2440 mptAlice.generateKeyPair(alice);
2441 mptAlice.generateKeyPair(bob);
2442 mptAlice.set({.issuerPubKey = mptAlice.getPubKey(alice)});
2443
2444 // Give bob a confidential spending balance to convert back from.
2445 mptAlice.convert({.account = bob, .amt = 100, .holderPubKey = mptAlice.getPubKey(bob)});
2446 mptAlice.mergeInbox({.account = bob});
2447
2448 // Bob delegates ConfidentialMPTConvertBack to carol.
2449 env(delegate::set(bob, carol, {"ConfidentialMPTConvertBack"}));
2450 env.close();
2451
2452 uint64_t const amt = 10;
2453
2454 // Every case below fails, so bob's spending balance and version never
2455 // change; capture the crypto material needed to build proofs once.
2456 auto const spendingBalance = requireOptional(
2457 mptAlice.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending),
2458 "Missing spending balance.");
2459 auto const encSpending = requireOptional(
2460 mptAlice.getEncryptedBalance(bob, MPTTester::holderEncryptedSpending),
2461 "Missing encrypted spending balance.");
2462 auto const version = mptAlice.getMPTokenVersion(bob);
2463 auto const pcBf = generateBlindingFactor();
2464 auto const pc = mptAlice.getPedersenCommitment(spendingBalance, pcBf);
2465
2466 // Build a ConvertBack proof bound to a given sequence.
2467 auto const proofForSeq = [&](std::uint32_t seq) {
2468 return mptAlice.getConvertBackProof(
2469 bob,
2470 amt,
2471 getConvertBackContextHash(bob, mptAlice.issuanceID(), seq, version),
2472 {
2473 .pedersenCommitment = pc,
2474 .amt = spendingBalance,
2475 .encryptedAmt = encSpending,
2476 .blindingFactor = pcBf,
2477 });
2478 };
2479
2480 // Invalid: proof built with wrong ticket sequence (ticketSeq + 1).
2481 {
2482 auto const ticketSeq = env.seq(bob) + 1;
2483 env(ticket::create(bob, 1));
2484
2485 mptAlice.convertBack({
2486 .account = bob,
2487 .amt = amt,
2488 .proof = proofForSeq(ticketSeq + 1),
2489 .pedersenCommitment = pc,
2490 .delegate = carol,
2491 .ticketSeq = ticketSeq,
2492 .err = tecBAD_PROOF,
2493 });
2494 }
2495
2496 // Invalid: proof built with account sequence instead of ticket sequence.
2497 {
2498 auto const ticketSeq = env.seq(bob) + 1;
2499 env(ticket::create(bob, 1));
2500
2501 mptAlice.convertBack({
2502 .account = bob,
2503 .amt = amt,
2504 .proof = proofForSeq(env.seq(bob)),
2505 .pedersenCommitment = pc,
2506 .delegate = carol,
2507 .ticketSeq = ticketSeq,
2508 .err = tecBAD_PROOF,
2509 });
2510 }
2511
2512 // Invalid: ticket sequence is far in the future and hasn't been created yet.
2513 {
2514 mptAlice.convertBack({
2515 .account = bob,
2516 .amt = amt,
2517 .delegate = carol,
2518 .ticketSeq = env.seq(bob) + 100,
2519 .err = terPRE_TICKET,
2520 });
2521 }
2522
2523 // Invalid: ticket sequence is in the past but was never created.
2524 {
2525 mptAlice.convertBack({
2526 .account = bob,
2527 .amt = amt,
2528 .delegate = carol,
2529 .ticketSeq = 1,
2530 .err = tefNO_TICKET,
2531 });
2532 }
2533
2534 // Invalid: the delegated account, carol, creates a ticket and uses it.
2535 // The ticket must belong to the delegator (bob), not the delegate.
2536 {
2537 auto const carolTicketSeq = env.seq(carol) + 1;
2538 env(ticket::create(carol, 1));
2539
2540 mptAlice.convertBack({
2541 .account = bob,
2542 .amt = amt,
2543 .delegate = carol,
2544 .ticketSeq = carolTicketSeq,
2545 .err = tefNO_TICKET,
2546 });
2547 }
2548
2549 // Invalid: proof bound to a ticket sequence but submitted without a ticket,
2550 // using account sequence.
2551 {
2552 auto const ticketSeq = env.seq(bob) + 1;
2553 env(ticket::create(bob, 1));
2554
2555 // Submit without a ticket; proof is bound to ticketSeq.
2556 mptAlice.convertBack({
2557 .account = bob,
2558 .amt = amt,
2559 .proof = proofForSeq(ticketSeq),
2560 .pedersenCommitment = pc,
2561 .delegate = carol,
2562 .err = tecBAD_PROOF,
2563 });
2564 }
2565
2566 // Valid: carol converts back on bob's behalf using a ticket owned by bob,
2567 // with a proof correctly bound to that ticket sequence. bob's spending
2568 // balance drops from 100 to 90.
2569 {
2570 auto const ticketSeq = env.seq(bob) + 1;
2571 env(ticket::create(bob, 1));
2572
2573 mptAlice.convertBack({
2574 .account = bob,
2575 .amt = amt,
2576 .delegate = carol,
2577 .ticketSeq = ticketSeq,
2578 });
2579 }
2580 }
2581
2582 // Verifies that delegation works correctly when the delegating account uses
2583 // tickets instead of regular sequence numbers. The proof must bind to the
2584 // ticket sequence, not the account sequence.
2585 void
2587 {
2588 testcase("Confidential delegation with tickets");
2589 using namespace test::jtx;
2590
2591 Env env{*this, features};
2592 Account const alice("alice");
2593 Account const bob("bob");
2594 Account const carol("carol");
2595 Account const dave("dave");
2596 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
2597 env.fund(XRP(10000), dave);
2598 env.close();
2599
2600 mptAlice.create({
2601 .ownerCount = 1,
2602 .flags = tfMPTCanTransfer | tfMPTCanHoldConfidentialBalance | tfMPTCanClawback,
2603 });
2604 mptAlice.authorize({.account = bob});
2605 mptAlice.authorize({.account = carol});
2606 mptAlice.pay(alice, bob, 200);
2607 mptAlice.pay(alice, carol, 100);
2608
2609 mptAlice.generateKeyPair(alice);
2610 mptAlice.generateKeyPair(bob);
2611 mptAlice.generateKeyPair(carol);
2612 mptAlice.set({.issuerPubKey = mptAlice.getPubKey(alice)});
2613
2614 // Bob grants dave permissions (Convert is not delegable).
2615 env(delegate::set(
2616 bob,
2617 dave,
2618 {"ConfidentialMPTMergeInbox", "ConfidentialMPTSend", "ConfidentialMPTConvertBack"}));
2619 // Alice grants dave permission to clawback on her behalf.
2620 env(delegate::set(alice, dave, {"ConfidentialMPTClawback"}));
2621 env.close();
2622
2623 // Bob converts using a ticket.
2624 auto ticketSeq = env.seq(bob) + 1;
2625 env(ticket::create(bob, 1));
2626 BEAST_EXPECT(env.seq(bob) != ticketSeq);
2627 mptAlice.convert({
2628 .account = bob,
2629 .amt = 100,
2630 .holderPubKey = mptAlice.getPubKey(bob),
2631 .ticketSeq = ticketSeq,
2632 });
2633 env.require(MptBalance(mptAlice, bob, 100));
2634
2635 // MergeInbox using ticket with delegation.
2636 ticketSeq = env.seq(bob) + 1;
2637 env(ticket::create(bob, 1));
2638 BEAST_EXPECT(env.seq(bob) != ticketSeq);
2639 mptAlice.mergeInbox({.account = bob, .delegate = dave, .ticketSeq = ticketSeq});
2640
2641 // Carol converts and merges inbox to receive from bob.
2642 mptAlice.convert({
2643 .account = carol,
2644 .amt = 50,
2645 .holderPubKey = mptAlice.getPubKey(carol),
2646 });
2647 mptAlice.mergeInbox({.account = carol});
2648
2649 // Send using ticket with delegation.
2650 ticketSeq = env.seq(bob) + 1;
2651 env(ticket::create(bob, 1));
2652 BEAST_EXPECT(env.seq(bob) != ticketSeq);
2653 mptAlice.send({
2654 .account = bob,
2655 .dest = carol,
2656 .amt = 20,
2657 .delegate = dave,
2658 .ticketSeq = ticketSeq,
2659 });
2660
2661 // ConvertBack using ticket with delegation.
2662 ticketSeq = env.seq(bob) + 1;
2663 env(ticket::create(bob, 1));
2664 BEAST_EXPECT(env.seq(bob) != ticketSeq);
2665 mptAlice.convertBack({
2666 .account = bob,
2667 .amt = 10,
2668 .delegate = dave,
2669 .ticketSeq = ticketSeq,
2670 });
2671
2672 // Clawback using ticket with delegation.
2673 ticketSeq = env.seq(alice) + 1;
2674 env(ticket::create(alice, 1));
2675 BEAST_EXPECT(env.seq(alice) != ticketSeq);
2676 mptAlice.confidentialClaw({
2677 .holder = bob,
2678 .amt = 70,
2679 .delegate = dave,
2680 .ticketSeq = ticketSeq,
2681 });
2682 }
2683
2684 void
2686 {
2687 // DepositAuth, credentials, and destination tag interactions.
2688 testSendDepositPreauth(features);
2690 testDestinationTag(features);
2691
2692 // AMM/pseudo-account interaction.
2695
2696 // Ticket interactions.
2697 testWithTickets(features);
2699 testTicketErrors(features);
2700
2701 // Batch interactions.
2702 testBatchConfidentialSend(features);
2705 testBatchAllOrNothing(features);
2706 testBatchOnlyOne(features);
2707 testBatchUntilFailure(features);
2708 testBatchIndependent(features);
2709 testBatchWithTickets(features);
2710
2711 // Permission delegation interactions.
2713 testDelegationRevocation(features);
2714 testDelegationWithAuditor(features);
2716 testBatchDelegatedSend(features);
2721 testDelegationWithTickets(features);
2722 }
2723
2724public:
2725 void
2726 run() override
2727 {
2728 using namespace test::jtx;
2729 FeatureBitset const all{testableAmendments()};
2730
2731 testWithFeats(all);
2732 }
2733};
2734
2735BEAST_DEFINE_TESTSUITE(ConfidentialTransferExtended, app, xrpl);
2736
2737} // namespace xrpl
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
Like std::vector<char> but better.
Definition Buffer.h:19
void testBatchDelegatedSendWithDelegateAsOuterAccount(FeatureBitset features)
void testAMMHolderCannotHaveConfidentialStateClawback(FeatureBitset features)
static T requireOptional(std::optional< T > value, char const *message)
static void setupBatchEnv(test::jtx::MPTTester &mpt, test::jtx::Account const &alice, test::jtx::Account const &bob, test::jtx::Account const &carol, test::jtx::Account const &dave, std::uint64_t bobAmt, std::uint64_t carolAmt)
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:26
void send(MPTConfidentialSend const &arg=MPTConfidentialSend{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:1463
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:547
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terNO_DELEGATE_PERMISSION
Definition TER.h:232
@ terPRE_TICKET
Definition TER.h:228
@ RpcEntryNotFound
Definition ErrorCodes.h:144
std::uint32_t ownerCount(SLE::ConstRef sle, beast::Journal j, std::int32_t ownerCountAdj=0)
Return number of the objects which reserve is covered by the account(sle) (so called "ownercount").
UInt256 getConvertContextHash(AccountID const &account, UInt192 const &issuanceID, std::uint32_t sequence)
Generates the context hash for ConfidentialMPTConvert transactions.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
@ tefNO_TICKET
Definition TER.h:179
UInt256 getConvertBackContextHash(AccountID const &account, UInt192 const &issuanceID, std::uint32_t sequence, std::uint32_t version)
Generates the context hash for ConfidentialMPTConvertBack transactions.
constexpr std::size_t kEcClawbackProofLength
Length of the ZKProof for ConfidentialMPTClawback.
Definition Protocol.h:568
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BaseUInt< 256 > UInt256
Definition base_uint.h:581
Buffer generateBlindingFactor()
Generates a cryptographically secure blinding factor (size=xrpl::kEcBlindingFactorLength).
@ temINVALID
Definition TER.h:98
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
@ temBAD_SIGNER
Definition TER.h:103
@ tecBAD_CREDENTIALS
Definition TER.h:370
@ tecINVARIANT_FAILED
Definition TER.h:324
@ tecBAD_PROOF
Definition TER.h:379
@ tecEXPIRED
Definition TER.h:325
@ tecNO_PERMISSION
Definition TER.h:316
@ tecDST_TAG_NEEDED
Definition TER.h:320
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
@ tesSUCCESS
Definition TER.h:253
T push_back(T... args)
T reserve(T... args)