From d8176e837799c65236fe5655b848b1a7118c0b5a Mon Sep 17 00:00:00 2001 From: emma31-dev Date: Sat, 7 Nov 2026 17:15:39 +0100 Subject: [PATCH] Propagate PSBT decoding and parsing errors - Decode Base64 and parse PSBTs without wrapping the error in `Error::Generic` - Use the dedicated `Electrum`, `Esplora`, `BitcoinCoreRpcError` and `Chain` error variants in `BlockchainClient` - Cover sign, finalize_psbt and broadcast error paths with integration tests Related to #363. --- src/client.rs | 28 +++++++----------- src/handlers/offline.rs | 12 +++----- src/handlers/online.rs | 8 ++--- tests/integration/offline.rs | 57 ++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src/client.rs b/src/client.rs index bce66a18..8937178f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -83,21 +83,21 @@ impl BlockchainClient { pub async fn broadcast(&self, tx: Transaction) -> Result { match self { #[cfg(feature = "electrum")] - Self::Electrum { client, .. } => client - .transaction_broadcast(&tx) - .map_err(|e| Error::Generic(e.to_string())), + Self::Electrum { client, .. } => { + client.transaction_broadcast(&tx).map_err(Error::Electrum) + } #[cfg(feature = "esplora")] Self::Esplora { client, .. } => client .broadcast(&tx) .await .map(|()| tx.compute_txid()) - .map_err(|e| Error::Generic(e.to_string())), + .map_err(Error::Esplora), #[cfg(feature = "rpc")] Self::RpcClient { client } => client .send_raw_transaction(&tx) - .map_err(|e| Error::Generic(e.to_string())), + .map_err(Error::BitcoinCoreRpcError), #[cfg(feature = "cbf")] Self::KyotoClient { client } => { @@ -128,9 +128,7 @@ impl BlockchainClient { client.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx)); let update = client.sync(request, *batch_size, false)?; - wallet - .apply_update(update) - .map_err(|e| Error::Generic(e.to_string())) + wallet.apply_update(update).map_err(Error::Chain) } #[cfg(feature = "esplora")] Self::Esplora { @@ -141,9 +139,7 @@ impl BlockchainClient { .sync(request, *parallel_requests) .await .map_err(|e| *e)?; - wallet - .apply_update(update) - .map_err(|e| Error::Generic(e.to_string())) + wallet.apply_update(update).map_err(Error::Chain) } #[cfg(feature = "rpc")] Self::RpcClient { client } => { @@ -190,9 +186,7 @@ impl BlockchainClient { Ok(()) } #[cfg(feature = "cbf")] - Self::KyotoClient { client } => sync_kyoto_client(wallet, client) - .await - .map_err(|e| Error::Generic(e.to_string())), + Self::KyotoClient { client } => sync_kyoto_client(wallet, client).await, } } } @@ -307,7 +301,7 @@ pub(crate) fn new_blockchain_client( ), }; let client = bdk_bitcoind_rpc::bitcoincore_rpc::Client::new(url, auth) - .map_err(|e| Error::Generic(e.to_string()))?; + .map_err(Error::BitcoinCoreRpcError)?; BlockchainClient::RpcClient { client: Box::new(client), } @@ -364,9 +358,7 @@ pub async fn sync_kyoto_client( let update = handle.update_subscriber.lock().await.update().await?; tracing::info!("Received update: applying to wallet"); - let events = wallet - .apply_update_events(update) - .map_err(|e| Error::Generic(format!("Failed to apply update: {e}")))?; + let events = wallet.apply_update_events(update).map_err(Error::Chain)?; crate::utils::print_wallet_events(&events); tracing::info!( diff --git a/src/handlers/offline.rs b/src/handlers/offline.rs index 80a6de47..5250c673 100644 --- a/src/handlers/offline.rs +++ b/src/handlers/offline.rs @@ -654,10 +654,8 @@ impl AppCommand>> for SignCommand { fn execute(&self, ctx: &mut AppContext>) -> Result { let wallet = &mut ctx.state.wallet; - let psbt_bytes = BASE64_STANDARD - .decode(&self.psbt) - .map_err(|e| Error::Generic(e.to_string()))?; - let mut psbt = Psbt::deserialize(&psbt_bytes).map_err(|e| Error::Generic(e.to_string()))?; + let psbt_bytes = BASE64_STANDARD.decode(&self.psbt)?; + let mut psbt = Psbt::deserialize(&psbt_bytes)?; let signopt = SignOptions { assume_height: self.assume_height, @@ -708,10 +706,8 @@ impl AppCommand>> for FinalizePsbtCommand { fn execute(&self, ctx: &mut AppContext>) -> Result { let wallet = &mut ctx.state.wallet; - let psbt_bytes = BASE64_STANDARD - .decode(&self.psbt) - .map_err(|e| Error::Generic(e.to_string()))?; - let mut psbt = Psbt::deserialize(&psbt_bytes).map_err(|e| Error::Generic(e.to_string()))?; + let psbt_bytes = BASE64_STANDARD.decode(&self.psbt)?; + let mut psbt = Psbt::deserialize(&psbt_bytes)?; let signopt = SignOptions { assume_height: self.assume_height, diff --git a/src/handlers/online.rs b/src/handlers/online.rs index 25baf889..1a10f857 100644 --- a/src/handlers/online.rs +++ b/src/handlers/online.rs @@ -296,9 +296,7 @@ impl AsyncAppCommand>> for SyncCommand { print_wallet_events(&evicted_events); } #[cfg(feature = "cbf")] - KyotoClient { client } => sync_kyoto_client(wallet, client) - .await - .map_err(|e| Error::Generic(e.to_string()))?, + KyotoClient { client } => sync_kyoto_client(wallet, client).await?, } Ok(StatusResult { message: "Wallet synced successfully.".to_string(), @@ -343,9 +341,7 @@ impl AsyncAppCommand>> for BroadcastCommand { let tx = match (&self.psbt, &self.tx) { (Some(psbt), None) => { - let psbt = BASE64_STANDARD - .decode(psbt) - .map_err(|e| Error::Generic(e.to_string()))?; + let psbt = BASE64_STANDARD.decode(psbt)?; let psbt: Psbt = Psbt::deserialize(&psbt)?; is_final(&psbt)?; psbt.extract_tx()? diff --git a/tests/integration/offline.rs b/tests/integration/offline.rs index c4be0a21..772ff53f 100644 --- a/tests/integration/offline.rs +++ b/tests/integration/offline.rs @@ -45,6 +45,63 @@ mod test_offline { (cli, cmd_init) } + #[test] + fn test_sign_rejects_invalid_base64() { + let (cli, mut cmd_init) = setup_wallet_config(); + cmd_init.assert().success(); + + cli.wallet_cmd(&["--wallet", WALLET_NAME, "sign", "!!!"]) + .assert() + .code(1) + .stderr(predicate::str::contains("Base64 decoding error:")); + } + + #[test] + fn test_sign_rejects_invalid_psbt() { + let (cli, mut cmd_init) = setup_wallet_config(); + cmd_init.assert().success(); + + // Valid Base64 for "hello", which is not a serialized PSBT. + cli.wallet_cmd(&["--wallet", WALLET_NAME, "sign", "aGVsbG8="]) + .assert() + .code(1) + .stderr(predicate::str::contains("PsbtError:")); + } + + #[test] + fn test_finalize_psbt_rejects_invalid_base64() { + let (cli, mut cmd_init) = setup_wallet_config(); + cmd_init.assert().success(); + + cli.wallet_cmd(&["--wallet", WALLET_NAME, "finalize_psbt", "!!!"]) + .assert() + .code(1) + .stderr(predicate::str::contains("Base64 decoding error:")); + } + + #[test] + fn test_finalize_psbt_rejects_invalid_psbt() { + let (cli, mut cmd_init) = setup_wallet_config(); + cmd_init.assert().success(); + + // Valid Base64 for "hello", which is not a serialized PSBT. + cli.wallet_cmd(&["--wallet", WALLET_NAME, "finalize_psbt", "aGVsbG8="]) + .assert() + .code(1) + .stderr(predicate::str::contains("PsbtError:")); + } + + #[test] + fn test_broadcast_rejects_invalid_base64() { + let (cli, mut cmd_init) = setup_wallet_config(); + cmd_init.assert().success(); + + cli.wallet_cmd(&["--wallet", WALLET_NAME, "broadcast", "--psbt", "!!!"]) + .assert() + .code(1) + .stderr(predicate::str::contains("Base64 decoding error:")); + } + #[test] fn test_new_and_unused_address() { let (cli, mut cmd_init) = setup_wallet_config();