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,
fs::default_config_path,
monero,
monero::{Amount, OpenOrCreate},
monero::Amount,
protocol::alice::EventLoop,
seed::Seed,
trace::init_tracing,

View File

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

View File

@ -9,8 +9,6 @@ pub use wallet_rpc::{WalletRpc, WalletRpcProcess};
use crate::bitcoin;
use ::bitcoin::hashes::core::fmt::Formatter;
use anyhow::Result;
use async_trait::async_trait;
use monero_rpc::wallet::{BlockHeight, Refreshed};
use rand::{CryptoRng, RngCore};
use rust_decimal::{
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)]
#[error("transaction does not pay enough: expected {expected}, got {actual}")]
pub struct InsufficientFunds {
@ -217,51 +193,6 @@ pub struct BalanceTooLow {
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)]
#[error("Overflow, cannot convert {0} to u64")]
pub struct OverflowError(pub String);

View File

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

View File

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

View File

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

View File

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

View File

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