2020-12-06 23:19:15 +00:00
|
|
|
use anyhow::{Context, Result};
|
2020-10-21 03:41:50 +00:00
|
|
|
use async_trait::async_trait;
|
2020-10-29 10:17:00 +00:00
|
|
|
use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _};
|
2020-11-25 05:27:57 +00:00
|
|
|
use bitcoin::util::psbt::PartiallySignedTransaction;
|
2020-12-06 23:19:15 +00:00
|
|
|
use bitcoin_harness::{bitcoind_rpc::PsbtBase64, BitcoindRpcApi};
|
2020-10-21 03:41:50 +00:00
|
|
|
use reqwest::Url;
|
2020-12-06 23:19:15 +00:00
|
|
|
use std::time::Duration;
|
2020-12-01 23:00:00 +00:00
|
|
|
use tokio::time::interval;
|
|
|
|
use xmr_btc::{
|
|
|
|
bitcoin::{
|
2020-12-23 04:40:56 +00:00
|
|
|
BroadcastSignedTransaction, BuildTxLockPsbt, GetBlockHeight, SignTxLock,
|
2020-12-01 23:00:00 +00:00
|
|
|
TransactionBlockHeight, WatchForRawTransaction,
|
|
|
|
},
|
|
|
|
config::Config,
|
2020-10-21 03:41:50 +00:00
|
|
|
};
|
|
|
|
|
2020-11-25 05:27:57 +00:00
|
|
|
pub use ::bitcoin::{Address, Transaction};
|
2020-11-06 19:57:35 +00:00
|
|
|
pub use xmr_btc::bitcoin::*;
|
2020-10-27 02:26:40 +00:00
|
|
|
|
2020-11-06 19:57:35 +00:00
|
|
|
pub const TX_LOCK_MINE_TIMEOUT: u64 = 3600;
|
2020-10-22 03:30:10 +00:00
|
|
|
|
2020-10-21 03:41:50 +00:00
|
|
|
#[derive(Debug)]
|
2020-12-04 05:27:17 +00:00
|
|
|
pub struct Wallet {
|
|
|
|
pub inner: bitcoin_harness::Wallet,
|
|
|
|
pub network: bitcoin::Network,
|
|
|
|
}
|
2020-10-21 03:41:50 +00:00
|
|
|
|
|
|
|
impl Wallet {
|
2020-12-04 05:27:17 +00:00
|
|
|
pub async fn new(name: &str, url: Url, network: bitcoin::Network) -> Result<Self> {
|
2020-11-03 05:08:46 +00:00
|
|
|
let wallet = bitcoin_harness::Wallet::new(name, url).await?;
|
2020-10-21 03:41:50 +00:00
|
|
|
|
2020-12-04 05:27:17 +00:00
|
|
|
Ok(Self {
|
|
|
|
inner: wallet,
|
|
|
|
network,
|
|
|
|
})
|
2020-10-21 03:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn balance(&self) -> Result<Amount> {
|
2020-12-04 05:27:17 +00:00
|
|
|
let balance = self.inner.balance().await?;
|
2020-10-21 03:41:50 +00:00
|
|
|
Ok(balance)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn new_address(&self) -> Result<Address> {
|
2020-12-04 05:27:17 +00:00
|
|
|
self.inner.new_address().await.map_err(Into::into)
|
2020-10-21 03:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn transaction_fee(&self, txid: Txid) -> Result<Amount> {
|
|
|
|
let fee = self
|
2020-12-04 05:27:17 +00:00
|
|
|
.inner
|
2020-10-21 03:41:50 +00:00
|
|
|
.get_wallet_transaction(txid)
|
|
|
|
.await
|
2020-12-06 23:19:15 +00:00
|
|
|
.map(|res| {
|
|
|
|
res.fee.map(|signed_amount| {
|
|
|
|
signed_amount
|
|
|
|
.abs()
|
|
|
|
.to_unsigned()
|
|
|
|
.expect("Absolute value is always positive")
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.context("Rpc response did not contain a fee")?;
|
2020-10-21 03:41:50 +00:00
|
|
|
|
|
|
|
Ok(fee)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl BuildTxLockPsbt for Wallet {
|
|
|
|
async fn build_tx_lock_psbt(
|
|
|
|
&self,
|
|
|
|
output_address: Address,
|
|
|
|
output_amount: Amount,
|
|
|
|
) -> Result<PartiallySignedTransaction> {
|
2020-12-04 05:27:17 +00:00
|
|
|
let psbt = self.inner.fund_psbt(output_address, output_amount).await?;
|
2020-10-21 03:41:50 +00:00
|
|
|
let as_hex = base64::decode(psbt)?;
|
|
|
|
|
|
|
|
let psbt = bitcoin::consensus::deserialize(&as_hex)?;
|
|
|
|
|
|
|
|
Ok(psbt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl SignTxLock for Wallet {
|
|
|
|
async fn sign_tx_lock(&self, tx_lock: TxLock) -> Result<Transaction> {
|
|
|
|
let psbt = PartiallySignedTransaction::from(tx_lock);
|
|
|
|
|
|
|
|
let psbt = bitcoin::consensus::serialize(&psbt);
|
|
|
|
let as_base64 = base64::encode(psbt);
|
|
|
|
|
2020-12-04 05:27:17 +00:00
|
|
|
let psbt = self
|
|
|
|
.inner
|
|
|
|
.wallet_process_psbt(PsbtBase64(as_base64))
|
|
|
|
.await?;
|
2020-10-21 03:41:50 +00:00
|
|
|
let PsbtBase64(signed_psbt) = PsbtBase64::from(psbt);
|
|
|
|
|
|
|
|
let as_hex = base64::decode(signed_psbt)?;
|
|
|
|
let psbt: PartiallySignedTransaction = bitcoin::consensus::deserialize(&as_hex)?;
|
|
|
|
|
|
|
|
let tx = psbt.extract_tx();
|
|
|
|
|
|
|
|
Ok(tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl BroadcastSignedTransaction for Wallet {
|
|
|
|
async fn broadcast_signed_transaction(&self, transaction: Transaction) -> Result<Txid> {
|
2020-12-04 05:27:17 +00:00
|
|
|
let txid = self.inner.send_raw_transaction(transaction).await?;
|
|
|
|
tracing::debug!("Bitcoin tx broadcasted! TXID = {}", txid);
|
|
|
|
Ok(txid)
|
2020-10-21 03:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 10:17:00 +00:00
|
|
|
// TODO: For retry, use `backoff::ExponentialBackoff` in production as opposed
|
|
|
|
// to `ConstantBackoff`.
|
2020-10-21 03:41:50 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl WatchForRawTransaction for Wallet {
|
|
|
|
async fn watch_for_raw_transaction(&self, txid: Txid) -> Transaction {
|
2020-12-04 05:27:17 +00:00
|
|
|
(|| async { Ok(self.inner.get_raw_transaction(txid).await?) })
|
2020-10-29 10:17:00 +00:00
|
|
|
.retry(ConstantBackoff::new(Duration::from_secs(1)))
|
2020-10-21 03:41:50 +00:00
|
|
|
.await
|
|
|
|
.expect("transient errors to be retried")
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 01:11:03 +00:00
|
|
|
|
2020-11-12 00:06:34 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl GetRawTransaction for Wallet {
|
2020-11-27 00:30:07 +00:00
|
|
|
// todo: potentially replace with option
|
2020-11-12 00:06:34 +00:00
|
|
|
async fn get_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
|
2020-12-04 05:27:17 +00:00
|
|
|
Ok(self.inner.get_raw_transaction(txid).await?)
|
2020-11-12 00:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 01:11:03 +00:00
|
|
|
#[async_trait]
|
2020-12-23 04:40:56 +00:00
|
|
|
impl GetBlockHeight for Wallet {
|
2020-12-23 05:04:06 +00:00
|
|
|
async fn get_block_height(&self) -> BlockHeight {
|
|
|
|
let height = (|| async { Ok(self.inner.client.getblockcount().await?) })
|
2020-10-29 10:17:00 +00:00
|
|
|
.retry(ConstantBackoff::new(Duration::from_secs(1)))
|
2020-10-27 01:11:03 +00:00
|
|
|
.await
|
2020-12-23 05:04:06 +00:00
|
|
|
.expect("transient errors to be retried");
|
|
|
|
|
|
|
|
BlockHeight::new(height)
|
2020-10-27 01:11:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl TransactionBlockHeight for Wallet {
|
2020-12-23 05:04:06 +00:00
|
|
|
async fn transaction_block_height(&self, txid: Txid) -> BlockHeight {
|
2020-10-27 01:11:03 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum Error {
|
|
|
|
Io,
|
|
|
|
NotYetMined,
|
|
|
|
}
|
|
|
|
|
2020-12-23 05:04:06 +00:00
|
|
|
let height = (|| async {
|
2020-10-27 01:11:03 +00:00
|
|
|
let block_height = self
|
2020-12-04 05:27:17 +00:00
|
|
|
.inner
|
2020-10-27 01:11:03 +00:00
|
|
|
.transaction_block_height(txid)
|
|
|
|
.await
|
|
|
|
.map_err(|_| backoff::Error::Transient(Error::Io))?;
|
|
|
|
|
|
|
|
let block_height =
|
|
|
|
block_height.ok_or_else(|| backoff::Error::Transient(Error::NotYetMined))?;
|
|
|
|
|
|
|
|
Result::<_, backoff::Error<Error>>::Ok(block_height)
|
|
|
|
})
|
2020-10-29 10:17:00 +00:00
|
|
|
.retry(ConstantBackoff::new(Duration::from_secs(1)))
|
2020-10-27 01:11:03 +00:00
|
|
|
.await
|
2020-12-23 05:04:06 +00:00
|
|
|
.expect("transient errors to be retried");
|
|
|
|
|
|
|
|
BlockHeight::new(height)
|
2020-10-27 01:11:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-24 03:42:51 +00:00
|
|
|
|
2020-11-25 01:22:52 +00:00
|
|
|
#[async_trait]
|
2020-11-25 05:16:04 +00:00
|
|
|
impl WaitForTransactionFinality for Wallet {
|
2020-12-01 23:00:00 +00:00
|
|
|
async fn wait_for_transaction_finality(&self, txid: Txid, config: Config) -> Result<()> {
|
|
|
|
// TODO(Franck): This assumes that bitcoind runs with txindex=1
|
|
|
|
|
|
|
|
// Divide by 4 to not check too often yet still be aware of the new block early
|
|
|
|
// on.
|
|
|
|
let mut interval = interval(config.bitcoin_avg_block_time / 4);
|
|
|
|
|
|
|
|
loop {
|
2020-12-04 05:27:17 +00:00
|
|
|
let tx = self.inner.client.get_raw_transaction_verbose(txid).await?;
|
2020-12-01 23:00:00 +00:00
|
|
|
if let Some(confirmations) = tx.confirmations {
|
|
|
|
if confirmations >= config.bitcoin_finality_confirmations {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
interval.tick().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-11-25 01:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 05:27:17 +00:00
|
|
|
|
|
|
|
impl Network for Wallet {
|
|
|
|
fn get_network(&self) -> bitcoin::Network {
|
|
|
|
self.network
|
|
|
|
}
|
|
|
|
}
|