Skip to content

Commit

Permalink
fix some bitcoin address parsing and fee rate parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Einliterflasche committed Dec 11, 2024
1 parent b048569 commit e82e2e6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
8 changes: 4 additions & 4 deletions swap/src/bitcoin/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mod tests {
#[tokio::test]
async fn given_bob_sends_good_psbt_when_reconstructing_then_succeeeds() {
let (A, B) = alice_and_bob();
let wallet = WalletBuilder::new(50_000).build();
let wallet = WalletBuilder::new(50_000).build().await;
let agreed_amount = Amount::from_sat(10000);

let psbt = bob_make_psbt(A, B, &wallet, agreed_amount).await;
Expand All @@ -223,7 +223,7 @@ mod tests {
let fees = 300;
let agreed_amount = Amount::from_sat(10000);
let amount = agreed_amount.to_sat() + fees;
let wallet = WalletBuilder::new(amount).build();
let wallet = WalletBuilder::new(amount).build().await;

let psbt = bob_make_psbt(A, B, &wallet, agreed_amount).await;
assert_eq!(
Expand All @@ -239,7 +239,7 @@ mod tests {
#[tokio::test]
async fn given_bob_is_sending_less_than_agreed_when_reconstructing_txlock_then_fails() {
let (A, B) = alice_and_bob();
let wallet = WalletBuilder::new(50_000).build();
let wallet = WalletBuilder::new(50_000).build().await;
let agreed_amount = Amount::from_sat(10000);

let bad_amount = Amount::from_sat(5000);
Expand All @@ -252,7 +252,7 @@ mod tests {
#[tokio::test]
async fn given_bob_is_sending_to_a_bad_output_reconstructing_txlock_then_fails() {
let (A, B) = alice_and_bob();
let wallet = WalletBuilder::new(50_000).build();
let wallet = WalletBuilder::new(50_000).build().await;
let agreed_amount = Amount::from_sat(10000);

let E = eve();
Expand Down
11 changes: 6 additions & 5 deletions swap/src/bitcoin/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,17 +819,18 @@ impl Client {
impl EstimateFeeRate for Client {
fn estimate_feerate(&self, target_block: usize) -> Result<FeeRate> {
// Get the fee rate in BTC/kvB
let fee_rate_btc_kvb = self.electrum.inner.estimate_fee(target_block)?;
// Convert to sat/vb
let fee_rate_sat_vb = Amount::from_btc(fee_rate_btc_kvb / 1_000.)?;
let btc_per_kvb = self.electrum.inner.estimate_fee(target_block)?;
let amount_per_kvb = Amount::from_btc(btc_per_kvb)?;
// Convert to sat/kwu
let amount_per_kwu = amount_per_kvb.checked_div(4).context("fee rate overflow")?;

FeeRate::from_sat_per_vb(fee_rate_sat_vb.to_sat()).ok_or(anyhow!("Fee rate overflow"))
Ok(FeeRate::from_sat_per_kwu(amount_per_kwu.to_sat()))
}

fn min_relay_fee(&self) -> Result<bitcoin::Amount> {
let relay_fee_btc = self.electrum.inner.relay_fee()?;

Amount::from_btc(relay_fee_btc).context("fee out of range")
Amount::from_btc(relay_fee_btc).context("relay fee out of range")
}
}

Expand Down
8 changes: 5 additions & 3 deletions swap/src/cli/api/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::network::swarm;
use crate::protocol::bob::{BobState, Swap};
use crate::protocol::{bob, State};
use crate::{bitcoin, cli, monero, rpc};
use ::bitcoin::address::NetworkUnchecked;
use ::bitcoin::Txid;
use ::monero::Network;
use anyhow::{bail, Context as AnyContext, Result};
Expand Down Expand Up @@ -57,8 +58,7 @@ pub struct BuyXmrArgs {
#[typeshare(serialized_as = "string")]
pub seller: Multiaddr,
#[typeshare(serialized_as = "Option<string>")]
#[serde(with = "crate::bitcoin::address_serde::option")]
pub bitcoin_change_address: Option<bitcoin::Address>,
pub bitcoin_change_address: Option<bitcoin::Address<NetworkUnchecked>>,
#[typeshare(serialized_as = "string")]
pub monero_receive_address: monero::Address,
}
Expand Down Expand Up @@ -591,7 +591,9 @@ pub async fn buy_xmr(
);

let bitcoin_change_address = match bitcoin_change_address {
Some(addr) => addr,
Some(addr) => addr
.require_network(bitcoin_wallet.network())
.context("Address is not on the correct network")?,
None => {
let internal_wallet_address = bitcoin_wallet.new_address().await?;

Expand Down
5 changes: 3 additions & 2 deletions swap/src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ where

let bitcoin_change_address = bitcoin_change_address
.map(|address| bitcoin_address::validate(address, is_testnet))
.transpose()?;
.transpose()?
.map(|address| address.into_unchecked());

let context = Arc::new(
ContextBuilder::new(is_testnet)
Expand All @@ -93,7 +94,7 @@ where
.build()
.await?,
);

BuyXmrArgs {
seller,
bitcoin_change_address,
Expand Down
4 changes: 2 additions & 2 deletions swap/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ pub fn register_modules(outer_context: Context) -> Result<RpcModule<Context>> {
params.bitcoin_change_address = params
.bitcoin_change_address
.map(|address| {
bitcoin_address::revalidate_network(
bitcoin_address::validate_network(
address,
context.config.env_config.bitcoin_network,
)
).map(|a| a.into_unchecked())
})
.transpose()
.to_jsonrpsee_result()?;
Expand Down

0 comments on commit e82e2e6

Please sign in to comment.