safe_zk_token_sdk/zk_token_proof_instruction.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 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 114 115 116 117 118 119 120
///! Instructions provided by the ZkToken Proof program
pub use crate::instruction::*;
use {
bytemuck::{bytes_of, Pod},
num_derive::{FromPrimitive, ToPrimitive},
num_traits::{FromPrimitive, ToPrimitive},
solana_program::instruction::Instruction,
};
#[derive(Clone, Copy, Debug, FromPrimitive, ToPrimitive, PartialEq, Eq)]
#[repr(u8)]
pub enum ProofInstruction {
/// Verify a `CloseAccountData` struct
///
/// Accounts expected by this instruction:
/// None
///
/// Data expected by this instruction:
/// `CloseAccountData`
///
VerifyCloseAccount,
/// Verify a `WithdrawData` struct
///
/// Accounts expected by this instruction:
/// None
///
/// Data expected by this instruction:
/// `WithdrawData`
///
VerifyWithdraw,
/// Verify a `WithdrawWithheldTokensData` struct
///
/// Accounts expected by this instruction:
/// None
///
/// Data expected by this instruction:
/// `WithdrawWithheldTokensData`
///
VerifyWithdrawWithheldTokens,
/// Verify a `TransferData` struct
///
/// Accounts expected by this instruction:
/// None
///
/// Data expected by this instruction:
/// `TransferData`
///
VerifyTransfer,
/// Verify a `TransferWithFeeData` struct
///
/// Accounts expected by this instruction:
/// None
///
/// Data expected by this instruction:
/// `TransferWithFeeData`
///
VerifyTransferWithFee,
/// Verify a `PubkeyValidityData` struct
///
/// Accounts expected by this instruction:
/// None
///
/// Data expected by this instruction:
/// `PubkeyValidityData`
///
VerifyPubkeyValidity,
}
impl ProofInstruction {
pub fn encode<T: Pod>(&self, proof: &T) -> Instruction {
let mut data = vec![ToPrimitive::to_u8(self).unwrap()];
data.extend_from_slice(bytes_of(proof));
Instruction {
program_id: crate::zk_token_proof_program::id(),
accounts: vec![],
data,
}
}
pub fn decode_type(input: &[u8]) -> Option<Self> {
input.first().and_then(|x| FromPrimitive::from_u8(*x))
}
pub fn decode_data<T: Pod>(input: &[u8]) -> Option<&T> {
if input.is_empty() {
None
} else {
bytemuck::try_from_bytes(&input[1..]).ok()
}
}
}
pub fn verify_close_account(proof_data: &CloseAccountData) -> Instruction {
ProofInstruction::VerifyCloseAccount.encode(proof_data)
}
pub fn verify_withdraw(proof_data: &WithdrawData) -> Instruction {
ProofInstruction::VerifyWithdraw.encode(proof_data)
}
pub fn verify_withdraw_withheld_tokens(proof_data: &WithdrawWithheldTokensData) -> Instruction {
ProofInstruction::VerifyWithdrawWithheldTokens.encode(proof_data)
}
pub fn verify_transfer(proof_data: &TransferData) -> Instruction {
ProofInstruction::VerifyTransfer.encode(proof_data)
}
pub fn verify_transfer_with_fee(proof_data: &TransferWithFeeData) -> Instruction {
ProofInstruction::VerifyTransferWithFee.encode(proof_data)
}
pub fn verify_pubkey_validity(proof_data: &PubkeyValidityData) -> Instruction {
ProofInstruction::VerifyPubkeyValidity.encode(proof_data)
}