Skip to content

Commit

Permalink
Fix verify_unlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Nov 9, 2023
1 parent 1523fa9 commit 86e03fc
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions sdk/src/types/block/unlock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,42 +133,60 @@ impl Unlocks {
}
}

fn verify_unlock<'a>(
unlocks: &'a [Unlock],
unlock: &'a Unlock,
index: u16,
seen_signatures: &mut HashSet<&'a SignatureUnlock>,
) -> Result<(), Error> {
match unlock {
Unlock::Signature(signature) => {
if !seen_signatures.insert(signature.as_ref()) {
return Err(Error::DuplicateSignatureUnlock(index));
}
}
Unlock::Reference(reference) => {
if index == 0
|| reference.index() >= index
|| !matches!(unlocks[reference.index() as usize], Unlock::Signature(_))
{
return Err(Error::InvalidUnlockReference(index));
}
}
Unlock::Account(account) => {
if index == 0 || account.index() >= index {
return Err(Error::InvalidUnlockAccount(index));
}
}
Unlock::Anchor(anchor) => {
if index == 0 || anchor.index() >= index {
return Err(Error::InvalidUnlockAnchor(index));
}
}
Unlock::Nft(nft) => {
if index == 0 || nft.index() >= index {
return Err(Error::InvalidUnlockNft(index));
}
}
Unlock::Multi(_) => return Err(Error::MultiUnlockRecursion),
Unlock::Empty(_) => {}
}

Ok(())
}

fn verify_unlocks<const VERIFY: bool>(unlocks: &[Unlock], _: &()) -> Result<(), Error> {
if VERIFY {
let mut seen_signatures = HashSet::new();

for (index, unlock) in (0u16..).zip(unlocks.iter()) {
match unlock {
Unlock::Signature(signature) => {
if !seen_signatures.insert(signature) {
return Err(Error::DuplicateSignatureUnlock(index));
}
}
Unlock::Reference(reference) => {
if index == 0
|| reference.index() >= index
|| !matches!(unlocks[reference.index() as usize], Unlock::Signature(_))
{
return Err(Error::InvalidUnlockReference(index));
}
}
Unlock::Account(account) => {
if index == 0 || account.index() >= index {
return Err(Error::InvalidUnlockAccount(index));
}
}
Unlock::Anchor(anchor) => {
if index == 0 || anchor.index() >= index {
return Err(Error::InvalidUnlockAnchor(index));
}
}
Unlock::Nft(nft) => {
if index == 0 || nft.index() >= index {
return Err(Error::InvalidUnlockNft(index));
Unlock::Multi(multi) => {
for unlock in multi.unlocks() {
verify_unlock(unlocks, unlock, index, &mut seen_signatures)?
}
}
Unlock::Multi(_) => todo!(),
Unlock::Empty(_) => todo!(),
_ => verify_unlock(unlocks, unlock, index, &mut seen_signatures)?,
}
}
}
Expand Down

0 comments on commit 86e03fc

Please sign in to comment.