mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-24 06:59:36 -05:00
Avoid carrying rng
This commit is contained in:
parent
25bf5df122
commit
e82383bcf6
@ -198,6 +198,7 @@ impl Builder {
|
||||
self.execution_params.bitcoin_punish_timelock,
|
||||
redeem_address,
|
||||
punish_address,
|
||||
rng,
|
||||
);
|
||||
|
||||
Ok(AliceState::Started { amounts, state0 })
|
||||
|
@ -16,7 +16,6 @@ use rand::{CryptoRng, RngCore};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::Sha256;
|
||||
use std::fmt;
|
||||
use tracing::info;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AliceState {
|
||||
@ -87,6 +86,7 @@ pub struct State0 {
|
||||
pub a: bitcoin::SecretKey,
|
||||
pub s_a: cross_curve_dleq::Scalar,
|
||||
pub v_a: monero::PrivateViewKey,
|
||||
pub dleq_proof_s_a: cross_curve_dleq::Proof,
|
||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||
pub btc: bitcoin::Amount,
|
||||
pub xmr: monero::Amount,
|
||||
@ -98,7 +98,7 @@ pub struct State0 {
|
||||
|
||||
impl State0 {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new(
|
||||
pub fn new<R>(
|
||||
a: bitcoin::SecretKey,
|
||||
s_a: cross_curve_dleq::Scalar,
|
||||
v_a: monero::PrivateViewKey,
|
||||
@ -108,11 +108,18 @@ impl State0 {
|
||||
punish_timelock: Timelock,
|
||||
redeem_address: bitcoin::Address,
|
||||
punish_address: bitcoin::Address,
|
||||
) -> Self {
|
||||
rng: &mut R,
|
||||
) -> Self
|
||||
where
|
||||
R: RngCore + CryptoRng,
|
||||
{
|
||||
let dleq_proof_s_a = cross_curve_dleq::Proof::new(rng, &s_a);
|
||||
|
||||
Self {
|
||||
a,
|
||||
s_a,
|
||||
v_a,
|
||||
dleq_proof_s_a,
|
||||
redeem_address,
|
||||
punish_address,
|
||||
btc,
|
||||
@ -122,17 +129,14 @@ impl State0 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_message<R: RngCore + CryptoRng>(&self, rng: &mut R) -> alice::Message0 {
|
||||
info!("Producing first message");
|
||||
let dleq_proof_s_a = cross_curve_dleq::Proof::new(rng, &self.s_a);
|
||||
|
||||
pub fn next_message(&self) -> alice::Message0 {
|
||||
alice::Message0 {
|
||||
A: self.a.public(),
|
||||
S_a_monero: monero::PublicKey::from_private_key(&monero::PrivateKey {
|
||||
scalar: self.s_a.into_ed25519(),
|
||||
}),
|
||||
S_a_bitcoin: self.s_a.into_secp256k1().into(),
|
||||
dleq_proof_s_a,
|
||||
dleq_proof_s_a: self.dleq_proof_s_a.clone(),
|
||||
v_a: self.v_a,
|
||||
redeem_address: self.redeem_address.clone(),
|
||||
punish_address: self.punish_address.clone(),
|
||||
|
@ -23,7 +23,6 @@ use futures::{
|
||||
pin_mut,
|
||||
};
|
||||
use libp2p::PeerId;
|
||||
use rand::rngs::OsRng;
|
||||
use sha2::Sha256;
|
||||
use std::sync::Arc;
|
||||
use tokio::time::timeout;
|
||||
@ -62,7 +61,7 @@ pub async fn negotiate(
|
||||
)
|
||||
.await??;
|
||||
|
||||
let alice_message0 = state0.next_message(&mut OsRng);
|
||||
let alice_message0 = state0.next_message();
|
||||
event_loop_handle
|
||||
.send_message0(channel, alice_message0)
|
||||
.await?;
|
||||
|
@ -74,6 +74,7 @@ pub struct State0 {
|
||||
b: bitcoin::SecretKey,
|
||||
s_b: cross_curve_dleq::Scalar,
|
||||
v_b: monero::PrivateViewKey,
|
||||
dleq_proof_s_b: cross_curve_dleq::Proof,
|
||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||
btc: bitcoin::Amount,
|
||||
xmr: monero::Amount,
|
||||
@ -97,6 +98,7 @@ impl State0 {
|
||||
|
||||
let s_b = cross_curve_dleq::Scalar::random(rng);
|
||||
let v_b = monero::PrivateViewKey::new_random(rng);
|
||||
let dleq_proof_s_b = cross_curve_dleq::Proof::new(rng, &s_b);
|
||||
|
||||
Self {
|
||||
b,
|
||||
@ -104,6 +106,7 @@ impl State0 {
|
||||
v_b,
|
||||
btc,
|
||||
xmr,
|
||||
dleq_proof_s_b,
|
||||
cancel_timelock,
|
||||
punish_timelock,
|
||||
refund_address,
|
||||
@ -111,16 +114,14 @@ impl State0 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_message<R: RngCore + CryptoRng>(&self, rng: &mut R) -> bob::Message0 {
|
||||
let dleq_proof_s_b = cross_curve_dleq::Proof::new(rng, &self.s_b);
|
||||
|
||||
pub fn next_message(&self) -> bob::Message0 {
|
||||
bob::Message0 {
|
||||
B: self.b.public(),
|
||||
S_b_monero: monero::PublicKey::from_private_key(&monero::PrivateKey {
|
||||
scalar: self.s_b.into_ed25519(),
|
||||
}),
|
||||
S_b_bitcoin: self.s_b.into_secp256k1().into(),
|
||||
dleq_proof_s_b,
|
||||
dleq_proof_s_b: self.dleq_proof_s_b.clone(),
|
||||
v_b: self.v_b,
|
||||
refund_address: self.refund_address.clone(),
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ use crate::{
|
||||
};
|
||||
use anyhow::{bail, Result};
|
||||
use async_recursion::async_recursion;
|
||||
use rand::{rngs::OsRng, CryptoRng, RngCore};
|
||||
use std::sync::Arc;
|
||||
use tokio::select;
|
||||
use tracing::info;
|
||||
@ -44,7 +43,6 @@ pub async fn run_until(
|
||||
swap.db,
|
||||
swap.bitcoin_wallet,
|
||||
swap.monero_wallet,
|
||||
OsRng,
|
||||
swap.swap_id,
|
||||
swap.execution_params,
|
||||
)
|
||||
@ -54,20 +52,16 @@ pub async fn run_until(
|
||||
// State machine driver for swap execution
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[async_recursion]
|
||||
async fn run_until_internal<R>(
|
||||
async fn run_until_internal(
|
||||
state: BobState,
|
||||
is_target_state: fn(&BobState) -> bool,
|
||||
mut event_loop_handle: EventLoopHandle,
|
||||
db: Database,
|
||||
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||
monero_wallet: Arc<monero::Wallet>,
|
||||
mut rng: R,
|
||||
swap_id: Uuid,
|
||||
execution_params: ExecutionParams,
|
||||
) -> Result<BobState>
|
||||
where
|
||||
R: RngCore + CryptoRng + Send,
|
||||
{
|
||||
) -> Result<BobState> {
|
||||
info!("Current state: {}", state);
|
||||
if is_target_state(&state) {
|
||||
Ok(state)
|
||||
@ -80,7 +74,6 @@ where
|
||||
state0,
|
||||
amounts,
|
||||
&mut event_loop_handle,
|
||||
&mut rng,
|
||||
bitcoin_wallet.clone(),
|
||||
)
|
||||
.await?;
|
||||
@ -95,7 +88,6 @@ where
|
||||
db,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
rng,
|
||||
swap_id,
|
||||
execution_params,
|
||||
)
|
||||
@ -117,7 +109,6 @@ where
|
||||
db,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
rng,
|
||||
swap_id,
|
||||
execution_params,
|
||||
)
|
||||
@ -170,7 +161,6 @@ where
|
||||
db,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
rng,
|
||||
swap_id,
|
||||
execution_params,
|
||||
)
|
||||
@ -217,7 +207,6 @@ where
|
||||
db,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
rng,
|
||||
swap_id,
|
||||
execution_params,
|
||||
)
|
||||
@ -260,7 +249,6 @@ where
|
||||
db,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
rng,
|
||||
swap_id,
|
||||
execution_params,
|
||||
)
|
||||
@ -296,7 +284,6 @@ where
|
||||
db,
|
||||
bitcoin_wallet.clone(),
|
||||
monero_wallet,
|
||||
rng,
|
||||
swap_id,
|
||||
execution_params,
|
||||
)
|
||||
@ -318,7 +305,6 @@ where
|
||||
db,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
rng,
|
||||
swap_id,
|
||||
execution_params,
|
||||
)
|
||||
@ -344,7 +330,6 @@ where
|
||||
db,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
rng,
|
||||
swap_id,
|
||||
execution_params,
|
||||
)
|
||||
@ -376,7 +361,6 @@ where
|
||||
db,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
rng,
|
||||
swap_id,
|
||||
execution_params,
|
||||
)
|
||||
@ -390,16 +374,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn negotiate<R>(
|
||||
pub async fn negotiate(
|
||||
state0: crate::protocol::bob::state::State0,
|
||||
amounts: SwapAmounts,
|
||||
swarm: &mut EventLoopHandle,
|
||||
mut rng: R,
|
||||
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
|
||||
) -> Result<bob::state::State2>
|
||||
where
|
||||
R: RngCore + CryptoRng + Send,
|
||||
{
|
||||
) -> Result<bob::state::State2> {
|
||||
tracing::trace!("Starting negotiate");
|
||||
swarm
|
||||
.send_swap_request(SwapRequest {
|
||||
@ -411,7 +391,7 @@ where
|
||||
// argument.
|
||||
let _swap_response = swarm.recv_swap_response().await?;
|
||||
|
||||
swarm.send_message0(state0.next_message(&mut rng)).await?;
|
||||
swarm.send_message0(state0.next_message()).await?;
|
||||
let msg0 = swarm.recv_message0().await?;
|
||||
let state1 = state0.receive(bitcoin_wallet.as_ref(), msg0).await?;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user