xrpld
Loading...
Searching...
No Matches
DepositAuth_test.cpp
1
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/balance.h> // IWYU pragma: keep
7#include <test/jtx/credentials.h>
8#include <test/jtx/deposit.h>
9#include <test/jtx/escrow.h>
10#include <test/jtx/fee.h>
11#include <test/jtx/flags.h>
12#include <test/jtx/noop.h>
13#include <test/jtx/offer.h>
14#include <test/jtx/owners.h>
15#include <test/jtx/paths.h>
16#include <test/jtx/pay.h>
17#include <test/jtx/require.h>
18#include <test/jtx/sendmax.h>
19#include <test/jtx/seq.h>
20#include <test/jtx/ter.h>
21#include <test/jtx/ticket.h>
22#include <test/jtx/trust.h>
23#include <test/jtx/txflags.h>
24#include <test/jtx/vault.h>
25
26#include <xrpl/basics/strHex.h>
27#include <xrpl/beast/unit_test/suite.h>
28#include <xrpl/json/json_value.h>
29#include <xrpl/json/to_string.h>
30#include <xrpl/protocol/AccountID.h>
31#include <xrpl/protocol/ErrorCodes.h>
32#include <xrpl/protocol/Feature.h>
33#include <xrpl/protocol/Issue.h>
34#include <xrpl/protocol/LedgerFormats.h>
35#include <xrpl/protocol/SField.h>
36#include <xrpl/protocol/STAmount.h>
37#include <xrpl/protocol/TER.h>
38#include <xrpl/protocol/TxFlags.h>
39#include <xrpl/protocol/XRPAmount.h>
40#include <xrpl/protocol/jss.h>
41
42#include <algorithm>
43#include <chrono>
44#include <cstdint>
45#include <random>
46#include <unordered_map>
47#include <utility>
48#include <vector>
49
50namespace xrpl::test {
51
52// Helper function that returns the reserve on an account based on
53// the passed in number of owners.
54static XRPAmount
55reserve(jtx::Env const& env, std::uint32_t count)
56{
57 return env.current()->fees().accountReserve(count, 1);
58}
59
60// Helper function that returns true if acct has the lsfDepositAuth flag set.
61static bool
62hasDepositAuth(jtx::Env const& env, jtx::Account const& acct)
63{
64 return env.le(acct)->isFlag(lsfDepositAuth);
65}
66
68{
69 void
71 {
72 testcase("Enable");
73
74 using namespace jtx;
75 Account const alice{"alice"};
76
77 {
78 Env env(*this);
79 env.fund(XRP(10000), alice);
80
81 env(fset(alice, asfDepositAuth));
82 env.close();
83 BEAST_EXPECT(hasDepositAuth(env, alice));
84
85 env(fclear(alice, asfDepositAuth));
86 env.close();
87 BEAST_EXPECT(!hasDepositAuth(env, alice));
88 }
89 }
90
91 void
93 {
94 // Exercise IOU payments and non-direct XRP payments to an account
95 // that has the lsfDepositAuth flag set.
96 testcase("Pay IOU");
97
98 using namespace jtx;
99 Account const alice{"alice"};
100 Account const bob{"bob"};
101 Account const carol{"carol"};
102 Account const gw{"gw"};
103 IOU const usd = gw["USD"];
104
105 Env env(*this);
106
107 env.fund(XRP(10000), alice, bob, carol, gw);
108 env.close();
109 env.trust(usd(1000), alice, bob);
110 env.close();
111
112 env(pay(gw, alice, usd(150)));
113 env(offer(carol, usd(100), XRP(100)));
114 env.close();
115
116 // Make sure bob's trust line is all set up so he can receive USD.
117 env(pay(alice, bob, usd(50)));
118 env.close();
119
120 // bob sets the lsfDepositAuth flag.
121 env(fset(bob, asfDepositAuth), Require(Flags(bob, asfDepositAuth)));
122 env.close();
123
124 // None of the following payments should succeed.
125 auto const failedIouPayments = [this, &env, &alice, &bob, &usd] {
126 env.require(Flags(bob, asfDepositAuth));
127
128 // Capture bob's balances before hand to confirm they don't change.
129 PrettyAmount const bobXrpBalance{env.balance(bob, XRP)};
130 PrettyAmount const bobUsdBalance{env.balance(bob, usd)};
131
132 env(pay(alice, bob, usd(50)), Ter(tecNO_PERMISSION));
133 env.close();
134
135 // Note that even though alice is paying bob in XRP, the payment
136 // is still not allowed since the payment passes through an offer.
137 env(pay(alice, bob, drops(1)), Sendmax(usd(1)), Ter(tecNO_PERMISSION));
138 env.close();
139
140 BEAST_EXPECT(bobXrpBalance == env.balance(bob, XRP));
141 BEAST_EXPECT(bobUsdBalance == env.balance(bob, usd));
142 };
143
144 // Test when bob has an XRP balance > base reserve.
145 failedIouPayments();
146
147 // Set bob's XRP balance == base reserve. Also demonstrate that
148 // bob can make payments while his lsfDepositAuth flag is set.
149 env(pay(bob, alice, usd(25)));
150 env.close();
151
152 {
153 STAmount const bobPaysXRP{env.balance(bob, XRP) - reserve(env, 1)};
154 XRPAmount const bobPaysFee{reserve(env, 1) - reserve(env, 0)};
155 env(pay(bob, alice, bobPaysXRP), Fee(bobPaysFee));
156 env.close();
157 }
158
159 // Test when bob's XRP balance == base reserve.
160 BEAST_EXPECT(env.balance(bob, XRP) == reserve(env, 0));
161 BEAST_EXPECT(env.balance(bob, usd) == usd(25));
162 failedIouPayments();
163
164 // Test when bob has an XRP balance == 0.
165 env(noop(bob), Fee(reserve(env, 0)));
166 env.close();
167
168 BEAST_EXPECT(env.balance(bob, XRP) == XRP(0));
169 failedIouPayments();
170
171 // Give bob enough XRP for the fee to clear the lsfDepositAuth flag.
172 env(pay(alice, bob, drops(env.current()->fees().base)));
173
174 // bob clears the lsfDepositAuth and the next payment succeeds.
175 env(fclear(bob, asfDepositAuth));
176 env.close();
177
178 env(pay(alice, bob, usd(50)));
179 env.close();
180
181 env(pay(alice, bob, drops(1)), Sendmax(usd(1)));
182 env.close();
183 }
184
185 void
187 {
188 // Exercise direct XRP payments to an account that has the
189 // lsfDepositAuth flag set.
190 testcase("Pay XRP");
191
192 using namespace jtx;
193 Account const alice{"alice"};
194 Account const bob{"bob"};
195
196 Env env(*this);
197 auto const baseFee = env.current()->fees().base;
198
199 env.fund(XRP(10000), alice, bob);
200 env.close();
201
202 // bob sets the lsfDepositAuth flag.
203 env(fset(bob, asfDepositAuth), Fee(drops(baseFee)));
204 env.close();
205 BEAST_EXPECT(env.balance(bob, XRP) == XRP(10000) - drops(baseFee));
206
207 // bob has more XRP than the base reserve. Any XRP payment should fail.
208 env(pay(alice, bob, drops(1)), Ter(tecNO_PERMISSION));
209 env.close();
210 BEAST_EXPECT(env.balance(bob, XRP) == XRP(10000) - drops(baseFee));
211
212 // Change bob's XRP balance to exactly the base reserve.
213 {
214 STAmount const bobPaysXRP{env.balance(bob, XRP) - reserve(env, 1)};
215 XRPAmount const bobPaysFee{reserve(env, 1) - reserve(env, 0)};
216 env(pay(bob, alice, bobPaysXRP), Fee(bobPaysFee));
217 env.close();
218 }
219
220 // bob has exactly the base reserve. A small enough direct XRP
221 // payment should succeed.
222 BEAST_EXPECT(env.balance(bob, XRP) == reserve(env, 0));
223 env(pay(alice, bob, drops(1)));
224 env.close();
225
226 // bob has exactly the base reserve + 1. No payment should succeed.
227 BEAST_EXPECT(env.balance(bob, XRP) == reserve(env, 0) + drops(1));
228 env(pay(alice, bob, drops(1)), Ter(tecNO_PERMISSION));
229 env.close();
230
231 // Take bob down to a balance of 0 XRP.
232 env(noop(bob), Fee(reserve(env, 0) + drops(1)));
233 env.close();
234 BEAST_EXPECT(env.balance(bob, XRP) == drops(0));
235
236 // We should not be able to pay bob more than the base reserve.
237 env(pay(alice, bob, reserve(env, 0) + drops(1)), Ter(tecNO_PERMISSION));
238 env.close();
239
240 // However a payment of exactly the base reserve should succeed.
241 env(pay(alice, bob, reserve(env, 0) + drops(0)));
242 env.close();
243 BEAST_EXPECT(env.balance(bob, XRP) == reserve(env, 0));
244
245 // We should be able to pay bob the base reserve one more time.
246 env(pay(alice, bob, reserve(env, 0) + drops(0)));
247 env.close();
248 BEAST_EXPECT(env.balance(bob, XRP) == (reserve(env, 0) + reserve(env, 0)));
249
250 // bob's above the threshold again. Any payment should fail.
251 env(pay(alice, bob, drops(1)), Ter(tecNO_PERMISSION));
252 env.close();
253 BEAST_EXPECT(env.balance(bob, XRP) == (reserve(env, 0) + reserve(env, 0)));
254
255 // Take bob back down to a zero XRP balance.
256 env(noop(bob), Fee(env.balance(bob, XRP)));
257 env.close();
258 BEAST_EXPECT(env.balance(bob, XRP) == drops(0));
259
260 // bob should not be able to clear lsfDepositAuth.
261 env(fclear(bob, asfDepositAuth), Ter(terINSUF_FEE_B));
262 env.close();
263
264 // We should be able to pay bob 1 drop now.
265 env(pay(alice, bob, drops(1)));
266 env.close();
267 BEAST_EXPECT(env.balance(bob, XRP) == drops(1));
268
269 // Pay bob enough so he can afford the fee to clear lsfDepositAuth.
270 env(pay(alice, bob, drops(baseFee - 1)));
271 env.close();
272
273 // Interestingly, at this point the terINSUF_FEE_B retry grabs the
274 // request to clear lsfDepositAuth. So the balance should be zero
275 // and lsfDepositAuth should be cleared.
276 BEAST_EXPECT(env.balance(bob, XRP) == drops(0));
277 env.require(Nflags(bob, asfDepositAuth));
278
279 // Since bob no longer has lsfDepositAuth set we should be able to
280 // pay him more than the base reserve.
281 env(pay(alice, bob, reserve(env, 0) + drops(1)));
282 env.close();
283 BEAST_EXPECT(env.balance(bob, XRP) == reserve(env, 0) + drops(1));
284 }
285
286 void
288 {
289 // It its current incarnation the DepositAuth flag does not change
290 // any behaviors regarding rippling and the NoRipple flag.
291 // Demonstrate that.
292 testcase("No Ripple");
293
294 using namespace jtx;
295 Account const gw1("gw1");
296 Account const gw2("gw2");
297 Account const alice("alice");
298 Account const bob("bob");
299
300 IOU const usD1(gw1["USD"]);
301 IOU const usD2(gw2["USD"]);
302
303 auto const testIssuer = [&](FeatureBitset const& features,
304 bool noRipplePrev,
305 bool noRippleNext,
306 bool withDepositAuth) {
307 Env env(*this, features);
308
309 env.fund(XRP(10000), gw1, alice, bob);
310 env.close();
311 env(trust(gw1, alice["USD"](10), noRipplePrev ? tfSetNoRipple : 0));
312 env(trust(gw1, bob["USD"](10), noRippleNext ? tfSetNoRipple : 0));
313 env.trust(usD1(10), alice, bob);
314
315 env(pay(gw1, alice, usD1(10)));
316
317 if (withDepositAuth)
318 env(fset(gw1, asfDepositAuth));
319
320 TER const result = (noRippleNext && noRipplePrev) ? TER{tecPATH_DRY} : TER{tesSUCCESS};
321 env(pay(alice, bob, usD1(10)), Path(gw1), Ter(result));
322 };
323
324 auto const testNonIssuer = [&](FeatureBitset const& features,
325 bool noRipplePrev,
326 bool noRippleNext,
327 bool withDepositAuth) {
328 Env env(*this, features);
329
330 env.fund(XRP(10000), gw1, gw2, alice);
331 env.close();
332 env(trust(alice, usD1(10), noRipplePrev ? tfSetNoRipple : 0));
333 env(trust(alice, usD2(10), noRippleNext ? tfSetNoRipple : 0));
334 env(pay(gw2, alice, usD2(10)));
335
336 if (withDepositAuth)
337 env(fset(alice, asfDepositAuth));
338
339 TER const result = (noRippleNext && noRipplePrev) ? TER{tecPATH_DRY} : TER{tesSUCCESS};
340 env(pay(gw1, gw2, usD2(10)), Path(alice), Sendmax(usD1(10)), Ter(result));
341 };
342
343 // Test every combo of noRipplePrev, noRippleNext, and withDepositAuth
344 for (int i = 0; i < 8; ++i)
345 {
346 auto const noRipplePrev = i & 0x1;
347 auto const noRippleNext = i & 0x2;
348 auto const withDepositAuth = i & 0x4;
349 testIssuer(
350 testableAmendments(), noRipplePrev != 0, noRippleNext != 0, withDepositAuth != 0);
351
352 testNonIssuer(
353 testableAmendments(), noRipplePrev != 0, noRippleNext != 0, withDepositAuth != 0);
354 }
355 }
356
357 void
358 run() override
359 {
360 testEnable();
361 testPayIOU();
362 testPayXRP();
363 testNoRipple();
364 }
365};
366
367static json::Value
369 jtx::Env& env,
370 jtx::Account const& acc,
372{
373 json::Value jvParams;
374 jvParams[jss::ledger_index] = jss::validated;
375 jvParams[jss::deposit_preauth][jss::owner] = acc.human();
376 jvParams[jss::deposit_preauth][jss::authorized_credentials] = json::ValueType::Array;
377 auto& arr(jvParams[jss::deposit_preauth][jss::authorized_credentials]);
378 for (auto const& o : auth)
379 {
380 arr.append(o.toLEJson());
381 }
382 return env.rpc("json", "ledger_entry", to_string(jvParams));
383}
384
386{
387 void
389 {
390 testcase("Enable");
391
392 using namespace jtx;
393 Account const alice{"alice"};
394 Account const becky{"becky"};
395 {
396 // o We should be able to add and remove an entry, and
397 // o That entry should cost one reserve.
398 // o The reserve should be returned when the entry is removed.
399 Env env(*this);
400 env.fund(XRP(10000), alice, becky);
401 env.close();
402
403 // Add a DepositPreauth to alice.
404 env(deposit::auth(alice, becky));
405 env.close();
406 env.require(Owners(alice, 1));
407 env.require(Owners(becky, 0));
408
409 // Remove a DepositPreauth from alice.
410 env(deposit::unauth(alice, becky));
411 env.close();
412 env.require(Owners(alice, 0));
413 env.require(Owners(becky, 0));
414 }
415 {
416 // Verify that an account can be preauthorized and unauthorized
417 // using tickets.
418 Env env(*this);
419 env.fund(XRP(10000), alice, becky);
420 env.close();
421
422 env(ticket::create(alice, 2));
423 std::uint32_t const aliceSeq{env.seq(alice)};
424 env.close();
425 env.require(Tickets(alice, 2));
426
427 // Consume the tickets from biggest seq to smallest 'cuz we can.
428 std::uint32_t aliceTicketSeq{env.seq(alice)};
429
430 // Add a DepositPreauth to alice.
431 env(deposit::auth(alice, becky), ticket::Use(--aliceTicketSeq));
432 env.close();
433 // Alice uses a ticket but gains a preauth entry.
434 env.require(Tickets(alice, 1));
435 env.require(Owners(alice, 2));
436 BEAST_EXPECT(env.seq(alice) == aliceSeq);
437 env.require(Owners(becky, 0));
438
439 // Remove a DepositPreauth from alice.
440 env(deposit::unauth(alice, becky), ticket::Use(--aliceTicketSeq));
441 env.close();
442 env.require(Tickets(alice, 0));
443 env.require(Owners(alice, 0));
444 BEAST_EXPECT(env.seq(alice) == aliceSeq);
445 env.require(Owners(becky, 0));
446 }
447 }
448
449 void
451 {
452 testcase("Invalid");
453
454 using namespace jtx;
455 Account const alice{"alice"};
456 Account const becky{"becky"};
457 Account const carol{"carol"};
458
459 Env env(*this, features);
460
461 // Tell env about alice, becky and carol since they are not yet funded.
462 env.memoize(alice);
463 env.memoize(becky);
464 env.memoize(carol);
465
466 // Add DepositPreauth to an unfunded account.
467 env(deposit::auth(alice, becky), Seq(1), Ter(terNO_ACCOUNT));
468
469 env.fund(XRP(10000), alice, becky);
470 env.close();
471
472 // Bad fee.
473 env(deposit::auth(alice, becky), Fee(drops(-10)), Ter(temBAD_FEE));
474 env.close();
475
476 // Bad flags.
477 env(deposit::auth(alice, becky), Txflags(tfSell), Ter(temINVALID_FLAG));
478 env.close();
479
480 {
481 // Neither auth not unauth.
482 json::Value tx{deposit::auth(alice, becky)};
483 tx.removeMember(sfAuthorize.jsonName);
484 env(tx, Ter(temMALFORMED));
485 env.close();
486 }
487 {
488 // Both auth and unauth.
489 json::Value tx{deposit::auth(alice, becky)};
490 tx[sfUnauthorize.jsonName] = becky.human();
491 env(tx, Ter(temMALFORMED));
492 env.close();
493 }
494 {
495 // Alice authorizes a zero account.
496 json::Value tx{deposit::auth(alice, becky)};
497 tx[sfAuthorize.jsonName] = to_string(xrpAccount());
498 env(tx, Ter(temINVALID_ACCOUNT_ID));
499 env.close();
500 }
501
502 // alice authorizes herself.
503 env(deposit::auth(alice, alice), Ter(temCANNOT_PREAUTH_SELF));
504 env.close();
505
506 // alice authorizes an unfunded account.
507 env(deposit::auth(alice, carol), Ter(tecNO_TARGET));
508 env.close();
509
510 // alice successfully authorizes becky.
511 env.require(Owners(alice, 0));
512 env.require(Owners(becky, 0));
513 env(deposit::auth(alice, becky));
514 env.close();
515 env.require(Owners(alice, 1));
516 env.require(Owners(becky, 0));
517
518 // alice attempts to create a duplicate authorization.
519 env(deposit::auth(alice, becky), Ter(tecDUPLICATE));
520 env.close();
521 env.require(Owners(alice, 1));
522 env.require(Owners(becky, 0));
523
524 // carol attempts to preauthorize but doesn't have enough reserve.
525 env.fund(drops(249'999'999), carol);
526 env.close();
527
528 env(deposit::auth(carol, becky), Ter(tecINSUFFICIENT_RESERVE));
529 env.close();
530 env.require(Owners(carol, 0));
531 env.require(Owners(becky, 0));
532
533 // carol gets enough XRP to (barely) meet the reserve.
534 env(pay(alice, carol, drops(env.current()->fees().base + 1)));
535 env.close();
536 env(deposit::auth(carol, becky));
537 env.close();
538 env.require(Owners(carol, 1));
539 env.require(Owners(becky, 0));
540
541 // But carol can't meet the reserve for another pre-authorization.
542 env(deposit::auth(carol, alice), Ter(tecINSUFFICIENT_RESERVE));
543 env.close();
544 env.require(Owners(carol, 1));
545 env.require(Owners(becky, 0));
546 env.require(Owners(alice, 1));
547
548 // alice attempts to remove an authorization she doesn't have.
549 env(deposit::unauth(alice, carol), Ter(tecNO_ENTRY));
550 env.close();
551 env.require(Owners(alice, 1));
552 env.require(Owners(becky, 0));
553
554 // alice successfully removes her authorization of becky.
555 env(deposit::unauth(alice, becky));
556 env.close();
557 env.require(Owners(alice, 0));
558 env.require(Owners(becky, 0));
559
560 // alice removes becky again and gets an error.
561 env(deposit::unauth(alice, becky), Ter(tecNO_ENTRY));
562 env.close();
563 env.require(Owners(alice, 0));
564 env.require(Owners(becky, 0));
565
566 {
567 // alice attempts to authorize a pseudo-account.
568 Vault const vault{env};
569 auto [tx, keylet] = vault.create({.owner = becky, .asset = xrpIssue()});
570 env(tx);
571 env.close();
572
573 auto const sleVault = env.le(keylet);
574 if (!BEAST_EXPECT(sleVault))
575 return;
576 Account const vaultPseudo{"vault", sleVault->at(sfAccount)};
577
578 auto const expectedResult =
579 features[fixCleanup3_3_0] ? Ter(tecPSEUDO_ACCOUNT) : Ter(tesSUCCESS);
580 env(deposit::auth(alice, vaultPseudo), expectedResult);
581 env.close();
582 env.require(Owners(alice, features[fixCleanup3_3_0] ? 0 : 1));
583 }
584 }
585
586 void
588 {
589 testcase("Payment");
590
591 using namespace jtx;
592 Account const alice{"alice"};
593 Account const becky{"becky"};
594 Account const gw{"gw"};
595 IOU const usd(gw["USD"]);
596
597 {
598 // The initial implementation of DepositAuth had a bug where an
599 // account with the DepositAuth flag set could not make a payment
600 // to itself. That bug was fixed in the DepositPreauth amendment.
601 Env env(*this, features);
602 env.fund(XRP(5000), alice, becky, gw);
603 env.close();
604
605 env.trust(usd(1000), alice);
606 env.trust(usd(1000), becky);
607 env.close();
608
609 env(pay(gw, alice, usd(500)));
610 env.close();
611
612 env(offer(alice, XRP(100), usd(100), tfPassive), Require(Offers(alice, 1)));
613 env.close();
614
615 // becky pays herself USD (10) by consuming part of alice's offer.
616 // Make sure the payment works if PaymentAuth is not involved.
617 env(pay(becky, becky, usd(10)), Path(~usd), Sendmax(XRP(10)));
618 env.close();
619
620 // becky decides to require authorization for deposits.
621 env(fset(becky, asfDepositAuth));
622 env.close();
623
624 // becky pays herself again.
625 env(pay(becky, becky, usd(10)), Path(~usd), Sendmax(XRP(10)), Ter(tesSUCCESS));
626 env.close();
627
628 {
629 // becky setup depositpreauth with credentials
630 char const credType[] = "abcde";
631 Account const carol{"carol"};
632 env.fund(XRP(5000), carol);
633 env.close();
634
635 bool const supportsCredentials = features[featureCredentials];
636
637 TER const expectTer(!supportsCredentials ? TER(temDISABLED) : TER(tesSUCCESS));
638
639 env(deposit::authCredentials(becky, {{.issuer = carol, .credType = credType}}),
640 Ter(expectTer));
641 env.close();
642
643 // gw accept credentials
644 env(credentials::create(gw, carol, credType), Ter(expectTer));
645 env.close();
646 env(credentials::accept(gw, carol, credType), Ter(expectTer));
647 env.close();
648
649 auto jv = credentials::ledgerEntry(env, gw, carol, credType);
650 std::string const credIdx = supportsCredentials
651 ? jv[jss::result][jss::index].asString()
652 : "48004829F915654A81B11C4AB8218D96FED67F209B58328A72314FB6"
653 "EA288BE4";
654
655 env(pay(gw, becky, usd(100)), credentials::Ids({credIdx}), Ter(expectTer));
656 env.close();
657 }
658 }
659
660 // Make sure DepositPreauthorization works for payments.
661
662 Account const carol{"carol"};
663
664 Env env(*this, features);
665 env.fund(XRP(5000), alice, becky, carol, gw);
666 env.close();
667
668 env.trust(usd(1000), alice);
669 env.trust(usd(1000), becky);
670 env.trust(usd(1000), carol);
671 env.close();
672
673 env(pay(gw, alice, usd(1000)));
674 env.close();
675
676 // Make XRP and IOU payments from alice to becky. Should be fine.
677 env(pay(alice, becky, XRP(100)));
678 env(pay(alice, becky, usd(100)));
679 env.close();
680
681 // becky decides to require authorization for deposits.
682 env(fset(becky, asfDepositAuth));
683 env.close();
684
685 // alice can no longer pay becky.
686 env(pay(alice, becky, XRP(100)), Ter(tecNO_PERMISSION));
687 env(pay(alice, becky, usd(100)), Ter(tecNO_PERMISSION));
688 env.close();
689
690 // becky preauthorizes carol for deposit, which doesn't provide
691 // authorization for alice.
692 env(deposit::auth(becky, carol));
693 env.close();
694
695 // alice still can't pay becky.
696 env(pay(alice, becky, XRP(100)), Ter(tecNO_PERMISSION));
697 env(pay(alice, becky, usd(100)), Ter(tecNO_PERMISSION));
698 env.close();
699
700 // becky preauthorizes alice for deposit.
701 env(deposit::auth(becky, alice));
702 env.close();
703
704 // alice can now pay becky.
705 env(pay(alice, becky, XRP(100)));
706 env(pay(alice, becky, usd(100)));
707 env.close();
708
709 // alice decides to require authorization for deposits.
710 env(fset(alice, asfDepositAuth));
711 env.close();
712
713 // Even though alice is authorized to pay becky, becky is not
714 // authorized to pay alice.
715 env(pay(becky, alice, XRP(100)), Ter(tecNO_PERMISSION));
716 env(pay(becky, alice, usd(100)), Ter(tecNO_PERMISSION));
717 env.close();
718
719 // becky unauthorizes carol. Should have no impact on alice.
720 env(deposit::unauth(becky, carol));
721 env.close();
722
723 env(pay(alice, becky, XRP(100)));
724 env(pay(alice, becky, usd(100)));
725 env.close();
726
727 // becky unauthorizes alice. alice now can't pay becky.
728 env(deposit::unauth(becky, alice));
729 env.close();
730
731 env(pay(alice, becky, XRP(100)), Ter(tecNO_PERMISSION));
732 env(pay(alice, becky, usd(100)), Ter(tecNO_PERMISSION));
733 env.close();
734
735 // becky decides to remove authorization for deposits. Now
736 // alice can pay becky again.
737 env(fclear(becky, asfDepositAuth));
738 env.close();
739
740 env(pay(alice, becky, XRP(100)));
741 env(pay(alice, becky, usd(100)));
742 env.close();
743 }
744
745 void
747 {
748 using namespace jtx;
749
750 char const credType[] = "abcde";
751 Account const issuer{"issuer"};
752 Account const alice{"alice"};
753 Account const bob{"bob"};
754 Account const maria{"maria"};
755 Account const john{"john"};
756
757 {
758 testcase("Payment failure with disabled credentials rule.");
759
760 Env env(*this, testableAmendments() - featureCredentials);
761
762 env.fund(XRP(5000), issuer, bob, alice);
763 env.close();
764
765 // Bob require pre-authorization
766 env(fset(bob, asfDepositAuth));
767 env.close();
768
769 // Setup DepositPreauth object failed - amendent is not supported
770 env(deposit::authCredentials(bob, {{.issuer = issuer, .credType = credType}}),
772 env.close();
773
774 // But can create old DepositPreauth
775 env(deposit::auth(bob, alice));
776 env.close();
777
778 // And alice can't pay with any credentials, amendment is not
779 // enabled
780 std::string const invalidIdx =
781 "0E0B04ED60588A758B67E21FBBE95AC5A63598BA951761DC0EC9C08D7E"
782 "01E034";
783 env(pay(alice, bob, XRP(10)), credentials::Ids({invalidIdx}), Ter(temDISABLED));
784 env.close();
785 }
786
787 {
788 testcase("Payment with credentials.");
789
790 Env env(*this);
791
792 env.fund(XRP(5000), issuer, alice, bob, john);
793 env.close();
794
795 // Issuer create credentials, but Alice didn't accept them yet
796 env(credentials::create(alice, issuer, credType));
797 env.close();
798
799 // Get the index of the credentials
800 auto const jv = credentials::ledgerEntry(env, alice, issuer, credType);
801 std::string const credIdx = jv[jss::result][jss::index].asString();
802
803 // Bob require pre-authorization
804 env(fset(bob, asfDepositAuth));
805 env.close();
806
807 // Bob will accept payments from accounts with credentials signed
808 // by 'issuer'
809 env(deposit::authCredentials(bob, {{.issuer = issuer, .credType = credType}}));
810 env.close();
811
812 auto const jDP =
813 ledgerEntryDepositPreauth(env, bob, {{.issuer = issuer, .credType = credType}});
814 BEAST_EXPECT(
815 jDP.isObject() && jDP.isMember(jss::result) &&
816 !jDP[jss::result].isMember(jss::error) && jDP[jss::result].isMember(jss::node) &&
817 jDP[jss::result][jss::node].isMember("LedgerEntryType") &&
818 jDP[jss::result][jss::node]["LedgerEntryType"] == jss::DepositPreauth);
819
820 // Alice can't pay - empty credentials array
821 {
822 auto jv = pay(alice, bob, XRP(100));
823 jv[sfCredentialIDs.jsonName] = json::ValueType::Array;
824 env(jv, Ter(temMALFORMED));
825 env.close();
826 }
827
828 // Alice can't pay - not accepted credentials
829 env(pay(alice, bob, XRP(100)), credentials::Ids({credIdx}), Ter(tecBAD_CREDENTIALS));
830 env.close();
831
832 // Alice accept the credentials
833 env(credentials::accept(alice, issuer, credType));
834 env.close();
835
836 // Now Alice can pay
837 env(pay(alice, bob, XRP(100)), credentials::Ids({credIdx}));
838 env.close();
839
840 // Alice can pay Maria without depositPreauth enabled
841 env(pay(alice, maria, XRP(250)), credentials::Ids({credIdx}));
842 env.close();
843
844 // john can accept payment with old depositPreauth and valid
845 // credentials
846 env(fset(john, asfDepositAuth));
847 env(deposit::auth(john, alice));
848 env(pay(alice, john, XRP(100)), credentials::Ids({credIdx}));
849 env.close();
850 }
851
852 {
853 testcase("Payment failure with invalid credentials.");
854
855 Env env(*this);
856
857 env.fund(XRP(10000), issuer, alice, bob, maria);
858 env.close();
859
860 // Issuer create credentials, but Alice didn't accept them yet
861 env(credentials::create(alice, issuer, credType));
862 env.close();
863 // Alice accept the credentials
864 env(credentials::accept(alice, issuer, credType));
865 env.close();
866 // Get the index of the credentials
867 auto const jv = credentials::ledgerEntry(env, alice, issuer, credType);
868 std::string const credIdx = jv[jss::result][jss::index].asString();
869
870 {
871 // Success as destination didn't enable pre-authorization so
872 // valid credentials will not fail
873 env(pay(alice, bob, XRP(100)), credentials::Ids({credIdx}));
874 }
875
876 // Bob require pre-authorization
877 env(fset(bob, asfDepositAuth));
878 env.close();
879
880 {
881 // Fail as destination didn't setup DepositPreauth object
882 env(pay(alice, bob, XRP(100)), credentials::Ids({credIdx}), Ter(tecNO_PERMISSION));
883 }
884
885 // Bob setup DepositPreauth object, duplicates is not allowed
887 bob,
888 {
889 {.issuer = issuer, .credType = credType},
890 {.issuer = issuer, .credType = credType},
891 }),
893
894 // Bob setup DepositPreauth object
895 env(deposit::authCredentials(bob, {{.issuer = issuer, .credType = credType}}));
896 env.close();
897
898 {
899 std::string const invalidIdx =
900 "0E0B04ED60588A758B67E21FBBE95AC5A63598BA951761DC0EC9C08D7E"
901 "01E034";
902 // Alice can't pay with non-existing credentials
903 env(pay(alice, bob, XRP(100)),
904 credentials::Ids({invalidIdx}),
906 }
907
908 { // maria can't pay using valid credentials but issued for
909 // different account
910 env(pay(maria, bob, XRP(100)),
911 credentials::Ids({credIdx}),
913 }
914
915 {
916 // create another valid credential
917 char const credType2[] = "fghij";
918 env(credentials::create(alice, issuer, credType2));
919 env.close();
920 env(credentials::accept(alice, issuer, credType2));
921 env.close();
922 auto const jv = credentials::ledgerEntry(env, alice, issuer, credType2);
923 std::string const credIdx2 = jv[jss::result][jss::index].asString();
924
925 // Alice can't pay with invalid set of valid credentials
926 env(pay(alice, bob, XRP(100)),
927 credentials::Ids({credIdx, credIdx2}),
929 }
930
931 // Error, duplicate credentials
932 env(pay(alice, bob, XRP(100)), credentials::Ids({credIdx, credIdx}), Ter(temMALFORMED));
933
934 // Alice can pay
935 env(pay(alice, bob, XRP(100)), credentials::Ids({credIdx}));
936 env.close();
937 }
938 }
939
940 void
942 {
943 testcase("Zero credential ID");
944
945 using namespace jtx;
946
947 char const credType[] = "abcde";
948 Account const issuer{"issuer"};
949 Account const alice{"alice"};
950 Account const bob{"bob"};
951
952 Env env(*this, features);
953
954 env.fund(XRP(5000), issuer, alice, bob);
955 env.close();
956
957 env(credentials::create(alice, issuer, credType));
958 env.close();
959 env(credentials::accept(alice, issuer, credType));
960 env.close();
961
962 auto const jv = credentials::ledgerEntry(env, alice, issuer, credType);
963 std::string const credIdx = jv[jss::result][jss::index].asString();
964
965 std::string const zeroIdx(64, '0');
966
967 // post-fixCleanup3_4_0: a zero ID is rejected by checkFields in
968 // preflight; pre-fixCleanup3_4_0, it will trigger assertion, so it is not testable.
969 env(pay(alice, bob, XRP(100)), credentials::Ids({zeroIdx}), Ter(temMALFORMED));
970 env.close();
971
972 env(pay(alice, bob, XRP(100)), credentials::Ids({credIdx, zeroIdx}), Ter(temMALFORMED));
973 env.close();
974
975 // A valid credential succeeds
976 env(pay(alice, bob, XRP(100)), credentials::Ids({credIdx}));
977 env.close();
978 }
979
980 void
982 {
983 using namespace jtx;
984
985 char const credType[] = "abcde";
986 Account const issuer{"issuer"};
987 Account const alice{"alice"};
988 Account const bob{"bob"};
989 Account const maria{"maria"};
990
991 {
992 testcase("Creating / deleting with credentials.");
993
994 Env env(*this);
995
996 env.fund(XRP(5000), issuer, alice, bob);
997 env.close();
998
999 {
1000 // both included [AuthorizeCredentials UnauthorizeCredentials]
1001 auto jv = deposit::authCredentials(bob, {{.issuer = issuer, .credType = credType}});
1002 jv[sfUnauthorizeCredentials.jsonName] = json::ValueType::Array;
1003 env(jv, Ter(temMALFORMED));
1004 }
1005
1006 {
1007 // both included [Unauthorize, AuthorizeCredentials]
1008 auto jv = deposit::authCredentials(bob, {{.issuer = issuer, .credType = credType}});
1009 jv[sfUnauthorize.jsonName] = issuer.human();
1010 env(jv, Ter(temMALFORMED));
1011 }
1012
1013 {
1014 // both included [Authorize, AuthorizeCredentials]
1015 auto jv = deposit::authCredentials(bob, {{.issuer = issuer, .credType = credType}});
1016 jv[sfAuthorize.jsonName] = issuer.human();
1017 env(jv, Ter(temMALFORMED));
1018 }
1019
1020 {
1021 // both included [Unauthorize, UnauthorizeCredentials]
1022 auto jv =
1023 deposit::unauthCredentials(bob, {{.issuer = issuer, .credType = credType}});
1024 jv[sfUnauthorize.jsonName] = issuer.human();
1025 env(jv, Ter(temMALFORMED));
1026 }
1027
1028 {
1029 // both included [Authorize, UnauthorizeCredentials]
1030 auto jv =
1031 deposit::unauthCredentials(bob, {{.issuer = issuer, .credType = credType}});
1032 jv[sfAuthorize.jsonName] = issuer.human();
1033 env(jv, Ter(temMALFORMED));
1034 }
1035
1036 {
1037 // AuthorizeCredentials is empty
1038 auto const jv = deposit::authCredentials(bob, {});
1039 env(jv, Ter(temARRAY_EMPTY));
1040 }
1041
1042 {
1043 // invalid issuer
1044 auto jv = deposit::authCredentials(bob, {});
1045 auto& arr(jv[sfAuthorizeCredentials.jsonName]);
1047 cred[jss::Issuer] = to_string(xrpAccount());
1048 cred[sfCredentialType.jsonName] = strHex(std::string_view(credType));
1049 json::Value credParent;
1050 credParent[jss::Credential] = cred;
1051 arr.append(std::move(credParent));
1052
1053 env(jv, Ter(temINVALID_ACCOUNT_ID));
1054 }
1055
1056 {
1057 // empty credential type
1058 auto const jv = deposit::authCredentials(bob, {{.issuer = issuer, .credType = {}}});
1059 env(jv, Ter(temMALFORMED));
1060 }
1061
1062 {
1063 // AuthorizeCredentials is larger than 8 elements
1064 Account const a("a"), b("b"), c("c"), d("d"), e("e"), f("f"), g("g"), h("h"),
1065 i("i");
1066 auto const& z = credType;
1067 auto const jv = deposit::authCredentials(
1068 bob,
1069 {
1070 {.issuer = a, .credType = z},
1071 {.issuer = b, .credType = z},
1072 {.issuer = c, .credType = z},
1073 {.issuer = d, .credType = z},
1074 {.issuer = e, .credType = z},
1075 {.issuer = f, .credType = z},
1076 {.issuer = g, .credType = z},
1077 {.issuer = h, .credType = z},
1078 {.issuer = i, .credType = z},
1079 });
1080 env(jv, Ter(temARRAY_TOO_LARGE));
1081 }
1082
1083 {
1084 // Can't create with non-existing issuer
1085 Account const rick{"rick"};
1086 auto const jv =
1087 deposit::authCredentials(bob, {{.issuer = rick, .credType = credType}});
1088 env(jv, Ter(tecNO_ISSUER));
1089 env.close();
1090 }
1091
1092 {
1093 // not enough reserve
1094 Account const john{"john"};
1095 env.fund(env.current()->fees().accountReserve(0, 1), john);
1096 env.close();
1097 auto const jv =
1098 deposit::authCredentials(john, {{.issuer = issuer, .credType = credType}});
1099 env(jv, Ter(tecINSUFFICIENT_RESERVE));
1100 }
1101
1102 {
1103 // NO deposit object exists
1104 env(deposit::unauthCredentials(bob, {{.issuer = issuer, .credType = credType}}),
1105 Ter(tecNO_ENTRY));
1106 }
1107
1108 // Create DepositPreauth object
1109 {
1110 env(deposit::authCredentials(bob, {{.issuer = issuer, .credType = credType}}));
1111 env.close();
1112
1113 auto const jDP =
1114 ledgerEntryDepositPreauth(env, bob, {{.issuer = issuer, .credType = credType}});
1115 BEAST_EXPECT(
1116 jDP.isObject() && jDP.isMember(jss::result) &&
1117 !jDP[jss::result].isMember(jss::error) &&
1118 jDP[jss::result].isMember(jss::node) &&
1119 jDP[jss::result][jss::node].isMember("LedgerEntryType") &&
1120 jDP[jss::result][jss::node]["LedgerEntryType"] == jss::DepositPreauth);
1121
1122 // Check object fields
1123 BEAST_EXPECT(jDP[jss::result][jss::node][jss::Account] == bob.human());
1124 auto const& credentials(jDP[jss::result][jss::node]["AuthorizeCredentials"]);
1125 BEAST_EXPECT(credentials.isArray() && credentials.size() == 1);
1126 for (auto const& o : credentials)
1127 {
1128 auto const& c(o[jss::Credential]);
1129 BEAST_EXPECT(c[jss::Issuer].asString() == issuer.human());
1130 BEAST_EXPECT(
1131 c["CredentialType"].asString() == strHex(std::string_view(credType)));
1132 }
1133
1134 // can't create duplicate
1135 env(deposit::authCredentials(bob, {{.issuer = issuer, .credType = credType}}),
1136 Ter(tecDUPLICATE));
1137 }
1138
1139 // Delete DepositPreauth object
1140 {
1141 env(deposit::unauthCredentials(bob, {{.issuer = issuer, .credType = credType}}));
1142 env.close();
1143 auto const jDP =
1144 ledgerEntryDepositPreauth(env, bob, {{.issuer = issuer, .credType = credType}});
1145 BEAST_EXPECT(
1146 jDP.isObject() && jDP.isMember(jss::result) &&
1147 jDP[jss::result][jss::error] == "entryNotFound" &&
1148 jDP[jss::result][jss::error_code] == RpcEntryNotFound &&
1149 jDP[jss::result][jss::error_message] == "Entry not found.");
1150 }
1151 }
1152 }
1153
1154 void
1156 {
1157 using namespace jtx;
1158 char const credType[] = "abcde";
1159 char const credType2[] = "fghijkl";
1160 Account const issuer{"issuer"};
1161 Account const alice{"alice"};
1162 Account const bob{"bob"};
1163 Account const gw{"gw"};
1164 IOU const usd = gw["USD"];
1165 Account const zelda{"zelda"};
1166
1167 {
1168 testcase("Payment failure with expired credentials.");
1169
1170 Env env(*this);
1171
1172 env.fund(XRP(10000), issuer, alice, bob, gw);
1173 env.close();
1174
1175 // Create credentials
1176 auto jv = credentials::create(alice, issuer, credType);
1177 // Current time in XRPL epoch.
1178 // Every time ledger close, unittest timer increase by 10s
1179 uint32_t const t =
1180 env.current()->header().parentCloseTime.time_since_epoch().count() + 60;
1181 jv[sfExpiration.jsonName] = t;
1182 env(jv);
1183 env.close();
1184
1185 // Alice accept the credentials
1186 env(credentials::accept(alice, issuer, credType));
1187 env.close();
1188
1189 // Create credential which not expired
1190 jv = credentials::create(alice, issuer, credType2);
1191 uint32_t const t2 =
1192 env.current()->header().parentCloseTime.time_since_epoch().count() + 1000;
1193 jv[sfExpiration.jsonName] = t2;
1194 env(jv);
1195 env.close();
1196 env(credentials::accept(alice, issuer, credType2));
1197 env.close();
1198
1199 BEAST_EXPECT(ownerCount(env, issuer) == 0);
1200 BEAST_EXPECT(ownerCount(env, alice) == 2);
1201
1202 // Get the index of the credentials
1203 jv = credentials::ledgerEntry(env, alice, issuer, credType);
1204 std::string const credIdx = jv[jss::result][jss::index].asString();
1205 jv = credentials::ledgerEntry(env, alice, issuer, credType2);
1206 std::string const credIdx2 = jv[jss::result][jss::index].asString();
1207
1208 // Bob require pre-authorization
1209 env(fset(bob, asfDepositAuth));
1210 env.close();
1211 // Bob setup DepositPreauth object
1213 bob,
1214 {
1215 {.issuer = issuer, .credType = credType},
1216 {.issuer = issuer, .credType = credType2},
1217 }));
1218 env.close();
1219
1220 {
1221 // Alice can pay
1222 env(pay(alice, bob, XRP(100)), credentials::Ids({credIdx, credIdx2}));
1223 env.close();
1224 env.close();
1225
1226 // Ledger closed, time increased, alice can't pay anymore
1227 env(pay(alice, bob, XRP(100)),
1228 credentials::Ids({credIdx, credIdx2}),
1229 Ter(tecEXPIRED));
1230 env.close();
1231
1232 {
1233 // check that expired credentials were deleted
1234 auto const jDelCred = credentials::ledgerEntry(env, alice, issuer, credType);
1235 BEAST_EXPECT(
1236 jDelCred.isObject() && jDelCred.isMember(jss::result) &&
1237 jDelCred[jss::result][jss::error] == "entryNotFound" &&
1238 jDelCred[jss::result][jss::error_code] == RpcEntryNotFound &&
1239 jDelCred[jss::result][jss::error_message] == "Entry not found.");
1240 }
1241
1242 {
1243 // check that non-expired credential still present
1244 auto const jle = credentials::ledgerEntry(env, alice, issuer, credType2);
1245 BEAST_EXPECT(
1246 jle.isObject() && jle.isMember(jss::result) &&
1247 !jle[jss::result].isMember(jss::error) &&
1248 jle[jss::result].isMember(jss::node) &&
1249 jle[jss::result][jss::node].isMember("LedgerEntryType") &&
1250 jle[jss::result][jss::node]["LedgerEntryType"] == jss::Credential &&
1251 jle[jss::result][jss::node][jss::Issuer] == issuer.human() &&
1252 jle[jss::result][jss::node][jss::Subject] == alice.human() &&
1253 jle[jss::result][jss::node]["CredentialType"] ==
1254 strHex(std::string_view(credType2)));
1255 }
1256
1257 BEAST_EXPECT(ownerCount(env, issuer) == 0);
1258 BEAST_EXPECT(ownerCount(env, alice) == 1);
1259 }
1260
1261 {
1262 auto jv = credentials::create(gw, issuer, credType);
1263 uint32_t const t =
1264 env.current()->header().parentCloseTime.time_since_epoch().count() + 40;
1265 jv[sfExpiration.jsonName] = t;
1266 env(jv);
1267 env.close();
1268 env(credentials::accept(gw, issuer, credType));
1269 env.close();
1270
1271 jv = credentials::ledgerEntry(env, gw, issuer, credType);
1272 std::string const credIdx = jv[jss::result][jss::index].asString();
1273
1274 BEAST_EXPECT(ownerCount(env, issuer) == 0);
1275 BEAST_EXPECT(ownerCount(env, gw) == 1);
1276
1277 env.close();
1278 env.close();
1279 env.close();
1280
1281 // credentials are expired
1282 env(pay(gw, bob, usd(150)), credentials::Ids({credIdx}), Ter(tecEXPIRED));
1283 env.close();
1284
1285 // check that expired credentials were deleted
1286 auto const jDelCred = credentials::ledgerEntry(env, gw, issuer, credType);
1287 BEAST_EXPECT(
1288 jDelCred.isObject() && jDelCred.isMember(jss::result) &&
1289 jDelCred[jss::result][jss::error] == "entryNotFound" &&
1290 jDelCred[jss::result][jss::error_code] == RpcEntryNotFound &&
1291 jDelCred[jss::result][jss::error_message] == "Entry not found.");
1292
1293 BEAST_EXPECT(ownerCount(env, issuer) == 0);
1294 BEAST_EXPECT(ownerCount(env, gw) == 0);
1295 }
1296 }
1297
1298 {
1299 using namespace std::chrono;
1300
1301 testcase("Escrow failure with expired credentials.");
1302
1303 Env env(*this);
1304
1305 env.fund(XRP(5000), issuer, alice, bob, zelda);
1306 env.close();
1307
1308 // Create credentials
1309 auto jv = credentials::create(zelda, issuer, credType);
1310 uint32_t const t =
1311 env.current()->header().parentCloseTime.time_since_epoch().count() + 50;
1312 jv[sfExpiration.jsonName] = t;
1313 env(jv);
1314 env.close();
1315
1316 // Zelda accept the credentials
1317 env(credentials::accept(zelda, issuer, credType));
1318 env.close();
1319
1320 // Get the index of the credentials
1321 jv = credentials::ledgerEntry(env, zelda, issuer, credType);
1322 std::string const credIdx = jv[jss::result][jss::index].asString();
1323
1324 // Bob require pre-authorization
1325 env(fset(bob, asfDepositAuth));
1326 env.close();
1327 // Bob setup DepositPreauth object
1328 env(deposit::authCredentials(bob, {{.issuer = issuer, .credType = credType}}));
1329 env.close();
1330
1331 auto const seq = env.seq(alice);
1332 env(escrow::create(alice, bob, XRP(1000)), escrow::kFinishTime(env.now() + 1s));
1333 env.close();
1334
1335 // zelda can't finish escrow with invalid credentials
1336 {
1337 env(escrow::finish(zelda, alice, seq), credentials::Ids({}), Ter(temMALFORMED));
1338 env.close();
1339 }
1340
1341 {
1342 // zelda can't finish escrow with invalid credentials
1343 std::string const invalidIdx =
1344 "0E0B04ED60588A758B67E21FBBE95AC5A63598BA951761DC0EC9C08D7E"
1345 "01E034";
1346
1347 env(escrow::finish(zelda, alice, seq),
1348 credentials::Ids({invalidIdx}),
1350 env.close();
1351 }
1352
1353 { // Ledger closed, time increased, zelda can't finish escrow
1354 env(escrow::finish(zelda, alice, seq),
1355 credentials::Ids({credIdx}),
1356 Fee(1500),
1357 Ter(tecEXPIRED));
1358 env.close();
1359 }
1360
1361 // check that expired credentials were deleted
1362 auto const jDelCred = credentials::ledgerEntry(env, zelda, issuer, credType);
1363 BEAST_EXPECT(
1364 jDelCred.isObject() && jDelCred.isMember(jss::result) &&
1365 jDelCred[jss::result][jss::error] == "entryNotFound" &&
1366 jDelCred[jss::result][jss::error_code] == RpcEntryNotFound &&
1367 jDelCred[jss::result][jss::error_message] == "Entry not found.");
1368 }
1369 }
1370
1371 void
1373 {
1374 using namespace jtx;
1375
1376 Account const stock{"stock"};
1377 Account const alice{"alice"};
1378 Account const bob{"bob"};
1379
1380 Env env(*this);
1381
1382 testcase("Sorting credentials.");
1383
1384 env.fund(XRP(5000), stock, alice, bob);
1385
1387 {.issuer = "a", .credType = "a"},
1388 {.issuer = "b", .credType = "b"},
1389 {.issuer = "c", .credType = "c"},
1390 {.issuer = "d", .credType = "d"},
1391 {.issuer = "e", .credType = "e"},
1392 {.issuer = "f", .credType = "f"},
1393 {.issuer = "g", .credType = "g"},
1394 {.issuer = "h", .credType = "h"},
1395 };
1396
1397 for (auto const& c : credentials)
1398 env.fund(XRP(5000), c.issuer);
1399 env.close();
1400
1402 std::mt19937 gen(rd());
1403
1404 {
1406 for (auto const& c : credentials)
1407 pubKey2Acc.emplace(c.issuer.human(), c.issuer);
1408
1409 // check sorting in object
1410 for (int i = 0; i < 10; ++i)
1411 {
1414 env.close();
1415
1416 auto const dp = ledgerEntryDepositPreauth(env, stock, credentials);
1417 auto const& authCred(dp[jss::result][jss::node]["AuthorizeCredentials"]);
1418 BEAST_EXPECT(authCred.isArray() && authCred.size() == credentials.size());
1420 for (auto const& o : authCred)
1421 {
1422 auto const& c(o[jss::Credential]);
1423 auto const issuer = c[jss::Issuer].asString();
1424
1425 if (BEAST_EXPECT(pubKey2Acc.contains(issuer)))
1426 {
1427 readCreds.emplace_back(
1428 pubKey2Acc.at(issuer), c["CredentialType"].asString());
1429 }
1430 }
1431
1432 BEAST_EXPECT(std::ranges::is_sorted(readCreds));
1433
1435 env.close();
1436 }
1437 }
1438
1439 {
1442 env.close();
1443
1444 // check sorting in params
1445 for (int i = 0; i < 10; ++i)
1446 {
1449 }
1450 }
1451
1452 testcase("Check duplicate credentials.");
1453 {
1454 // check duplicates in depositPreauth params
1456 credentials.begin(), credentials.end() - 1);
1457
1458 std::ranges::shuffle(copyCredentials, gen);
1459 for (auto const& c : copyCredentials)
1460 {
1461 auto credentials2 = copyCredentials;
1462 credentials2.push_back(c);
1463 env(deposit::authCredentials(stock, credentials2), Ter(temMALFORMED));
1464 }
1465
1466 // create batch of credentials and save their hashes
1467 std::vector<std::string> credentialIDs;
1468 for (auto const& c : credentials)
1469 {
1470 env(credentials::create(alice, c.issuer, c.credType));
1471 env.close();
1472 env(credentials::accept(alice, c.issuer, c.credType));
1473 env.close();
1474
1475 credentialIDs.push_back(
1477 env, alice, c.issuer, c.credType)[jss::result][jss::index]
1478 .asString());
1479 }
1480
1481 // check duplicates in payment params
1482 for (auto const& h : credentialIDs)
1483 {
1484 auto credentialIDs2 = credentialIDs;
1485 credentialIDs2.push_back(h);
1486
1487 env(pay(alice, bob, XRP(100)), credentials::Ids(credentialIDs2), Ter(temMALFORMED));
1488 }
1489 }
1490 }
1491
1492 void
1493 run() override
1494 {
1495 testEnable();
1496 auto const supported{jtx::testableAmendments()};
1497 testInvalid(supported);
1498 testInvalid(supported - fixCleanup3_3_0);
1499 testPayment(supported - featureCredentials);
1500 testPayment(supported);
1502 testZeroCredentialID(supported);
1506 }
1507};
1508
1509BEAST_DEFINE_TESTSUITE(DepositAuth, app, xrpl);
1511
1512} // namespace xrpl::test
T at(T... args)
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
Value removeMember(char const *key)
Remove and return the named member.
Value & append(Value const &value)
Append value to array at the end.
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:1058
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
Definition Env.cpp:201
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
void memoize(Account const &account)
Associate AccountID with account.
Definition Env.cpp:174
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:766
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
NetClock::time_point now()
Returns the current network time.
Definition Env.h:326
Set the fee on a JTx.
Definition fee.h:20
Match set account flags.
Definition flags.h:119
Converts to IOU Issue or STAmount.
Match clear account flags.
Definition flags.h:137
Match the number of items in the account's owner directory.
Definition owners.h:55
Add a path.
Definition paths.h:47
Check a set of conditions.
Definition require.h:49
Sets the SendMax on a JTx.
Definition sendmax.h:16
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
Set the flags on a JTx.
Definition txflags.h:14
Set a ticket sequence on a JTx.
Definition ticket.h:36
T contains(T... args)
T emplace_back(T... args)
T emplace(T... args)
T is_sorted(T... args)
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
Keylet computation functions.
Definition Indexes.h:39
json::Value accept(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:29
json::Value create(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:16
json::Value ledgerEntry(jtx::Env &env, jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:56
json::Value unauthCredentials(jtx::Account const &account, std::vector< AuthorizeCredentials > const &auth)
Definition deposit.cpp:56
json::Value authCredentials(jtx::Account const &account, std::vector< AuthorizeCredentials > const &auth)
Definition deposit.cpp:38
json::Value auth(Account const &account, Account const &auth)
Preauthorize for deposit.
Definition deposit.cpp:16
json::Value unauth(Account const &account, Account const &unauth)
Remove pre-authorization for deposit.
Definition deposit.cpp:27
json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount)
Definition escrow.cpp:24
auto const kFinishTime
Set the "FinishAfter" time tag on a JTx.
Definition escrow.h:93
json::Value finish(AccountID const &account, AccountID const &from, std::uint32_t seq)
Definition escrow.cpp:36
json::Value create(Account const &account, std::uint32_t count)
Create one of more tickets.
Definition ticket.cpp:16
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
json::Value noop(Account const &account)
The null transaction.
Definition noop.h:14
std::uint32_t ownerCount(Env const &env, Account const &account)
json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
Definition flags.h:110
FeatureBitset testableAmendments()
Definition Env.h:92
OwnerCount< ltOFFER > Offers
Match the number of offers in the account's owner directory.
Definition owners.h:137
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
OwnerCount< ltTICKET > Tickets
Match the number of tickets on the account.
Definition ticket.h:54
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Definition flags.cpp:15
static bool hasDepositAuth(jtx::Env const &env, jtx::Account const &acct)
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
static XRPAmount reserve(jtx::Env const &env, std::uint32_t count)
static json::Value ledgerEntryDepositPreauth(jtx::Env &env, jtx::Account const &acc, std::vector< jtx::deposit::AuthorizeCredentials > const &auth)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terINSUF_FEE_B
Definition TER.h:218
@ terNO_ACCOUNT
Definition TER.h:219
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
@ RpcEntryNotFound
Definition ErrorCodes.h:144
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
@ temARRAY_TOO_LARGE
Definition TER.h:129
@ temBAD_FEE
Definition TER.h:80
@ temINVALID_FLAG
Definition TER.h:99
@ temCANNOT_PREAUTH_SELF
Definition TER.h:108
@ temMALFORMED
Definition TER.h:75
@ temARRAY_EMPTY
Definition TER.h:128
@ temINVALID_ACCOUNT_ID
Definition TER.h:107
@ temDISABLED
Definition TER.h:102
TERSubset< CanCvtToTER > TER
Definition TER.h:657
AccountID const & xrpAccount()
Compute AccountID from public key.
@ tecPSEUDO_ACCOUNT
Definition TER.h:373
@ tecNO_ENTRY
Definition TER.h:317
@ tecNO_TARGET
Definition TER.h:315
@ tecPATH_DRY
Definition TER.h:305
@ tecBAD_CREDENTIALS
Definition TER.h:370
@ tecEXPIRED
Definition TER.h:325
@ tecINSUFFICIENT_RESERVE
Definition TER.h:318
@ tecNO_PERMISSION
Definition TER.h:316
@ tecNO_ISSUER
Definition TER.h:310
@ tecDUPLICATE
Definition TER.h:326
@ tesSUCCESS
Definition TER.h:253
T push_back(T... args)
T shuffle(T... args)
void run() override
Runs the suite.
void run() override
Runs the suite.
void testInvalid(FeatureBitset features)
void testPayment(FeatureBitset features)
void testZeroCredentialID(FeatureBitset features)
Represents an XRP, IOU, or MPT quantity This customizes the string conversion and supports XRP conver...
Set the sequence number on a JTx.
Definition seq.h:16