2020-10-14 22:10:31 -04:00
|
|
|
use anyhow::Result;
|
2020-09-28 02:18:50 -04:00
|
|
|
use async_trait::async_trait;
|
2020-10-21 19:57:42 -04:00
|
|
|
use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _};
|
2020-10-19 21:18:27 -04:00
|
|
|
use monero_harness::rpc::wallet;
|
2020-11-30 15:41:22 -05:00
|
|
|
use std::{str::FromStr, time::Duration};
|
2020-09-29 01:36:50 -04:00
|
|
|
use xmr_btc::monero::{
|
2020-11-30 15:41:22 -05:00
|
|
|
Address, Amount, CreateWalletForOutput, InsufficientFunds, Network, PrivateKey, PrivateViewKey,
|
|
|
|
PublicKey, PublicViewKey, Transfer, TransferProof, TxHash, WatchForTransfer,
|
2020-09-29 01:36:50 -04:00
|
|
|
};
|
2020-09-28 02:18:50 -04:00
|
|
|
|
2020-11-30 15:41:22 -05:00
|
|
|
pub struct Wallet(pub wallet::Client);
|
2020-09-28 02:18:50 -04:00
|
|
|
|
2020-10-21 19:57:42 -04:00
|
|
|
impl Wallet {
|
|
|
|
/// Get the balance of the primary account.
|
|
|
|
pub async fn get_balance(&self) -> Result<Amount> {
|
2020-11-30 15:41:22 -05:00
|
|
|
let amount = self.0.get_balance(0).await?;
|
2020-10-21 19:57:42 -04:00
|
|
|
|
|
|
|
Ok(Amount::from_piconero(amount))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 02:18:50 -04:00
|
|
|
#[async_trait]
|
2020-10-19 21:18:27 -04:00
|
|
|
impl Transfer for Wallet {
|
2020-09-28 02:18:50 -04:00
|
|
|
async fn transfer(
|
|
|
|
&self,
|
|
|
|
public_spend_key: PublicKey,
|
|
|
|
public_view_key: PublicViewKey,
|
|
|
|
amount: Amount,
|
2020-11-30 15:41:22 -05:00
|
|
|
) -> Result<(TransferProof, Amount)> {
|
2020-09-28 02:18:50 -04:00
|
|
|
let destination_address =
|
|
|
|
Address::standard(Network::Mainnet, public_spend_key, public_view_key.into());
|
|
|
|
|
|
|
|
let res = self
|
2020-11-30 15:41:22 -05:00
|
|
|
.0
|
2020-10-19 21:18:27 -04:00
|
|
|
.transfer(0, amount.as_piconero(), &destination_address.to_string())
|
2020-09-28 02:18:50 -04:00
|
|
|
.await?;
|
|
|
|
|
2020-11-30 15:41:22 -05:00
|
|
|
let tx_hash = TxHash(res.tx_hash);
|
|
|
|
let tx_key = PrivateKey::from_str(&res.tx_key)?;
|
|
|
|
|
|
|
|
let fee = Amount::from_piconero(res.fee);
|
|
|
|
|
|
|
|
Ok((TransferProof::new(tx_hash, tx_key), fee))
|
2020-09-29 01:36:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
2020-10-19 21:18:27 -04:00
|
|
|
impl CreateWalletForOutput for Wallet {
|
2020-10-07 20:27:54 -04:00
|
|
|
async fn create_and_load_wallet_for_output(
|
2020-09-29 01:36:50 -04:00
|
|
|
&self,
|
|
|
|
private_spend_key: PrivateKey,
|
|
|
|
private_view_key: PrivateViewKey,
|
|
|
|
) -> Result<()> {
|
|
|
|
let public_spend_key = PublicKey::from_private_key(&private_spend_key);
|
|
|
|
let public_view_key = PublicKey::from_private_key(&private_view_key.into());
|
|
|
|
|
|
|
|
let address = Address::standard(Network::Mainnet, public_spend_key, public_view_key);
|
|
|
|
|
|
|
|
let _ = self
|
2020-11-30 15:41:22 -05:00
|
|
|
.0
|
2020-09-29 01:36:50 -04:00
|
|
|
.generate_from_keys(
|
|
|
|
&address.to_string(),
|
2020-11-30 15:41:22 -05:00
|
|
|
&private_spend_key.to_string(),
|
2020-09-29 01:36:50 -04:00
|
|
|
&PrivateKey::from(private_view_key).to_string(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
2020-10-19 21:18:27 -04:00
|
|
|
impl WatchForTransfer for Wallet {
|
2020-10-14 22:10:31 -04:00
|
|
|
async fn watch_for_transfer(
|
2020-11-04 01:06:17 -05:00
|
|
|
&self,
|
2020-11-30 15:41:22 -05:00
|
|
|
public_spend_key: PublicKey,
|
|
|
|
public_view_key: PublicViewKey,
|
|
|
|
transfer_proof: TransferProof,
|
2020-11-04 01:06:17 -05:00
|
|
|
expected_amount: Amount,
|
2020-11-30 15:41:22 -05:00
|
|
|
expected_confirmations: u32,
|
|
|
|
) -> Result<(), InsufficientFunds> {
|
|
|
|
enum Error {
|
|
|
|
TxNotFound,
|
|
|
|
InsufficientConfirmations,
|
|
|
|
InsufficientFunds { expected: Amount, actual: Amount },
|
|
|
|
}
|
|
|
|
|
|
|
|
let address = Address::standard(Network::Mainnet, public_spend_key, public_view_key.into());
|
|
|
|
|
|
|
|
let res = (|| async {
|
|
|
|
// NOTE: Currently, this is conflating IO errors with the transaction not being
|
|
|
|
// in the blockchain yet, or not having enough confirmations on it. All these
|
|
|
|
// errors warrant a retry, but the strategy should probably differ per case
|
|
|
|
let proof = self
|
|
|
|
.0
|
|
|
|
.check_tx_key(
|
|
|
|
&String::from(transfer_proof.tx_hash()),
|
|
|
|
&transfer_proof.tx_key().to_string(),
|
|
|
|
&address.to_string(),
|
|
|
|
)
|
2020-11-04 04:16:54 -05:00
|
|
|
.await
|
2020-11-30 15:41:22 -05:00
|
|
|
.map_err(|_| backoff::Error::Transient(Error::TxNotFound))?;
|
2020-11-04 04:16:54 -05:00
|
|
|
|
2020-11-30 15:41:22 -05:00
|
|
|
if proof.received != expected_amount.as_piconero() {
|
|
|
|
return Err(backoff::Error::Permanent(Error::InsufficientFunds {
|
|
|
|
expected: expected_amount,
|
|
|
|
actual: Amount::from_piconero(proof.received),
|
|
|
|
}));
|
2020-11-04 04:16:54 -05:00
|
|
|
}
|
|
|
|
|
2020-11-30 15:41:22 -05:00
|
|
|
if proof.confirmations < expected_confirmations {
|
|
|
|
return Err(backoff::Error::Transient(Error::InsufficientConfirmations));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(proof)
|
|
|
|
})
|
|
|
|
.retry(ConstantBackoff::new(Duration::from_secs(1)))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Err(Error::InsufficientFunds { expected, actual }) = res {
|
|
|
|
return Err(InsufficientFunds { expected, actual });
|
2020-11-04 01:06:17 -05:00
|
|
|
};
|
|
|
|
|
2020-11-30 15:41:22 -05:00
|
|
|
Ok(())
|
2020-11-04 01:06:17 -05:00
|
|
|
}
|
|
|
|
}
|