1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
use crate::{
    error::RuleSetError,
    payload::Payload,
    // TODO: Uncomment this after on-curve sycall available.
    // utils::is_on_curve,
    utils::{assert_derivation, compute_merkle_root, is_zeroed},
};
use serde::{Deserialize, Serialize};
#[cfg(feature = "serde-with-feature")]
use serde_with::{As, DisplayFromStr};
use solana_program::{
    account_info::AccountInfo, entrypoint::ProgramResult, msg, program_error::ProgramError,
    pubkey::Pubkey, system_program,
};
use std::collections::{HashMap, HashSet};

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
/// Operators that can be used to compare against an `Amount` rule.
pub enum CompareOp {
    /// Less Than
    Lt,
    /// Less Than or Equal To
    LtEq,
    /// Equal To
    Eq,
    /// Greater Than or Equal To
    GtEq,
    /// Greater Than
    Gt,
}

/// Enum representation of Rule failure conditions
pub enum RuleResult {
    /// The rule succeeded.
    Success(ProgramError),
    /// The rule failed.
    Failure(ProgramError),
    /// The program failed to execute the rule.
    Error(ProgramError),
}

use RuleResult::*;

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
/// The struct containing every type of Rule and its associated data.
pub enum Rule {
    /// Group AND, where every rule contained must pass.
    All {
        /// The vector of Rules contained under All.
        rules: Vec<Rule>,
    },
    /// Group OR, where at least one rule contained must pass.
    Any {
        /// The vector of Rules contained under Any.
        rules: Vec<Rule>,
    },
    /// Negation, where the contained rule must fail.
    Not {
        /// The Rule contained under Not.
        rule: Box<Rule>,
    },
    /// An additional signer must be present.  When the `Validate` instruction is called, this rule
    /// does not require any `Payload` values, but the additional signer account must be provided
    /// to `Validate` via the `additional_rule_accounts` argument so that whether it is a signer
    /// can be retrieved from its `AccountInfo` struct.
    AdditionalSigner {
        /// The public key that must have also signed the transaction.
        #[cfg_attr(feature = "serde-with-feature", serde(with = "As::<DisplayFromStr>"))]
        account: Pubkey,
    },
    /// Direct comparison between `Pubkey`s.  When the `Validate` instruction is called, this rule
    /// requires a `PayloadType` value of `PayloadType::Pubkey`.  The `field` value in the rule is
    /// used to locate the `Pubkey` in the payload to compare to the `Pubkey` in the rule.
    PubkeyMatch {
        /// The public key to be compared against.
        #[cfg_attr(feature = "serde-with-feature", serde(with = "As::<DisplayFromStr>"))]
        pubkey: Pubkey,
        /// The field in the `Payload` to be compared.
        field: String,
    },
    /// The comparing `Pubkey` must be in the list of `Pubkey`s.  When the `Validate` instruction
    /// is called, this rule requires a `PayloadType` value of `PayloadType::Pubkey`.  The `field`
    /// value in the Rule is used to locate the `Pubkey` in the payload to compare to the `Pubkey`
    /// list in the rule.
    PubkeyListMatch {
        /// The list of public keys to be compared against.
        pubkeys: Vec<Pubkey>,
        /// The field in the `Payload` to be compared.
        field: String,
    },
    /// The comparing `Pubkey` must be a member of the Merkle tree in the rule.  When the
    /// `Validate` instruction is called, this rule requires `PayloadType` values of
    /// `PayloadType::Pubkey` and `PayloadType::MerkleProof`.  The `field` values in the Rule are
    /// used to locate them in the `Payload`.  The `Pubkey` and the proof are used to calculate
    /// a Merkle root which is compared against the root stored in the rule.
    PubkeyTreeMatch {
        /// The root of the Merkle tree.
        root: [u8; 32],
        /// The field in the `Payload` to be compared
        /// when looking for the `Pubkey`.
        pubkey_field: String,
        /// The field in the `Payload` to be compared
        /// when looking for the Merkle proof.
        proof_field: String,
    },
    /// A resulting PDA derivation of seeds must prove the account is a PDA.  When the `Validate`
    /// instruction is called, this rule requires `PayloadType` values of `PayloadType::Seeds`.
    /// The `field` values in the Rule are used to locate them in the `Payload`.  The seeds in the
    /// `Payload` and the program ID stored in the Rule are used to derive the PDA from the
    /// `Payload`.
    PDAMatch {
        /// The program used for the PDA derivation.  If
        /// `None` then the account owner is used.
        program: Option<Pubkey>,
        /// The field in the `Payload` to be compared
        /// when looking for the PDA.
        pda_field: String,
        /// The field in the `Payload` to be compared
        /// when looking for the seeds.
        seeds_field: String,
    },
    /// The `Pubkey` must be owned by a given program.  When the `Validate` instruction is called,
    /// this rule requires a `PayloadType` value of `PayloadType::Pubkey`.  The `field` value in
    /// the rule is used to locate the `Pubkey` in the payload for which the owner must be the
    /// program in the rule.  Note this same `Pubkey` account must also be provided to `Validate`
    /// via the `additional_rule_accounts` argument.  This is so that the `Pubkey`'s owner can be
    /// found from its `AccountInfo` struct.
    ProgramOwned {
        /// The program that must own the `Pubkey`.
        #[cfg_attr(feature = "serde-with-feature", serde(with = "As::<DisplayFromStr>"))]
        program: Pubkey,
        /// The field in the `Payload` to be compared.
        field: String,
    },
    /// The `Pubkey` must be owned by a program in the list of `Pubkey`s.  When the `Validate`
    /// instruction is called, this rule requires a `PayloadType` value of `PayloadType::Pubkey`.
    /// The `field` value in the rule is used to locate the `Pubkey` in the payload for which the
    /// owner must be a program in the list in the rule.  Note this same `Pubkey` account must also
    /// be provided to `Validate` via the `additional_rule_accounts` argument.  This is so that the
    /// `Pubkey`'s owner can be found from its `AccountInfo` struct.
    ProgramOwnedList {
        /// The program that must own the `Pubkey`.
        programs: Vec<Pubkey>,
        /// The field in the `Payload` to be compared.
        field: String,
    },
    /// The `Pubkey` must be owned by a member of the Merkle tree in the rule.  When the `Validate`
    /// instruction is called, this rule requires `PayloadType` values of `PayloadType::Pubkey` and
    /// `PayloadType::MerkleProof`.  The `field` values in the Rule are used to locate them in the
    /// `Payload`.  Note this same `Pubkey` account must also be provided to `Validate` via the
    /// `additional_rule_accounts` argument.  This is so that the `Pubkey`'s owner can be found
    /// from its `AccountInfo` struct.  The owner and the proof are then used to calculate a Merkle
    /// root, which is compared against the root stored in the rule.
    ProgramOwnedTree {
        /// The root of the Merkle tree.
        root: [u8; 32],
        /// The field in the `Payload` to be compared
        /// when looking for the `Pubkey`.
        pubkey_field: String,
        /// The field in the `Payload` to be compared
        /// when looking for the Merkle proof.
        proof_field: String,
    },
    /// Comparison against the amount of tokens being transferred.   When the `Validate`
    /// instruction is called, this rule requires a `PayloadType` value of `PayloadType::Amount`.
    /// The `field` value in the Rule is used to locate the numerical amount in the payload to
    /// compare to the amount stored in the rule, using the comparison operator stored in the rule.
    Amount {
        /// The amount to be compared against.
        amount: u64,
        /// The operator to be used in the comparison.
        operator: CompareOp,
        /// The field the amount is stored in.
        field: String,
    },
    /// Comparison based on time between operations.  Currently not implemented.  This rule
    /// is planned check to ensure a certain amount of time has passed.  This rule will make use
    /// of the `rule_set_state_pda` optional account passed into `Validate`, and will require
    /// the optional `rule_authority` account to sign.
    Frequency {
        /// The authority of the frequency account.
        #[cfg_attr(feature = "serde-with-feature", serde(with = "As::<DisplayFromStr>"))]
        authority: Pubkey,
    },
    /// The true test if a pubkey can be signed from a client and therefore is a true wallet account.
    /// The details of this rule are as follows: a wallet is defined as being both owned by the
    /// System Program and the address is on-curve.  The `field` value in the rule is used to
    /// locate the `Pubkey` in the payload that must be on-curve and for which the owner must be
    /// the System Program.  Note this same `Pubkey` account must also be provided to `Validate`
    /// via the `additional_rule_accounts` argument.  This is so that the `Pubkey`'s owner can be
    /// found from its `AccountInfo` struct.
    IsWallet {
        /// The field in the `Payload` to be checked.
        field: String,
    },
    /// An operation that always succeeds.
    Pass,
    /// The `Pubkey` must be owned by a program in the set of `Pubkey`s.  When the `Validate`
    /// instruction is called, this rule requires a `PayloadType` value of `PayloadType::Pubkey`.
    /// The `field` value in the rule is used to locate the `Pubkey` in the payload for which the
    /// owner must be a program in the set in the rule.  Note this same `Pubkey` account must also
    /// be provided to `Validate` via the `additional_rule_accounts` argument.  This is so that the
    /// `Pubkey`'s owner can be found from its `AccountInfo` struct.
    ProgramOwnedSet {
        /// The program that must own the `Pubkey`.
        programs: HashSet<Pubkey>,
        /// The field in the `Payload` to be compared.
        field: String,
    },
    /// A rule that tells the operation finder to use the default namespace rule.
    Namespace,
}

