Remove rng argument

This commit is contained in:
Franck Royer 2020-11-25 14:37:37 +11:00 committed by rishflab
parent 6c0df836ca
commit 66866f8fbd
2 changed files with 36 additions and 33 deletions

View File

@ -43,7 +43,7 @@ use xmr_btc::{
TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction, TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction,
WatchForTransactionFinality, WatchForTransactionFinality,
}, },
bob, bob, cross_curve_dleq,
monero::{CreateWalletForOutput, Transfer}, monero::{CreateWalletForOutput, Transfer},
}; };
@ -53,11 +53,18 @@ mod message1;
mod message2; mod message2;
mod message3; mod message3;
trait Rng: RngCore + CryptoRng + Send {}
impl<T> Rng for T where T: RngCore + CryptoRng + Send {}
// The same data structure is used for swap execution and recovery. // The same data structure is used for swap execution and recovery.
// This allows for a seamless transition from a failed swap to recovery. // This allows for a seamless transition from a failed swap to recovery.
pub enum AliceState { pub enum AliceState {
Started { Started {
amounts: SwapAmounts, amounts: SwapAmounts,
a: bitcoin::SecretKey,
s_a: cross_curve_dleq::Scalar,
v_a: monero::PrivateViewKey,
}, },
Negotiated { Negotiated {
swap_id: Uuid, swap_id: Uuid,
@ -107,21 +114,20 @@ pub enum AliceState {
// State machine driver for swap execution // State machine driver for swap execution
#[async_recursion] #[async_recursion]
pub async fn simple_swap<R>( pub async fn simple_swap(
state: AliceState, state: AliceState,
// TODO: Would it make it better if it's in the `Started` enum variant so we don't carry it
// along?
rng: &mut R,
mut swarm: Swarm, mut swarm: Swarm,
db: Database, db: Database,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>, bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>, monero_wallet: Arc<crate::monero::Wallet>,
) -> Result<AliceState> ) -> Result<AliceState> {
where
R: RngCore + CryptoRng + Send,
{
match state { match state {
AliceState::Started { amounts } => { AliceState::Started {
amounts,
a,
s_a,
v_a,
} => {
// Bob dials us // Bob dials us
let bob_peer_id = match swarm.next().await { let bob_peer_id = match swarm.next().await {
OutEvent::ConnectionEstablished(bob_peer_id) => bob_peer_id, OutEvent::ConnectionEstablished(bob_peer_id) => bob_peer_id,
@ -149,7 +155,9 @@ where
let punish_address = redeem_address.clone(); let punish_address = redeem_address.clone();
let state0 = State0::new( let state0 = State0::new(
rng, a,
s_a,
v_a,
btc, btc,
xmr, xmr,
REFUND_TIMELOCK, REFUND_TIMELOCK,
@ -204,7 +212,6 @@ where
channel, channel,
amounts, amounts,
}, },
rng,
swarm, swarm,
db, db,
bitcoin_wallet, bitcoin_wallet,
@ -238,7 +245,6 @@ where
amounts, amounts,
state3, state3,
}, },
rng,
swarm, swarm,
db, db,
bitcoin_wallet, bitcoin_wallet,
@ -280,7 +286,6 @@ where
simple_swap( simple_swap(
AliceState::XmrLocked { state3 }, AliceState::XmrLocked { state3 },
rng,
swarm, swarm,
db, db,
bitcoin_wallet, bitcoin_wallet,
@ -312,7 +317,6 @@ where
simple_swap( simple_swap(
AliceState::WaitingToCancel { state3 }, AliceState::WaitingToCancel { state3 },
rng,
swarm, swarm,
db, db,
bitcoin_wallet, bitcoin_wallet,
@ -325,7 +329,6 @@ where
simple_swap( simple_swap(
AliceState::WaitingToCancel { state3 }, AliceState::WaitingToCancel { state3 },
rng,
swarm, swarm,
db, db,
bitcoin_wallet, bitcoin_wallet,
@ -341,7 +344,6 @@ where
state3, state3,
encrypted_signature, encrypted_signature,
}, },
rng,
swarm, swarm,
db, db,
bitcoin_wallet, bitcoin_wallet,
@ -394,7 +396,6 @@ where
simple_swap( simple_swap(
AliceState::BtcRedeemed, AliceState::BtcRedeemed,
rng,
swarm, swarm,
db, db,
bitcoin_wallet, bitcoin_wallet,
@ -439,7 +440,6 @@ where
simple_swap( simple_swap(
AliceState::BtcCancelled { state3, tx_cancel }, AliceState::BtcCancelled { state3, tx_cancel },
rng,
swarm, swarm,
db, db,
bitcoin_wallet, bitcoin_wallet,
@ -467,7 +467,6 @@ where
Either::Left(_) => { Either::Left(_) => {
simple_swap( simple_swap(
AliceState::BtcPunishable { tx_refund, state3 }, AliceState::BtcPunishable { tx_refund, state3 },
rng,
swarm, swarm,
db, db,
bitcoin_wallet.clone(), bitcoin_wallet.clone(),
@ -482,7 +481,6 @@ where
published_refund_tx, published_refund_tx,
state3, state3,
}, },
rng,
swarm, swarm,
db, db,
bitcoin_wallet.clone(), bitcoin_wallet.clone(),
@ -553,7 +551,6 @@ where
punished_tx_id, punished_tx_id,
state3, state3,
}, },
rng,
swarm, swarm,
db, db,
bitcoin_wallet.clone(), bitcoin_wallet.clone(),
@ -577,7 +574,6 @@ where
Either::Left(_) => { Either::Left(_) => {
simple_swap( simple_swap(
AliceState::Punished, AliceState::Punished,
rng,
swarm, swarm,
db, db,
bitcoin_wallet.clone(), bitcoin_wallet.clone(),
@ -592,7 +588,6 @@ where
published_refund_tx, published_refund_tx,
state3, state3,
}, },
rng,
swarm, swarm,
db, db,
bitcoin_wallet.clone(), bitcoin_wallet.clone(),
@ -677,8 +672,13 @@ pub async fn swap(
// TODO: Pass this in using <R: RngCore + CryptoRng> // TODO: Pass this in using <R: RngCore + CryptoRng>
let rng = &mut OsRng; let rng = &mut OsRng;
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 state = State0::new( let state = State0::new(
rng, a,
s_a,
v_a,
btc, btc,
xmr, xmr,
REFUND_TIMELOCK, REFUND_TIMELOCK,

View File

@ -416,8 +416,14 @@ impl State {
redeem_address: bitcoin::Address, redeem_address: bitcoin::Address,
punish_address: bitcoin::Address, punish_address: bitcoin::Address,
) -> Self { ) -> Self {
let a = bitcoin::SecretKey::new_random(rng);
let s_a = cross_curve_dleq::Scalar::random(rng);
let v_a = monero::PrivateViewKey::new_random(rng);
Self::State0(State0::new( Self::State0(State0::new(
rng, a,
s_a,
v_a,
btc, btc,
xmr, xmr,
refund_timelock, refund_timelock,
@ -443,8 +449,10 @@ pub struct State0 {
} }
impl State0 { impl State0 {
pub fn new<R: RngCore + CryptoRng>( pub fn new(
rng: &mut R, a: bitcoin::SecretKey,
s_a: cross_curve_dleq::Scalar,
v_a: monero::PrivateViewKey,
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -452,11 +460,6 @@ impl State0 {
redeem_address: bitcoin::Address, redeem_address: bitcoin::Address,
punish_address: bitcoin::Address, punish_address: bitcoin::Address,
) -> Self { ) -> Self {
let a = bitcoin::SecretKey::new_random(rng);
let s_a = cross_curve_dleq::Scalar::random(rng);
let v_a = monero::PrivateViewKey::new_random(rng);
Self { Self {
a, a,
s_a, s_a,