Skip to content

Commit

Permalink
construct expected wtxid commitment and compare with extracted wtxid …
Browse files Browse the repository at this point in the history
…commitment
  • Loading branch information
manlikeHB committed Sep 9, 2024
1 parent 25edef4 commit e120fd2
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/validation/coinbase.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -70,42 +70,45 @@ pub fn validate_coinbase(

// validate BIP-141 segwit output
if *tx.is_segwit {
let outputs = *tx.outputs;
let mut outputs = *tx.outputs;
let mut is_wtxid_commitment_present: bool = false;
let mut extracted_wtxid_commitment: ByteArray = "";
let mut i = 0;
let mut x = 6;

while i < outputs.len() {
let pk_script = *outputs[i].pk_script;

// check for OP_RETURN and the fixed prefix "0xaa21a9ed"
if pk_script.len() >= MINIMUM_WITNESS_COMMITMENT
&& pk_script[0] == 0x6a
&& pk_script[1] == 0x24
&& pk_script[2] == 0xaa
&& pk_script[3] == 0x21
&& pk_script[4] == 0xa9
&& pk_script[5] == 0xed {
let mut x = 0;

// construct expected wtxid commitment
let mut fixed_prefix_arr = array![0x6a, 0x24, 0xaa, 0x21, 0xa9, 0xed];
let mut fixed_prefix_byte: ByteArray = "";

for i in fixed_prefix_arr {
fixed_prefix_byte.append_byte(i);
};

let mut expected_wtxid_commitment = ByteArrayTrait::concat(
@fixed_prefix_byte, @wtxid_commitment.into()
);

while let Option::Some(output) = outputs.pop_back() {
let pk_script = *output.pk_script;

// check for pk_script with at least 38 bytes commitment length
if pk_script.len() >= MINIMUM_WITNESS_COMMITMENT {
// get wtxid commitment
while x < pk_script.len() {
while x < MINIMUM_WITNESS_COMMITMENT {
extracted_wtxid_commitment.append_word(pk_script[x].into(), 1);
x += 1;
};

is_wtxid_commitment_present = true;
break;
// compare expected and extracted wtxid commitment
if expected_wtxid_commitment == extracted_wtxid_commitment {
is_wtxid_commitment_present = true;
break;
}
}
i += 1;
};

if !is_wtxid_commitment_present {
return Result::Err("No wtxid commitment found");
}

if !(extracted_wtxid_commitment == wtxid_commitment.into()) {
return Result::Err("Wrong wtxid commitment");
}
}
}

Expand Down

0 comments on commit e120fd2

Please sign in to comment.