Skip to content

Commit

Permalink
spending_reqs: Small code improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Sep 2, 2024
1 parent 5c74b12 commit c43a337
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/ledger/spending_requirements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ impl Ledger {
}

let mut witness = tx.input[input_idx].witness.to_vec();
let mut annex: Option<Vec<u8>> = None;

// Key path spend.
if witness.len() == 1 {
Expand Down Expand Up @@ -196,21 +195,30 @@ impl Ledger {
};
}

let mut annex: Option<Vec<u8>> = None;
if witness.len() >= 2 && witness[witness.len() - 1][0] == 0x50 {
annex = Some(witness.pop().unwrap());
} else if witness.len() < 2 {
return Err(LedgerError::SpendingRequirements("The number of witness elements should be at least two (the script and the control block).".to_owned()));
return Err(
LedgerError::SpendingRequirements(
"The number of witness elements should be at least two (the script and the control block).".to_owned()
)
);
}

let control_block = ControlBlock::decode(&witness.pop().unwrap()).unwrap();
let script_buf = witness.pop().unwrap();
let script = Script::from_bytes(&script_buf);

let out_pk = XOnlyPublicKey::from_slice(&sig_pub_key_bytes[2..]).unwrap();
let out_pk = TweakedPublicKey::dangerous_assume_tweaked(out_pk);
let x_only_public_key = XOnlyPublicKey::from_slice(&sig_pub_key_bytes[2..]).unwrap();
let tweaked_x_only_public_key =
TweakedPublicKey::dangerous_assume_tweaked(x_only_public_key);

let res = control_block.verify_taproot_commitment(&secp, out_pk.to_inner(), script);
if !res {
if !control_block.verify_taproot_commitment(
&secp,
tweaked_x_only_public_key.to_inner(),
script,
) {
return Err(LedgerError::SpendingRequirements(
"The taproot commitment does not match the Taproot public key.".to_owned(),
));
Expand Down

0 comments on commit c43a337

Please sign in to comment.