Skip to content

Commit

Permalink
compare wtxid commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
manlikeHB committed Sep 8, 2024
1 parent fb5f5f8 commit 25edef4
Showing 1 changed file with 111 additions and 12 deletions.
123 changes: 111 additions & 12 deletions src/validation/coinbase.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const MINIMUM_WITNESS_COMMITMENT: u32 = 38;

/// Validates coinbase transaction.
pub fn validate_coinbase(
tx: @Transaction, total_fees: u64, block_height: u32, _wtxid_commitment: Hash,
tx: @Transaction, total_fees: u64, block_height: u32, wtxid_commitment: Hash,
) -> Result<(), ByteArray> {
// Validate the coinbase input
// Ensure there is exactly one coinbase input
Expand Down Expand Up @@ -72,32 +72,41 @@ pub fn validate_coinbase(
if *tx.is_segwit {
let 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 6-byte fixed prefix 0xaa21a9ed
// 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 {
//TODO: compare the wtxid commitment
println!("wtxid commitment present");
// get wtxid commitment
while x < pk_script.len() {
extracted_wtxid_commitment.append_word(pk_script[x].into(), 1);
x += 1;
};

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");
}
}
/// Verify wTXID Commitment
// Locate the OP_RETURN output, ensure that it contains the wTXID commitment ()
}

Result::Ok(())
Expand All @@ -111,7 +120,7 @@ fn compute_block_reward(block_height: u32) -> u64 {
#[cfg(test)]
mod tests {
use crate::types::transaction::{TxIn, TxOut, Transaction, OutPoint};
use crate::utils::hex::from_hex;
use crate::utils::{hex::from_hex, hash::Hash};
use super::{compute_block_reward, validate_coinbase};

// Ref implementation here:
Expand Down Expand Up @@ -738,7 +747,52 @@ mod tests {
}

#[test]
fn test_validate_coinbase_segwit_output() {
fn test_validate_coinbase_segwit_output_with_no_wtxid_commitment() {
let tx = Transaction {
version: 1,
is_segwit: true,
inputs: array![
TxIn {
script: @from_hex(
"0320a107046f0a385a632f4254432e434f4d2ffabe6d6dbdd0ee86f9a1badfd0aa1b3c9dac8d90840cf973f7b2590d6c9adde1a6e0974a010000000000000001283da9a172020000000000"
),
sequence: 4294967295,
previous_output: OutPoint {
txid: 0x0_u256.into(),
vout: 0xffffffff_u32,
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![
from_hex("0000000000000000000000000000000000000000000000000000000000000000")
]
.span(),
}
]
.span(),
outputs: array![
TxOut {
value: 0_u64,
pk_script: @from_hex(
"10109f4b82aa3ed7ec9d02a2a90246478b3308c8b85daf62fe501d58d05727a4"
),
cached: false,
}
]
.span(),
lock_time: 0
};

let total_fees = 0_u64;
let block_height = 500_000;

validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap_err();
}

#[test]
fn test_validate_coinbase_segwit_output_with_wrong_wtxid_commitment() {
let tx = Transaction {
version: 1,
is_segwit: true,
Expand Down Expand Up @@ -778,12 +832,24 @@ mod tests {

let total_fees = 0_u64;
let block_height = 500_000;
let wtxid_commitment = Hash {
value: [
0x10109985,
0x82aa3ed7,
0xec9d02a2,
0xa9024647,
0x8b3308c8,
0xb85daf62,
0xfe501d58,
0xd05727a4
]
};

validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap();
validate_coinbase(@tx, total_fees, block_height, wtxid_commitment).unwrap_err();
}

#[test]
fn test_validate_coinbase_segwit_output_with_no_wtxid_commitment() {
fn test_validate_coinbase_segwit_output() {
let tx = Transaction {
version: 1,
is_segwit: true,
Expand Down Expand Up @@ -812,7 +878,28 @@ mod tests {
TxOut {
value: 0_u64,
pk_script: @from_hex(
"10109f4b82aa3ed7ec9d02a2a90246478b3308c8b85daf62fe501d58d05727a4"
"0000000000000000000000000000000000000000000000000000000000000000"
),
cached: false,
},
TxOut {
value: 0_u64,
pk_script: @from_hex(
"6a24aa21a9ed10109f4b82aa3ed7ec9d02a2a90246478b3308c8b85daf62fe501d58d05727a4"
),
cached: false,
},
TxOut {
value: 0_u64,
pk_script: @from_hex(
"0000000000000000000000000000000000000000000000000000000000000000"
),
cached: false,
},
TxOut {
value: 0_u64,
pk_script: @from_hex(
"0000000000000000000000000000000000000000000000000000000000000000"
),
cached: false,
}
Expand All @@ -823,7 +910,19 @@ mod tests {

let total_fees = 0_u64;
let block_height = 500_000;
let wtxid_commitment = Hash {
value: [
0x10109f4b,
0x82aa3ed7,
0xec9d02a2,
0xa9024647,
0x8b3308c8,
0xb85daf62,
0xfe501d58,
0xd05727a4
]
};

validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap_err();
validate_coinbase(@tx, total_fees, block_height, wtxid_commitment).unwrap();
}
}

0 comments on commit 25edef4

Please sign in to comment.