Skip to content

Commit

Permalink
add dedicated routine for key agg cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rajarshimaitra committed Apr 13, 2024
1 parent 26e9887 commit d9f5515
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/swaps/bitcoinv2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ impl BtcSwapScriptV2 {
})
}

pub fn musig_keyagg_cache(&self) -> MusigKeyAggCache {
match self.swap_type {
SwapType::Submarine => {
let pubkeys = [self.receiver_pubkey.inner, self.sender_pubkey.inner];
MusigKeyAggCache::new(&Secp256k1::new(), &pubkeys)
}

SwapType::ReverseSubmarine => {
let pubkeys = [self.sender_pubkey.inner, self.receiver_pubkey.inner];
MusigKeyAggCache::new(&Secp256k1::new(), &pubkeys)
}
}
}

/// Create the struct from a reverse swap create request.
pub fn reverse_from_swap_resp(
reverse_response: &ReverseResp,
Expand Down Expand Up @@ -219,7 +233,7 @@ impl BtcSwapScriptV2 {
// Setup Key Aggregation cache
let pubkeys = [self.receiver_pubkey.inner, self.sender_pubkey.inner];

let mut key_agg_cache = MusigKeyAggCache::new(&secp, &pubkeys);
let mut key_agg_cache = self.musig_keyagg_cache();

// Construct the Taproot
let internal_key = key_agg_cache.agg_pk();
Expand Down Expand Up @@ -414,7 +428,7 @@ impl BtcSwapTxV2 {
self.swap_script.sender_pubkey.inner,
];

let mut key_agg_cache = MusigKeyAggCache::new(&secp, &pubkeys);
let mut key_agg_cache = self.swap_script.musig_keyagg_cache();

let tweak = SecretKey::from_slice(
self.swap_script
Expand Down Expand Up @@ -553,13 +567,7 @@ impl BtcSwapTxV2 {

// Step 2: Get the Public and Secret nonces

let mut key_agg_cache = MusigKeyAggCache::new(
&secp,
&[
self.swap_script.receiver_pubkey.inner,
self.swap_script.sender_pubkey.inner,
],
);
let mut key_agg_cache = self.swap_script.musig_keyagg_cache();

let tweak = SecretKey::from_slice(
self.swap_script
Expand Down
4 changes: 3 additions & 1 deletion tests/bitcoin_v2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::FromStr;
use std::{str::FromStr, time::Duration};

use boltz_client::{
network::electrum::ElectrumConfig,
Expand Down Expand Up @@ -262,6 +262,8 @@ fn bitcoin_v2_reverse() {
if update.status == "transaction.mempool" {
log::info!("Boltz broadcasted funding tx");

std::thread::sleep(Duration::from_secs(30));

let claim_tx = BtcSwapTxV2::new_claim(
swap_script.clone(),
claim_address.clone(),
Expand Down

0 comments on commit d9f5515

Please sign in to comment.