xrpld
Loading...
Searching...
No Matches
Delegate_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/CaptureLogs.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/acctdelete.h>
6#include <test/jtx/amount.h>
7#include <test/jtx/balance.h>
8#include <test/jtx/batch.h>
9#include <test/jtx/delegate.h>
10#include <test/jtx/delivermin.h>
11#include <test/jtx/did.h>
12#include <test/jtx/domain.h>
13#include <test/jtx/fee.h>
14#include <test/jtx/flags.h>
15#include <test/jtx/mpt.h>
16#include <test/jtx/multisign.h>
17#include <test/jtx/noop.h>
18#include <test/jtx/offer.h>
19#include <test/jtx/paths.h>
20#include <test/jtx/pay.h>
21#include <test/jtx/permissioned_domains.h>
22#include <test/jtx/rate.h>
23#include <test/jtx/regkey.h>
24#include <test/jtx/sendmax.h>
25#include <test/jtx/sig.h>
26#include <test/jtx/sponsor.h>
27#include <test/jtx/ter.h>
28#include <test/jtx/trust.h>
29#include <test/jtx/txflags.h>
30#include <test/jtx/vault.h>
31
32#include <xrpl/basics/Number.h>
33#include <xrpl/basics/Slice.h>
34#include <xrpl/basics/base_uint.h>
35#include <xrpl/basics/strHex.h>
36#include <xrpl/beast/unit_test/suite.h>
37#include <xrpl/core/ServiceRegistry.h>
38#include <xrpl/json/json_value.h>
39#include <xrpl/json/to_string.h>
40#include <xrpl/ledger/helpers/DelegateHelpers.h>
41#include <xrpl/protocol/ErrorCodes.h>
42#include <xrpl/protocol/Feature.h>
43#include <xrpl/protocol/Indexes.h>
44#include <xrpl/protocol/Issue.h>
45#include <xrpl/protocol/KeyType.h>
46#include <xrpl/protocol/Permissions.h>
47#include <xrpl/protocol/SField.h>
48#include <xrpl/protocol/STObject.h>
49#include <xrpl/protocol/STTx.h>
50#include <xrpl/protocol/SecretKey.h>
51#include <xrpl/protocol/TER.h>
52#include <xrpl/protocol/TxFlags.h>
53#include <xrpl/protocol/TxSettings.h>
54#include <xrpl/protocol/XRPAmount.h>
55#include <xrpl/protocol/jss.h>
56
57#include <algorithm>
58#include <cstddef>
59#include <cstdint>
60#include <limits>
61#include <memory>
62#include <string>
63#include <tuple>
64#include <unordered_map>
65#include <unordered_set>
66#include <vector>
67
68namespace xrpl::test {
70{
71 void
73 {
74 testcase("test feature not enabled");
75 using namespace jtx;
76
77 Env env{*this, features};
78 Account const gw{"gateway"};
79 Account const alice{"alice"};
80 Account const bob{"bob"};
81 env.fund(XRP(1000000), gw, alice, bob);
82 env.close();
83
84 auto const res =
85 features[featurePermissionDelegationV1_1] ? Ter(tesSUCCESS) : Ter(temDISABLED);
86
87 // can not set Delegate when feature disabled
88 env(delegate::set(gw, alice, {"Payment"}), res);
89 env.close();
90
91 // can not send delegating transaction when feature disabled
92 env(pay(gw, bob, XRP(100)), delegate::As(alice), res);
93 }
94
95 void
97 {
98 testcase("test valid request creating, updating, deleting permissions");
99 using namespace jtx;
100
101 Env env(*this);
102 Account const gw{"gateway"};
103 Account const alice{"alice"};
104 env.fund(XRP(100000), gw, alice);
105 env.close();
106
107 // delegating an empty permission list when the delegate ledger object
108 // does not exist is not allowed
109 env(delegate::set(gw, alice, {}), Ter(tecNO_ENTRY));
110 env.close();
111
112 auto const permissions = std::vector<std::string>{
113 "Payment",
114 "EscrowCreate",
115 "EscrowFinish",
116 "TrustlineAuthorize",
117 "CheckCreate",
118 };
119 env(delegate::set(gw, alice, permissions));
120 env.close();
121
122 // this lambda function is used to compare the json value of ledger
123 // entry response with the given vector of permissions.
124 auto const comparePermissions = [&](json::Value const& jle,
125 std::vector<std::string> const& permissions,
126 Account const& account,
127 Account const& authorize) {
128 BEAST_EXPECT(
129 !jle[jss::result].isMember(jss::error) && jle[jss::result].isMember(jss::node));
130 BEAST_EXPECT(jle[jss::result][jss::node]["LedgerEntryType"] == jss::Delegate);
131 BEAST_EXPECT(jle[jss::result][jss::node][jss::Account] == account.human());
132 BEAST_EXPECT(jle[jss::result][jss::node][sfAuthorize.jsonName] == authorize.human());
133
134 auto const& jPermissions = jle[jss::result][jss::node][sfPermissions.jsonName];
135 unsigned i = 0;
136 for (auto const& permission : permissions)
137 {
138 BEAST_EXPECT(
139 jPermissions[i][sfPermission.jsonName][sfPermissionValue.jsonName] ==
140 permission);
141 i++;
142 }
143 };
144
145 // get ledger entry with valid parameter
146 comparePermissions(delegate::entry(env, gw, alice), permissions, gw, alice);
147
148 // gw updates permission
149 auto const newPermissions =
150 std::vector<std::string>{"Payment", "AMMCreate", "AMMDeposit", "AMMWithdraw"};
151 env(delegate::set(gw, alice, newPermissions));
152 env.close();
153
154 // get ledger entry again, permissions should be updated to
155 // newPermissions
156 comparePermissions(delegate::entry(env, gw, alice), newPermissions, gw, alice);
157
158 // gw deletes all permissions delegated to alice, this will delete the ledger entry
159 env(delegate::set(gw, alice, {}));
160 env.close();
161 auto const jle = delegate::entry(env, gw, alice);
162 BEAST_EXPECT(jle[jss::result][jss::error] == "entryNotFound");
163 BEAST_EXPECT(jle[jss::result][jss::error_code] == RpcEntryNotFound);
164 BEAST_EXPECT(jle[jss::result][jss::error_message] == "Entry not found.");
165
166 // alice can delegate permissions to gw as well
167 env(delegate::set(alice, gw, permissions));
168 env.close();
169 comparePermissions(delegate::entry(env, alice, gw), permissions, alice, gw);
170 auto const response = delegate::entry(env, gw, alice);
171 // alice has not been granted any permissions by gw
172 BEAST_EXPECT(response[jss::result][jss::error] == "entryNotFound");
173 BEAST_EXPECT(response[jss::result][jss::error_code] == RpcEntryNotFound);
174 BEAST_EXPECT(response[jss::result][jss::error_message] == "Entry not found.");
175 }
176
177 void
179 {
180 testcase("test invalid DelegateSet");
181 using namespace jtx;
182
183 Env env(*this, features);
184 Account const gw{"gateway"};
185 Account const alice{"alice"};
186 Account const bob{"bob"};
187 env.fund(XRP(100000), gw, alice, bob);
188 env.close();
189
190 // when permissions size exceeds the limit 10, should return
191 // temARRAY_TOO_LARGE
192 {
193 env(delegate::set(
194 gw,
195 alice,
196 {
197 "Payment",
198 "EscrowCreate",
199 "EscrowFinish",
200 "EscrowCancel",
201 "CheckCreate",
202 "CheckCash",
203 "CheckCancel",
204 "DepositPreauth",
205 "TrustSet",
206 "NFTokenMint",
207 "NFTokenBurn",
208 }),
210 }
211
212 // alice can not authorize herself
213 {
214 env(delegate::set(alice, alice, {"Payment"}), Ter(temMALFORMED));
215 }
216
217 // bad fee
218 {
219 json::Value jv;
220 jv[jss::TransactionType] = jss::DelegateSet;
221 jv[jss::Account] = gw.human();
222 jv[sfAuthorize.jsonName] = alice.human();
223 json::Value permissionsJson(json::ValueType::Array);
224 json::Value permissionValue;
225 permissionValue[sfPermissionValue.jsonName] = "Payment";
226 json::Value permissionObj;
227 permissionObj[sfPermission.jsonName] = permissionValue;
228 permissionsJson.append(permissionObj);
229 jv[sfPermissions.jsonName] = permissionsJson;
230 jv[sfFee.jsonName] = -1;
231 env(jv, Ter(temBAD_FEE));
232 }
233
234 // when provided permissions contains duplicate values, should return
235 // temMALFORMED
236 {
237 env(delegate::set(
238 gw,
239 alice,
240 {
241 "Payment",
242 "EscrowCreate",
243 "EscrowFinish",
244 "TrustlineAuthorize",
245 "CheckCreate",
246 "TrustlineAuthorize",
247 }),
249 }
250
251 // when authorizing account which does not exist, should return
252 // tecNO_TARGET
253 {
254 env(delegate::set(gw, Account("unknown"), {"Payment"}), Ter(tecNO_TARGET));
255 }
256
257 // Delegating to a pseudo-account is not allowed, should return tecPSEUDO_ACCOUNT
258 {
259 Vault const vault{env};
260 auto [tx, keylet] = vault.create({.owner = gw, .asset = xrpIssue()});
261 env(tx);
262 env.close();
263
264 auto const sleVault = env.le(keylet);
265 BEAST_EXPECT(sleVault);
266 Account const vaultPseudo{"vault", sleVault->at(sfAccount)};
267 env(delegate::set(gw, vaultPseudo, {"Payment"}), Ter(tecPSEUDO_ACCOUNT));
268 }
269
270 // non-delegable transaction
271 {
272 env(delegate::set(gw, alice, {"SetRegularKey"}), Ter(temMALFORMED));
273 env(delegate::set(gw, alice, {"AccountSet"}), Ter(temMALFORMED));
274 env(delegate::set(gw, alice, {"SignerListSet"}), Ter(temMALFORMED));
275 env(delegate::set(gw, alice, {"DelegateSet"}), Ter(temMALFORMED));
276 env(delegate::set(gw, alice, {"EnableAmendment"}), Ter(temMALFORMED));
277 env(delegate::set(gw, alice, {"UNLModify"}), Ter(temMALFORMED));
278 env(delegate::set(gw, alice, {"SetFee"}), Ter(temMALFORMED));
279 env(delegate::set(gw, alice, {"Batch"}), Ter(temMALFORMED));
280 }
281 }
282
283 void
285 {
286 testcase("test reserve");
287 using namespace jtx;
288
289 // reserve requirement not met
290 {
291 Env env(*this);
292 Account const alice{"alice"};
293 Account const bob{"bob"};
294
295 auto const txFee = env.current()->fees().base;
296 env.fund(env.current()->fees().accountReserve(0, 1) + txFee, alice);
297 env.fund(XRP(100000), bob);
298 env.close();
299
300 // alice does not have enough reserve to create Delegate
301 env(delegate::set(alice, bob, {"Payment"}), Ter(tecINSUFFICIENT_RESERVE));
302 }
303
304 // reserve recovered after deleting delegation object
305 {
306 Env env(*this);
307 Account const bob{"bob"};
308 Account const alice{"alice"};
309 Account const carol{"carol"};
310
311 auto const txFee = env.current()->fees().base;
312
313 env.fund(env.current()->fees().accountReserve(1, 1) + (txFee * 4), alice);
314 env.fund(XRP(100000), bob, carol);
315 env.close();
316
317 // alice consumes 1 txFee and requires 1 object reserve
318 env(delegate::set(alice, bob, {"Payment"}));
319 env.close();
320
321 // alice does not have enough reserve to create another delegation object
322 env(delegate::set(alice, carol, {"Payment"}), Ter(tecINSUFFICIENT_RESERVE));
323 env.close();
324
325 // deleting delegation object recovers 1 reserve
326 env(delegate::set(alice, bob, {}));
327 env.close();
328
329 // now alice can delegate again
330 env(delegate::set(alice, carol, {"Payment"}));
331 }
332
333 // test reserve when sending transaction on behalf of other account
334 {
335 Env env(*this);
336 Account const alice{"alice"};
337 Account const bob{"bob"};
338
339 env.fund(drops(env.current()->fees().accountReserve(1, 1)), alice);
340 env.fund(drops(env.current()->fees().accountReserve(2, 1)), bob);
341 env.close();
342
343 // alice gives bob permission
344 env(delegate::set(alice, bob, {"DIDSet", "DIDDelete"}));
345 env.close();
346
347 // bob set DID on behalf of alice, but alice does not have enough
348 // reserve
349 env(did::set(alice), did::Uri("uri"), delegate::As(bob), Ter(tecINSUFFICIENT_RESERVE));
350
351 // bob can set DID for himself because he has enough reserve
352 env(did::set(bob), did::Uri("uri"));
353 env.close();
354 }
355 }
356
357 void
359 {
360 testcase("test fee");
361 using namespace jtx;
362
363 // Common setup: fund alice, bob, carol with 1000 XRP.
364 auto const setup = [&](Env& env) {
365 Account const alice{"alice"};
366 Account const bob{"bob"};
367 Account const carol{"carol"};
368 env.fund(XRP(1000), alice, bob, carol);
369 env.close();
370 return std::make_tuple(alice, bob, carol);
371 };
372
373 // No fee deduction for terNO_DELEGATE_PERMISSION.
374 {
375 Env env(*this);
376 auto [alice, bob, carol] = setup(env);
377
378 auto const aliceBalance = env.balance(alice);
379 auto const bobBalance = env.balance(bob);
380 auto const carolBalance = env.balance(carol);
381
382 env(pay(alice, carol, XRP(100)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
383 env.close();
384 BEAST_EXPECT(env.balance(alice) == aliceBalance);
385 BEAST_EXPECT(env.balance(bob) == bobBalance);
386 BEAST_EXPECT(env.balance(carol) == carolBalance);
387 }
388
389 // Delegate pays the fee successfully.
390 {
391 Env env(*this);
392 auto [alice, bob, carol] = setup(env);
393 env(delegate::set(alice, bob, {"Payment"}));
394 env.close();
395
396 auto const aliceBalance = env.balance(alice);
397 auto const bobBalance = env.balance(bob);
398 auto const carolBalance = env.balance(carol);
399
400 auto const sendAmt = XRP(100);
401 auto const feeAmt = XRP(10);
402 env(pay(alice, carol, sendAmt), Fee(feeAmt), delegate::As(bob));
403 env.close();
404 BEAST_EXPECT(env.balance(alice) == aliceBalance - sendAmt);
405 BEAST_EXPECT(env.balance(bob) == bobBalance - feeAmt);
406 BEAST_EXPECT(env.balance(carol) == carolBalance + sendAmt);
407 }
408
409 // Bob has insufficient balance to pay the fee, will get terINSUF_FEE_B.
410 {
411 Env env(*this);
412 auto [alice, bob, carol] = setup(env);
413 env(delegate::set(alice, bob, {"Payment"}));
414 env.close();
415
416 auto const aliceBalance = env.balance(alice);
417 auto const bobBalance = env.balance(bob);
418 auto const carolBalance = env.balance(carol);
419
420 env(pay(alice, carol, XRP(100)),
421 Fee(XRP(2000)),
422 delegate::As(bob),
424 env.close();
425 BEAST_EXPECT(env.balance(alice) == aliceBalance);
426 BEAST_EXPECT(env.balance(bob) == bobBalance);
427 BEAST_EXPECT(env.balance(carol) == carolBalance);
428 }
429
430 // The delegated account has enough balance to pay and delegator has enough reserve
431 {
432 // Common setup: fund accounts and grant Bob permission to pay on Alice's behalf.
433 // Alice is funded with exactly (paymentAmount + reserve + baseFee): baseFee covers
434 // the DelegateSet tx cost, leaving Alice with exactly (paymentAmount + reserve).
435 // highFee = reserve + baseFee, strictly greater than reserve, so that
436 // max(reserve, highFee) = highFee — making the direct payment check fail.
437 auto const setup = [&](Env& env) {
438 Account const alice{"alice"};
439 Account const bob{"bob"};
440 Account const carol{"carol"};
441
442 auto const baseFee = env.current()->fees().base;
443 auto const reserve = env.current()->fees().accountReserve(1, 1);
444 auto const paymentAmount = XRP(1);
445 auto const highFee = reserve + baseFee;
446 BEAST_EXPECT(highFee > reserve);
447
448 env.fund(paymentAmount + reserve + baseFee, alice);
449 env.fund(XRP(1000), bob);
450 env.fund(XRP(1000), carol);
451 env.close();
452
453 env(delegate::set(alice, bob, {"Payment"}));
454 env.close();
455
456 env.require(Balance(alice, paymentAmount + reserve));
457
458 return std::make_tuple(alice, bob, carol, paymentAmount, highFee, reserve);
459 };
460
461 // Alice's balance (paymentAmount + reserve) is insufficient to cover both
462 // the payment and highFee directly. Even though fees are allowed to dip
463 // below reserve, when Alice pays the fee herself the required funds =
464 // paymentAmount + max(reserve, highFee) = paymentAmount + highFee
465 // (since highFee > reserve), which still exceeds her balance.
466 // tec: highFee is consumed from Alice's balance.
467 {
468 Env env(*this);
469 auto [alice, bob, carol, paymentAmount, highFee, reserve] = setup(env);
470 auto const aliceBalance = env.balance(alice);
471 auto const bobBalance = env.balance(bob);
472 auto const carolBalance = env.balance(carol);
473
474 env(pay(alice, carol, paymentAmount), Fee(highFee), Ter(tecUNFUNDED_PAYMENT));
475
476 // tec consumes the fee from Alice; carol and bob are unaffected.
477 BEAST_EXPECT(env.balance(alice) == aliceBalance - highFee);
478 BEAST_EXPECT(env.balance(bob) == bobBalance);
479 BEAST_EXPECT(env.balance(carol) == carolBalance);
480 }
481
482 // The payment succeeds because the delegated account pays the fee.
483 // Alice only needs (paymentAmount + reserve).
484 {
485 Env env(*this);
486 auto [alice, bob, carol, paymentAmount, highFee, reserve] = setup(env);
487
488 auto const alicePrePay = env.balance(alice, XRP);
489 auto const bobPrePay = env.balance(bob, XRP);
490 auto const carolPrePay = env.balance(carol, XRP);
491
492 env(pay(alice, carol, paymentAmount), delegate::As(bob), Fee(highFee));
493 env.close();
494
495 env.require(Balance(alice, alicePrePay - paymentAmount));
496 env.require(Balance(bob, bobPrePay - highFee));
497 env.require(Balance(carol, carolPrePay + paymentAmount));
498 }
499 }
500
501 // Delegated account can pay the fee even if it dips below reserve.
502 {
503 Env env(*this);
504 Account const alice{"alice"};
505 Account const bob{"bob"};
506 Account const carol{"carol"};
507
508 auto const baseFee = env.current()->fees().base;
509 auto const baseReserve = env.current()->fees().accountReserve(0, 1);
510
511 env.fund(env.current()->fees().accountReserve(1, 1) + baseFee + XRP(1), alice);
512 env.fund(baseReserve, bob);
513 env.fund(XRP(1000), carol);
514 env.close();
515
516 env(delegate::set(alice, bob, {"Payment"}));
517 env.close();
518
519 auto const alicePreTx = env.balance(alice, XRP);
520 auto const bobPreTx = env.balance(bob, XRP);
521
522 // After paying for this transaction, bob's balance will
523 // dip below the base reserve
524 env(pay(alice, carol, XRP(1)), delegate::As(bob));
525 env.close();
526
527 // Bob's balance is now less than the base reserve.
528 BEAST_EXPECT(env.balance(bob, XRP) < baseReserve);
529 env.require(Balance(bob, bobPreTx - drops(baseFee)));
530
531 // Alice's balance only decreased by the 1.0 XRP she sent.
532 env.require(Balance(alice, alicePreTx - XRP(1)));
533 }
534
535 // The delegated account has enough balance for the fee, but delegator
536 // runs into tecUNFUNDED_PAYMENT.
537 {
538 Env env(*this);
539 Account const alice{"alice"};
540 Account const bob{"bob"};
541 Account const carol{"carol"};
542
543 auto const baseFee = env.current()->fees().base;
544 auto const reserve = env.current()->fees().accountReserve(1, 1);
545
546 // Alice is funded with (reserve + baseFee): after DelegateSet she has
547 // exactly 'reserve', which is insufficient to send XRP(10) while keeping
548 // reserve. Bob has plenty to pay the fee.
549 env.fund(reserve + baseFee, alice);
550 env.fund(XRP(1000), bob);
551 env.fund(XRP(1000), carol);
552 env.close();
553
554 env(delegate::set(alice, bob, {"Payment"}));
555 env.close();
556
557 auto const alicePrePay = env.balance(alice, XRP);
558 auto const bobPrePay = env.balance(bob, XRP);
559 auto const carolPrePay = env.balance(carol, XRP);
560
561 // Bob pays the fee, but Alice has insufficient balance to send XRP(10).
562 env(pay(alice, carol, XRP(10)), delegate::As(bob), Ter(tecUNFUNDED_PAYMENT));
563
564 env.require(Balance(alice, alicePrePay));
565 env.require(Balance(bob, bobPrePay - drops(baseFee)));
566 env.require(Balance(carol, carolPrePay));
567 }
568 }
569
570 void
572 {
573 testcase("test sequence");
574 using namespace jtx;
575
576 Env env(*this);
577 Account const alice{"alice"};
578 Account const bob{"bob"};
579 Account const carol{"carol"};
580 env.fund(XRP(10000), alice, bob, carol);
581 env.close();
582
583 auto aliceSeq = env.seq(alice);
584 auto bobSeq = env.seq(bob);
585 env(delegate::set(alice, bob, {"Payment"}));
586 env(delegate::set(bob, alice, {"Payment"}));
587 env.close();
588 BEAST_EXPECT(env.seq(alice) == aliceSeq + 1);
589 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
590 aliceSeq = env.seq(alice);
591 bobSeq = env.seq(bob);
592
593 for (auto i = 0; i < 20; ++i)
594 {
595 // bob is the delegated account, his sequence won't kIncrement
596 env(pay(alice, carol, XRP(10)), Fee(XRP(10)), delegate::As(bob));
597 env.close();
598 BEAST_EXPECT(env.seq(alice) == aliceSeq + 1);
599 BEAST_EXPECT(env.seq(bob) == bobSeq);
600 aliceSeq = env.seq(alice);
601
602 // bob sends payment for himself, his sequence will kIncrement
603 env(pay(bob, carol, XRP(10)), Fee(XRP(10)));
604 BEAST_EXPECT(env.seq(alice) == aliceSeq);
605 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
606 bobSeq = env.seq(bob);
607
608 // alice is the delegated account, her sequence won't kIncrement
609 env(pay(bob, carol, XRP(10)), Fee(XRP(10)), delegate::As(alice));
610 env.close();
611 BEAST_EXPECT(env.seq(alice) == aliceSeq);
612 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
613 bobSeq = env.seq(bob);
614
615 // alice sends payment for herself, her sequence will kIncrement
616 env(pay(alice, carol, XRP(10)), Fee(XRP(10)));
617 BEAST_EXPECT(env.seq(alice) == aliceSeq + 1);
618 BEAST_EXPECT(env.seq(bob) == bobSeq);
619 aliceSeq = env.seq(alice);
620 }
621 }
622
623 void
625 {
626 testcase("test deleting account");
627 using namespace jtx;
628
629 // Delegator (alice) deletes account: Delegate object is cleaned up from
630 // both alice's and bob's owner directories.
631 {
632 Env env(*this);
633 Account const alice{"alice"};
634 Account const bob{"bob"};
635 Account const carol{"carol"};
636 env.fund(XRP(100000), alice, bob, carol);
637 env.close();
638
639 env(delegate::set(alice, bob, {"Payment"}));
640 env.close();
641
642 auto const delegateKey = keylet::delegate(alice.id(), bob.id());
643 BEAST_EXPECT(env.closed()->exists(delegateKey));
644
645 auto const hasKey = [](std::vector<SLE::const_pointer> const& dir, UInt256 const& key) {
646 return std::any_of( // NOLINT(modernize-use-ranges)
647 dir.begin(), dir.end(), [&](auto const& sle) { return sle->key() == key; });
648 };
649
650 // Delegate object should appear in both alice's and bob's directories
651 BEAST_EXPECT(hasKey(ownerDirEntries(*env.closed(), alice), delegateKey.key));
652 BEAST_EXPECT(hasKey(ownerDirEntries(*env.closed(), bob), delegateKey.key));
653
654 for (std::uint32_t i = 0; i < 256; ++i)
655 env.close();
656
657 auto const aliceBalance = env.balance(alice);
658 auto const carolBalance = env.balance(carol);
659
660 // alice deletes account, this will remove the Delegate object from
661 // both alice's and bob's owner directories
662 auto const deleteFee = drops(env.current()->fees().increment);
663 env(acctdelete(alice, carol), Fee(deleteFee));
664 env.close();
665
666 BEAST_EXPECT(!env.closed()->exists(keylet::account(alice.id())));
667 BEAST_EXPECT(!env.closed()->exists(keylet::ownerDir(alice.id())));
668 BEAST_EXPECT(!env.closed()->exists(delegateKey));
669 // bob's directory should no longer reference the Delegate object
670 BEAST_EXPECT(!hasKey(ownerDirEntries(*env.closed(), bob), delegateKey.key));
671 BEAST_EXPECT(env.balance(carol) == carolBalance + aliceBalance - deleteFee);
672 }
673
674 // Delegatee (bob) deletes account: Delegate object is cleaned up from
675 // both alice's and bob's owner directories, freeing alice's reserve so
676 // she can subsequently delete her own account.
677 {
678 Env env(*this);
679 Account const alice{"alice"};
680 Account const bob{"bob"};
681 Account const carol{"carol"};
682 env.fund(XRP(100000), alice, bob, carol);
683 env.close();
684
685 env(delegate::set(alice, bob, {"Payment"}));
686 env.close();
687
688 auto const delegateKey = keylet::delegate(alice.id(), bob.id());
689 BEAST_EXPECT(env.closed()->exists(delegateKey));
690
691 auto const hasKey = [](std::vector<SLE::const_pointer> const& dir, UInt256 const& key) {
692 return std::any_of( // NOLINT(modernize-use-ranges)
693 dir.begin(), dir.end(), [&](auto const& sle) { return sle->key() == key; });
694 };
695
696 BEAST_EXPECT(hasKey(ownerDirEntries(*env.closed(), alice), delegateKey.key));
697 BEAST_EXPECT(hasKey(ownerDirEntries(*env.closed(), bob), delegateKey.key));
698
699 // The Delegate entry counts against alice's ownerCount.
700 auto const sleAlice = env.closed()->read(keylet::account(alice.id()));
701 BEAST_EXPECT(sleAlice);
702 BEAST_EXPECT(sleAlice->getFieldU32(sfOwnerCount) == 1);
703
704 for (std::uint32_t i = 0; i < 256; ++i)
705 env.close();
706
707 auto const bobBalance = env.balance(bob);
708 auto const carolBalance = env.balance(carol);
709
710 // bob (the authorized/delegatee account) deletes his account.
711 // This must clean up the Delegate object from both alice's and
712 // bob's owner directories so alice's delegation does not survive
713 // a potential account resurrection.
714 auto const deleteFee = drops(env.current()->fees().increment);
715 env(acctdelete(bob, carol), Fee(deleteFee));
716 env.close();
717
718 BEAST_EXPECT(!env.closed()->exists(keylet::account(bob.id())));
719 BEAST_EXPECT(!env.closed()->exists(keylet::ownerDir(bob.id())));
720 BEAST_EXPECT(!env.closed()->exists(delegateKey));
721 // alice's directory should no longer reference the Delegate object
722 BEAST_EXPECT(!hasKey(ownerDirEntries(*env.closed(), alice), delegateKey.key));
723 BEAST_EXPECT(env.balance(carol) == carolBalance + bobBalance - deleteFee);
724
725 // alice's ownerCount is now 0; she can delete her own account.
726 auto const sleAlice2 = env.closed()->read(keylet::account(alice.id()));
727 BEAST_EXPECT(sleAlice2);
728 BEAST_EXPECT(sleAlice2->getFieldU32(sfOwnerCount) == 0);
729
730 auto const aliceDeleteFee = drops(env.current()->fees().increment);
731 env(acctdelete(alice, carol), Fee(aliceDeleteFee));
732 env.close();
733
734 BEAST_EXPECT(!env.closed()->exists(keylet::account(alice.id())));
735 }
736
737 // Multiple delegators -> same delegatee: when the delegatee (bob)
738 // deletes his account, ALL Delegate objects (from alice and carol)
739 // must be cleaned up from every delegator's directory.
740 {
741 Env env(*this);
742 Account const alice{"alice"};
743 Account const bob{"bob"};
744 Account const carol{"carol"};
745 Account const dave{"dave"};
746 env.fund(XRP(100000), alice, bob, carol, dave);
747 env.close();
748
749 // Both alice and carol delegate to bob
750 env(delegate::set(alice, bob, {"Payment"}));
751 env(delegate::set(carol, bob, {"EscrowCreate"}));
752 env.close();
753
754 auto const aliceBobKey = keylet::delegate(alice.id(), bob.id());
755 auto const carolBobKey = keylet::delegate(carol.id(), bob.id());
756
757 auto const hasKey = [](std::vector<SLE::const_pointer> const& dir, UInt256 const& key) {
758 return std::any_of( // NOLINT(modernize-use-ranges)
759 dir.begin(), dir.end(), [&](auto const& sle) { return sle->key() == key; });
760 };
761
762 // Both Delegate objects exist and are in bob's directory
763 BEAST_EXPECT(env.closed()->exists(aliceBobKey));
764 BEAST_EXPECT(env.closed()->exists(carolBobKey));
765 BEAST_EXPECT(hasKey(ownerDirEntries(*env.closed(), bob), aliceBobKey.key));
766 BEAST_EXPECT(hasKey(ownerDirEntries(*env.closed(), bob), carolBobKey.key));
767
768 for (std::uint32_t i = 0; i < 256; ++i)
769 env.close();
770
771 auto const bobBalance = env.balance(bob);
772 auto const daveBalance = env.balance(dave);
773
774 auto const deleteFee = drops(env.current()->fees().increment);
775 env(acctdelete(bob, dave), Fee(deleteFee));
776 env.close();
777
778 // bob's account and directory are gone
779 BEAST_EXPECT(!env.closed()->exists(keylet::account(bob.id())));
780 BEAST_EXPECT(!env.closed()->exists(keylet::ownerDir(bob.id())));
781
782 // Both Delegate objects are erased
783 BEAST_EXPECT(!env.closed()->exists(aliceBobKey));
784 BEAST_EXPECT(!env.closed()->exists(carolBobKey));
785
786 // alice's and carol's directories no longer reference the objects
787 BEAST_EXPECT(!hasKey(ownerDirEntries(*env.closed(), alice), aliceBobKey.key));
788 BEAST_EXPECT(!hasKey(ownerDirEntries(*env.closed(), carol), carolBobKey.key));
789
790 BEAST_EXPECT(env.balance(dave) == daveBalance + bobBalance - deleteFee);
791 }
792 }
793
794 void
796 {
797 testcase("test delegate transaction");
798 using namespace jtx;
799
800 Env env(*this);
801 Account const alice{"alice"};
802 Account const bob{"bob"};
803 Account const carol{"carol"};
804
805 XRPAmount const baseFee{env.current()->fees().base};
806
807 // use different initial amount to distinguish the source balance
808 env.fund(XRP(10000), alice);
809 env.fund(XRP(20000), bob);
810 env.fund(XRP(30000), carol);
811 env.close();
812
813 auto aliceBalance = env.balance(alice, XRP);
814 auto bobBalance = env.balance(bob, XRP);
815 auto carolBalance = env.balance(carol, XRP);
816
817 // can not send transaction on one's own behalf
818 env(pay(alice, bob, XRP(50)), delegate::As(alice), Ter(temBAD_SIGNER));
819 env.require(Balance(alice, aliceBalance));
820
821 env(delegate::set(alice, bob, {"Payment"}));
822 env.close();
823 env.require(Balance(alice, aliceBalance - drops(baseFee)));
824 aliceBalance = env.balance(alice, XRP);
825
826 // bob pays 50 XRP to carol on behalf of alice
827 env(pay(alice, carol, XRP(50)), delegate::As(bob));
828 env.close();
829 env.require(Balance(alice, aliceBalance - XRP(50)));
830 env.require(Balance(carol, carolBalance + XRP(50)));
831 // bob pays the fee
832 env.require(Balance(bob, bobBalance - drops(baseFee)));
833 aliceBalance = env.balance(alice, XRP);
834 bobBalance = env.balance(bob, XRP);
835 carolBalance = env.balance(carol, XRP);
836
837 // bob pays 50 XRP to bob self on behalf of alice
838 env(pay(alice, bob, XRP(50)), delegate::As(bob));
839 env.close();
840 env.require(Balance(alice, aliceBalance - XRP(50)));
841 env.require(Balance(bob, bobBalance + XRP(50) - drops(baseFee)));
842 aliceBalance = env.balance(alice, XRP);
843 bobBalance = env.balance(bob, XRP);
844
845 // bob pay 50 XRP to alice herself on behalf of alice
846 env(pay(alice, alice, XRP(50)), delegate::As(bob), Ter(temREDUNDANT));
847 env.close();
848
849 // bob does not have permission to create check
850 env(check::create(alice, bob, XRP(10)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
851
852 // carol does not have permission to create check
853 env(check::create(alice, bob, XRP(10)),
854 delegate::As(carol),
856 }
857
858 void
860 {
861 testcase("test payment granular");
862 using namespace jtx;
863
864 // test PaymentMint and PaymentBurn
865 {
866 Env env(*this);
867 Account const alice{"alice"};
868 Account const bob{"bob"};
869 Account const gw{"gateway"};
870 Account const gw2{"gateway2"};
871 auto const usd = gw["USD"];
872 auto const eur = gw2["EUR"];
873
874 env.fund(XRP(10000), alice);
875 env.fund(XRP(20000), bob);
876 env.fund(XRP(40000), gw, gw2);
877 env.trust(usd(200), alice);
878 env.trust(eur(400), gw);
879 env.close();
880
881 XRPAmount const baseFee{env.current()->fees().base};
882 auto aliceBalance = env.balance(alice, XRP);
883 auto bobBalance = env.balance(bob, XRP);
884 auto gwBalance = env.balance(gw, XRP);
885 auto gw2Balance = env.balance(gw2, XRP);
886
887 // delegate ledger object is not created yet
888 env(pay(gw, alice, usd(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
889 env.require(Balance(bob, bobBalance));
890
891 // gw gives bob burn permission
892 env(delegate::set(gw, bob, {"PaymentBurn"}));
893 env.close();
894 env.require(Balance(gw, gwBalance - drops(baseFee)));
895 gwBalance = env.balance(gw, XRP);
896
897 // bob sends a payment transaction on behalf of gw
898 env(pay(gw, alice, usd(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
899 env.close();
900 env.require(Balance(bob, bobBalance));
901
902 // gw gives bob mint permission, alice gives bob burn permission
903 env(delegate::set(gw, bob, {"PaymentMint"}));
904 env(delegate::set(alice, bob, {"PaymentBurn"}));
905 env.close();
906 env.require(Balance(alice, aliceBalance - drops(baseFee)));
907 env.require(Balance(gw, gwBalance - drops(baseFee)));
908 aliceBalance = env.balance(alice, XRP);
909 gwBalance = env.balance(gw, XRP);
910
911 // can not send XRP
912 env(pay(gw, alice, XRP(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
913 env.close();
914 env.require(Balance(bob, bobBalance));
915
916 // mint 50 USD
917 env(pay(gw, alice, usd(50)), delegate::As(bob));
918 env.close();
919 env.require(Balance(bob, bobBalance - drops(baseFee)));
920 env.require(Balance(gw, gwBalance));
921 env.require(Balance(gw, alice["USD"](-50)));
922 env.require(Balance(alice, usd(50)));
923 BEAST_EXPECT(env.balance(bob, usd) == usd(0));
924 bobBalance = env.balance(bob, XRP);
925
926 // burn 30 USD
927 env(pay(alice, gw, usd(30)), delegate::As(bob));
928 env.close();
929 env.require(Balance(bob, bobBalance - drops(baseFee)));
930 env.require(Balance(gw, gwBalance));
931 env.require(Balance(gw, alice["USD"](-20)));
932 env.require(Balance(alice, usd(20)));
933 BEAST_EXPECT(env.balance(bob, usd) == usd(0));
934 bobBalance = env.balance(bob, XRP);
935
936 // bob has both mint and burn permissions
937 env(delegate::set(gw, bob, {"PaymentMint", "PaymentBurn"}));
938 env.close();
939 env.require(Balance(gw, gwBalance - drops(baseFee)));
940 gwBalance = env.balance(gw, XRP);
941
942 // mint 100 USD for gw
943 env(pay(gw, alice, usd(100)), delegate::As(bob));
944 env.close();
945 env.require(Balance(gw, alice["USD"](-120)));
946 env.require(Balance(alice, usd(120)));
947 env.require(Balance(bob, bobBalance - drops(baseFee)));
948 bobBalance = env.balance(bob, XRP);
949
950 // gw2 pays gw 200 EUR
951 env(pay(gw2, gw, eur(200)));
952 env.close();
953 env.require(Balance(gw2, gw2Balance - drops(baseFee)));
954 gw2Balance = env.balance(gw2, XRP);
955 env.require(Balance(gw2, gw["EUR"](-200)));
956 env.require(Balance(gw, eur(200)));
957
958 // burn 100 EUR for gw
959 env(pay(gw, gw2, eur(100)), delegate::As(bob));
960 env.close();
961 env.require(Balance(gw2, gw["EUR"](-100)));
962 env.require(Balance(gw, eur(100)));
963 env.require(Balance(bob, bobBalance - drops(baseFee)));
964 env.require(Balance(gw, gwBalance));
965 env.require(Balance(gw2, gw2Balance));
966 env.require(Balance(alice, aliceBalance));
967 }
968
969 // test PaymentMint won't affect Payment transaction level delegation.
970 {
971 Env env(*this);
972 Account const alice{"alice"};
973 Account const bob{"bob"};
974 Account const gw{"gateway"};
975 auto const usd = gw["USD"];
976
977 env.fund(XRP(10000), alice);
978 env.fund(XRP(20000), bob);
979 env.fund(XRP(40000), gw);
980 env.trust(usd(200), alice);
981 env.close();
982
983 XRPAmount const baseFee{env.current()->fees().base};
984
985 auto const aliceBalance = env.balance(alice, XRP);
986 auto const bobBalance = env.balance(bob, XRP);
987 auto gwBalance = env.balance(gw, XRP);
988
989 // gw gives bob PaymentBurn permission
990 env(delegate::set(gw, bob, {"PaymentBurn"}));
991 env.close();
992 env.require(Balance(gw, gwBalance - drops(baseFee)));
993 gwBalance = env.balance(gw, XRP);
994
995 // bob can not mint on behalf of gw because he only has burn
996 // permission
997 env(pay(gw, alice, usd(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
998 env.close();
999 env.require(Balance(bob, bobBalance));
1000
1001 // gw gives bob Payment permission as well
1002 env(delegate::set(gw, bob, {"PaymentBurn", "Payment"}));
1003 env.close();
1004 env.require(Balance(gw, gwBalance - drops(baseFee)));
1005 gwBalance = env.balance(gw, XRP);
1006
1007 // bob now can mint on behalf of gw
1008 env(pay(gw, alice, usd(50)), delegate::As(bob));
1009 env.close();
1010 env.require(Balance(bob, bobBalance - drops(baseFee)));
1011 env.require(Balance(gw, gwBalance));
1012 env.require(Balance(alice, aliceBalance));
1013 env.require(Balance(gw, alice["USD"](-50)));
1014 env.require(Balance(alice, usd(50)));
1015 BEAST_EXPECT(env.balance(bob, usd) == usd(0));
1016 }
1017
1018 // disallow cross currency payment with only PaymentBurn/PaymentMint
1019 // permission
1020 {
1021 Env env(*this, features);
1022 Account const alice{"alice"};
1023 Account const bob{"bob"};
1024 Account const gw{"gateway"};
1025 Account const carol{"carol"};
1026 auto const usd = gw["USD"];
1027
1028 env.fund(XRP(10000), alice, bob, carol, gw);
1029 env.close();
1030 env.trust(usd(50000), alice);
1031 env.trust(usd(50000), bob);
1032 env.trust(usd(50000), carol);
1033 env(pay(gw, alice, usd(10000)));
1034 env(pay(gw, bob, usd(10000)));
1035 env(pay(gw, carol, usd(10000)));
1036 env.close();
1037
1038 // PaymentMint
1039 {
1040 env(offer(carol, XRP(100), usd(501)));
1041 BEAST_EXPECT(expectOffers(env, carol, 1));
1042 env(delegate::set(gw, bob, {"PaymentMint"}));
1043 env.close();
1044
1045 // Cross-currency sfSendMax without any flag passes the granular
1046 // template and is rejected by the granular semantic check, because
1047 // the sfSendMax asset (XRP) is not the same as the sfAmount asset (USD).
1048 env(pay(gw, alice, usd(500)),
1049 Sendmax(XRP(1001)),
1050 delegate::As(bob),
1052 BEAST_EXPECT(expectOffers(env, carol, 1));
1053
1054 // bob can not send cross currency payment on behalf of the gw,
1055 // even with PaymentMint permission and gw being the issuer.
1056 env(pay(gw, alice, usd(5000)),
1057 Sendmax(XRP(1001)),
1058 Txflags(tfPartialPayment),
1059 delegate::As(bob),
1061 BEAST_EXPECT(expectOffers(env, carol, 1));
1062
1063 env(pay(gw, alice, usd(5000)),
1064 Path(~XRP),
1065 Txflags(tfPartialPayment),
1066 delegate::As(bob),
1068 BEAST_EXPECT(expectOffers(env, carol, 1));
1069
1070 // succeed with direct payment
1071 env(pay(gw, alice, usd(100)), delegate::As(bob));
1072 env.close();
1073 }
1074
1075 // PaymentBurn
1076 {
1077 env(offer(bob, XRP(100), usd(501)));
1078 BEAST_EXPECT(expectOffers(env, bob, 1));
1079 env(delegate::set(alice, bob, {"PaymentBurn"}));
1080 env.close();
1081
1082 // Cross-currency sfSendMax without any flag passes the granular
1083 // template and is rejected by the granular semantic check, because
1084 // the sfSendMax asset (XRP) is not the same as the sfAmount asset (USD).
1085 env(pay(alice, gw, usd(500)),
1086 Sendmax(XRP(1001)),
1087 delegate::As(bob),
1089 BEAST_EXPECT(expectOffers(env, bob, 1));
1090
1091 // bob can not send cross currency payment on behalf of alice,
1092 // even with PaymentBurn permission and gw being the issuer.
1093 env(pay(alice, gw, usd(5000)),
1094 Sendmax(XRP(1001)),
1095 Txflags(tfPartialPayment),
1096 delegate::As(bob),
1098 BEAST_EXPECT(expectOffers(env, bob, 1));
1099
1100 env(pay(alice, gw, usd(5000)),
1101 Path(~XRP),
1102 Txflags(tfPartialPayment),
1103 delegate::As(bob),
1105 BEAST_EXPECT(expectOffers(env, bob, 1));
1106
1107 // succeed with direct payment
1108 env(pay(alice, gw, usd(100)), delegate::As(bob));
1109 env.close();
1110 }
1111 }
1112
1113 // PaymentMint/PaymentBurn with sfSendMax of the same asset is allowed,
1114 // same-asset SendMax is still a direct payment, not cross-currency.
1115 {
1116 Env env(*this, features);
1117 Account const alice{"alice"};
1118 Account const bob{"bob"};
1119 Account const gw{"gw"};
1120 auto const usd = gw["USD"];
1121 env.fund(XRP(10000), alice, bob, gw);
1122 env.trust(usd(200), alice);
1123 env.close();
1124
1125 env(delegate::set(gw, bob, {"PaymentMint"}));
1126 env.close();
1127
1128 // sfSendMax with same asset as sfAmount, still a direct payment
1129 env(pay(gw, alice, usd(50)), Sendmax(usd(50)), delegate::As(bob));
1130 env.require(Balance(alice, usd(50)));
1131
1132 env(delegate::set(alice, bob, {"PaymentBurn"}));
1133 env.close();
1134
1135 env(pay(alice, gw, usd(30)), delegate::As(bob));
1136 env.require(Balance(alice, usd(20)));
1137 }
1138
1139 // PaymentBurn is authorized by balance direction, not trust limit.
1140 // holder is allowed to burn even if trust limit is 0.
1141 {
1142 Env env(*this);
1143 Account const alice{"alice"};
1144 Account const bob{"bob"};
1145 Account const gw{"gateway"};
1146 auto const gwUSD = gw["USD"];
1147 auto const aliceUSD = alice["USD"];
1148
1149 env.fund(XRP(10000), alice, bob, gw);
1150 env.trust(gwUSD(200), alice);
1151 env.close();
1152
1153 env(pay(gw, alice, gwUSD(50)));
1154 env.require(Balance(alice, gwUSD(50)));
1155 env.close();
1156
1157 env(delegate::set(alice, bob, {"PaymentBurn"}));
1158 env.close();
1159
1160 env.trust(gwUSD(0), alice);
1161 env.close();
1162 BEAST_EXPECT(env.limit(alice, gwUSD.issue()) == gwUSD(0));
1163
1164 env(trust(gw, aliceUSD(200)));
1165 env.close();
1166
1167 env(pay(alice, gw, gwUSD(30)), delegate::As(bob));
1168 env.require(Balance(alice, gwUSD(20)));
1169 env.require(Balance(gw, aliceUSD(-20)));
1170 }
1171
1172 // PaymentBurn must not exceed the balance the account holds. Redeeming past
1173 // zero makes the payment engine issue the account's own IOUs, which is a mint.
1174 {
1175 Env env(*this, features);
1176 Account const alice{"alice"};
1177 Account const bob{"bob"};
1178 Account const gw{"gateway"};
1179 auto const gwUSD = gw["USD"];
1180 auto const aliceUSD = alice["USD"];
1181
1182 env.fund(XRP(10000), alice, bob, gw);
1183 env.trust(gwUSD(200), alice);
1184 env.close();
1185
1186 env(pay(gw, alice, gwUSD(50)));
1187 env.close();
1188 env.require(Balance(alice, gwUSD(50)));
1189
1190 // gw accepts alice-issued USD, so the engine has issuing liquidity
1191 // available once the trustline reaches zero.
1192 env(trust(gw, aliceUSD(200)));
1193 env.close();
1194
1195 env(delegate::set(alice, bob, {"PaymentBurn"}));
1196 env.close();
1197
1198 if (!features[fixCleanup3_4_0])
1199 {
1200 // Pre-fixCleanup3_4_0: the balance direction alone authorizes the payment, so it
1201 // redeems alice's 50 and then mints 50 alice-issued USD.
1202 env(pay(alice, gw, gwUSD(100)), delegate::As(bob));
1203 env.require(Balance(alice, gwUSD(-50)));
1204 env.require(Balance(gw, aliceUSD(50)));
1205 }
1206 else
1207 {
1208 // Post-fixCleanup3_4_0: Rejected because it exceeds what alice holds.
1209 env(pay(alice, gw, gwUSD(100)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1210 env.require(Balance(alice, gwUSD(50)));
1211 env.require(Balance(gw, aliceUSD(-50)));
1212
1213 // Allowed because it is less than what alice holds.
1214 env(pay(alice, gw, gwUSD(20)), delegate::As(bob));
1215 env.require(Balance(alice, gwUSD(30)));
1216 env.close();
1217
1218 // Exactly what alice holds: allowed, and settles at zero.
1219 env(pay(alice, gw, gwUSD(30)), delegate::As(bob));
1220 env.require(Balance(alice, gwUSD(0)));
1221 env.close();
1222
1223 // Nothing left to burn: rejected.
1224 env(pay(alice, gw, gwUSD(1)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1225 env.require(Balance(gw, aliceUSD(0)));
1226 }
1227 }
1228
1229 // A delegate holding both PaymentMint and PaymentBurn may cross zero.
1230 {
1231 Env env(*this, features);
1232 Account const alice{"alice"};
1233 Account const bob{"bob"};
1234 Account const gw{"gateway"};
1235 auto const gwUSD = gw["USD"];
1236 auto const aliceUSD = alice["USD"];
1237
1238 env.fund(XRP(10000), alice, bob, gw);
1239 env.trust(gwUSD(200), alice);
1240 env.close();
1241
1242 env(pay(gw, alice, gwUSD(50)));
1243 env(trust(gw, aliceUSD(200)));
1244 env.close();
1245
1246 env(delegate::set(alice, bob, {"PaymentBurn", "PaymentMint"}));
1247 env.close();
1248
1249 env(pay(alice, gw, gwUSD(100)), delegate::As(bob));
1250 env.require(Balance(alice, gwUSD(-50)));
1251 env.require(Balance(gw, aliceUSD(50)));
1252 }
1253
1254 // Test invalid fields or flags not allowed in granular permission template
1255 {
1256 Env env(*this, features);
1257 Account const alice{"alice"};
1258 Account const bob{"bob"};
1259 Account const gw{"gw"};
1260 auto const usd = gw["USD"];
1261 env.fund(XRP(10000), alice, bob, gw);
1262 env.trust(usd(200), alice);
1263 env.close();
1264
1265 env(delegate::set(gw, bob, {"PaymentMint"}));
1266 env(delegate::set(alice, bob, {"PaymentBurn"}));
1267 env.close();
1268
1269 // sfDeliverMin (with tfPartialPayment) is not in the PaymentMint
1270 // or PaymentBurn template.
1271 env(pay(gw, alice, usd(100)),
1272 DeliverMin(usd(50)),
1273 Txflags(tfPartialPayment),
1274 delegate::As(bob),
1276 env(pay(alice, gw, usd(50)),
1277 DeliverMin(usd(25)),
1278 Txflags(tfPartialPayment),
1279 delegate::As(bob),
1281
1282 // sfDomainID is not in the PaymentMint or PaymentBurn template.
1283 env(pay(gw, alice, usd(100)),
1284 Domain(UInt256{1}),
1285 delegate::As(bob),
1287 env(pay(alice, gw, usd(50)),
1288 Domain(UInt256{1}),
1289 delegate::As(bob),
1291
1292 // sfPaths is not in the PaymentMint or PaymentBurn template.
1293 env(pay(gw, alice, usd(100)),
1294 Path(~XRP),
1295 delegate::As(bob),
1297 env(pay(alice, gw, usd(50)),
1298 Path(~XRP),
1299 delegate::As(bob),
1301
1302 // Valid Payment flags that are not in the PaymentMint or PaymentBurn template.
1303 for (auto const flag : {tfPartialPayment, tfNoRippleDirect, tfLimitQuality})
1304 {
1305 env(pay(gw, alice, usd(100)),
1306 Txflags(flag),
1307 delegate::As(bob),
1309 env(pay(alice, gw, usd(50)),
1310 Txflags(flag),
1311 delegate::As(bob),
1313 }
1314
1315 // The same payments without the extra field or flag succeed.
1316 env(pay(gw, alice, usd(100)), delegate::As(bob));
1317 env(pay(alice, gw, usd(50)), delegate::As(bob));
1318 env.require(Balance(alice, usd(50)));
1319 }
1320
1321 // Delegate account holds no granular permissions for the tx type:
1322 // getGranularPermission returns empty set.
1323 {
1324 Env env(*this, features);
1325 Account const alice{"alice"};
1326 Account const bob{"bob"};
1327 Account const gw{"gw"};
1328 auto const usd = gw["USD"];
1329 env.fund(XRP(10000), alice, bob, gw);
1330 env.trust(usd(200), alice);
1331 env.close();
1332
1333 // Bob holds only an AccountSet granular permission.
1334 env(delegate::set(alice, bob, {"AccountDomainSet"}));
1335 env.close();
1336
1337 // Payment has granular permissions defined in permissions.macro,
1338 // but bob only holds AccountSet's granular permission,
1339 // getGranularPermission returns empty.
1340 env(pay(alice, gw, usd(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1341 }
1342
1343 // PaymentMint and PaymentBurn for MPT
1344 {
1345 std::string logs;
1346 Env env(*this, features, std::make_unique<CaptureLogs>(&logs));
1347 Account const alice{"alice"};
1348 Account const bob{"bob"};
1349 Account const gw{"gateway"};
1350
1351 MPTTester mpt(env, gw, {.holders = {alice, bob}});
1352 mpt.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
1353
1354 mpt.authorize({.account = alice});
1355 mpt.authorize({.account = bob});
1356
1357 auto const gwMPT = mpt["MPT"];
1358 env(pay(gw, alice, gwMPT(500)));
1359 env(pay(gw, bob, gwMPT(500)));
1360 env.close();
1361 auto aliceMPT = env.balance(alice, gwMPT);
1362 auto const bobMPT = env.balance(bob, gwMPT);
1363
1364 // PaymentMint
1365 {
1366 env(delegate::set(gw, bob, {"PaymentMint"}));
1367 env.close();
1368
1369 env(pay(gw, alice, gwMPT(50)), delegate::As(bob));
1370 BEAST_EXPECT(env.balance(alice, gwMPT) == aliceMPT + gwMPT(50));
1371 BEAST_EXPECT(env.balance(bob, gwMPT) == bobMPT);
1372 aliceMPT = env.balance(alice, gwMPT);
1373 }
1374
1375 // PaymentBurn
1376 {
1377 env(delegate::set(alice, bob, {"PaymentBurn"}));
1378 env.close();
1379
1380 env(pay(alice, gw, gwMPT(50)), delegate::As(bob));
1381 BEAST_EXPECT(env.balance(alice, gwMPT) == aliceMPT - gwMPT(50));
1382 BEAST_EXPECT(env.balance(bob, gwMPT) == bobMPT);
1383 aliceMPT = env.balance(alice, gwMPT);
1384 }
1385
1386 // Grant both granular permissions and tx level permission.
1387 {
1388 env(delegate::set(alice, bob, {"PaymentBurn", "PaymentMint", "Payment"}));
1389 env.close();
1390 env(pay(alice, gw, gwMPT(50)), delegate::As(bob));
1391 BEAST_EXPECT(env.balance(alice, gwMPT) == aliceMPT - gwMPT(50));
1392 BEAST_EXPECT(env.balance(bob, gwMPT) == bobMPT);
1393 aliceMPT = env.balance(alice, gwMPT);
1394 env(pay(alice, bob, gwMPT(100)), delegate::As(bob));
1395 BEAST_EXPECT(env.balance(alice, gwMPT) == aliceMPT - gwMPT(100));
1396 BEAST_EXPECT(env.balance(bob, gwMPT) == bobMPT + gwMPT(100));
1397 }
1398 }
1399
1400 // PaymentMint/PaymentBurn must not trust IOU issuer aliases.
1401 // In a direct IOU payment, sfAmount.issuer may be encoded as either
1402 // endpoint, and PaySteps normalizes those aliases to the same execution.
1403 // These cases ensure a delegate cannot flip the encoded issuer to turn a
1404 // mint into an apparent burn, or a burn into an apparent mint.
1405 {
1406 Env env(*this);
1407 Account const alice{"alice"};
1408 Account const bob{"bob"};
1409 Account const gw{"gateway"};
1410 auto const gwUSD = gw["USD"];
1411 auto const aliceUSD = alice["USD"];
1412
1413 env.fund(XRP(10000), alice, bob, gw);
1414 env.trust(gwUSD(200), alice);
1415 env.close();
1416
1417 // Alice holds 100 USD issued by gw.
1418 env(pay(gw, alice, gwUSD(100)));
1419 env.close();
1420 env.require(Balance(alice, gwUSD(100)));
1421
1422 // Delegate with only PaymentBurn tries to mint by encoding
1423 // Amount.issuer as the destination alias, alice. The actual issuer
1424 // is gw, so this requires PaymentMint and must be rejected.
1425 {
1426 env(delegate::set(gw, bob, {"PaymentBurn"}));
1427 env.close();
1428
1429 // Amount.issuer = alice (destination), rejected because gw is
1430 // the actual issuer and PaymentMint is required.
1431 env(pay(gw, alice, aliceUSD(50)),
1432 delegate::As(bob),
1434 env.require(Balance(alice, gwUSD(100)));
1435
1436 // Fails because bob holds PaymentBurn, not PaymentMint.
1437 env(pay(gw, alice, gwUSD(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1438 env.require(Balance(alice, gwUSD(100)));
1439 }
1440
1441 // Delegate with only PaymentMint tries to burn by encoding
1442 // Amount.issuer as the source alias, alice. The actual issuer is
1443 // gw, so this requires PaymentBurn and must be rejected.
1444 {
1445 env(delegate::set(alice, bob, {"PaymentMint"}));
1446 env.close();
1447
1448 // Amount.issuer = alice (account), rejected because gw is the
1449 // actual issuer and PaymentBurn is required.
1450 env(pay(alice, gw, aliceUSD(50)),
1451 delegate::As(bob),
1453 env.require(Balance(alice, gwUSD(100)));
1454
1455 // Fails because bob holds PaymentMint, not PaymentBurn.
1456 env(pay(alice, gw, gwUSD(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1457 env.require(Balance(alice, gwUSD(100)));
1458 }
1459 }
1460
1461 // Neither account nor destination is issuer.
1462 // PaymentMint and PaymentBurn do not authorize these payments.
1463 {
1464 // IOU
1465 {
1466 Env env(*this);
1467 Account const alice{"alice"};
1468 Account const bob{"bob"};
1469 Account const gw{"gateway"};
1470 Account const gw2{"gateway2"};
1471 auto const gwUSD = gw["USD"];
1472
1473 env.fund(XRP(10000), alice, bob, gw, gw2);
1474 env.close();
1475
1476 env.trust(gwUSD(200), alice);
1477 env.close();
1478
1479 env(pay(gw, alice, gwUSD(100)));
1480 env.close();
1481
1482 env(delegate::set(alice, bob, {"PaymentMint", "PaymentBurn"}));
1483 env.close();
1484
1485 env(pay(alice, gw, gw2["USD"](50)),
1486 delegate::As(bob),
1488 }
1489
1490 // MPT
1491 {
1492 Env env(*this, features);
1493 Account const alice{"alice"};
1494 Account const bob{"bob"};
1495 Account const gw{"gateway"};
1496 Account const gw2{"gateway2"};
1497
1498 env.fund(XRP(10000), gw2);
1499 env.close();
1500
1501 MPTTester mpt(env, gw, {.holders = {alice, bob}});
1502 mpt.create({.ownerCount = 1, .flags = tfMPTCanTransfer});
1503
1504 mpt.authorize({.account = alice});
1505 mpt.authorize({.account = bob});
1506
1507 auto const gwMPT = mpt["MPT"];
1508 env(pay(gw, alice, gwMPT(500)));
1509 env(pay(gw, bob, gwMPT(500)));
1510 env.close();
1511
1512 env(delegate::set(alice, bob, {"PaymentMint", "PaymentBurn"}));
1513 env.close();
1514
1515 env(pay(alice, gw2, gwMPT(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1516 }
1517 }
1518
1519 // IOU issuer is an endpoint, but no trustline exists.
1520 {
1521 Env env(*this);
1522 Account const alice{"alice"};
1523 Account const bob{"bob"};
1524 Account const gw{"gateway"};
1525
1526 env.fund(XRP(10000), alice, bob, gw);
1527 env.close();
1528
1529 env(delegate::set(alice, bob, {"PaymentMint", "PaymentBurn"}));
1530 env.close();
1531
1532 env(pay(alice, gw, alice["USD"](50)),
1533 delegate::As(bob),
1535 }
1536
1537 // Both trust limits (who is the designated issuer) and balance direction
1538 // (which way DirectStepI executes) must be checked. Neither alone is sufficient.
1539 {
1540 Env env(*this);
1541 Account const alice{"alice"};
1542 Account const bob{"bob"};
1543 Account const gw{"gateway"};
1544 auto const gwUSD = gw["USD"];
1545 auto const aliceUSD = alice["USD"];
1546
1547 env.fund(XRP(10000), alice, bob, gw);
1548
1549 // Alice trusts gw but holds zero gw-issued USD. With balance == 0,
1550 // DirectStepI would issue rather than redeem, so PaymentBurn must
1551 // be rejected even though Alice's trust limit to gw is positive.
1552 {
1553 env.trust(gwUSD(200), alice);
1554 env.close();
1555
1556 // Alice has nothing to burn.
1557 env(delegate::set(alice, bob, {"PaymentBurn"}));
1558 env.close();
1559
1560 env(pay(alice, gw, gwUSD(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1561 env(pay(alice, gw, aliceUSD(50)),
1562 delegate::As(bob),
1564 }
1565
1566 // Set up a trust line where gw holds alice-issued USD. DirectStepI
1567 // would redeem rather than issue, so PaymentMint must be rejected
1568 // even though the endpoint identity matches.
1569 {
1570 // Gw sets trust to accept alice-issued USD.
1571 env(trust(gw, aliceUSD(200)));
1572 env.close();
1573
1574 // Alice issues her own USD to gw; now gw holds alice's IOUs.
1575 env(pay(alice, gw, aliceUSD(100)));
1576 env.close();
1577
1578 // In gw's view, accountHolds(gw, USD, alice) > 0, so DirectStepI redeems.
1579 // PaymentMint must be rejected because the step would redeem, not issue.
1580 env(delegate::set(gw, bob, {"PaymentMint"}));
1581 env.close();
1582
1583 env(pay(gw, alice, gwUSD(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1584 env(pay(gw, alice, aliceUSD(50)),
1585 delegate::As(bob),
1587 }
1588 }
1589
1590 // Alice trusts gw but gw is not willing to hold alice's IOU (destLimit == 0).
1591 {
1592 Env env(*this);
1593 Account const alice{"alice"};
1594 Account const bob{"bob"};
1595 Account const gw{"gateway"};
1596 env.fund(XRP(10000), alice, bob, gw);
1597 env.trust(gw["USD"](200), alice);
1598 env.close();
1599
1600 env(delegate::set(alice, bob, {"PaymentMint"}));
1601 env.close();
1602
1603 env(pay(alice, gw, gw["USD"](50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1604 env(pay(alice, gw, alice["USD"](50)),
1605 delegate::As(bob),
1607 }
1608
1609 // Verify granular permissions of different tx types in the same SLE are scoped
1610 // correctly. AccountSet permissions don't apply to Payment and vice versa
1611 {
1612 Env env(*this);
1613 Account const alice{"alice"};
1614 Account const bob{"bob"};
1615 Account const gw{"gw"};
1616 auto const usd = gw["USD"];
1617 env.fund(XRP(10000), alice, bob, gw);
1618 env.trust(usd(200), alice);
1619 env.close();
1620
1621 // Alice granted bob with both AccountDomainSet and PaymentMint.
1622 env(delegate::set(alice, bob, {"AccountDomainSet", "PaymentMint"}));
1623 env.close();
1624
1625 // PaymentMint fails at granular semantic check because alice is not the issuer.
1626 env(pay(alice, gw, usd(50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1627
1628 // AccountDomainSet applies correctly to AccountSet
1629 std::string const domain = "example.com";
1630 auto jt = noop(alice);
1631 jt[sfDomain] = strHex(domain);
1632 jt[sfDelegate] = bob.human();
1633 env(jt);
1634 BEAST_EXPECT((*env.le(alice))[sfDomain] == makeSlice(domain));
1635
1636 // gw gives bob PaymentMint and bob can mint on gw's behalf
1637 env(delegate::set(gw, bob, {"PaymentMint"}));
1638 env.close();
1639 env(pay(gw, alice, usd(50)), delegate::As(bob));
1640 env.require(Balance(alice, usd(50)));
1641 }
1642 }
1643
1644 void
1646 {
1647 testcase("test TrustSet granular permissions");
1648 using namespace jtx;
1649
1650 // test TrustlineUnfreeze, TrustlineFreeze and TrustlineAuthorize
1651 {
1652 Env env(*this);
1653 Account const gw{"gw"};
1654 Account const alice{"alice"};
1655 Account const bob{"bob"};
1656 env.fund(XRP(10000), gw, alice, bob);
1657 env(fset(gw, asfRequireAuth));
1658 env.close();
1659
1660 env(delegate::set(alice, bob, {"TrustlineUnfreeze"}));
1661 env.close();
1662 // bob can not create trustline on behalf of alice because he only
1663 // has unfreeze permission
1664 env(trust(alice, gw["USD"](50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1665 env.close();
1666
1667 // alice creates trustline by herself
1668 env(trust(alice, gw["USD"](50)));
1669 env.close();
1670
1671 // gw gives bob unfreeze permission
1672 env(delegate::set(gw, bob, {"TrustlineUnfreeze"}));
1673 env.close();
1674
1675 // unsupported flags
1676 env(trust(alice, gw["USD"](50), tfSetNoRipple),
1677 delegate::As(bob),
1679 env(trust(alice, gw["USD"](50), tfClearNoRipple),
1680 delegate::As(bob),
1682 env(trust(gw, gw["USD"](0), alice, tfSetDeepFreeze),
1683 delegate::As(bob),
1685 env(trust(gw, gw["USD"](0), alice, tfClearDeepFreeze),
1686 delegate::As(bob),
1688 env.close();
1689
1690 // supported flags with wrong permission
1691 env(trust(gw, gw["USD"](0), alice, tfSetfAuth),
1692 delegate::As(bob),
1694 env(trust(gw, gw["USD"](0), alice, tfSetFreeze),
1695 delegate::As(bob),
1697 env.close();
1698
1699 env(delegate::set(gw, bob, {"TrustlineAuthorize"}));
1700 env.close();
1701 env(trust(gw, gw["USD"](0), alice, tfClearFreeze),
1702 delegate::As(bob),
1704 env.close();
1705 // although trustline authorize is granted, bob can not change the
1706 // limit number
1707 env(trust(gw, gw["USD"](50), alice, tfSetfAuth),
1708 delegate::As(bob),
1710 env.close();
1711
1712 // supported flags with correct permission
1713 env(trust(gw, gw["USD"](0), alice, tfSetfAuth), delegate::As(bob));
1714 env.close();
1715 env(delegate::set(gw, bob, {"TrustlineAuthorize", "TrustlineFreeze"}));
1716 env.close();
1717 env(trust(gw, gw["USD"](0), alice, tfSetFreeze), delegate::As(bob));
1718 env.close();
1719 env(delegate::set(gw, bob, {"TrustlineAuthorize", "TrustlineUnfreeze"}));
1720 env.close();
1721 env(trust(gw, gw["USD"](0), alice, tfClearFreeze), delegate::As(bob));
1722 env.close();
1723 // but bob can not freeze trustline because he no longer has freeze
1724 // permission
1725 env(trust(gw, gw["USD"](0), alice, tfSetFreeze),
1726 delegate::As(bob),
1728
1729 // cannot update LimitAmount with granular permission, both high and
1730 // low account
1731 env(trust(alice, gw["USD"](100)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1732 env(trust(gw, alice["USD"](100)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1733
1734 // can not set QualityIn or QualityOut
1735 auto tx = trust(alice, gw["USD"](50));
1736 tx["QualityIn"] = "1000";
1738 auto tx2 = trust(alice, gw["USD"](50));
1739 tx2["QualityOut"] = "1000";
1741 auto tx3 = trust(gw, alice["USD"](50));
1742 tx3["QualityIn"] = "1000";
1744 auto tx4 = trust(gw, alice["USD"](50));
1745 tx4["QualityOut"] = "1000";
1747
1748 // granting TrustSet can make it work
1749 env(delegate::set(gw, bob, {"TrustSet"}));
1750 env.close();
1751 auto tx5 = trust(gw, alice["USD"](50));
1752 tx5["QualityOut"] = "1000";
1753 env(tx5, delegate::As(bob));
1754 auto tx6 = trust(alice, gw["USD"](50));
1755 tx6["QualityOut"] = "1000";
1757 env(delegate::set(alice, bob, {"TrustSet"}));
1758 env.close();
1759 env(tx6, delegate::As(bob));
1760 }
1761
1762 // test mix of transaction level delegation and granular delegation
1763 {
1764 Env env(*this);
1765 Account const gw{"gw"};
1766 Account const alice{"alice"};
1767 Account const bob{"bob"};
1768 env.fund(XRP(10000), gw, alice, bob);
1769 env(fset(gw, asfRequireAuth));
1770 env.close();
1771
1772 // bob does not have permission
1773 env(trust(alice, gw["USD"](50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1774 env(delegate::set(alice, bob, {"TrustlineUnfreeze", "NFTokenCreateOffer"}));
1775 env.close();
1776 // bob still does not have permission
1777 env(trust(alice, gw["USD"](50)), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1778
1779 // add TrustSet permission and some unrelated permission
1780 env(delegate::set(
1781 alice,
1782 bob,
1783 {"TrustlineUnfreeze", "NFTokenCreateOffer", "TrustSet", "AccountTransferRateSet"}));
1784 env.close();
1785 env(trust(alice, gw["USD"](50)), delegate::As(bob));
1786 env.close();
1787
1788 env(delegate::set(
1789 gw,
1790 bob,
1791 {"TrustlineUnfreeze", "NFTokenCreateOffer", "TrustSet", "AccountTransferRateSet"}));
1792 env.close();
1793
1794 // since bob has TrustSet permission, he does not need
1795 // TrustlineFreeze granular permission to freeze the trustline
1796 env(trust(gw, gw["USD"](0), alice, tfSetFreeze), delegate::As(bob));
1797 env(trust(gw, gw["USD"](0), alice, tfClearFreeze), delegate::As(bob));
1798 // bob can perform all the operations regarding TrustSet
1799 env(trust(gw, gw["USD"](0), alice, tfSetFreeze), delegate::As(bob));
1800 env(trust(gw, gw["USD"](0), alice, tfSetDeepFreeze), delegate::As(bob));
1801 env(trust(gw, gw["USD"](0), alice, tfClearDeepFreeze), delegate::As(bob));
1802 env(trust(gw, gw["USD"](0), alice, tfSetfAuth), delegate::As(bob));
1803 env(trust(alice, gw["USD"](50), tfSetNoRipple), delegate::As(bob));
1804 env(trust(alice, gw["USD"](50), tfClearNoRipple), delegate::As(bob));
1805 }
1806
1807 // tfFullyCanonicalSig won't block delegated transaction
1808 {
1809 Env env(*this);
1810 Account const gw{"gw"};
1811 Account const alice{"alice"};
1812 Account const bob{"bob"};
1813 env.fund(XRP(10000), gw, alice, bob);
1814 env(fset(gw, asfRequireAuth));
1815 env.close();
1816 env(trust(alice, gw["USD"](50)));
1817 env.close();
1818
1819 env(delegate::set(gw, bob, {"TrustlineAuthorize"}));
1820 env.close();
1821 env(trust(gw, gw["USD"](0), alice, tfSetfAuth | tfFullyCanonicalSig),
1822 delegate::As(bob));
1823 }
1824
1825 {
1826 Env env(*this);
1827 Account const gw{"gw"};
1828 Account const alice{"alice"};
1829 Account const bob{"bob"};
1830 env.fund(XRP(10000), gw, alice, bob);
1831
1832 env(fset(gw, asfRequireAuth));
1833 env.close();
1834 env(trust(alice, gw["USD"](50)));
1835 env.close();
1836 env(delegate::set(gw, bob, {"TrustlineAuthorize"}));
1837 env.close();
1838
1839 env(trust(gw, gw["USD"](0), alice, tfSetfAuth), delegate::As(bob));
1840 env.close();
1841
1842 // sfQualityOut is a valid TrustSet field, but not permitted in granular template
1843 json::Value txJson = trust(gw, gw["USD"](0), alice, tfSetfAuth);
1844 txJson[sfQualityOut.jsonName] = 100;
1845 env(txJson, delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1846
1847 // tfSetNoRipple is a valid flag for TrustSet, but not permitted in granular template
1848 env(trust(gw, gw["USD"](0), alice, tfSetfAuth | tfSetNoRipple),
1849 delegate::As(bob),
1851 }
1852 }
1853
1854 void
1856 {
1857 testcase("test AccountSet granular permissions");
1858 using namespace jtx;
1859
1860 // test AccountDomainSet, AccountEmailHashSet,
1861 // AccountMessageKeySet,AccountTransferRateSet, and AccountTickSizeSet
1862 // granular permissions
1863 {
1864 Env env(*this);
1865 auto const alice = Account{"alice"};
1866 auto const bob = Account{"bob"};
1867 env.fund(XRP(10000), alice, bob);
1868 env.close();
1869
1870 // alice gives bob some random permission, which is not related to
1871 // the AccountSet transaction
1872 env(delegate::set(alice, bob, {"TrustlineUnfreeze"}));
1873 env.close();
1874
1875 // bob does not have permission to set domain
1876 // on behalf of alice
1877 std::string const domain = "example.com";
1878 auto jt = noop(alice);
1879 jt[sfDomain] = strHex(domain);
1880 jt[sfDelegate] = bob.human();
1881
1882 // add granular permission related to AccountSet but is not the
1883 // correct permission for domain set
1884 env(delegate::set(alice, bob, {"TrustlineUnfreeze", "AccountEmailHashSet"}));
1885 env.close();
1887
1888 // alice give granular permission of AccountDomainSet to bob
1889 env(delegate::set(alice, bob, {"AccountDomainSet"}));
1890 env.close();
1891
1892 // bob set account domain on behalf of alice
1893 env(jt);
1894 BEAST_EXPECT((*env.le(alice))[sfDomain] == makeSlice(domain));
1895
1896 // bob can reset domain
1897 jt[sfDomain] = "";
1898 env(jt);
1899 BEAST_EXPECT(!env.le(alice)->isFieldPresent(sfDomain));
1900
1901 // bob tries to set unauthorized flag, it will fail
1902 std::string const failDomain = "fail_domain_update";
1903 jt[sfFlags] = tfRequireAuth;
1904 jt[sfDomain] = strHex(failDomain);
1906 // reset flag number
1907 jt[sfFlags] = 0;
1908
1909 // bob tries to update domain and set email hash,
1910 // but he does not have permission to set email hash
1911 jt[sfDomain] = strHex(domain);
1912 std::string const mh("5F31A79367DC3137FADA860C05742EE6");
1913 jt[sfEmailHash] = mh;
1915
1916 // alice give granular permission of AccountEmailHashSet to bob
1917 env(delegate::set(alice, bob, {"AccountDomainSet", "AccountEmailHashSet"}));
1918 env.close();
1919 env(jt);
1920 BEAST_EXPECT(to_string((*env.le(alice))[sfEmailHash]) == mh);
1921 BEAST_EXPECT((*env.le(alice))[sfDomain] == makeSlice(domain));
1922
1923 // bob does not have permission to set message key for alice
1924 auto const rkp = randomKeyPair(KeyType::Ed25519);
1925 jt[sfMessageKey] = strHex(rkp.first.slice());
1927
1928 // alice give granular permission of AccountMessageKeySet to bob
1929 env(delegate::set(
1930 alice, bob, {"AccountDomainSet", "AccountEmailHashSet", "AccountMessageKeySet"}));
1931 env.close();
1932
1933 // bob can set message key for alice
1934 env(jt);
1935 BEAST_EXPECT(strHex((*env.le(alice))[sfMessageKey]) == strHex(rkp.first.slice()));
1936 jt[sfMessageKey] = "";
1937 env(jt);
1938 BEAST_EXPECT(!env.le(alice)->isFieldPresent(sfMessageKey));
1939
1940 // bob does not have permission to set transfer rate for alice
1941 env(rate(alice, 2.0), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1942
1943 // alice give granular permission of AccountTransferRateSet to bob
1944 env(delegate::set(
1945 alice,
1946 bob,
1947 {
1948 "AccountDomainSet",
1949 "AccountEmailHashSet",
1950 "AccountMessageKeySet",
1951 "AccountTransferRateSet",
1952 }));
1953 env.close();
1954 auto jtRate = rate(alice, 2.0);
1955 jtRate[sfDelegate] = bob.human();
1956 env(jtRate, delegate::As(bob));
1957 BEAST_EXPECT((*env.le(alice))[sfTransferRate] == 2000000000);
1958
1959 // bob does not have permission to set ticksize for alice
1960 jt[sfTickSize] = 8;
1962
1963 // alice give granular permission of AccountTickSizeSet to bob
1964 env(delegate::set(
1965 alice,
1966 bob,
1967 {
1968 "AccountDomainSet",
1969 "AccountEmailHashSet",
1970 "AccountMessageKeySet",
1971 "AccountTransferRateSet",
1972 "AccountTickSizeSet",
1973 }));
1974 env.close();
1975 env(jt);
1976 BEAST_EXPECT((*env.le(alice))[sfTickSize] == 8);
1977
1978 // can not set asfRequireAuth flag for alice
1979 env(fset(alice, asfRequireAuth), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1980
1981 // reset Delegate will delete the Delegate
1982 // object
1983 env(delegate::set(alice, bob, {}));
1984 // bib still does not have permission to set asfRequireAuth for
1985 // alice
1986 env(fset(alice, asfRequireAuth), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
1987 // alice can set for herself
1988 env(fset(alice, asfRequireAuth));
1989 env.require(Flags(alice, asfRequireAuth));
1990 env.close();
1991
1992 // can not update tick size because bob no longer has permission
1993 jt[sfTickSize] = 7;
1995
1996 env(delegate::set(
1997 alice, bob, {"AccountDomainSet", "AccountEmailHashSet", "AccountMessageKeySet"}));
1998 env.close();
1999
2000 // bob does not have permission to set wallet locater for alice
2001 std::string const locator =
2002 "9633EC8AF54F16B5286DB1D7B519EF49EEFC050C0C8AC4384F1D88ACD1BFDF"
2003 "05";
2004 auto jv2 = noop(alice);
2005 jv2[sfDomain] = strHex(domain);
2006 jv2[sfDelegate] = bob.human();
2007 jv2[sfWalletLocator] = locator;
2008 env(jv2, Ter(terNO_DELEGATE_PERMISSION));
2009 }
2010
2011 // can not set AccountSet flags on behalf of other account,
2012 // in permissions.macro, the template for AccountSet does
2013 // not allow any flag set or clear.
2014 {
2015 Env env(*this);
2016 auto const alice = Account{"alice"};
2017 auto const bob = Account{"bob"};
2018 env.fund(XRP(10000), alice, bob);
2019 env.close();
2020
2021 auto const testSetClearFlag = [&](std::uint32_t flag) {
2022 // bob can not set flag on behalf of alice
2023 env(fset(alice, flag), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
2024 // alice set by herself
2025 env(fset(alice, flag));
2026 env.close();
2027 env.require(Flags(alice, flag));
2028 // bob can not clear on behalf of alice
2029 env(fclear(alice, flag), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
2030 };
2031
2032 // testSetClearFlag(asfNoFreeze);
2033 testSetClearFlag(asfRequireAuth);
2034 testSetClearFlag(asfAllowTrustLineClawback);
2035
2036 // alice gives some granular permissions to bob
2037 env(delegate::set(
2038 alice, bob, {"AccountDomainSet", "AccountEmailHashSet", "AccountMessageKeySet"}));
2039 env.close();
2040
2041 testSetClearFlag(asfDefaultRipple);
2042 testSetClearFlag(asfDepositAuth);
2043 testSetClearFlag(asfDisallowIncomingCheck);
2044 testSetClearFlag(asfDisallowIncomingNFTokenOffer);
2045 testSetClearFlag(asfDisallowIncomingPayChan);
2046 testSetClearFlag(asfDisallowIncomingTrustline);
2047 testSetClearFlag(asfDisallowXRP);
2048 testSetClearFlag(asfRequireDest);
2049 testSetClearFlag(asfGlobalFreeze);
2050
2051 // bob can not set asfAccountTxnID on behalf of alice
2052 env(fset(alice, asfAccountTxnID), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
2053 env(fset(alice, asfAccountTxnID));
2054 env.close();
2055 BEAST_EXPECT(env.le(alice)->isFieldPresent(sfAccountTxnID));
2056 env(fclear(alice, asfAccountTxnID), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
2057
2058 // bob can not set asfAuthorizedNFTokenMinter on behalf of alice
2059 json::Value jt = fset(alice, asfAuthorizedNFTokenMinter);
2060 jt[sfDelegate] = bob.human();
2061 jt[sfNFTokenMinter] = bob.human();
2063
2064 // bob gives alice some permissions
2065 env(delegate::set(
2066 bob, alice, {"AccountDomainSet", "AccountEmailHashSet", "AccountMessageKeySet"}));
2067 env.close();
2068
2069 // since we can not set asfNoFreeze if asfAllowTrustLineClawback is
2070 // set, which can not be clear either. Test alice set asfNoFreeze on
2071 // behalf of bob.
2072 env(fset(alice, asfNoFreeze), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
2073 env(fset(bob, asfNoFreeze));
2074 env.close();
2075 env.require(Flags(bob, asfNoFreeze));
2076 // alice can not clear on behalf of bob
2077 env(fclear(alice, asfNoFreeze), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
2078
2079 // bob can not set asfDisableMaster on behalf of alice
2080 Account const bobKey{"bobKey", KeyType::Secp256k1};
2081 env(regkey(bob, bobKey));
2082 env.close();
2083 env(fset(alice, asfDisableMaster),
2084 delegate::As(bob),
2085 Sig(bob),
2087 }
2088
2089 // tfFullyCanonicalSig won't block delegated transaction
2090 {
2091 Env env(*this);
2092 Account const alice{"alice"};
2093 Account const bob{"bob"};
2094 env.fund(XRP(10000), alice, bob);
2095 env.close();
2096
2097 env(delegate::set(alice, bob, {"AccountDomainSet", "AccountEmailHashSet"}));
2098 env.close();
2099
2100 std::string const domain = "example.com";
2101 auto jt = noop(alice);
2102 jt[sfDomain] = strHex(domain);
2103 jt[sfDelegate] = bob.human();
2104 jt[sfFlags] = tfFullyCanonicalSig;
2105
2106 env(jt);
2107 BEAST_EXPECT((*env.le(alice))[sfDomain] == makeSlice(domain));
2108 }
2109
2110 // setting invalid field not in permissions.macro template will be rejected.
2111 {
2112 Env env(*this);
2113 auto const alice = Account{"alice"};
2114 auto const bob = Account{"bob"};
2115 env.fund(XRP(10000), alice, bob);
2116 env.close();
2117
2118 // Alice gives Bob permission to set her Domain
2119 env(delegate::set(alice, bob, {"AccountDomainSet"}));
2120 env.close();
2121
2122 std::string const domain = "example.com";
2123 auto txJson = noop(alice);
2124 txJson[sfDomain] = strHex(domain);
2125 txJson[sfDelegate] = bob.human();
2126
2127 // sfNFTokenMinter is a valid field in AccountSet tx, but
2128 // it is not permitted for granular template
2129 txJson[sfNFTokenMinter] = bob.human();
2130
2131 env(txJson, Ter(terNO_DELEGATE_PERMISSION));
2132 }
2133
2134 // Delegated AccountSet with no fields and no flags is allowed,
2135 // because it is allowed in the non-delegated case as well.
2136 {
2137 Env env(*this);
2138 Account const alice{"alice"};
2139 Account const bob{"bob"};
2140 env.fund(XRP(10000), alice, bob);
2141 env.close();
2142
2143 env(delegate::set(alice, bob, {"AccountDomainSet"}));
2144 env.close();
2145
2146 auto jt = noop(alice);
2147 jt[sfDelegate] = bob.human();
2148 env(jt);
2149 }
2150
2151 // Revoking all permissions deletes the SLE and subsequent attempts are rejected.
2152 {
2153 Env env(*this);
2154 Account const alice{"alice"};
2155 Account const bob{"bob"};
2156 env.fund(XRP(10000), alice, bob);
2157 env.close();
2158
2159 env(delegate::set(alice, bob, {"AccountDomainSet"}));
2160 env.close();
2161
2162 std::string const domain = "example.com";
2163 auto jt = noop(alice);
2164 jt[sfDomain] = strHex(domain);
2165 jt[sfDelegate] = bob.human();
2166 env(jt);
2167
2168 // empty DelegateSet deletes the SLE
2169 env(delegate::set(alice, bob, {}));
2170 env.close();
2171
2173 }
2174 }
2175
2176 void
2178 {
2179 testcase("test MPTokenIssuanceSet granular");
2180 using namespace jtx;
2181
2182 // test MPTokenIssuanceUnlock and MPTokenIssuanceLock permissions
2183 {
2184 Env env(*this);
2185 Account const alice{"alice"};
2186 Account const bob{"bob"};
2187 env.fund(XRP(100000), alice, bob);
2188 env.close();
2189
2190 MPTTester mpt(env, alice, {.fund = false});
2191 env.close();
2192 mpt.create({.flags = tfMPTCanLock});
2193 env.close();
2194
2195 // delegate ledger object is not created yet
2196 mpt.set({
2197 .account = alice,
2198 .flags = tfMPTLock,
2199 .delegate = bob,
2201 });
2202
2203 // alice gives granular permission to bob of MPTokenIssuanceUnlock
2204 env(delegate::set(alice, bob, {"MPTokenIssuanceUnlock"}));
2205 env.close();
2206 // bob does not have lock permission
2207 mpt.set({
2208 .account = alice,
2209 .flags = tfMPTLock,
2210 .delegate = bob,
2212 });
2213 // bob now has lock permission, but does not have unlock permission
2214 env(delegate::set(alice, bob, {"MPTokenIssuanceLock"}));
2215 env.close();
2216 mpt.set({.account = alice, .flags = tfMPTLock, .delegate = bob});
2217 mpt.set({
2218 .account = alice,
2219 .flags = tfMPTUnlock,
2220 .delegate = bob,
2222 });
2223
2224 // now bob can lock and unlock
2225 env(delegate::set(alice, bob, {"MPTokenIssuanceLock", "MPTokenIssuanceUnlock"}));
2226 env.close();
2227 mpt.set({.account = alice, .flags = tfMPTUnlock, .delegate = bob});
2228 mpt.set({.account = alice, .flags = tfMPTLock, .delegate = bob});
2229 env.close();
2230 }
2231
2232 // test mix of granular and transaction level permission
2233 {
2234 Env env(*this);
2235 Account const alice{"alice"};
2236 Account const bob{"bob"};
2237 env.fund(XRP(100000), alice, bob);
2238 env.close();
2239
2240 MPTTester mpt(env, alice, {.fund = false});
2241 env.close();
2242 mpt.create({.flags = tfMPTCanLock});
2243 env.close();
2244
2245 // alice gives granular permission to bob of MPTokenIssuanceLock
2246 env(delegate::set(alice, bob, {"MPTokenIssuanceLock"}));
2247 env.close();
2248 mpt.set({.account = alice, .flags = tfMPTLock, .delegate = bob});
2249 // bob does not have unlock permission
2250 mpt.set({
2251 .account = alice,
2252 .flags = tfMPTUnlock,
2253 .delegate = bob,
2255 });
2256
2257 // alice gives bob some unrelated permission with
2258 // MPTokenIssuanceLock
2259 env(delegate::set(alice, bob, {"NFTokenMint", "MPTokenIssuanceLock", "NFTokenBurn"}));
2260 env.close();
2261 // bob can not unlock
2262 mpt.set({
2263 .account = alice,
2264 .flags = tfMPTUnlock,
2265 .delegate = bob,
2267 });
2268
2269 // alice add MPTokenIssuanceSet to permissions
2270 env(delegate::set(
2271 alice,
2272 bob,
2273 {"NFTokenMint", "MPTokenIssuanceLock", "NFTokenBurn", "MPTokenIssuanceSet"}));
2274 mpt.set({.account = alice, .flags = tfMPTUnlock, .delegate = bob});
2275 // alice can lock by herself
2276 mpt.set({.account = alice, .flags = tfMPTLock});
2277 mpt.set({.account = alice, .flags = tfMPTUnlock, .delegate = bob});
2278 mpt.set({.account = alice, .flags = tfMPTLock, .delegate = bob});
2279 }
2280
2281 // tfFullyCanonicalSig won't block delegated transaction
2282 {
2283 Env env(*this);
2284 Account const alice{"alice"};
2285 Account const bob{"bob"};
2286 env.fund(XRP(100000), alice, bob);
2287 env.close();
2288
2289 MPTTester mpt(env, alice, {.fund = false});
2290 env.close();
2291 mpt.create({.flags = tfMPTCanLock});
2292 env.close();
2293
2294 // alice gives granular permission to bob of MPTokenIssuanceLock
2295 env(delegate::set(alice, bob, {"MPTokenIssuanceLock"}));
2296 env.close();
2297 mpt.set({.account = alice, .flags = tfMPTLock | tfFullyCanonicalSig, .delegate = bob});
2298 }
2299
2300 // field not permitted to exist in granular delegation
2301 {
2302 Env env(*this);
2303 Account const alice{"alice"};
2304 Account const bob{"bob"};
2305 env.fund(XRP(100000), alice, bob);
2306
2307 MPTTester mpt(env, alice, {.fund = false});
2308 mpt.create({.flags = tfMPTCanLock});
2309 env.close();
2310
2311 // alice gives granular permission to bob for MPTokenIssuanceLock
2312 env(delegate::set(alice, bob, {"MPTokenIssuanceLock"}));
2313 env.close();
2314
2315 // tfMPTSetCanLock is a valid MPTokenIssuanceSet flag but is not
2316 // covered by the MPTokenIssuanceLock granular permission, so a
2317 // delegate holding only that permission cannot set it.
2318 mpt.set({
2319 .account = alice,
2320 .flags = tfMPTSetCanLock,
2321 .delegate = bob,
2323 });
2324
2325 // Notice: flags not defined in permissions.macro are not permitted for delegation.
2326 // Since preflight will check invalid flag for the tx, it is not reachable.
2327 // If any new flag is defined into the transaction in the future,
2328 // but is not allowed for delegation, the transaction will be rejected with
2329 // terNO_DELEGATE_PERMISSION. The set of permitted flags for delegation is defined in
2330 // permissions.macro.
2331 }
2332
2333 // Fields and flags not in permissions.macro template are not permitted for delegation.
2334 {
2335 Env env(*this);
2336 Account const alice{"alice"};
2337 Account const bob{"bob"};
2338 Account const credIssuer{"credIssuer"};
2339 env.fund(XRP(100000), alice, bob, credIssuer);
2340 env.close();
2341
2342 env(pdomain::setTx(credIssuer, {{.issuer = credIssuer, .credType = "credential"}}));
2343 auto const domainID = pdomain::getNewDomain(env.meta());
2344 env.close();
2345
2346 MPTTester mpt(env, alice, {.fund = false});
2347 mpt.create({.flags = tfMPTCanLock | tfMPTCanTransfer});
2348 env.close();
2349
2350 // bob holds every granular permission defined for MPTokenIssuanceSet
2351 env(delegate::set(alice, bob, {"MPTokenIssuanceLock", "MPTokenIssuanceUnlock"}));
2352 env.close();
2353
2354 // Any of those fields or flags not defined in permissions.macro are not permitted for
2355 // delegation.
2356 std::vector<MPTSet> const args = {
2357 {.transferFee = 100},
2358 {.metadata = "test"},
2359 {.domainID = domainID},
2360 {.flags = tfMPTSetCanTrade},
2361 {.flags = tfMPTSetCanClawback},
2362 {.immutableFlags = tifMPTTransferFee},
2363 // a permitted flag does not unlock the extra fields
2364 {.flags = tfMPTLock, .domainID = domainID},
2365 };
2366
2367 for (auto arg : args)
2368 {
2369 arg.account = alice;
2370 arg.delegate = bob;
2372 mpt.set(arg);
2373 env.close();
2374 }
2375 }
2376 }
2377
2378 void
2380 {
2381 testcase("test granular permission with sponsor common fields");
2382 using namespace jtx;
2383
2384 Account const alice{"alice"};
2385 Account const bob{"bob"};
2386 Account const gw{"gw"};
2387 Account const sponsor{"sponsor"};
2388 auto const usd = gw["USD"];
2389 auto const feeAmt = XRP(10);
2390
2391 // Sponsor fields are common fields, so they are permitted in every granular template.
2392 // Fee sponsorship is allowed for a granular delegated transaction.
2393 Env env(*this);
2394 env.fund(XRP(10000), alice, bob, gw, sponsor);
2395 env.trust(usd(200), alice);
2396 env.close();
2397
2398 env(delegate::set(gw, bob, {"PaymentMint"}));
2399 env.close();
2400
2401 // Co-signed: the sponsor account pays the fee.
2402 auto const gwBalance = env.balance(gw);
2403 auto const bobBalance = env.balance(bob);
2404 auto const sponsorBalance = env.balance(sponsor);
2405 env(pay(gw, alice, usd(50)),
2406 delegate::As(bob),
2407 Fee(feeAmt),
2409 Sig(sfSponsorSignature, sponsor));
2410 env.close();
2411 env.require(Balance(alice, usd(50)));
2412 BEAST_EXPECT(env.balance(gw) == gwBalance);
2413 BEAST_EXPECT(env.balance(bob) == bobBalance);
2414 BEAST_EXPECT(env.balance(sponsor) == sponsorBalance - feeAmt);
2415
2416 // Pre-funded: sponsorship(sponsor, bob) pays the fee, bob is sfDelegate.
2417 env(sponsor::set_fee(sponsor, 0, XRP(100)), sponsor::SponseeAcc(bob));
2418 env.close();
2419 auto const sponsorFee = sponsor::sponsorshipFeeBalance(env, sponsor, bob);
2420 env(pay(gw, alice, usd(50)),
2421 delegate::As(bob),
2422 Fee(feeAmt),
2424 env.close();
2425 env.require(Balance(alice, usd(100)));
2426 BEAST_EXPECT(env.balance(gw) == gwBalance);
2427 BEAST_EXPECT(env.balance(bob) == bobBalance);
2428 BEAST_EXPECT(sponsor::sponsorshipFeeBalance(env, sponsor, bob) == sponsorFee - feeAmt);
2429
2430 // Reserve sponsorship is not tested here: checkSponsor rejects it with temINVALID for any
2431 // delegated transaction before the permission check, regardless of the delegate's
2432 // permissions. It is covered by testDelegateBlockReserveSponsor in Sponsor_test.
2433 }
2434
2435 void
2437 {
2438 testcase("test single sign");
2439 using namespace jtx;
2440
2441 Env env(*this);
2442 Account const alice{"alice"};
2443 Account const bob{"bob"};
2444 Account const carol{"carol"};
2445 env.fund(XRP(100000), alice, bob, carol);
2446 env.close();
2447
2448 env(delegate::set(alice, bob, {"Payment"}));
2449 env.close();
2450
2451 auto const aliceBalance = env.balance(alice);
2452 auto const bobBalance = env.balance(bob);
2453 auto const carolBalance = env.balance(carol);
2454
2455 env(pay(alice, carol, XRP(100)), Fee(XRP(10)), delegate::As(bob), Sig(bob));
2456 env.close();
2457 BEAST_EXPECT(env.balance(alice) == aliceBalance - XRP(100));
2458 BEAST_EXPECT(env.balance(bob) == bobBalance - XRP(10));
2459 BEAST_EXPECT(env.balance(carol) == carolBalance + XRP(100));
2460 }
2461
2462 void
2464 {
2465 testcase("test single sign with bad secret");
2466 using namespace jtx;
2467
2468 {
2469 Env env(*this);
2470 Account const alice{"alice"};
2471 Account const bob{"bob"};
2472 Account const carol{"carol"};
2473 env.fund(XRP(100000), alice, bob, carol);
2474 env.close();
2475
2476 env(delegate::set(alice, bob, {"Payment"}));
2477 env.close();
2478
2479 auto const aliceBalance = env.balance(alice);
2480 auto const bobBalance = env.balance(bob);
2481 auto const carolBalance = env.balance(carol);
2482
2483 env(pay(alice, carol, XRP(100)),
2484 Fee(XRP(10)),
2485 delegate::As(bob),
2486 Sig(alice),
2487 Ter(tefBAD_AUTH));
2488 env.close();
2489 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2490 BEAST_EXPECT(env.balance(bob) == bobBalance);
2491 BEAST_EXPECT(env.balance(carol) == carolBalance);
2492 }
2493
2494 {
2495 Env env(*this);
2496 Account const alice{"alice"};
2497 Account const bob{"bob"};
2498 Account const carol{"carol"};
2499 env.fund(XRP(100000), alice, bob, carol);
2500 env.close();
2501
2502 env(delegate::set(alice, bob, {"TrustSet"}));
2503 env.close();
2504
2505 auto const aliceBalance = env.balance(alice);
2506 auto const bobBalance = env.balance(bob);
2507 auto const carolBalance = env.balance(carol);
2508
2509 env(pay(alice, carol, XRP(100)),
2510 Fee(XRP(10)),
2511 delegate::As(bob),
2512 Sig(carol),
2514 env.close();
2515 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2516 BEAST_EXPECT(env.balance(bob) == bobBalance);
2517 BEAST_EXPECT(env.balance(carol) == carolBalance);
2518
2519 env(pay(alice, carol, XRP(100)),
2520 Fee(XRP(10)),
2521 delegate::As(bob),
2522 Sig(alice),
2524 env.close();
2525 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2526 BEAST_EXPECT(env.balance(bob) == bobBalance);
2527 BEAST_EXPECT(env.balance(carol) == carolBalance);
2528 }
2529
2530 {
2531 Env env(*this);
2532 Account const alice{"alice"};
2533 Account const bob{"bob"};
2534 Account const carol{"carol"};
2535 env.fund(XRP(100000), alice, bob, carol);
2536 env.close();
2537
2538 auto const aliceBalance = env.balance(alice);
2539 auto const bobBalance = env.balance(bob);
2540 auto const carolBalance = env.balance(carol);
2541
2542 env(pay(alice, carol, XRP(100)),
2543 Fee(XRP(10)),
2544 delegate::As(bob),
2545 Sig(alice),
2547 env.close();
2548 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2549 BEAST_EXPECT(env.balance(bob) == bobBalance);
2550 BEAST_EXPECT(env.balance(carol) == carolBalance);
2551
2552 env(pay(alice, carol, XRP(100)),
2553 Fee(XRP(10)),
2554 delegate::As(bob),
2555 Sig(carol),
2557 env.close();
2558 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2559 BEAST_EXPECT(env.balance(bob) == bobBalance);
2560 BEAST_EXPECT(env.balance(carol) == carolBalance);
2561 }
2562 }
2563
2564 void
2566 {
2567 testcase("test multi sign");
2568 using namespace jtx;
2569
2570 Env env(*this);
2571 Account const alice{"alice"};
2572 Account const bob{"bob"};
2573 Account const carol{"carol"};
2574 Account const daria{"daria"};
2575 Account const edward{"edward"};
2576 env.fund(XRP(100000), alice, bob, carol, daria, edward);
2577 env.close();
2578
2579 env(signers(bob, 2, {{daria, 1}, {edward, 1}}));
2580 env.close();
2581
2582 env(delegate::set(alice, bob, {"Payment"}));
2583 env.close();
2584
2585 auto const aliceBalance = env.balance(alice);
2586 auto const bobBalance = env.balance(bob);
2587 auto const carolBalance = env.balance(carol);
2588 auto const dariaBalance = env.balance(daria);
2589 auto const edwardBalance = env.balance(edward);
2590
2591 env(pay(alice, carol, XRP(100)), Fee(XRP(10)), delegate::As(bob), Msig(daria, edward));
2592 env.close();
2593 BEAST_EXPECT(env.balance(alice) == aliceBalance - XRP(100));
2594 BEAST_EXPECT(env.balance(bob) == bobBalance - XRP(10));
2595 BEAST_EXPECT(env.balance(carol) == carolBalance + XRP(100));
2596 BEAST_EXPECT(env.balance(daria) == dariaBalance);
2597 BEAST_EXPECT(env.balance(edward) == edwardBalance);
2598 }
2599
2600 void
2602 {
2603 testcase("test multi sign which does not meet quorum");
2604 using namespace jtx;
2605
2606 Env env(*this);
2607 Account const alice{"alice"};
2608 Account const bob{"bob"};
2609 Account const carol{"carol"};
2610 Account const daria{"daria"};
2611 Account const edward{"edward"};
2612 Account const fred{"fred"};
2613 env.fund(XRP(100000), alice, bob, carol, daria, edward, fred);
2614 env.close();
2615
2616 env(signers(bob, 3, {{daria, 1}, {edward, 1}, {fred, 1}}));
2617 env.close();
2618
2619 env(delegate::set(alice, bob, {"Payment"}));
2620 env.close();
2621
2622 auto const aliceBalance = env.balance(alice);
2623 auto const bobBalance = env.balance(bob);
2624 auto const carolBalance = env.balance(carol);
2625 auto const dariaBalance = env.balance(daria);
2626 auto const edwardBalance = env.balance(edward);
2627
2628 env(pay(alice, carol, XRP(100)),
2629 Fee(XRP(10)),
2630 delegate::As(bob),
2631 Msig(daria, edward),
2633 env.close();
2634 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2635 BEAST_EXPECT(env.balance(bob) == bobBalance);
2636 BEAST_EXPECT(env.balance(carol) == carolBalance);
2637 BEAST_EXPECT(env.balance(daria) == dariaBalance);
2638 BEAST_EXPECT(env.balance(edward) == edwardBalance);
2639 }
2640
2641 void
2643 {
2644 // checkMultiSign disallows the owner of the account to
2645 // be part of the multisigner list. When it is a delegated transaction,
2646 // the delegate account should not be part of the multisigner list.
2647 testcase("test delegator as multisigner in delegate's signer list");
2648 using namespace jtx;
2649
2650 Env env(*this);
2651 Account const alice{"alice"};
2652 Account const bob{"bob"};
2653 Account const carol{"carol"};
2654 Account const daria{"daria"};
2655 env.fund(XRP(100000), alice, bob, carol, daria);
2656 env.close();
2657
2658 env(delegate::set(alice, bob, {"Payment"}));
2659 env.close();
2660
2661 // bob's signer list includes the delegator alice and daria
2662 env(signers(bob, 2, {{alice, 1}, {daria, 1}}));
2663 env.close();
2664
2665 auto aliceBalance = env.balance(alice);
2666 auto bobBalance = env.balance(bob);
2667 auto carolBalance = env.balance(carol);
2668 auto const amt = 100;
2669
2670 // alice can sign as a multisigner for bob
2671 env(pay(alice, carol, XRP(100)), Fee(XRP(10)), delegate::As(bob), Msig(alice, daria));
2672 env.close();
2673
2674 BEAST_EXPECT(env.balance(alice) == aliceBalance - XRP(amt));
2675 BEAST_EXPECT(env.balance(bob) == bobBalance - XRP(10));
2676 BEAST_EXPECT(env.balance(carol) == carolBalance + XRP(amt));
2677
2678 // alice can not sign as a multisigner if she sent the transaction by herself.
2679 env(pay(alice, carol, XRP(100)), Fee(XRP(10)), Msig(alice, daria), Ter(telENV_RPC_FAILED));
2680 env.close();
2681
2682 // Get new balances
2683 aliceBalance = env.balance(alice);
2684 bobBalance = env.balance(bob);
2685 carolBalance = env.balance(carol);
2686
2687 // bob (the delegate) should not appear as a multisigner in his transaction sent on behalf
2688 // of alice. STTx::checkMultiSign catches this at the local-check stage, so the jtx
2689 // framework returns telENV_RPC_FAILED.
2690 env(pay(alice, carol, XRP(50)),
2691 Fee(XRP(10)),
2692 delegate::As(bob),
2693 Msig(alice, bob),
2695 env.close();
2696 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2697 BEAST_EXPECT(env.balance(bob) == bobBalance);
2698 BEAST_EXPECT(env.balance(carol) == carolBalance);
2699 }
2700
2701 void
2703 {
2704 // In sortAndValidateSigners, if it is a delegated transaction, the delegate account is
2705 // the forbidden account from appearing in its own Signers array.
2706 testcase("test sign_for with delegated transaction");
2707 using namespace jtx;
2708
2709 Env env(*this);
2710 Account const alice{"alice"};
2711 Account const bob{"bob"};
2712 Account const carol{"carol"};
2713 Account const daria{"daria"};
2714 env.fund(XRP(100000), alice, bob, carol, daria);
2715 env.close();
2716
2717 env(delegate::set(alice, bob, {"Payment"}));
2718 env.close();
2719
2720 // bob's signer list includes the delegator alice and daria
2721 env(signers(bob, 2, {{alice, 1}, {daria, 1}}));
2722 env.close();
2723
2724 auto const baseFee = env.current()->fees().base;
2725
2726 auto const sendAmt = 1'000'000;
2727 auto const makeDelegateTx = [&] -> json::Value {
2728 json::Value jv;
2729 jv[jss::tx_json][jss::Account] = alice.human();
2730 jv[jss::tx_json][sfDelegate.jsonName] = bob.human();
2731 jv[jss::tx_json][jss::TransactionType] = jss::Payment;
2732 jv[jss::tx_json][jss::Destination] = carol.human();
2733 jv[jss::tx_json][jss::Amount] = sendAmt;
2734 jv[jss::tx_json][jss::Fee] = std::to_string((10 * baseFee).drops());
2735 jv[jss::tx_json][jss::Sequence] = env.seq(alice);
2736 jv[jss::tx_json][jss::SigningPubKey] = "";
2737 return jv;
2738 };
2739
2740 // The delegator alice and daria both sign via sign_for, which is valid
2741 {
2742 auto const aliceBalance = env.balance(alice);
2743 auto const bobBalance = env.balance(bob);
2744 auto const dariaBalance = env.balance(daria);
2745 auto const carolBalance = env.balance(carol);
2746
2747 json::Value jv = makeDelegateTx();
2748 jv[jss::account] = alice.human();
2749 jv[jss::secret] = alice.name();
2750 auto jrr = env.rpc("json", "sign_for", to_string(jv))[jss::result];
2751 BEAST_EXPECT(jrr[jss::status] == "success");
2752
2753 json::Value jv2;
2754 jv2[jss::tx_json] = jrr[jss::tx_json];
2755 jv2[jss::account] = daria.human();
2756 jv2[jss::secret] = daria.name();
2757 jrr = env.rpc("json", "sign_for", to_string(jv2))[jss::result];
2758 BEAST_EXPECT(jrr[jss::status] == "success");
2759
2760 json::Value jvSubmit;
2761 jvSubmit[jss::tx_json] = jrr[jss::tx_json];
2762 jrr = env.rpc("json", "submit_multisigned", to_string(jvSubmit))[jss::result];
2763 BEAST_EXPECT(jrr[jss::status] == "success");
2764 env.close();
2765 BEAST_EXPECT(env.balance(alice) == aliceBalance - XRPAmount(sendAmt));
2766 BEAST_EXPECT(env.balance(bob) == bobBalance - (10 * baseFee));
2767 BEAST_EXPECT(env.balance(daria) == dariaBalance);
2768 BEAST_EXPECT(env.balance(carol) == carolBalance + XRPAmount(sendAmt));
2769 }
2770
2771 // The delegated account bob attempts sign_for, will be rejected.
2772 {
2773 json::Value jv = makeDelegateTx();
2774 jv[jss::account] = bob.human();
2775 jv[jss::secret] = bob.name();
2776 auto jrr = env.rpc("json", "sign_for", to_string(jv))[jss::result];
2777 BEAST_EXPECT(jrr[jss::status] == "error");
2778 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
2779 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
2780 BEAST_EXPECT(
2781 jrr[jss::error_message] ==
2782 "A Signer may not be the transaction's Account (" + bob.human() + ").");
2783 }
2784 }
2785
2786 void
2788 {
2789 testcase("test permission value");
2790 using namespace jtx;
2791
2792 Env env(*this, features);
2793
2794 Account const alice{"alice"};
2795 Account const bob{"bob"};
2796 env.fund(XRP(100000), alice, bob);
2797 env.close();
2798
2799 auto const buildRequest = [&](auto value) -> json::Value {
2800 json::Value jv;
2801 jv[jss::TransactionType] = jss::DelegateSet;
2802 jv[jss::Account] = alice.human();
2803 jv[sfAuthorize.jsonName] = bob.human();
2804
2805 json::Value permissionsJson(json::ValueType::Array);
2806 json::Value permissionValue;
2807 permissionValue[sfPermissionValue.jsonName] = value;
2808 json::Value permissionObj;
2809 permissionObj[sfPermission.jsonName] = permissionValue;
2810 permissionsJson.append(permissionObj);
2811 jv[sfPermissions.jsonName] = permissionsJson;
2812
2813 return jv;
2814 };
2815
2816 // invalid permission value.
2817 // neither granular permission nor transaction level permission
2818 for (auto const value : {0, 100000, 54321})
2819 {
2820 auto const jv = buildRequest(value);
2821 env(jv, Ter(temMALFORMED));
2822 }
2823 }
2824
2825 void
2827 {
2828 testcase("test delegate disabled tx");
2829 using namespace jtx;
2830
2831 // map of tx and required feature.
2832 // non-delegable tx are not included.
2833 // NFTokenMint, NFTokenBurn, NFTokenCreateOffer, NFTokenCancelOffer,
2834 // NFTokenAcceptOffer are not included, they are tested separately.
2836 {"AMMClawback", featureAMMClawback},
2837 {"DIDSet", featureDID},
2838 {"DIDDelete", featureDID},
2839 {"OracleSet", featurePriceOracle},
2840 {"OracleDelete", featurePriceOracle},
2841 {"LedgerStateFix", fixNFTokenPageLinks},
2842 {"MPTokenIssuanceCreate", featureMPTokensV1},
2843 {"MPTokenIssuanceDestroy", featureMPTokensV1},
2844 {"MPTokenIssuanceSet", featureMPTokensV1},
2845 {"MPTokenAuthorize", featureMPTokensV1},
2846 {"CredentialCreate", featureCredentials},
2847 {"CredentialAccept", featureCredentials},
2848 {"CredentialDelete", featureCredentials},
2849 {"NFTokenModify", featureDynamicNFT},
2850 {"PermissionedDomainSet", featurePermissionedDomains},
2851 {"PermissionedDomainDelete", featurePermissionedDomains},
2852 {"SponsorshipSet", featureSponsor},
2853 };
2854
2855 // Can not delegate tx if any required feature disabled.
2856 {
2857 auto const txAmendmentDisabled = [&](FeatureBitset features, std::string const& tx) {
2858 BEAST_EXPECT(txRequiredFeatures.contains(tx));
2859
2860 Env env(*this, features - txRequiredFeatures[tx]);
2861
2862 Account const alice{"alice"};
2863 Account const bob{"bob"};
2864 env.fund(XRP(100000), alice, bob);
2865 env.close();
2866
2867 env(delegate::set(alice, bob, {tx}), Ter(temMALFORMED));
2868 };
2869
2870 for (auto const& tx : txRequiredFeatures)
2871 txAmendmentDisabled(features, tx.first);
2872 }
2873
2874 // if all the required features in txRequiredFeatures are enabled, will
2875 // succeed
2876 {
2877 auto const txAmendmentEnabled = [&](std::string const& tx) {
2878 Env env(*this, features);
2879
2880 Account const alice{"alice"};
2881 Account const bob{"bob"};
2882 env.fund(XRP(100000), alice, bob);
2883 env.close();
2884
2885 env(delegate::set(alice, bob, {tx}));
2886 };
2887
2888 for (auto const& tx : txRequiredFeatures)
2889 txAmendmentEnabled(tx.first);
2890 }
2891
2892 // Granular permissions also require the amendment for their underlying
2893 // transaction type.
2894 {
2895 for (auto const permission : {"MPTokenIssuanceLock", "MPTokenIssuanceUnlock"})
2896 {
2897 Env env(*this, features - featureMPTokensV1);
2898
2899 Account const alice{"alice"};
2900 Account const bob{"bob"};
2901 env.fund(XRP(100000), alice, bob);
2902 env.close();
2903
2904 env(delegate::set(alice, bob, {permission}), Ter(temMALFORMED));
2905 }
2906 }
2907 }
2908
2909 void
2911 {
2912 testcase("Make sure GranularSandbox is checked after transaction-level permission");
2913
2914 using namespace jtx;
2915
2916 Env env(*this);
2917 Account const gw{"gw"};
2918 Account const alice{"alice"};
2919 Account const bob{"bob"};
2920 env.fund(XRP(10000), gw, alice, bob);
2921
2922 env(fset(gw, asfRequireAuth));
2923 env.close();
2924 env(trust(alice, gw["USD"](50)));
2925 env.close();
2926 env(delegate::set(gw, bob, {"TrustlineAuthorize"}));
2927 env.close();
2928
2929 env(trust(gw, gw["USD"](0), alice, tfSetfAuth), delegate::As(bob));
2930 env.close();
2931
2932 // sfQualityOut is a valid TrustSet field, but not permitted in granular template
2933 json::Value txJson = trust(gw, gw["USD"](0), alice, tfSetfAuth);
2934 txJson[sfQualityOut.jsonName] = 100;
2935 env(txJson, delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
2936
2937 // Now Alice grants Bob with transaction level permission
2938 env(delegate::set(gw, bob, {"TrustlineAuthorize", "TrustSet"}));
2939 env.close();
2940
2941 // NOTE: This case is to ensure that if a delegate possesses a
2942 // transaction-level permission (e.g., TrustSet), the granular sandbox must not incorrectly
2943 // block the transaction. The function checkGranularSandbox MUST be called after the
2944 // transaction-level permission check. This test case is to avoid future refactor mistakes,
2945 // modifying the order will fail here.
2946 env(txJson, delegate::As(bob));
2947 }
2948
2949 void
2951 {
2952 testcase("Delegable Transactions Completeness");
2953
2954 std::size_t delegableCount = 0;
2955
2956#pragma push_macro("UNWRAP")
2957#undef UNWRAP
2958#pragma push_macro("TRANSACTION")
2959#undef TRANSACTION
2960
2961#define UNWRAP(...) __VA_ARGS__
2962#define TRANSACTION(tag, value, name, settings, ...) \
2963 if ((xrpl::TxSettings UNWRAP settings).delegable == xrpl::Delegation::Delegable) \
2964 { \
2965 delegableCount++; \
2966 }
2967
2968#include <xrpl/protocol/detail/transactions.macro>
2969
2970#undef TRANSACTION
2971#pragma pop_macro("TRANSACTION")
2972#undef UNWRAP
2973#pragma pop_macro("UNWRAP")
2974
2975 // ====================================================================
2976 // IMPORTANT NOTICE:
2977 //
2978 // If this test fails, it indicates that the 'Delegation::delegable' status
2979 // in transactions.macro has been changed. Delegation allows accounts to act
2980 // on behalf of others, significantly increasing the security surface.
2981 //
2982 //
2983 // To ENSURE any added transaction is safe and compatible with delegation:
2984 //
2985 // 1. Verify that the transaction is intended to be delegable.
2986 // 2. Every standard test case for that transaction MUST be
2987 // duplicated and verified for a Delegated context.
2988 // 3. Ensure that Fee, Reserve, and Signing are correctly handled.
2989 //
2990 // DO NOT modify expectedDelegableCount unless all scenarios, including
2991 // edge cases, have been fully tested and verified.
2992 // ====================================================================
2993 std::size_t const expectedDelegableCount = 49;
2994
2995 BEAST_EXPECTS(
2996 delegableCount == expectedDelegableCount,
2997 "\n[SECURITY] New delegable transaction detected!"
2998 "\n Expected: " +
2999 std::to_string(expectedDelegableCount) +
3000 "\n Actual: " + std::to_string(delegableCount) +
3001 "\n Action: Verify security requirements to interact with Delegation feature");
3002 }
3003
3004 void
3006 {
3007 testcase("non-delegable tx with sfDelegate is rejected at preflight");
3008 using namespace jtx;
3009
3010 Env env(*this, features);
3011 Account const alice{"alice"};
3012 Account const bob{"bob"};
3013 env.fund(XRP(10000), alice, bob);
3014 env.close();
3015
3016 // Transactions that are notDelegable and have no granular permissions
3017 // will be rejected with temINVALID at preflight.
3018 // Note: pseudo-transactions (EnableAmendment, SetFee and UNLModify) are also
3019 // notDelegable but are excluded here — passesLocalChecks() blocks them
3020 // before preflight1 is ever reached.
3021 {
3022 // SetRegularKey, SignerListSet, AccountDelete, DelegateSet.
3023 env(regkey(alice, bob), delegate::As(bob), Ter(temINVALID));
3024 env(signers(alice, 1, {{bob, 1}}), delegate::As(bob), Ter(temINVALID));
3025 env(acctdelete(alice, bob), delegate::As(bob), Ter(temINVALID));
3026 env(delegate::set(alice, bob, {"Payment"}), delegate::As(bob), Ter(temINVALID));
3027
3028 // SAV transactions.
3029 {
3030 Vault const vault{env};
3031 auto [createTx, keylet] = vault.create({.owner = alice, .asset = xrpIssue()});
3032 env(createTx, delegate::As(bob), Ter(temINVALID));
3033
3034 env(vault.set({.owner = alice, .id = keylet.key}),
3035 delegate::As(bob),
3036 Ter(temINVALID));
3037 env(vault.del({.owner = alice, .id = keylet.key}),
3038 delegate::As(bob),
3039 Ter(temINVALID));
3040 env(vault.deposit({.depositor = alice, .id = keylet.key, .amount = XRP(1)}),
3041 delegate::As(bob),
3042 Ter(temINVALID));
3043 env(vault.withdraw({.depositor = alice, .id = keylet.key, .amount = XRP(1)}),
3044 delegate::As(bob),
3045 Ter(temINVALID));
3046 env(vault.clawback({.issuer = alice, .id = keylet.key, .holder = bob}),
3047 delegate::As(bob),
3048 Ter(temINVALID));
3049 }
3050
3051 // Batch transaction: the outer Batch itself is non-delegable.
3052 {
3053 auto const seq = env.seq(alice);
3054 auto const batchFee = batch::calcBatchFee(env, 0, 1);
3055 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
3056 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
3057 delegate::As(bob),
3058 Ter(temINVALID));
3059 }
3060
3061 // Lending protocol transactions
3062 {
3063 Vault const vault{env};
3064 auto [createTx, keylet] = vault.create({.owner = alice, .asset = xrpIssue()});
3065 env(createTx);
3066
3067 env(loan_broker::set(alice, keylet.key), delegate::As(bob), Ter(temINVALID));
3068 env(loan_broker::del(alice, keylet.key), delegate::As(bob), Ter(temINVALID));
3069 env(loan_broker::coverDeposit(alice, keylet.key, XRP(1)),
3070 delegate::As(bob),
3071 Ter(temINVALID));
3072 env(loan_broker::coverWithdraw(alice, keylet.key, XRP(1)),
3073 delegate::As(bob),
3074 Ter(temINVALID));
3076
3077 env(loan::set(alice, keylet.key, Number(100)), delegate::As(bob), Ter(temINVALID));
3078 env(loan::manage(alice, keylet.key, 0), delegate::As(bob), Ter(temINVALID));
3079 env(loan::del(alice, keylet.key), delegate::As(bob), Ter(temINVALID));
3080 env(loan::pay(alice, keylet.key, XRP(1)), delegate::As(bob), Ter(temINVALID));
3081 }
3082 }
3083
3084 // AccountSet is notDelegable at tx level but has granular permissions,
3085 // so sfDelegate passes preflight and is rejected at invokeCheckPermission with
3086 // terNO_DELEGATE_PERMISSION.
3087 {
3088 env(fset(alice, asfDefaultRipple), delegate::As(bob), Ter(terNO_DELEGATE_PERMISSION));
3089 }
3090 }
3091
3092 void
3094 {
3095 testcase("DelegateUtils nullptr check");
3096
3097 // checkTxPermission nullptr check
3098 STTx const tx{ttPAYMENT, [](STObject&) {}};
3099 BEAST_EXPECT(checkTxPermission(nullptr, tx) == terNO_DELEGATE_PERMISSION);
3100
3101 // getGranularPermission nullptr check
3102 auto const granularPermissions = getGranularPermission(nullptr, ttPAYMENT);
3103 BEAST_EXPECT(granularPermissions.empty());
3104 }
3105
3106 void
3108 {
3109 testcase("test Permission to Tx type");
3110
3111 // 0 is not a valid permission value
3112 BEAST_EXPECT(!Permission::permissionToTxType(0));
3113
3114 // 1 maps to Payment transaction
3115 BEAST_EXPECT(Permission::permissionToTxType(1) == ttPAYMENT);
3116
3117 // UINT16_MAX+1 is the maximum possible tx-level permission value
3118 constexpr uint32_t maxTxPermission = std::numeric_limits<uint16_t>::max() + 1u;
3119 BEAST_EXPECT(Permission::permissionToTxType(maxTxPermission).has_value());
3120
3121 // exceeding maximum value should return nullopt
3122 BEAST_EXPECT(!Permission::permissionToTxType(maxTxPermission + 1));
3123
3124 // All granular permission values should return nullopt since they do not map to a TxType.
3125 for (auto const gp : {
3126#pragma push_macro("GRANULAR_PERMISSION")
3127#undef GRANULAR_PERMISSION
3128#define GRANULAR_PERMISSION(type, txType, value, ...) GranularPermissionType::type,
3129#include <xrpl/protocol/detail/permissions.macro>
3130#undef GRANULAR_PERMISSION
3131#pragma pop_macro("GRANULAR_PERMISSION")
3132 })
3133 {
3134 BEAST_EXPECT(!Permission::permissionToTxType(static_cast<uint32_t>(gp)));
3135 }
3136 }
3137
3138 void
3172};
3174} // namespace xrpl::test
T any_of(T... args)
T begin(T... args)
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
std::string const & arg() const
Return the argument associated with the runner.
Definition suite.h:292
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
static std::optional< TxType > permissionToTxType(std::uint32_t value)
void testNonDelegableTxWithDelegate(FeatureBitset features)
void testFeatureDisabled(FeatureBitset features)
void testInvalidRequest(FeatureBitset features)
void run() override
Runs the suite.
void testGranularPermissionWithSponsorCommonFields()
void testPaymentGranular(FeatureBitset features)
void testPermissionValue(FeatureBitset features)
void testTxRequireFeatures(FeatureBitset features)
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
std::string const & name() const
Return the name.
Definition jtx/Account.h:75
AccountID id() const
Returns the Account ID.
A balance matches.
Definition balance.h:24
Sets the DeliverMin on a JTx.
Definition delivermin.h:16
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
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
Definition Env.cpp:127
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
PrettyAmount limit(Account const &account, Issue const &issue) const
Returns the IOU limit on an account.
Definition Env.cpp:254
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
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
Definition Env.cpp:538
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
Set the fee on a JTx.
Definition fee.h:20
Match set account flags.
Definition flags.h:119
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
void create(MPTCreate const &arg=MPTCreate{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:346
Set a multisignature on a JTx.
Definition multisign.h:53
Add a path.
Definition paths.h:47
Sets the SendMax on a JTx.
Definition sendmax.h:16
Set the regular signature on a JTx.
Definition sig.h:19
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
Adds an inner Batch transaction to a JTx and autofills it.
Definition batch.h:91
Sets the optional URI on a DIDSet.
Definition did.h:48
T contains(T... args)
T end(T... args)
T make_tuple(T... args)
T make_unique(T... args)
T max(T... args)
@ Array
array value (ordered list)
Definition json_value.h:32
Keylet computation functions.
Definition Indexes.h:39
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:415
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Definition Indexes.cpp:523
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
json::Value outer(jtx::Account const &account, uint32_t seq, STAmount const &fee, std::uint32_t flags)
Build an outer Batch transaction JSON object.
Definition batch.cpp:55
XRPAmount calcBatchFee(jtx::Env const &env, uint32_t const &numSigners, uint32_t const &txns=0)
Calculate the expected outer Batch transaction fee.
Definition batch.cpp:37
json::Value create(A const &account, A const &dest, STAmount const &sendMax)
Create a check.
json::Value entry(jtx::Env &env, jtx::Account const &account, jtx::Account const &authorize)
Definition delegate.cpp:41
json::Value set(jtx::Account const &account, jtx::Account const &authorize, std::vector< std::string > const &permissions)
Definition delegate.cpp:17
json::Value set(jtx::Account const &account)
Definition dids.cpp:16
json::Value set(AccountID const &account, UInt256 const &vaultId, uint32_t flags)
json::Value coverWithdraw(AccountID const &account, UInt256 const &brokerID, STAmount const &amount, uint32_t flags)
json::Value coverDeposit(AccountID const &account, UInt256 const &brokerID, STAmount const &amount, uint32_t flags)
json::Value del(AccountID const &account, UInt256 const &brokerID, uint32_t flags)
json::Value coverClawback(AccountID const &account, std::uint32_t flags)
json::Value manage(AccountID const &account, UInt256 const &loanID, std::uint32_t flags)
json::Value set(AccountID const &account, UInt256 const &loanBrokerID, Number principalRequested, std::uint32_t flags)
json::Value del(AccountID const &account, UInt256 const &loanID, std::uint32_t flags)
json::Value pay(AccountID const &account, UInt256 const &loanID, STAmount const &amount, std::uint32_t flags)
UInt256 getNewDomain(std::shared_ptr< STObject const > const &meta)
json::Value setTx(AccountID const &account, Credentials const &credentials, std::optional< UInt256 > domain)
json::Value set_fee(jtx::Account const &account, std::uint32_t flags, STAmount feeAmountDelta, std::optional< STAmount > maxFee=std::nullopt)
Definition sponsor.h:26
STAmount sponsorshipFeeBalance(jtx::Env const &env, jtx::Account const &sponsor, jtx::Account const &sponsee)
Definition sponsor.cpp:93
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
json::Value regkey(Account const &account, DisabledT)
Disable the regular key.
Definition regkey.cpp:13
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
bool expectOffers(Env const &env, AccountID const &account, std::uint16_t size, std::vector< Amounts > const &toMatch)
XRPAmount txFee(Env const &env, std::uint16_t n)
json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
Definition flags.h:110
FeatureBitset testableAmendments()
Definition Env.h:92
json::Value acctdelete(Account const &account, Account const &dest)
Delete account.
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
std::vector< SLE::const_pointer > ownerDirEntries(ReadView const &view, AccountID const &account)
Return the ledger entries in an account's owner directory.
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.
json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
Definition rate.cpp:15
json::Value signers(Account const &account, std::uint32_t quorum, std::vector< Signer > const &v)
Definition multisign.cpp:31
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Definition flags.cpp:15
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
STTx createTx(bool disabling, LedgerIndex seq, PublicKey const &txKey)
Create ttUNL_MODIFY Tx.
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
static XRPAmount reserve(jtx::Env const &env, std::uint32_t count)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr FlagValue spfSponsorFee
Definition TxFlags.h:467
@ telENV_RPC_FAILED
Definition TER.h:54
@ terINSUF_FEE_B
Definition TER.h:218
@ terNO_DELEGATE_PERMISSION
Definition TER.h:232
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
@ RpcEntryNotFound
Definition ErrorCodes.h:144
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::unordered_set< GranularPermissionType > getGranularPermission(SLE::ConstRef delegate, TxType const &type)
Load the granular permissions granted to the delegate account for the specified transaction type.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
BaseUInt< 256 > Domain
Domain is a 256-bit hash representing a specific domain.
Definition UintTypes.h:59
@ tefBAD_QUORUM
Definition TER.h:174
@ tefBAD_AUTH
Definition TER.h:163
constexpr FlagValue tifMPTTransferFee
Definition TxFlags.h:383
NotTEC checkTxPermission(SLE::ConstRef delegate, STTx const &tx)
Check if the delegate account has permission to execute the transaction.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
BaseUInt< 256 > UInt256
Definition base_uint.h:581
@ temARRAY_TOO_LARGE
Definition TER.h:129
@ temBAD_FEE
Definition TER.h:80
@ temINVALID
Definition TER.h:98
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
@ temREDUNDANT
Definition TER.h:100
@ temBAD_SIGNER
Definition TER.h:103
@ tecPSEUDO_ACCOUNT
Definition TER.h:373
@ tecUNFUNDED_PAYMENT
Definition TER.h:296
@ tecNO_ENTRY
Definition TER.h:317
@ tecNO_TARGET
Definition TER.h:315
@ tecINSUFFICIENT_RESERVE
Definition TER.h:318
@ tesSUCCESS
Definition TER.h:253
constexpr FlagValue tfFullyCanonicalSig
Definition TxFlags.h:43
T to_string(T... args)