Skip to content

Commit

Permalink
Operator BitVM assert tx Winternitz public key generation (#379)
Browse files Browse the repository at this point in the history
* operator: Add get_winternitz_public_keys.

* operator: Remove watchtower index in get_winternitz_public_keys.

* operator: Update test_utls function in test.
  • Loading branch information
ceyhunsen authored Dec 22, 2024
1 parent 086f108 commit 7f09b9a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use bitvm::signatures::winternitz::{
pub enum TxType {
TimeTx,
KickoffTx,
BitVM,
}

/// Derivation path specification for Winternitz one time public key generation.
Expand Down
46 changes: 45 additions & 1 deletion core/src/operator.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Vec<winternitz::PublicKey>>`]: Winternitz public keys for
/// `watchtower index` row and `BitVM assert tx index` column.
pub fn get_winternitz_public_keys(&self) -> Result<Vec<winternitz::PublicKey>, 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)]
Expand Down Expand Up @@ -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);
}
}
11 changes: 9 additions & 2 deletions core/src/rpc/operator.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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::<Vec<_>>();

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,
};
Expand Down
2 changes: 0 additions & 2 deletions core/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>`
/// type.
///
Expand Down

0 comments on commit 7f09b9a

Please sign in to comment.