Files
solana/programs/zk-token-proof/src/lib.rs

57 lines
2.0 KiB
Rust
Raw Normal View History

2021-09-21 12:26:05 -07:00
#![forbid(unsafe_code)]
use {
bytemuck::Pod,
2021-09-21 12:26:05 -07:00
solana_sdk::{
ic_msg, instruction::InstructionError, process_instruction::InvokeContext, pubkey::Pubkey,
},
spl_zk_token_crypto::instruction::*,
std::result::Result,
};
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)?
{
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
}
ProofInstruction::VerifyWithdraw => {
ic_msg!(invoke_context, "VerifyWithdraw");
verify::<WithdrawData>(input, invoke_context)
}
ProofInstruction::VerifyTransferWithRangeProofData => {
ic_msg!(invoke_context, "VerifyTransferWithRangeProofData");
verify::<TransferWithRangeProofData>(input, invoke_context)
}
ProofInstruction::VerifyTransferWithValidityProofData => {
ic_msg!(invoke_context, "VerifyTransferWithValidityProofData");
verify::<TransferWithValidityProofData>(input, invoke_context)
}
2021-09-21 12:26:05 -07:00
}
}