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
//! PDA (Program Derived Address) functions.
//!
//! These wrapper functions provide PDA derivation across native and Solana
//! targets.
//!
//! Seed-based APIs require deterministic seed ordering and consistent program
//! IDs across derivation and verification.
use crateAddress;
use crateProgramError;
/// Find a valid program derived address and its corresponding bump seed.
///
/// Returns `None` if no valid PDA exists.
///
/// This is the preferred PDA derivation API in `pina` because it is explicit
/// about failure and avoids panics in on-chain code paths.
///
/// # Examples
///
/// ```
/// use pina::try_find_program_address;
///
/// let program_id = pina::address!("11111111111111111111111111111111");
/// let seeds: &[&[u8]] = &[b"vault"];
///
/// if let Some((pda, bump)) = try_find_program_address(seeds, &program_id) {
/// // `pda` is the derived address, `bump` is the canonical bump seed.
/// assert!(bump <= 255);
/// }
/// ```
/// Find a valid program derived address and its corresponding bump seed.
///
/// # Panics
///
/// Panics if no valid PDA exists.
///
/// Prefer [`try_find_program_address`] for recoverable error handling.
/// Create a valid program derived address without searching for a bump seed.
///
/// Use this when your instruction already carries a bump and you want to
/// verify exact PDA derivation against user-provided seeds.
///
/// <!-- {=pinaPdaSeedContract|trim|linePrefix:"/// ":true} -->/// Seed-based APIs require deterministic seed ordering.
///
/// Program IDs must stay consistent across derivation and verification.
///
/// When a bump is required, prefer canonical bump derivation.
///
/// Use explicit bumps when needed.<!-- {/pinaPdaSeedContract} -->
///
/// # Examples
///
/// ```
/// use pina::create_program_address;
/// use pina::try_find_program_address;
///
/// let program_id = pina::address!("11111111111111111111111111111111");
/// let seeds: &[&[u8]] = &[b"vault"];
///
/// // First derive the canonical PDA and bump:
/// let (pda, bump) =
/// try_find_program_address(seeds, &program_id).unwrap_or_else(|| panic!("no valid PDA"));
///
/// // Then recreate the address using the known bump:
/// let bump_seed = [bump];
/// let recreated = create_program_address(&[b"vault", &bump_seed], &program_id)
/// .unwrap_or_else(|e| panic!("failed to recreate: {e:?}"));
/// assert_eq!(pda, recreated);
/// ```