Skip to content

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Flouse <1297478+Flouse@users.noreply.github.com>
Co-authored-by: Quake Wang <quake.wang@gmail.com>
  • Loading branch information
3 people authored and liyukun committed Jan 6, 2024
1 parent c0dd5d5 commit e0fb7cc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
4 changes: 2 additions & 2 deletions contracts/spore/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ fn process_transfer() -> Result<(), Error> {
if transfer.nft_id().as_slice() != load_type_args(0, GroupInput).as_ref() {
return Err(Error::SporeActionFieldMismatch);
}
check_spore_address(GroupInput, transfer.to())?;
check_spore_address(GroupOutput, transfer.from())?;
check_spore_address(GroupInput, transfer.from())?;
check_spore_address(GroupOutput, transfer.to())?;

Ok(())
}
Expand Down
25 changes: 9 additions & 16 deletions lib/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ pub fn verify_type_id(output_index: usize, source: Source) -> Option<[u8; 32]> {
None
}

pub fn calc_type_id(prevout_hash: &[u8], output_index: usize) -> [u8; 32] {
/// The type ID is calculated as the blake2b (with CKB's personalization) of
/// the first CellInput in current transaction, and the created output cell
/// index(in 64-bit little endian unsigned integer).
pub fn calc_type_id(tx_first_input: &[u8], output_index: usize) -> [u8; 32] {
let mut blake2b = Blake2bBuilder::new(32)
.personal(b"ckb-default-hash")
.build();
Expand Down Expand Up @@ -145,7 +148,7 @@ pub fn calc_capacity_sum(lock_hash: &[u8; 32], source: Source) -> u128 {
}

pub fn check_spore_address(
gourp_source: Source,
group_source: Source,
spore_address: action::Address,
) -> Result<(), Error> {
let address = load_cell_lock(0, gourp_source)?;
Expand All @@ -162,19 +165,9 @@ pub fn extract_spore_action() -> Result<action::SporeAction, Error> {
.ok_or(Error::InvliadCoBuildWitnessLayout)?;
let script_hash = load_script_hash()?;

let mut action = None;
for value in message.actions().into_iter() {
if value.script_hash().as_slice() == script_hash.as_slice() {
if action.is_some() {
return Err(Error::SporeActionDuplicated);
}
action = Some(value);
}
let mut iter = message.actions().into_iter().filter(|value| value.script_hash().as_slice() == script_hash.as_slice());
match (iter.next(), iter.next()) {
(Some(action), None) => action::SporeAction::from_slice(&action.data().raw_data()).map_err(|_| Error::InvliadCoBuildMessage),
_ => Err(Error::InvliadCoBuildMessage)
}
let Some(action) = action else {
return Err(Error::InvliadCoBuildMessage);
};
let spore_action = action::SporeAction::from_slice(&action.data().raw_data())
.map_err(|_| Error::InvliadCoBuildMessage)?;
Ok(spore_action)
}

0 comments on commit e0fb7cc

Please sign in to comment.