diff --git a/core/src/operator.rs b/core/src/operator.rs index dec65c9d..c8612e3b 100644 --- a/core/src/operator.rs +++ b/core/src/operator.rs @@ -11,6 +11,7 @@ use crate::{script_builder, utils, EVMAddress, UTXO}; use bitcoin::address::NetworkUnchecked; use bitcoin::consensus::deserialize; use bitcoin::hashes::Hash; +use bitcoin::script::PushBytesBuf; use bitcoin::sighash::SighashCache; use bitcoin::{Address, OutPoint, TapSighash, Transaction, TxOut, Txid}; use bitcoin_mock_rpc::RpcApiWrapper; @@ -266,7 +267,9 @@ where &Message::from_digest_slice(sighash.as_byte_array()).expect("should be hash"), &user_xonly_pk, )?; - let op_return_txout = script_builder::op_return_txout(self.idx.to_be_bytes()); + let mut push_bytes = PushBytesBuf::new(); + push_bytes.extend_from_slice(&utils::usize_to_var_len_bytes(self.idx)).unwrap(); + let op_return_txout = script_builder::op_return_txout(push_bytes); tx.output.push(op_return_txout.clone()); let funded_tx = self .rpc diff --git a/core/src/transaction_builder.rs b/core/src/transaction_builder.rs index 3e1b2d0d..d9c636e6 100644 --- a/core/src/transaction_builder.rs +++ b/core/src/transaction_builder.rs @@ -290,13 +290,7 @@ impl TransactionBuilder { network, ); let mut op_return_script: Vec = hex::decode(move_txid.to_string()).unwrap(); - let usize_bytes = (usize::BITS / 8) as usize; - let bits = operator_idx.max(1).ilog2() + 1; - let len = ((bits + 7) / 8) as usize; - let empty = usize_bytes - len; - let op_idx_bytes = operator_idx.to_be_bytes(); - let op_idx_bytes = &op_idx_bytes[empty..]; - op_return_script.extend(op_idx_bytes); + op_return_script.extend(utils::usize_to_var_len_bytes(operator_idx)); let mut push_bytes = PushBytesBuf::new(); push_bytes.extend_from_slice(&op_return_script).unwrap(); let op_return_txout = script_builder::op_return_txout(push_bytes); diff --git a/core/src/utils.rs b/core/src/utils.rs index 49359110..faa28a2a 100644 --- a/core/src/utils.rs +++ b/core/src/utils.rs @@ -58,6 +58,16 @@ pub fn parse_hex_to_btc_tx( } } +pub fn usize_to_var_len_bytes(x: usize) -> Vec { + let usize_bytes = (usize::BITS / 8) as usize; + let bits = x.max(1).ilog2() + 1; + let len = ((bits + 7) / 8) as usize; + let empty = usize_bytes - len; + let op_idx_bytes = x.to_be_bytes(); + let op_idx_bytes = &op_idx_bytes[empty..]; + op_idx_bytes.to_vec() +} + pub fn handle_taproot_witness_new>( tx: &mut TxHandler, witness_elements: &[T], diff --git a/core/tests/flow.rs b/core/tests/flow.rs index 7af4ce4f..d31fe92a 100644 --- a/core/tests/flow.rs +++ b/core/tests/flow.rs @@ -52,15 +52,17 @@ async fn test_honest_operator_takes_refund() { .new_withdrawal_sig_rpc(0, user_sig, empty_utxo, withdrawal_tx_out) .await .unwrap(); - println!("{:?}", withdrawal_provide_txid); + println!("Withdrawal provide: {:?}", withdrawal_provide_txid); let txs_to_be_sent = operators[0] .0 .withdrawal_proved_on_citrea_rpc(0, deposit_outpoint) .await .unwrap(); + tracing::debug!("txs_to_be_sent: {:#?}", txs_to_be_sent); for tx in txs_to_be_sent.iter().take(txs_to_be_sent.len() - 1) { - rpc.send_raw_transaction(tx.clone()).unwrap(); + let outpoint = rpc.send_raw_transaction(tx.clone()).unwrap(); + tracing::debug!("outpoint: {:#?}", outpoint); } rpc.mine_blocks(OPERATOR_TAKES_AFTER as u64).unwrap(); // send the last tx