From 8e8994ea8514378aa1adf4c446f38e22e3fdb302 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Wed, 5 Jun 2024 23:53:32 +0800 Subject: [PATCH] parse sudt amount should unpack first --- src/transaction/builder/sudt.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/transaction/builder/sudt.rs b/src/transaction/builder/sudt.rs index b137b5b6..f7a25b83 100644 --- a/src/transaction/builder/sudt.rs +++ b/src/transaction/builder/sudt.rs @@ -90,15 +90,20 @@ impl SudtTransactionBuilder { } } -fn parse_u128(data: &[u8]) -> Result { - if data.len() > std::mem::size_of::() { +fn parse_u128(data: Bytes) -> Result { + if data.as_slice().len() <= 4 { + return Err(TxBuilderError::Other(anyhow!( + "stdt_amount is not valid molecule encoded value" + ))); + } + let mut data_bytes: Vec = data.unpack(); + if data_bytes.len() > std::mem::size_of::() { return Err(TxBuilderError::Other(anyhow!( "stdt_amount bytes length greater than 128" ))); } - let mut data_bytes: Vec = data.into(); - data_bytes.extend(std::iter::repeat(0_u8).take(std::mem::size_of::() - data.len())); + data_bytes.extend(std::iter::repeat(0_u8).take(std::mem::size_of::() - data_bytes.len())); Ok(u128::from_le_bytes(data_bytes.try_into().unwrap())) } @@ -138,7 +143,7 @@ impl CkbTransactionBuilder for SudtTransactionBuilder { let outputs_sudt_amount: u128 = tx .outputs_data .iter() - .map(|data| parse_u128(data.as_slice())) + .map(|data| parse_u128(data.to_owned())) .collect::, TxBuilderError>>() .map(|u128_vec| u128_vec.iter().sum())?; @@ -146,7 +151,8 @@ impl CkbTransactionBuilder for SudtTransactionBuilder { for input in sudt_input_iter { let input = input?; - let input_amount = parse_u128(input.live_cell.output_data.as_ref())?; + let input_amount = + parse_u128(Bytes::new_unchecked(input.live_cell.output_data.clone()))?; inputs_sudt_amount += input_amount; input_iter.push_input(input); if inputs_sudt_amount >= outputs_sudt_amount {