impl Rule {
    /// The top level validation function which parses an entire rule tree.
    pub fn validate(
        &self,
        accounts: &HashMap<Pubkey, &AccountInfo>,
        payload: &Payload,
        update_rule_state: bool,
        rule_set_state_pda: &Option<&AccountInfo>,
        rule_authority: &Option<&AccountInfo>,
    ) -> ProgramResult {
        let result = self.low_level_validate(
            accounts,
            payload,
            update_rule_state,
            rule_set_state_pda,
            rule_authority,
        );

        match result {
            Success(_) => Ok(()),
            Failure(err) => Err(err),
            Error(err) => Err(err),
        }
    }

    /// Lower level validation function which iterates through a rule tree and applies boolean logic to rule results.
    pub fn low_level_validate(
        &self,
        accounts: &HashMap<Pubkey, &AccountInfo>,
        payload: &Payload,
        _update_rule_state: bool,
        _rule_set_state_pda: &Option<&AccountInfo>,
        rule_authority: &Option<&AccountInfo>,
    ) -> RuleResult {
        match self {
            Rule::All { rules } => {
                msg!("Validating All");
                let mut last: Option<ProgramError> = None;
                for rule in rules {
                    let result = rule.low_level_validate(
                        accounts,
                        payload,
                        _update_rule_state,
                        _rule_set_state_pda,
                        rule_authority,
                    );
                    // Return failure on the first failing rule.
                    match result {
                        Success(err) => last = Some(err),
                        _ => return result,
                    }
                }

                // Return pass if and only if all rules passed.
                Success(last.unwrap_or_else(|| RuleSetError::UnexpectedRuleSetFailure.into()))
            }
            Rule::Any { rules } => {
                msg!("Validating Any");
                let mut last_failure: Option<ProgramError> = None;
                let mut last_error: Option<ProgramError> = None;
                for rule in rules {
                    let result = rule.low_level_validate(
                        accounts,
                        payload,
                        _update_rule_state,
                        _rule_set_state_pda,
                        rule_authority,
                    );
                    match result {
                        Success(_) => return result,
                        Failure(err) => last_failure = Some(err),
                        Error(err) => last_error = Some(err),
                    }
                }

                // Return failure if and only if all rules failed.  Use the last failure.
                if let Some(err) = last_failure {
                    Failure(err)
                } else if let Some(err) = last_error {
                    // Return invalid if and only if all rules were invalid.  Use the last invalid.
                    Error(err)
                } else {
                    Error(RuleSetError::UnexpectedRuleSetFailure.into())
                }
            }
            Rule::Not { rule } => {
                let result = rule.low_level_validate(
                    accounts,
                    payload,
                    _update_rule_state,
                    _rule_set_state_pda,
                    rule_authority,
                );

                // Negate the result.
                match result {
                    Success(err) => Failure(err),
                    Failure(err) => Success(err),
                    Error(err) => Error(err),
                }
            }
            Rule::AdditionalSigner { account } => {
                msg!("Validating AdditionalSigner");
                if let Some(signer) = accounts.get(account) {
                    if signer.is_signer {
                        Success(self.to_error())
                    } else {
                        Failure(self.to_error())
                    }
                } else {
                    Error(RuleSetError::MissingAccount.into())
                }
            }
            Rule::PubkeyMatch { pubkey, field } => {
                msg!("Validating PubkeyMatch");

                let key = match payload.get_pubkey(field) {
                    Some(pubkey) => pubkey,
                    _ => return Error(RuleSetError::MissingPayloadValue.into()),
                };

                if key == pubkey {
                    Success(self.to_error())
                } else {
                    Failure(self.to_error())
                }
            }
            Rule::PubkeyListMatch { pubkeys, field } => {
                msg!("Validating PubkeyListMatch");

                let fields = field.split('|').collect::<Vec<&str>>();

                if fields.len() > 1 {
                    let new_rule = Rule::Any {
                        rules: fields
                            .iter()
                            .map(|field| Rule::ProgramOwnedList {
                                programs: pubkeys.clone(),
                                field: field.to_string(),
                            })
                            .collect(),
                    };

                    return new_rule.low_level_validate(
                        accounts,
                        payload,
                        _update_rule_state,
                        _rule_set_state_pda,
                        rule_authority,
                    );
                } else {
                    let key = match payload.get_pubkey(&field.to_owned()) {
                        Some(pubkey) => pubkey,
                        _ => return Error(RuleSetError::MissingPayloadValue.into()),
                    };

                    if pubkeys.iter().any(|pubkey| pubkey == key) {
                        return Success(self.to_error());
                    }
                }

                Failure(self.to_error())
            }
            Rule::PubkeyTreeMatch {
                root,
                pubkey_field,
                proof_field,
            } => {
                msg!("Validating PubkeyTreeMatch");

                // Get the `Pubkey` we are checking from the payload.
                let leaf = match payload.get_pubkey(pubkey_field) {
                    Some(pubkey) => pubkey,
                    _ => return Error(RuleSetError::MissingPayloadValue.into()),
                };

                // Get the Merkle proof from the payload.
                let merkle_proof = match payload.get_merkle_proof(proof_field) {
                    Some(merkle_proof) => merkle_proof,
                    _ => return Error(RuleSetError::MissingPayloadValue.into()),
                };

                // Check if the computed hash (root) is equal to the root in the rule.
                let computed_root = compute_merkle_root(leaf, merkle_proof);
                if computed_root == *root {
                    Success(self.to_error())
                } else {
                    Failure(self.to_error())
                }
            }
            Rule::PDAMatch {
                program,
                pda_field,
                seeds_field,
            } => {
                msg!("Validating PDAMatch");

                // Get the PDA from the payload.
                let account = match payload.get_pubkey(pda_field) {
                    Some(pubkey) => pubkey,
                    _ => return Error(RuleSetError::MissingPayloadValue.into()),
                };

                // Get the derivation seeds from the payload.
                let seeds = match payload.get_seeds(seeds_field) {
                    Some(seeds) => seeds,
                    _ => return Error(RuleSetError::MissingPayloadValue.into()),
                };

                // Get the program ID to use for the PDA derivation from the Rule.
                let program = match program {
                    // If the Pubkey is stored in the rule, use that value.
                    Some(program) => program,
                    None => {
                        // If one is not stored, then assume the program ID is the account owner.
                        match accounts.get(account) {
                            Some(account) => account.owner,
                            _ => return Error(RuleSetError::MissingAccount.into()),
                        }
                    }
                };

                // Convert the Vec of Vec into Vec of u8 slices.
                let vec_of_slices = seeds
                    .seeds
                    .iter()
                    .map(Vec::as_slice)
                    .collect::<Vec<&[u8]>>();

                if let Ok(_bump) = assert_derivation(program, account, &vec_of_slices) {
                    Success(self.to_error())
                } else {
                    Failure(self.to_error())
                }
            }
            Rule::ProgramOwned { program, field } => {
                msg!("Validating ProgramOwned");

                let key = match payload.get_pubkey(field) {
                    Some(pubkey) => pubkey,
                    _ => return Error(RuleSetError::MissingPayloadValue.into()),
                };

                if let Some(account) = accounts.get(key) {
                    let data = match account.data.try_borrow() {
                        Ok(data) => data,
                        Err(_) => return Error(ProgramError::AccountBorrowFailed),
                    };

                    if is_zeroed(&data) {
                        // Print helpful errors.
                        if data.len() == 0 {
                            msg!("Account data is empty");
                        } else {
                            msg!("Account data is zeroed");
                        }

                        // Account must have nonzero data to count as program-owned.
                        return Error(self.to_error());
                    } else if *account.owner == *program {
                        return Success(self.to_error());
                    }
                } else {
                    return Error(RuleSetError::MissingAccount.into());
                }

                Failure(self.to_error())
            }
            Rule::ProgramOwnedList { programs, field } => {
                msg!("Validating ProgramOwnedList");

                let fields = field.split('|').collect::<Vec<&str>>();

                if fields.len() > 1 {
                    let new_rule = Rule::Any {
                        rules: fields
                            .iter()
                            .map(|field| Rule::ProgramOwnedList {
                                programs: programs.clone(),
                                field: field.to_string(),
                            })
                            .collect(),
                    };

                    return new_rule.low_level_validate(
                        accounts,
                        payload,
                        _update_rule_state,
                        _rule_set_state_pda,
                        rule_authority,
                    );
                } else {
                    let key = match payload.get_pubkey(&field.to_string()) {
                        Some(pubkey) => pubkey,
                        _ => return Error(RuleSetError::MissingPayloadValue.into()),
                    };

                    let account = match accounts.get(key) {
                        Some(account) => account,
                        _ => return Error(RuleSetError::MissingAccount.into()),
                    };

                    let data = match account.data.try_borrow() {
                        Ok(data) => data,
                        Err(_) => return Error(ProgramError::AccountBorrowFailed),
                    };

                    if is_zeroed(&data) {
                        // Print helpful errors.
                        if data.len() == 0 {
                            msg!("Account data is empty");
                        } else {
                            msg!("Account data is zeroed");
                        }

                        return Error(RuleSetError::DataIsEmpty.into());
                    } else if programs.contains(account.owner) {
                        // Account owner must be in the set.
                        return Success(self.to_error());
                    }
                }

                Failure(self.to_error())
            }
            Rule::ProgramOwnedTree {
                root,
                pubkey_field,
                proof_field,
            } => {
                msg!("Validating ProgramOwnedTree");

                // Get the `Pubkey` we are checking from the payload.
                let key = match payload.get_pubkey(pubkey_field) {
                    Some(pubkey) => pubkey,
                    _ => return Error(RuleSetError::MissingPayloadValue.into()),
                };

                // Get the `AccountInfo` struct for the `Pubkey`.
                let account = match accounts.get(key) {
                    Some(account) => account,
                    _ => return Error(RuleSetError::MissingAccount.into()),
                };

                let data = match account.data.try_borrow() {
                    Ok(data) => data,
                    Err(_) => return Error(ProgramError::AccountBorrowFailed),
                };

                // Account must have nonzero data to count as program-owned.
                if is_zeroed(&data) {
                    // Print helpful errors.
                    if data.len() == 0 {
                        msg!("Account data is empty");
                    } else {
                        msg!("Account data is zeroed");
                    }

                    return Error(RuleSetError::DataIsEmpty.into());
                }

                // The account owner is the leaf.
                let leaf = account.owner;

                // Get the Merkle proof from the payload.
                let merkle_proof = match payload.get_merkle_proof(proof_field) {
                    Some(merkle_proof) => merkle_proof,
                    _ => return Error(RuleSetError::MissingPayloadValue.into()),
                };

                // Check if the computed hash (root) is equal to the root in the rule.
                let computed_root = compute_merkle_root(leaf, merkle_proof);
                if computed_root == *root {
                    Success(self.to_error())
                } else {
                    Failure(self.to_error())
                }
            }
            Rule::Amount {
                amount: rule_amount,
                operator,
                field,
            } => {
                msg!("Validating Amount");
                if let Some(payload_amount) = &payload.get_amount(field) {
                    let operator_fn = match operator {
                        CompareOp::Lt => PartialOrd::lt,
                        CompareOp::LtEq => PartialOrd::le,
                        CompareOp::Eq => PartialEq::eq,
                        CompareOp::Gt => PartialOrd::gt,
                        CompareOp::GtEq => PartialOrd::ge,
                    };

                    if operator_fn(payload_amount, rule_amount) {
                        Success(self.to_error())
                    } else {
                        Failure(self.to_error())
                    }
                } else {
                    Error(RuleSetError::MissingPayloadValue.into())
                }
            }
            Rule::Frequency { authority } => {
                msg!("Validating Frequency");

                if let Some(rule_authority) = rule_authority {
                    // TODO: If it's the wrong account (first condition) the `IsNotASigner`
                    // is misleading.  Should be improved, perhaps with a `Mismatch` error.
                    if authority != rule_authority.key || !rule_authority.is_signer {
                        return Error(RuleSetError::RuleAuthorityIsNotSigner.into());
                    }
                } else {
                    return Error(RuleSetError::MissingAccount.into());
                }

                Error(RuleSetError::NotImplemented.into())
            }
            Rule::Pass => {
                msg!("Validating Pass");
                Success(self.to_error())
            }
            Rule::IsWallet { field } => {
                msg!("Validating IsWallet");

                // Get the `Pubkey` we are checking from the payload.
                let key = match payload.get_pubkey(field) {
                    Some(pubkey) => pubkey,
                    _ => return Error(RuleSetError::MissingPayloadValue.into()),
                };

                // Get the `AccountInfo` struct for the `Pubkey` and verify that
                // its owner is the System Program.
                if let Some(account) = accounts.get(key) {
                    if *account.owner != system_program::ID {
                        // TODO: Change error return to commented line after on-curve syscall
                        // available.
                        return Error(RuleSetError::NotImplemented.into());
                        //return (false, self.to_error());
                    }
                } else {
                    return Error(RuleSetError::MissingAccount.into());
                }

                // TODO: Uncomment call to `is_on_curve()` after on-curve sycall available.
                Error(RuleSetError::NotImplemented.into())
                //(is_on_curve(key), self.to_error())
            }
            Rule::ProgramOwnedSet { programs, field } => {
                msg!("Validating ProgramOwnedSet");

                let fields = field.split('|').collect::<Vec<&str>>();

                if fields.len() > 1 {
                    let new_rule = Rule::Any {
                        rules: fields
                            .iter()
                            .map(|field| Rule::ProgramOwnedSet {
                                programs: programs.clone(),
                                field: field.to_string(),
                            })
                            .collect(),
                    };

                    return new_rule.low_level_validate(
                        accounts,
                        payload,
                        _update_rule_state,
                        _rule_set_state_pda,
                        rule_authority,
                    );
                } else {
                    let key = match payload.get_pubkey(&field.to_string()) {
                        Some(pubkey) => pubkey,
                        _ => return Error(RuleSetError::MissingPayloadValue.into()),
                    };

                    let account = match accounts.get(key) {
                        Some(account) => account,
                        _ => return Error(RuleSetError::MissingAccount.into()),
                    };

                    let data = match account.data.try_borrow() {
                        Ok(data) => data,
                        Err(_) => return Error(ProgramError::AccountBorrowFailed),
                    };

                    if is_zeroed(&data) {
                        // Print helpful errors.
                        if data.len() == 0 {
                            msg!("Account data is empty");
                        } else {
                            msg!("Account data is zeroed");
                        }

                        return Error(RuleSetError::DataIsEmpty.into());
                    } else if programs.contains(account.owner) {
                        // Account owner must be in the set.
                        return Success(self.to_error());
                    }
                }

                Failure(self.to_error())
            }
            Rule::Namespace => {
                msg!("Validating Namespace");
                Failure(self.to_error())
            }
        }
    }

