rush_ecs_svm/pda/
user_pda.rs

1use solana_program::pubkey::Pubkey;
2
3/// Finds the [`UserPDA`] PDA with canonical bump
4///
5/// - Used for validating User seeds
6/// - Used for identifying specific User account onchain
7pub struct UserPDA {}
8
9impl UserPDA {
10    /// Tag seed for differentiating state
11    pub const TAG: &'static str = "User";
12
13    /// Find PDA for User State
14    ///
15    /// Also searches for canonical Bump Seed,
16    /// hence is very expensive.
17    ///
18    /// If Bump Seed is available,
19    /// use [`Self::create_pda`] instead
20    ///
21    /// Returns (PDA, Bump Seed)
22    pub fn find_pda(
23        program_id: &Pubkey,
24        user_authority: &Pubkey,
25        world_pda: &Pubkey,
26        user_agent_salt: String,
27    ) -> (Pubkey, u8) {
28        Pubkey::find_program_address(
29            &[
30                Self::TAG.as_bytes(),
31                world_pda.as_ref(),
32                user_authority.as_ref(),
33                user_agent_salt.as_bytes(),
34            ],
35            program_id,
36        )
37    }
38
39    /// Create PDA for User State
40    ///
41    /// Doesn't search for canonical Bump Seed.
42    ///
43    /// Cheaper and encouraged to use if Bump
44    /// Seed is available.
45    ///
46    /// Returns PDA
47    pub fn create_pda(
48        program_id: &Pubkey,
49        user_authority: &Pubkey,
50        world_pda: &Pubkey,
51        user_agent_salt: String,
52        bump_seed: u8,
53    ) -> Pubkey {
54        // expects a valid set of seeds
55        Pubkey::create_program_address(
56            &[
57                Self::TAG.as_bytes(),
58                world_pda.as_ref(),
59                user_authority.as_ref(),
60                user_agent_salt.as_bytes(),
61                &[bump_seed],
62            ],
63            program_id,
64        )
65        .expect("Invalid seeds")
66    }
67}