Skip to content

Commit

Permalink
Check if sudt amount bytes length is less than u128
Browse files Browse the repository at this point in the history
  • Loading branch information
eval-exec committed Mar 6, 2024
1 parent f0619b1 commit a68117a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/transaction/builder/sudt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
tx_builder::{BalanceTxCapacityError, TxBuilderError},
NetworkInfo, NetworkType, TransactionWithScriptGroups,
};
use anyhow::anyhow;

use ckb_types::{
core::{Capacity, ScriptHashType},
Expand Down Expand Up @@ -125,8 +126,25 @@ impl CkbTransactionBuilder for SudtTransactionBuilder {
let outputs_sudt_amount: u128 = tx
.outputs_data
.iter()
.map(|data| u128::from_le_bytes(data.raw_data().as_ref().try_into().unwrap()))
.sum();
.map(|data| {
if data.len() > std::mem::size_of::<u128>() {
return Err(TxBuilderError::Other(anyhow!(
"stdt_amount bytes length greater than 128"
)));
}
if data.len() % std::mem::size_of::<u8>() != 0 {
return Err(TxBuilderError::Other(anyhow!(
"stdt_amount bytes length is not a multiple of u8 size"
)));
}

let mut data_bytes: Vec<u8> =
vec![0_u8; std::mem::size_of::<u128>() - data.len()];
data_bytes.extend_from_slice(data.as_slice());
Ok(u128::from_le_bytes(data_bytes.try_into().unwrap()))
})
.collect::<Result<Vec<u128>, TxBuilderError>>()
.map(|u128_vec| u128_vec.iter().sum())?;

let mut inputs_sudt_amount = 0;

Expand Down

0 comments on commit a68117a

Please sign in to comment.