    /// Convert the rule to a corresponding error resulting from the rule failure.
    pub fn to_error(&self) -> ProgramError {
        match self {
            Rule::All { .. }
            | Rule::Any { .. }
            | Rule::Not { .. }
            | Rule::Pass
            | Rule::Namespace => RuleSetError::UnexpectedRuleSetFailure.into(),
            Rule::AdditionalSigner { .. } => RuleSetError::AdditionalSignerCheckFailed.into(),
            Rule::PubkeyMatch { .. } => RuleSetError::PubkeyMatchCheckFailed.into(),
            Rule::PubkeyListMatch { .. } => RuleSetError::PubkeyListMatchCheckFailed.into(),
            Rule::PubkeyTreeMatch { .. } => RuleSetError::PubkeyTreeMatchCheckFailed.into(),
            Rule::PDAMatch { .. } => RuleSetError::PDAMatchCheckFailed.into(),
            Rule::ProgramOwned { .. } => RuleSetError::ProgramOwnedCheckFailed.into(),
            Rule::ProgramOwnedList { .. } => RuleSetError::ProgramOwnedListCheckFailed.into(),
            Rule::ProgramOwnedTree { .. } => RuleSetError::ProgramOwnedTreeCheckFailed.into(),
            Rule::Amount { .. } => RuleSetError::AmountCheckFailed.into(),
            Rule::Frequency { .. } => RuleSetError::FrequencyCheckFailed.into(),
            Rule::IsWallet { .. } => RuleSetError::IsWalletCheckFailed.into(),
            Rule::ProgramOwnedSet { .. } => RuleSetError::ProgramOwnedSetCheckFailed.into(),
        }
    }
}