Skip to content

Commit

Permalink
-refac: check if transcation is coinbase transaction before hashing
Browse files Browse the repository at this point in the history
- refac: represent witness reserved value in string literal
  • Loading branch information
manlikeHB committed Sep 13, 2024
1 parent baf4dbf commit 9b5cf2f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/consensus/src/validation/block.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ pub fn compute_and_validate_tx_data(
let tx_bytes_segwit = @tx.encode_with_witness(tx_bytes_legacy);

let txid = double_sha256_byte_array(tx_bytes_legacy);
let mut wtxid = double_sha256_byte_array(tx_bytes_segwit);

/// The wTXID for the coinbase transaction must be set to all zeros. This is because it's
/// eventually going to contain the commitment inside it
/// see https://learnmeabitcoin.com/technical/transaction/wtxid/#commitment
if i == 0 {
wtxid = Default::default();
let mut wtxid = Default::default();
if i != 0 {
wtxid = double_sha256_byte_array(tx_bytes_segwit);
}

// tx_byte_segwit represents all the bytes in the transaction, so the bytes in the segwit
Expand Down
10 changes: 4 additions & 6 deletions packages/consensus/src/validation/coinbase.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ use utils::{
bit_shifts::shr, hash::{Digest, DigestIntoByteArray}, sha256::{double_sha256_byte_array}
};


const BIP_34_BLOCK_HEIGHT: u32 = 227_836;
const BIP_141_BLOCK_HEIGHT: u32 = 481_824;
const WTNS_PK_SCRIPT_LEN: u32 = 38;
const WTNS_PK_SCRIPT_PREFIX: felt252 = 116705705699821; // 0x6a24aa21a9ed
const WITNESS_VALUE: felt252 = 0;

/// Validates coinbase transaction.
pub fn validate_coinbase(
Expand Down Expand Up @@ -41,8 +39,8 @@ pub fn validate_coinbase(
let witness = tx.inputs[0].witness[0];

// check witness value
let mut witness_value_byte: ByteArray = "";
witness_value_byte.append_word(WITNESS_VALUE, 32);
let witness_value_byte: ByteArray =
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

if witness != @witness_value_byte {
return Result::Err("Wrong coinbase witness");
Expand Down Expand Up @@ -134,8 +132,8 @@ fn compute_block_reward(block_height: u32) -> u64 {
fn calculate_wtxid_commitment(wtxid_root: Digest) -> Digest {
// construct witness reserved value
// 0000000000000000000000000000000000000000000000000000000000000000
let mut witness_value_byte: ByteArray = "";
witness_value_byte.append_word(WITNESS_VALUE, 32);
let witness_value_byte: ByteArray =
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

// convert wtxid_root to ByteArray
let wtxid_root_bytes: ByteArray = wtxid_root.into();
Expand Down

0 comments on commit 9b5cf2f

Please sign in to comment.