From fd084b764da54bcf52b2f9891def898b921d5a29 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 4 Feb 2021 17:01:08 +1100 Subject: [PATCH] Move generation of keys inside `State0::new` The event loop will now use this function so I want to simplify its usage to avoid having to instantiate too many items to use it. --- swap/src/protocol/alice.rs | 18 ++++-------------- swap/src/protocol/alice/state.rs | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index 9f741412..2bd30db8 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -175,30 +175,20 @@ impl Builder { btc_to_swap: bitcoin::Amount, xmr_to_swap: monero::Amount, ) -> Result { - let rng = &mut OsRng; - let amounts = SwapAmounts { btc: btc_to_swap, xmr: xmr_to_swap, }; - let a = bitcoin::SecretKey::new_random(rng); - let s_a = cross_curve_dleq::Scalar::random(rng); - let v_a = monero::PrivateViewKey::new_random(rng); - let redeem_address = self.bitcoin_wallet.new_address().await?; - let punish_address = redeem_address.clone(); let state0 = State0::new( - a, - s_a, - v_a, amounts.btc, amounts.xmr, self.execution_params.bitcoin_cancel_timelock, self.execution_params.bitcoin_punish_timelock, - redeem_address, - punish_address, - rng, - ); + self.bitcoin_wallet.as_ref(), + &mut OsRng, + ) + .await?; Ok(AliceState::Started { amounts, state0 }) } diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index a6bbf21f..2e8b4dd5 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -101,25 +101,25 @@ pub struct State0 { } impl State0 { - #[allow(clippy::too_many_arguments)] - pub fn new( - a: bitcoin::SecretKey, - s_a: cross_curve_dleq::Scalar, - v_a: monero::PrivateViewKey, + pub async fn new( btc: bitcoin::Amount, xmr: monero::Amount, cancel_timelock: Timelock, punish_timelock: Timelock, - redeem_address: bitcoin::Address, - punish_address: bitcoin::Address, + bitcoin_wallet: &bitcoin::Wallet, rng: &mut R, - ) -> Self + ) -> Result where R: RngCore + CryptoRng, { + let a = bitcoin::SecretKey::new_random(rng); + let s_a = cross_curve_dleq::Scalar::random(rng); + let v_a = monero::PrivateViewKey::new_random(rng); + let redeem_address = bitcoin_wallet.new_address().await?; + let punish_address = redeem_address.clone(); let dleq_proof_s_a = cross_curve_dleq::Proof::new(rng, &s_a); - Self { + Ok(Self { a, s_a, v_a, @@ -130,7 +130,7 @@ impl State0 { xmr, cancel_timelock, punish_timelock, - } + }) } pub fn receive(self, msg: Message0) -> Result {