Skip to content

Commit

Permalink
Use new validate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
b-j-roberts committed Dec 3, 2024
1 parent 91c2e1a commit 888ee06
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 98 deletions.
29 changes: 14 additions & 15 deletions packages/tests/src/tests/test_p2sh.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use shinigami_engine::transaction::EngineInternalTransactionTrait;
use shinigami_engine::engine::EngineImpl;
use shinigami_engine::hash_cache::HashCacheImpl;
use shinigami_engine::flags::ScriptFlags;
use crate::utxo::UTXO;
use crate::validate::validate_p2sh;
use crate::validate;
use shinigami_utils::bytecode::hex_to_bytecode;


Expand All @@ -19,9 +20,8 @@ fn test_p2sh_transaction_1() {
amount: 10000000, pubkey_script: hex_to_bytecode(@prevout_pubkey), block_height: 177625
};

let utxo_hints = array![prev_out_1of2_invalid];

let res = validate_p2sh(@transaction, 0, utxo_hints, 2);
let flags: u32 = ScriptFlags::ScriptBip16.into();
let res = validate::validate_transaction_at(@transaction, flags, prev_out_1of2_invalid, 2);
assert!(res.is_ok(), "P2SH failed!:{:?}", res.unwrap_err());
}

Expand All @@ -40,8 +40,8 @@ fn test_p2sh_transaction_2() {
};

let utxo_hints = array![prev_out_1of2_invalid];

let res = validate_p2sh(@transaction, 0, utxo_hints, 0);
let flags: u32 = ScriptFlags::ScriptBip16.into();
let res = validate::validate_transaction(@transaction, flags, utxo_hints);
assert!(res.is_ok(), "P2SH failed!:{:?}", res.unwrap_err());
}

Expand All @@ -60,9 +60,8 @@ fn test_p2sh_transaction_3() {
amount: 10000000, pubkey_script: hex_to_bytecode(@prevout_pubkey), block_height: 183729
};

let utxo_hints = array![prev_out];

let res = validate_p2sh(@transaction, 0, utxo_hints, 11);
let flags: u32 = ScriptFlags::ScriptBip16.into();
let res = validate::validate_transaction_at(@transaction, flags, prev_out, 11);
assert!(res.is_ok(), "P2SH failed!:{:?}", res.unwrap_err());
}

Expand All @@ -82,8 +81,8 @@ fn test_p2sh_transaction_4() {
};

let utxo_hints = array![prev_out];

let res = validate_p2sh(@transaction, 1, utxo_hints, 0);
let flags: u32 = ScriptFlags::ScriptBip16.into();
let res = validate::validate_transaction(@transaction, flags, utxo_hints);
assert!(res.is_ok(), "P2SH failed!:{:?}", res.unwrap_err());
}

Expand All @@ -103,8 +102,8 @@ fn test_p2sh_transaction_5() {
};

let utxo_hints = array![prev_out];

let res = validate_p2sh(@transaction, 0, utxo_hints, 0);
let flags: u32 = ScriptFlags::ScriptBip16.into();
let res = validate::validate_transaction(@transaction, flags, utxo_hints);
assert!(res.is_ok(), "P2SH failed!:{:?}", res.unwrap_err());
}

Expand All @@ -122,7 +121,7 @@ fn test_p2sh_transaction_6() {
amount: 100000000, pubkey_script: hex_to_bytecode(@prevout_pubkey), block_height: 257797
};

let utxo_hints = array![prev_out];
let res = validate_p2sh(@transaction, 0, utxo_hints, 0);
let flags: u32 = ScriptFlags::ScriptBip16.into();
let res = validate::validate_transaction_at(@transaction, flags, prev_out, 4);
assert!(res.is_ok(), "P2SH failed!:{:?}", res.unwrap_err());
}
83 changes: 0 additions & 83 deletions packages/tests/src/validate.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ use shinigami_engine::hash_cache::HashCacheImpl;
use shinigami_engine::transaction::EngineTransaction;
use shinigami_engine::opcodes::Opcode;
use crate::utxo::UTXO;
use crate::utils::find_last_index;
use shinigami_utils::hash::sha256_byte_array;
use shinigami_utils::bytecode::bytecode_to_hex;
use shinigami_utils::bytecode::hex_to_bytecode;
use shinigami_utils::byte_array::sub_byte_array;

// TODO: Move validate coinbase here

Expand Down Expand Up @@ -148,81 +143,3 @@ pub fn validate_p2ms(
Result::Ok(())
}
}


pub fn validate_p2sh(
tx: @EngineTransaction, flags: u32, utxo_hints: Array<UTXO>, indx: u32
) -> Result<(), felt252> {
if tx.transaction_inputs.len() == 0 {
return Result::Err('P2SH: No inputs');
}

let signature_script = bytecode_to_hex(tx.transaction_inputs[indx].signature_script);
let scriptSig_bytes = hex_to_bytecode(@signature_script);

let mut redeem_Script_start_index = 0;
let mut redeem_script_size = 0;
if scriptSig_bytes[0] == 0 || scriptSig_bytes[0] == 1 || scriptSig_bytes[0] == 2 {
//OP_0 OP_PushData <Sig> OP_PushData <RedeemScript> Standard locking scripts
if (flags == 0) {
redeem_Script_start_index = (2 + scriptSig_bytes[1] + 1).into();
} else if (flags == 1) {
redeem_Script_start_index =
(1
+ 1
+ scriptSig_bytes[1]
+ 1
+ scriptSig_bytes[(1 + 1 + scriptSig_bytes[1]).into()]
+ scriptSig_bytes[(1
+ 1
+ scriptSig_bytes[1]
+ scriptSig_bytes[(1 + 1 + scriptSig_bytes[1]).into()])
.into()]
+ 1)
.into();
}
redeem_script_size = (scriptSig_bytes.len()) - redeem_Script_start_index;
} else {
// non-standard locking script containing a mathematical puzzle
redeem_Script_start_index = find_last_index(scriptSig_bytes.clone());
redeem_script_size = (scriptSig_bytes.len()) - redeem_Script_start_index;
}

let redeem_script = sub_byte_array(
@scriptSig_bytes, ref redeem_Script_start_index, redeem_script_size
);
if redeem_script.len() == 0 {
return Result::Err('P2SH: Redeem Script size = 0');
}
if redeem_script.len() > 520 {
return Result::Err('P2SH: Redeem Script size > 520');
}

let hashed_redeem_script: ByteArray = ripemd160::ripemd160_hash(
@sha256_byte_array(@redeem_script)
)
.into();

let script_pubkey = utxo_hints[0].pubkey_script;
let mut script_hash_start_index = 2;
let script_hash: ByteArray = sub_byte_array(script_pubkey, ref script_hash_start_index, 20);

if hashed_redeem_script != script_hash {
return Result::Err('P2SH: Signature mismatch');
}

let hash_cache = HashCacheImpl::new(tx);
let mut engine = EngineImpl::new(
script_pubkey, tx, indx, flags, *utxo_hints[0].amount, @hash_cache
)
.unwrap();

let res = engine.execute();

if res.is_err() {
let err = res.unwrap_err();
Result::Err(err)
} else {
Result::Ok(())
}
}

0 comments on commit 888ee06

Please sign in to comment.