-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.rs
63 lines (48 loc) · 1.58 KB
/
helpers.rs
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
use borsh::BorshSerialize;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
pubkey::Pubkey,
};
use crate::error::MyProgramError;
pub fn validate_pda(
seeds: Vec<&[u8]>,
pda: &Pubkey,
bump: u8,
program_id: &Pubkey,
) -> Result<(), MyProgramError> {
let mut seeds_with_bump: Vec<&[u8]> = Vec::new();
for seed in seeds {
seeds_with_bump.push(seed);
}
let binding = [bump];
seeds_with_bump.push(&binding);
let actual_pda = Pubkey::create_program_address(&seeds_with_bump, program_id)
.map_err(|_| MyProgramError::PdaPubekyMismatch)?;
if actual_pda.ne(pda) {
return Err(MyProgramError::PdaPubekyMismatch.into());
}
Ok(())
}
pub fn get_bytes32_from_string(data: String) -> Result<[u8; 32], ProgramError> {
if data.len() > 32 {
return Err(MyProgramError::SizeOverflow.into());
}
let mut bytes = [0u8; 32];
let end = data.len();
bytes[0..end].copy_from_slice(&data.as_bytes());
Ok(bytes)
}
pub trait SerializeAndWriteToSolanaAccount: BorshSerialize {
fn serialize_and_write_to_sol_acc(&self, solana_acc: &AccountInfo) -> ProgramResult {
let mut data_bytes: Vec<u8> = Vec::new();
self.serialize(&mut data_bytes)
.map_err(|_| MyProgramError::SerializeError)?;
if solana_acc.data_len() < data_bytes.len() {
return Err(MyProgramError::SizeOverflow.into());
}
solana_acc
.try_borrow_mut_data()?
.copy_from_slice(&data_bytes);
Ok(())
}
}