2021-09-21 12:26:05 -07:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
|
|
|
|
|
use {
|
2021-09-21 19:58:14 -07:00
|
|
|
bytemuck::Pod,
|
2021-09-21 12:26:05 -07:00
|
|
|
solana_sdk::{
|
|
|
|
|
ic_msg, instruction::InstructionError, process_instruction::InvokeContext, pubkey::Pubkey,
|
|
|
|
|
},
|
2021-09-29 21:45:35 -07:00
|
|
|
spl_zk_token_sdk::zk_token_proof_instruction::*,
|
2021-09-21 12:26:05 -07:00
|
|
|
std::result::Result,
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-21 19:58:14 -07:00
|
|
|
fn verify<T: Pod + Verifiable>(
|
|
|
|
|
input: &[u8],
|
|
|
|
|
invoke_context: &mut dyn InvokeContext,
|
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
|
let proof = ProofInstruction::decode_data::<T>(input).ok_or_else(|| {
|
|
|
|
|
ic_msg!(invoke_context, "invalid proof data");
|
|
|
|
|
InstructionError::InvalidInstructionData
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
proof.verify().map_err(|err| {
|
|
|
|
|
ic_msg!(invoke_context, "proof verification failed: {:?}", err);
|
|
|
|
|
InstructionError::InvalidInstructionData
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 12:26:05 -07:00
|
|
|
pub fn process_instruction(
|
|
|
|
|
program_id: &Pubkey,
|
|
|
|
|
input: &[u8],
|
|
|
|
|
invoke_context: &mut dyn InvokeContext,
|
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
|
match ProofInstruction::decode_type(program_id, input)
|
|
|
|
|
.ok_or(InstructionError::InvalidInstructionData)?
|
|
|
|
|
{
|
2021-09-21 19:58:14 -07:00
|
|
|
ProofInstruction::VerifyUpdateAccountPk => {
|
|
|
|
|
ic_msg!(invoke_context, "VerifyUpdateAccountPk");
|
|
|
|
|
verify::<UpdateAccountPkData>(input, invoke_context)
|
|
|
|
|
}
|
|
|
|
|
ProofInstruction::VerifyCloseAccount => {
|
|
|
|
|
ic_msg!(invoke_context, "VerifyCloseAccount");
|
|
|
|
|
verify::<CloseAccountData>(input, invoke_context)
|
2021-09-21 12:26:05 -07:00
|
|
|
}
|
2021-09-21 20:34:29 -07:00
|
|
|
ProofInstruction::VerifyWithdraw => {
|
|
|
|
|
ic_msg!(invoke_context, "VerifyWithdraw");
|
|
|
|
|
verify::<WithdrawData>(input, invoke_context)
|
|
|
|
|
}
|
2021-09-27 09:01:43 -07:00
|
|
|
ProofInstruction::VerifyTransferRangeProofData => {
|
|
|
|
|
ic_msg!(invoke_context, "VerifyTransferRangeProofData");
|
|
|
|
|
verify::<TransferRangeProofData>(input, invoke_context)
|
2021-09-27 08:21:45 -07:00
|
|
|
}
|
2021-09-27 09:01:43 -07:00
|
|
|
ProofInstruction::VerifyTransferValidityProofData => {
|
|
|
|
|
ic_msg!(invoke_context, "VerifyTransferValidityProofData");
|
|
|
|
|
verify::<TransferValidityProofData>(input, invoke_context)
|
2021-09-27 08:21:45 -07:00
|
|
|
}
|
2021-09-21 12:26:05 -07:00
|
|
|
}
|
|
|
|
|
}
|