From 7f09b9a02ade7a65061c62d97fdcab1d147cf13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Sun, 22 Dec 2024 21:32:57 +0300 Subject: [PATCH] Operator BitVM assert tx Winternitz public key generation (#379) * operator: Add get_winternitz_public_keys. * operator: Remove watchtower index in get_winternitz_public_keys. * operator: Update test_utls function in test. --- core/src/actor.rs | 1 + core/src/operator.rs | 46 +++++++++++++++++++++++++++++++++++++++- core/src/rpc/operator.rs | 11 ++++++++-- core/src/test_utils.rs | 2 -- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/core/src/actor.rs b/core/src/actor.rs index 9bee0289..596b4992 100644 --- a/core/src/actor.rs +++ b/core/src/actor.rs @@ -18,6 +18,7 @@ use bitvm::signatures::winternitz::{ pub enum TxType { TimeTx, KickoffTx, + BitVM, } /// Derivation path specification for Winternitz one time public key generation. diff --git a/core/src/operator.rs b/core/src/operator.rs index 90f7d155..d6b7d647 100644 --- a/core/src/operator.rs +++ b/core/src/operator.rs @@ -1,4 +1,4 @@ -use crate::actor::Actor; +use crate::actor::{Actor, WinternitzDerivationPath}; use crate::builder::transaction::KICKOFF_UTXO_AMOUNT_SATS; use crate::builder::{self}; use crate::config::BridgeConfig; @@ -15,6 +15,7 @@ use bitcoin::script::PushBytesBuf; use bitcoin::sighash::SighashCache; use bitcoin::{Address, Amount, OutPoint, TapSighash, Transaction, TxOut, Txid}; use bitcoincore_rpc::{RawTx, RpcApi}; +use bitvm::signatures::winternitz; use jsonrpsee::core::client::ClientT; use jsonrpsee::http_client::HttpClientBuilder; use jsonrpsee::rpc_params; @@ -695,6 +696,33 @@ impl Operator { Ok(txs_to_be_sent) } + + /// Generates Winternitz public keys for every watchtower challenge and + /// BitVM assert tx. + /// + /// # Returns + /// + /// - [`Vec>`]: Winternitz public keys for + /// `watchtower index` row and `BitVM assert tx index` column. + pub fn get_winternitz_public_keys(&self) -> Result, BridgeError> { + let mut winternitz_pubkeys = Vec::new(); + + for time_tx in 0..self.config.num_time_txs as u32 { + let path = WinternitzDerivationPath { + message_length: 480, + log_d: 4, + tx_type: crate::actor::TxType::BitVM, + index: Some(self.idx as u32), + operator_idx: None, + watchtower_idx: None, + time_tx_idx: Some(time_tx), + }; + + winternitz_pubkeys.push(self.signer.derive_winternitz_pk(path)?); + } + + Ok(winternitz_pubkeys) + } } #[cfg(test)] @@ -776,4 +804,20 @@ mod tests { config.operator_withdrawal_fee_sats.unwrap() - Amount::from_sat(1) )); } + + #[tokio::test] + async fn get_winternitz_public_keys() { + let config = create_test_config_with_thread_name!(None); + let rpc = ExtendedRpc::new( + config.bitcoin_rpc_url.clone(), + config.bitcoin_rpc_user.clone(), + config.bitcoin_rpc_password.clone(), + ) + .await; + + let operator = Operator::new(config.clone(), rpc).await.unwrap(); + + let winternitz_public_key = operator.get_winternitz_public_keys().unwrap(); + assert_eq!(winternitz_public_key.len(), config.num_time_txs); + } } diff --git a/core/src/rpc/operator.rs b/core/src/rpc/operator.rs index 1e7ddc0a..4d1a7404 100644 --- a/core/src/rpc/operator.rs +++ b/core/src/rpc/operator.rs @@ -1,7 +1,7 @@ use super::clementine::{ self, clementine_operator_server::ClementineOperator, DepositSignSession, Empty, NewWithdrawalSigParams, NewWithdrawalSigResponse, OperatorBurnSig, OperatorParams, - WithdrawalFinalizedParams, + WinternitzPubkey, WithdrawalFinalizedParams, }; use crate::{builder, errors::BridgeError, operator::Operator}; use bitcoin::{hashes::Hash, Amount, OutPoint}; @@ -52,9 +52,16 @@ impl ClementineOperator for Operator { .collect() .await; + // Generate Winternitz public keys and convert them to RPC type. + let winternitz_pubkeys = self.get_winternitz_public_keys()?; + let winternitz_pubkeys = winternitz_pubkeys + .into_iter() + .map(WinternitzPubkey::from_bitvm) + .collect::>(); + let operator_params = clementine::OperatorParams { operator_details: Some(operator_config), - winternitz_pubkeys: vec![], // TODO: Implement this. + winternitz_pubkeys, assert_empty_public_key: vec![], // TODO: Implement this. timeout_tx_sigs, }; diff --git a/core/src/test_utils.rs b/core/src/test_utils.rs index ddb87fe7..824bfe1f 100644 --- a/core/src/test_utils.rs +++ b/core/src/test_utils.rs @@ -9,8 +9,6 @@ /// /// # Parameters /// -/// - `config_file`: Test configuration file in `str` type. Rest of the config -/// will be read from here and only `db_name` will be overwritten. /// - `suffix`: Optional suffix added to the thread handle in `Option` /// type. ///