Remove unnecessary monero wallet trait abstractions

This commit is contained in:
Daniel Karzel 2021-03-03 15:42:26 +11:00
parent 1041212a60
commit d63790c2a6
8 changed files with 15 additions and 119 deletions

View File

@ -32,7 +32,7 @@ use swap::{
execution_params::GetExecutionParams, execution_params::GetExecutionParams,
fs::default_config_path, fs::default_config_path,
monero, monero,
monero::{Amount, OpenOrCreate}, monero::Amount,
protocol::alice::EventLoop, protocol::alice::EventLoop,
seed::Seed, seed::Seed,
trace::init_tracing, trace::init_tracing,

View File

@ -28,7 +28,6 @@ use swap::{
execution_params, execution_params,
execution_params::GetExecutionParams, execution_params::GetExecutionParams,
monero, monero,
monero::OpenOrCreate,
protocol::{ protocol::{
bob, bob,
bob::{cancel::CancelError, Builder, EventLoop}, bob::{cancel::CancelError, Builder, EventLoop},

View File

@ -9,8 +9,6 @@ pub use wallet_rpc::{WalletRpc, WalletRpcProcess};
use crate::bitcoin; use crate::bitcoin;
use ::bitcoin::hashes::core::fmt::Formatter; use ::bitcoin::hashes::core::fmt::Formatter;
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait;
use monero_rpc::wallet::{BlockHeight, Refreshed};
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use rust_decimal::{ use rust_decimal::{
prelude::{FromPrimitive, ToPrimitive}, prelude::{FromPrimitive, ToPrimitive},
@ -182,28 +180,6 @@ impl From<TxHash> for String {
} }
} }
#[async_trait]
pub trait Transfer {
async fn transfer(
&self,
public_spend_key: PublicKey,
public_view_key: PublicViewKey,
amount: Amount,
) -> Result<TransferProof>;
}
#[async_trait]
pub trait WatchForTransfer {
async fn watch_for_transfer(
&self,
public_spend_key: PublicKey,
public_view_key: PublicViewKey,
transfer_proof: TransferProof,
amount: Amount,
expected_confirmations: u32,
) -> Result<(), InsufficientFunds>;
}
#[derive(Debug, Clone, Copy, thiserror::Error)] #[derive(Debug, Clone, Copy, thiserror::Error)]
#[error("transaction does not pay enough: expected {expected}, got {actual}")] #[error("transaction does not pay enough: expected {expected}, got {actual}")]
pub struct InsufficientFunds { pub struct InsufficientFunds {
@ -217,51 +193,6 @@ pub struct BalanceTooLow {
pub balance: Amount, pub balance: Amount,
} }
#[async_trait]
pub trait CreateFromAndLoad {
async fn create_from_and_load(
&self,
private_spend_key: PrivateKey,
private_view_key: PrivateViewKey,
restore_height: BlockHeight,
) -> Result<()>;
}
#[async_trait]
pub trait CreateFrom {
async fn create_from(
&self,
private_spend_key: PrivateKey,
private_view_key: PrivateViewKey,
restore_height: BlockHeight,
) -> Result<()>;
}
#[async_trait]
pub trait OpenWallet {
async fn open(&self) -> Result<()>;
}
#[async_trait]
pub trait OpenOrCreate {
async fn open_or_create(&self) -> Result<()>;
}
#[async_trait]
pub trait WalletBlockHeight {
async fn block_height(&self) -> Result<BlockHeight>;
}
#[async_trait]
pub trait GetAddress {
async fn get_main_address(&self) -> Result<Address>;
}
#[async_trait]
pub trait Refresh {
async fn refresh(&self) -> Result<Refreshed>;
}
#[derive(thiserror::Error, Debug, Clone, PartialEq)] #[derive(thiserror::Error, Debug, Clone, PartialEq)]
#[error("Overflow, cannot convert {0} to u64")] #[error("Overflow, cannot convert {0} to u64")]
pub struct OverflowError(pub String); pub struct OverflowError(pub String);

View File

@ -1,10 +1,8 @@
use crate::monero::{ use crate::monero::{
Amount, CreateFrom, CreateFromAndLoad, InsufficientFunds, OpenOrCreate, OpenWallet, Amount, InsufficientFunds, PrivateViewKey, PublicViewKey, TransferProof, TxHash,
PrivateViewKey, PublicViewKey, Transfer, TransferProof, TxHash, WatchForTransfer,
}; };
use ::monero::{Address, Network, PrivateKey, PublicKey}; use ::monero::{Address, Network, PrivateKey, PublicKey};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait;
use backoff::{backoff::Constant as ConstantBackoff, future::retry}; use backoff::{backoff::Constant as ConstantBackoff, future::retry};
use bitcoin::hashes::core::sync::atomic::AtomicU32; use bitcoin::hashes::core::sync::atomic::AtomicU32;
use monero_rpc::{ use monero_rpc::{
@ -76,11 +74,8 @@ impl Wallet {
// Median tx fees on Monero as found here: https://www.monero.how/monero-transaction-fees, 0.000_015 * 2 (to be on the safe side) // Median tx fees on Monero as found here: https://www.monero.how/monero-transaction-fees, 0.000_015 * 2 (to be on the safe side)
Amount::from_monero(0.000_03f64).expect("static fee to be convertible without problems") Amount::from_monero(0.000_03f64).expect("static fee to be convertible without problems")
} }
}
#[async_trait] pub async fn transfer(
impl Transfer for Wallet {
async fn transfer(
&self, &self,
public_spend_key: PublicKey, public_spend_key: PublicKey,
public_view_key: PublicViewKey, public_view_key: PublicViewKey,
@ -108,11 +103,8 @@ impl Transfer for Wallet {
PrivateKey::from_str(&res.tx_key)?, PrivateKey::from_str(&res.tx_key)?,
)) ))
} }
}
#[async_trait] pub async fn create_from_and_load(
impl CreateFromAndLoad for Wallet {
async fn create_from_and_load(
&self, &self,
private_spend_key: PrivateKey, private_spend_key: PrivateKey,
private_view_key: PrivateViewKey, private_view_key: PrivateViewKey,
@ -140,11 +132,8 @@ impl CreateFromAndLoad for Wallet {
Ok(()) Ok(())
} }
}
#[async_trait] pub async fn create_from(
impl CreateFrom for Wallet {
async fn create_from(
&self, &self,
private_spend_key: PrivateKey, private_spend_key: PrivateKey,
private_view_key: PrivateViewKey, private_view_key: PrivateViewKey,
@ -174,11 +163,8 @@ impl CreateFrom for Wallet {
Ok(()) Ok(())
} }
}
#[async_trait] pub async fn open(&self) -> Result<()> {
impl OpenWallet for Wallet {
async fn open(&self) -> Result<()> {
self.inner self.inner
.lock() .lock()
.await .await
@ -186,11 +172,8 @@ impl OpenWallet for Wallet {
.await?; .await?;
Ok(()) Ok(())
} }
}
#[async_trait] pub async fn open_or_create(&self) -> Result<()> {
impl OpenOrCreate for Wallet {
async fn open_or_create(&self) -> Result<()> {
let open_wallet_response = self.open().await; let open_wallet_response = self.open().await;
if open_wallet_response.is_err() { if open_wallet_response.is_err() {
self.inner.lock().await.create_wallet(self.name.as_str()).await.context( self.inner.lock().await.create_wallet(self.name.as_str()).await.context(
@ -204,13 +187,8 @@ impl OpenOrCreate for Wallet {
Ok(()) Ok(())
} }
}
// TODO: For retry, use `backoff::ExponentialBackoff` in production as opposed pub async fn watch_for_transfer(
// to `ConstantBackoff`.
#[async_trait]
impl WatchForTransfer for Wallet {
async fn watch_for_transfer(
&self, &self,
public_spend_key: PublicKey, public_spend_key: PublicKey,
public_view_key: PublicViewKey, public_view_key: PublicViewKey,

View File

@ -6,7 +6,6 @@ use crate::{
}, },
execution_params::ExecutionParams, execution_params::ExecutionParams,
monero, monero,
monero::Transfer,
protocol::{ protocol::{
alice, alice,
alice::{event_loop::EventLoopHandle, TransferProof}, alice::{event_loop::EventLoopHandle, TransferProof},
@ -49,15 +48,12 @@ pub async fn wait_for_locked_bitcoin(
Ok(()) Ok(())
} }
pub async fn lock_xmr<W>( pub async fn lock_xmr(
bob_peer_id: PeerId, bob_peer_id: PeerId,
state3: alice::State3, state3: alice::State3,
event_loop_handle: &mut EventLoopHandle, event_loop_handle: &mut EventLoopHandle,
monero_wallet: Arc<W>, monero_wallet: Arc<monero::Wallet>,
) -> Result<()> ) -> Result<()> {
where
W: Transfer,
{
let S_a = monero::PublicKey::from_private_key(&monero::PrivateKey { scalar: state3.s_a }); let S_a = monero::PublicKey::from_private_key(&monero::PrivateKey { scalar: state3.s_a });
let public_spend_key = S_a + state3.S_b_monero; let public_spend_key = S_a + state3.S_b_monero;

View File

@ -7,7 +7,6 @@ use crate::{
database::Database, database::Database,
execution_params::ExecutionParams, execution_params::ExecutionParams,
monero, monero,
monero::CreateFrom,
monero_ext::ScalarExt, monero_ext::ScalarExt,
protocol::{ protocol::{
alice, alice,

View File

@ -316,15 +316,12 @@ pub struct State3 {
} }
impl State3 { impl State3 {
pub async fn watch_for_lock_xmr<W>( pub async fn watch_for_lock_xmr(
self, self,
xmr_wallet: &W, xmr_wallet: &monero::Wallet,
transfer_proof: TransferProof, transfer_proof: TransferProof,
monero_wallet_restore_blockheight: BlockHeight, monero_wallet_restore_blockheight: BlockHeight,
) -> Result<Result<State4, InsufficientFunds>> ) -> Result<Result<State4, InsufficientFunds>> {
where
W: monero::WatchForTransfer,
{
let S_b_monero = let S_b_monero =
monero::PublicKey::from_private_key(&monero::PrivateKey::from_scalar(self.s_b)); monero::PublicKey::from_private_key(&monero::PrivateKey::from_scalar(self.s_b));
let S = self.S_a_monero + S_b_monero; let S = self.S_a_monero + S_b_monero;
@ -572,10 +569,7 @@ pub struct State5 {
} }
impl State5 { impl State5 {
pub async fn claim_xmr<W>(&self, monero_wallet: &W) -> Result<()> pub async fn claim_xmr(&self, monero_wallet: &monero::Wallet) -> Result<()> {
where
W: monero::CreateFromAndLoad,
{
let s_b = monero::PrivateKey { scalar: self.s_b }; let s_b = monero::PrivateKey { scalar: self.s_b };
let s = self.s_a + s_b; let s = self.s_a + s_b;

View File

@ -22,7 +22,6 @@ use swap::{
execution_params, execution_params,
execution_params::{ExecutionParams, GetExecutionParams}, execution_params::{ExecutionParams, GetExecutionParams},
monero, monero,
monero::OpenWallet,
protocol::{alice, alice::AliceState, bob, bob::BobState}, protocol::{alice, alice::AliceState, bob, bob::BobState},
seed::Seed, seed::Seed,
}; };