Skip to content

Commit

Permalink
fix: multisig witness implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ptisserand committed Nov 6, 2024
1 parent d509dfe commit 46e17db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
22 changes: 17 additions & 5 deletions packages/engine/src/opcodes/crypto.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::opcodes::utils;
use crate::scriptnum::ScriptNum;
use crate::errors::Error;
use crate::taproot::TaprootContextTrait;
use crate::hash_cache::SigHashMidstateTrait;

const MAX_KEYS_PER_MULTISIG: i64 = 20;
const BASE_SEGWIT_VERSION: i64 = 0;
Expand Down Expand Up @@ -213,8 +214,7 @@ pub fn opcode_checkmultisig<
}

let mut script = engine.sub_script();

if (engine.is_witness_active(0)) {
if (engine.is_witness_active(BASE_SEGWIT_VERSION)) {
let mut s: u32 = 0;
let end = sigs.len();
while s != end {
Expand Down Expand Up @@ -250,9 +250,21 @@ pub fn opcode_checkmultisig<
}

let (parsed_pub_key, parsed_sig, hash_type) = res.unwrap();
let sig_hash: u256 = sighash::calc_signature_hash(
@script, hash_type, engine.transaction, engine.tx_idx
);
let mut sig_hash: u256 = 0;

let transaction = engine.transaction;
let tx_idx = engine.tx_idx;
let amount = engine.amount;

if engine.is_witness_active(BASE_SEGWIT_VERSION) {
let sig_hashes = SigHashMidstateTrait::new(transaction);
sig_hash =
sighash::calc_witness_signature_hash(
@script, @sig_hashes, hash_type, transaction, tx_idx, amount
);
} else {
sig_hash = sighash::calc_signature_hash(@script, hash_type, transaction, tx_idx);
};

if is_valid_signature(sig_hash, parsed_sig.r, parsed_sig.s, parsed_pub_key) {
sig_idx += 1;
Expand Down
1 change: 0 additions & 1 deletion packages/tests/src/tests/test_p2wsh.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use shinigami_utils::bytecode::hex_to_bytecode;

// P2WSH with P2MS
// https://learnmeabitcoin.com/explorer/tx/b38a88b073743bcc84170071cff4b68dec6fb5dc0bc8ffcb3d4ca632c2c78255
#[ignore]
#[test]
fn test_learnmeabitcoin_usage() {
let prevout_pk_script =
Expand Down

0 comments on commit 46e17db

Please sign in to comment.