solana_zk_token_sdk/
zk_token_proof_state.rs1use {
2 crate::{zk_token_elgamal::pod::PodProofType, zk_token_proof_instruction::ProofType},
3 bytemuck::{bytes_of, Pod, Zeroable},
4 num_traits::ToPrimitive,
5 solana_program::{
6 instruction::{InstructionError, InstructionError::InvalidAccountData},
7 pubkey::Pubkey,
8 },
9 std::mem::size_of,
10};
11
12#[derive(Clone, Copy, Debug, PartialEq)]
14#[repr(C)]
15pub struct ProofContextState<T: Pod> {
16 pub context_state_authority: Pubkey,
18 pub proof_type: PodProofType,
20 pub proof_context: T,
22}
23
24unsafe impl<T: Pod> Zeroable for ProofContextState<T> {}
29unsafe impl<T: Pod> Pod for ProofContextState<T> {}
30
31impl<T: Pod> ProofContextState<T> {
32 pub fn encode(
33 context_state_authority: &Pubkey,
34 proof_type: ProofType,
35 proof_context: &T,
36 ) -> Vec<u8> {
37 let mut buf = Vec::with_capacity(size_of::<Self>());
38 buf.extend_from_slice(context_state_authority.as_ref());
39 buf.push(ToPrimitive::to_u8(&proof_type).unwrap());
40 buf.extend_from_slice(bytes_of(proof_context));
41 buf
42 }
43
44 pub fn try_from_bytes(input: &[u8]) -> Result<&Self, InstructionError> {
50 bytemuck::try_from_bytes(input).map_err(|_| InvalidAccountData)
51 }
52}
53
54#[derive(Clone, Copy, Debug, PartialEq, bytemuck_derive::Pod, bytemuck_derive::Zeroable)]
57#[repr(C)]
58pub struct ProofContextStateMeta {
59 pub context_state_authority: Pubkey,
61 pub proof_type: PodProofType,
63}
64
65impl ProofContextStateMeta {
66 pub fn try_from_bytes(input: &[u8]) -> Result<&Self, InstructionError> {
67 input
68 .get(..size_of::<ProofContextStateMeta>())
69 .and_then(|data| bytemuck::try_from_bytes(data).ok())
70 .ok_or(InvalidAccountData)
71 }
72}