From 6fa7633f88afaecdbb4b39ff05e1e4df3eb0313d Mon Sep 17 00:00:00 2001 From: benthecarman Date: Thu, 21 Mar 2024 17:30:33 -0500 Subject: [PATCH] Return address we sent to for onchain --- src/main.rs | 4 ++-- src/onchain.rs | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 610c3bd..62bcea6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,9 +141,9 @@ async fn onchain_handler( Extension(state): Extension, Json(payload): Json, ) -> Result, AppError> { - let txid = pay_onchain(state, payload.clone()).await?; + let res = pay_onchain(state, payload).await?; - Ok(Json(OnchainResponse { txid })) + Ok(Json(res)) } #[axum::debug_handler] diff --git a/src/onchain.rs b/src/onchain.rs index 013d55c..1ff290b 100644 --- a/src/onchain.rs +++ b/src/onchain.rs @@ -16,14 +16,18 @@ pub struct OnchainRequest { #[derive(Clone, Serialize)] pub struct OnchainResponse { pub txid: String, + pub address: String, } -pub async fn pay_onchain(state: AppState, payload: OnchainRequest) -> anyhow::Result { +pub async fn pay_onchain( + state: AppState, + payload: OnchainRequest, +) -> anyhow::Result { if payload.sats > MAX_SEND_AMOUNT { anyhow::bail!("max amount is 10,000,000"); } - let txid = { + let res = { let network = state.network; // need to convert from different rust-bitcoin versions @@ -34,7 +38,7 @@ pub async fn pay_onchain(state: AppState, payload: OnchainRequest) -> anyhow::Re let address = Address::from_str(&address_str.to_string()).map_err(|e| anyhow::anyhow!(e))?; - let address = if address.is_valid_for_network(network) { + let address = if let Ok(address) = address.require_network(network) { address } else { anyhow::bail!( @@ -47,19 +51,15 @@ pub async fn pay_onchain(state: AppState, payload: OnchainRequest) -> anyhow::Re let amount = Amount::from_sat(payload.sats); - task::block_in_place(|| { - bitcoin_client.send_to_address( - &address.assume_checked(), // we just checked it above, - amount, - None, - None, - None, - None, - None, - None, - ) - })? + let txid = task::block_in_place(|| { + bitcoin_client.send_to_address(&address, amount, None, None, None, None, None, None) + })?; + + OnchainResponse { + txid: txid.to_string(), + address: address.to_string(), + } }; - Ok(txid.to_string()) + Ok(res) }