(
tmp_storage_pda_data: &ChecksAndTransferState,
)
| 25 | |
| 26 | #[allow(clippy::comparison_chain)] |
| 27 | pub fn check_external_amount( |
| 28 | tmp_storage_pda_data: &ChecksAndTransferState, |
| 29 | ) -> Result<(u64, u64), ProgramError> { |
| 30 | let ext_amount = |
| 31 | i64::from_le_bytes(tmp_storage_pda_data.ext_amount.clone().try_into().unwrap()); |
| 32 | // ext_amount includes relayer_fee |
| 33 | let relayer_fee = |
| 34 | u64::from_le_bytes(tmp_storage_pda_data.relayer_fee.clone().try_into().unwrap()); |
| 35 | // pub_amount is the public amount included in public inputs for proof verification |
| 36 | let pub_amount = <BigInteger256 as FromBytes>::read(&tmp_storage_pda_data.amount[..]).unwrap(); |
| 37 | |
| 38 | if ext_amount > 0 { |
| 39 | if pub_amount.0[1] != 0 || pub_amount.0[2] != 0 || pub_amount.0[3] != 0 { |
| 40 | msg!("Public amount is larger than u64."); |
| 41 | return Err(ProgramError::InvalidInstructionData); |
| 42 | } |
| 43 | |
| 44 | let pub_amount_fits_i64 = i64::try_from(pub_amount.0[0]); |
| 45 | |
| 46 | if pub_amount_fits_i64.is_err() { |
| 47 | msg!("Public amount is larger than i64."); |
| 48 | return Err(ProgramError::InvalidInstructionData); |
| 49 | } |
| 50 | |
| 51 | //check amount |
| 52 | if pub_amount.0[0].checked_add(relayer_fee).unwrap() != ext_amount.try_into().unwrap() { |
| 53 | msg!( |
| 54 | "Deposit invalid external amount (relayer_fee) {} != {}", |
| 55 | pub_amount.0[0] + relayer_fee, |
| 56 | ext_amount |
| 57 | ); |
| 58 | return Err(ProgramError::InvalidInstructionData); |
| 59 | } |
| 60 | Ok((ext_amount.try_into().unwrap(), relayer_fee)) |
| 61 | } else if ext_amount < 0 { |
| 62 | // calculate ext_amount from pubAmount: |
| 63 | let mut field = FqParameters::MODULUS; |
| 64 | field.sub_noborrow(&pub_amount); |
| 65 | |
| 66 | // field.0[0] is the positive value |
| 67 | if field.0[1] != 0 || field.0[2] != 0 || field.0[3] != 0 { |
| 68 | msg!("Public amount is larger than u64."); |
| 69 | return Err(ProgramError::InvalidInstructionData); |
| 70 | } |
| 71 | let pub_amount_fits_i64 = i64::try_from(pub_amount.0[0]); |
| 72 | if pub_amount_fits_i64.is_err() { |
| 73 | msg!("Public amount is larger than i64."); |
| 74 | return Err(ProgramError::InvalidInstructionData); |
| 75 | } |
| 76 | |
| 77 | if field.0[0] |
| 78 | != u64::try_from(-ext_amount) |
| 79 | .unwrap() |
| 80 | .checked_add(relayer_fee) |
| 81 | .unwrap() |
| 82 | { |
| 83 | msg!( |
| 84 | "Withdrawal invalid external amount: {} != {}", |
no outgoing calls
no test coverage detected