@@ -34,8 +34,8 @@ use pinocchio::syscalls::sol_sha256;
3434///
3535/// # Important
3636///
37- /// This function differs from [`pinocchio::pubkey::create_program_address`] in that it
38- /// does not perform a validation to ensure that the derived address is a valid
37+ /// This function differs from [`pinocchio::pubkey::create_program_address`] in that
38+ /// it does not perform a validation to ensure that the derived address is a valid
3939/// (off-curve) program derived address. It is intended for use in cases where the
4040/// seeds, bump, and program id are known to be valid, and the caller wants to derive
4141/// the address without incurring the cost of the `create_program_address` syscall.
@@ -61,13 +61,15 @@ pub fn derive_address<const N: usize>(
6161 i += 1 ;
6262 }
6363
64- let bump = bump. as_slice ( ) ;
64+ // TODO: replace this with `as_slice` when the msrv is upgraded
65+ // to `1.84.0+`.
66+ let bump_seed = [ bump. unwrap_or_default ( ) ] ;
6567
6668 // SAFETY: `data` is guaranteed to have enough space for `MAX_SEEDS + 2`
6769 // elements, and `MAX_SEEDS` is as large as `N`.
6870 unsafe {
69- if ! bump. is_empty ( ) {
70- data. get_unchecked_mut ( i) . write ( bump ) ;
71+ if bump. is_some ( ) {
72+ data. get_unchecked_mut ( i) . write ( & bump_seed ) ;
7173 i += 1 ;
7274 }
7375 data. get_unchecked_mut ( i) . write ( program_id. as_ref ( ) ) ;
@@ -82,7 +84,7 @@ pub fn derive_address<const N: usize>(
8284 unsafe {
8385 sol_sha256 (
8486 data. as_ptr ( ) as * const u8 ,
85- ( N + 2 ) as u64 ,
87+ ( i + 2 ) as u64 ,
8688 pda. as_mut_ptr ( ) as * mut u8 ,
8789 ) ;
8890 }
@@ -111,8 +113,8 @@ pub fn derive_address<const N: usize>(
111113///
112114/// # Important
113115///
114- /// This function differs from [`pinocchio::pubkey::create_program_address`] in that it
115- /// does not perform a validation to ensure that the derived address is a valid
116+ /// This function differs from [`pinocchio::pubkey::create_program_address`] in that
117+ /// it does not perform a validation to ensure that the derived address is a valid
116118/// (off-curve) program derived address. It is intended for use in cases where the
117119/// seeds, bump, and program id are known to be valid, and the caller wants to derive
118120/// the address without incurring the cost of the `create_program_address` syscall.
@@ -136,10 +138,8 @@ pub const fn derive_address_const<const N: usize>(
136138 i += 1 ;
137139 }
138140
139- let bump = bump. as_slice ( ) ;
140-
141- if !bump. is_empty ( ) {
142- hasher = hasher. update ( bump) ;
141+ if bump. is_some ( ) {
142+ hasher = hasher. update ( & [ bump. unwrap ( ) ] ) ;
143143 }
144144
145145 hasher. update ( program_id) . update ( PDA_MARKER ) . finalize ( )
0 commit comments