Skip to content

Commit

Permalink
- check for output with the wtxid commitment
Browse files Browse the repository at this point in the history
- return error if no wtxid commitment
- add test
  • Loading branch information
manlikeHB committed Sep 8, 2024
1 parent 6f3a468 commit fb5f5f8
Showing 1 changed file with 112 additions and 45 deletions.
157 changes: 112 additions & 45 deletions src/validation/coinbase.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::utils::{bit_shifts::shr, hash::Hash};

const BIP_34_BLOCK_HEIGHT: u32 = 227_836;
const BIP_141_BLOCK_HEIGHT: u32 = 481_824;
const MINIMUM_WITNESS_COMMITMENT: u32 = 38;

/// Validates coinbase transaction.
pub fn validate_coinbase(
Expand Down Expand Up @@ -67,14 +68,32 @@ pub fn validate_coinbase(
return Result::Err("Wrong coinbase witness");
}

// TODO: validate BIP-141 segwit output
//// check if transaction is a sigwit tx
// validate BIP-141 segwit output
if *tx.is_segwit {
let outputs = *tx.outputs;
let mut is_wtxid_commitment_present: bool = false;
let mut i = 0;

while i < outputs.len() {
println!("pkscript {}", outputs[i].pk_script);
let pk_script = *outputs[i].pk_script;

// check for OP_RETURN and 6-byte 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");
is_wtxid_commitment_present = true;
}
i += 1;
};

if !is_wtxid_commitment_present {
return Result::Err("No wtxid commitment found");
}
}
/// Verify wTXID Commitment
Expand Down Expand Up @@ -717,46 +736,94 @@ mod tests {

validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap_err();
}
// #[test]
// fn test_validate_coinbase_segwit_output() {
// let tx = Transaction {
// version: 1,
// is_segwit: false,
// inputs: array![
// TxIn {
// script: @from_hex("04ffff001d0102"),
// 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(
// "4104d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725bac"
// ),
// 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() {
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(
"6a24aa21a9ed10109f4b82aa3ed7ec9d02a2a90246478b3308c8b85daf62fe501d58d05727a4"
),
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();
}

#[test]
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();
}
}

0 comments on commit fb5f5f8

Please sign in to comment.