Skip to content

Commit

Permalink
parse sudt amount should unpack first
Browse files Browse the repository at this point in the history
  • Loading branch information
eval-exec committed Jun 5, 2024
1 parent 79859e0 commit 8e8994e
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/transaction/builder/sudt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,20 @@ impl SudtTransactionBuilder {
}
}

fn parse_u128(data: &[u8]) -> Result<u128, TxBuilderError> {
if data.len() > std::mem::size_of::<u128>() {
fn parse_u128(data: Bytes) -> Result<u128, TxBuilderError> {
if data.as_slice().len() <= 4 {
return Err(TxBuilderError::Other(anyhow!(
"stdt_amount is not valid molecule encoded value"
)));
}
let mut data_bytes: Vec<u8> = data.unpack();
if data_bytes.len() > std::mem::size_of::<u128>() {
return Err(TxBuilderError::Other(anyhow!(
"stdt_amount bytes length greater than 128"
)));
}

let mut data_bytes: Vec<u8> = data.into();
data_bytes.extend(std::iter::repeat(0_u8).take(std::mem::size_of::<u128>() - data.len()));
data_bytes.extend(std::iter::repeat(0_u8).take(std::mem::size_of::<u128>() - data_bytes.len()));
Ok(u128::from_le_bytes(data_bytes.try_into().unwrap()))
}

Expand Down Expand Up @@ -138,15 +143,16 @@ 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::<Result<Vec<u128>, TxBuilderError>>()
.map(|u128_vec| u128_vec.iter().sum())?;

let mut inputs_sudt_amount = 0;

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 {
Expand Down

0 comments on commit 8e8994e

Please sign in to comment.