Skip to content

Commit 2fceeba

Browse files
committed
Merge #363: Propagate errors correctly when decoding and parsing PSBTs
d8176e8 Propagate PSBT decoding and parsing errors (emma31-dev) Pull request description: ### Description Propagate PSBT decoding and parsing errors instead of wrapping them in `Error::Generic`. - `BASE64_STANDARD.decode()` failures in `sign`, `finalize_psbt`, and `broadcast` now propagate directly via `?` - `Psbt::deserialize()` failures are no longer swallowed into a generic string message - `BlockchainClient::broadcast()` maps backend errors to dedicated typed variants (`Error::Electrum`, `Error::Esplora`, `Error::BitcoinCoreRpcError`) - `wallet.apply_update()` / `apply_update_events()` map to `Error::Chain` instead of `Error::Generic` - Integration tests added for `sign`, `finalize_psbt`, and `broadcast` error paths Fixes #357 . Related to the broader error-propagation cleanup across the codebase. ### Notes to the reviewers Uses the existing `From` conversions for `DecodeError` and `PsbtError`. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk-cli/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### Bugfixes: * [x] I'm linking the issue being fixed by this PR ACKs for top commit: tvpeter: ACK d8176e8 vadim-anfv: ACK d8176e8 Tree-SHA512: 81a703a8758e3a91a46278346b425f71b526d34982d3da36279703fec309eaa407674ea641cc130d5ec2ae36fdf30744e7893d71b0407751e21ed6fdca0cfbb1
2 parents 86a3d6b + d8176e8 commit 2fceeba

4 files changed

Lines changed: 73 additions & 32 deletions

File tree

‎src/client.rs‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,21 @@ impl BlockchainClient {
8383
pub async fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> {
8484
match self {
8585
#[cfg(feature = "electrum")]
86-
Self::Electrum { client, .. } => client
87-
.transaction_broadcast(&tx)
88-
.map_err(|e| Error::Generic(e.to_string())),
86+
Self::Electrum { client, .. } => {
87+
client.transaction_broadcast(&tx).map_err(Error::Electrum)
88+
}
8989

9090
#[cfg(feature = "esplora")]
9191
Self::Esplora { client, .. } => client
9292
.broadcast(&tx)
9393
.await
9494
.map(|()| tx.compute_txid())
95-
.map_err(|e| Error::Generic(e.to_string())),
95+
.map_err(Error::Esplora),
9696

9797
#[cfg(feature = "rpc")]
9898
Self::RpcClient { client } => client
9999
.send_raw_transaction(&tx)
100-
.map_err(|e| Error::Generic(e.to_string())),
100+
.map_err(Error::BitcoinCoreRpcError),
101101

102102
#[cfg(feature = "cbf")]
103103
Self::KyotoClient { client } => {
@@ -128,9 +128,7 @@ impl BlockchainClient {
128128
client.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
129129

130130
let update = client.sync(request, *batch_size, false)?;
131-
wallet
132-
.apply_update(update)
133-
.map_err(|e| Error::Generic(e.to_string()))
131+
wallet.apply_update(update).map_err(Error::Chain)
134132
}
135133
#[cfg(feature = "esplora")]
136134
Self::Esplora {
@@ -141,9 +139,7 @@ impl BlockchainClient {
141139
.sync(request, *parallel_requests)
142140
.await
143141
.map_err(|e| *e)?;
144-
wallet
145-
.apply_update(update)
146-
.map_err(|e| Error::Generic(e.to_string()))
142+
wallet.apply_update(update).map_err(Error::Chain)
147143
}
148144
#[cfg(feature = "rpc")]
149145
Self::RpcClient { client } => {
@@ -190,9 +186,7 @@ impl BlockchainClient {
190186
Ok(())
191187
}
192188
#[cfg(feature = "cbf")]
193-
Self::KyotoClient { client } => sync_kyoto_client(wallet, client)
194-
.await
195-
.map_err(|e| Error::Generic(e.to_string())),
189+
Self::KyotoClient { client } => sync_kyoto_client(wallet, client).await,
196190
}
197191
}
198192
}
@@ -307,7 +301,7 @@ pub(crate) fn new_blockchain_client(
307301
),
308302
};
309303
let client = bdk_bitcoind_rpc::bitcoincore_rpc::Client::new(url, auth)
310-
.map_err(|e| Error::Generic(e.to_string()))?;
304+
.map_err(Error::BitcoinCoreRpcError)?;
311305
BlockchainClient::RpcClient {
312306
client: Box::new(client),
313307
}
@@ -364,9 +358,7 @@ pub async fn sync_kyoto_client(
364358

365359
let update = handle.update_subscriber.lock().await.update().await?;
366360
tracing::info!("Received update: applying to wallet");
367-
let events = wallet
368-
.apply_update_events(update)
369-
.map_err(|e| Error::Generic(format!("Failed to apply update: {e}")))?;
361+
let events = wallet.apply_update_events(update).map_err(Error::Chain)?;
370362
crate::utils::print_wallet_events(&events);
371363

372364
tracing::info!(

‎src/handlers/offline.rs‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,8 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for SignCommand {
654654

655655
fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
656656
let wallet = &mut ctx.state.wallet;
657-
let psbt_bytes = BASE64_STANDARD
658-
.decode(&self.psbt)
659-
.map_err(|e| Error::Generic(e.to_string()))?;
660-
let mut psbt = Psbt::deserialize(&psbt_bytes).map_err(|e| Error::Generic(e.to_string()))?;
657+
let psbt_bytes = BASE64_STANDARD.decode(&self.psbt)?;
658+
let mut psbt = Psbt::deserialize(&psbt_bytes)?;
661659

662660
let signopt = SignOptions {
663661
assume_height: self.assume_height,
@@ -708,10 +706,8 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for FinalizePsbtCommand {
708706

709707
fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
710708
let wallet = &mut ctx.state.wallet;
711-
let psbt_bytes = BASE64_STANDARD
712-
.decode(&self.psbt)
713-
.map_err(|e| Error::Generic(e.to_string()))?;
714-
let mut psbt = Psbt::deserialize(&psbt_bytes).map_err(|e| Error::Generic(e.to_string()))?;
709+
let psbt_bytes = BASE64_STANDARD.decode(&self.psbt)?;
710+
let mut psbt = Psbt::deserialize(&psbt_bytes)?;
715711

716712
let signopt = SignOptions {
717713
assume_height: self.assume_height,

‎src/handlers/online.rs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,7 @@ impl AsyncAppCommand<AppContext<OnlineOperations<'_>>> for SyncCommand {
369369
print_wallet_events(&evicted_events);
370370
}
371371
#[cfg(feature = "cbf")]
372-
KyotoClient { client } => sync_kyoto_client(wallet, client)
373-
.await
374-
.map_err(|e| Error::Generic(e.to_string()))?,
372+
KyotoClient { client } => sync_kyoto_client(wallet, client).await?,
375373
}
376374
Ok(StatusResult {
377375
message: "Wallet synced successfully.".to_string(),
@@ -416,9 +414,7 @@ impl AsyncAppCommand<AppContext<OnlineOperations<'_>>> for BroadcastCommand {
416414

417415
let tx = match (&self.psbt, &self.tx) {
418416
(Some(psbt), None) => {
419-
let psbt = BASE64_STANDARD
420-
.decode(psbt)
421-
.map_err(|e| Error::Generic(e.to_string()))?;
417+
let psbt = BASE64_STANDARD.decode(psbt)?;
422418
let psbt: Psbt = Psbt::deserialize(&psbt)?;
423419
is_final(&psbt)?;
424420
psbt.extract_tx()?

‎tests/integration/offline.rs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,63 @@ mod test_offline {
4545
(cli, cmd_init)
4646
}
4747

48+
#[test]
49+
fn test_sign_rejects_invalid_base64() {
50+
let (cli, mut cmd_init) = setup_wallet_config();
51+
cmd_init.assert().success();
52+
53+
cli.wallet_cmd(&["--wallet", WALLET_NAME, "sign", "!!!"])
54+
.assert()
55+
.code(1)
56+
.stderr(predicate::str::contains("Base64 decoding error:"));
57+
}
58+
59+
#[test]
60+
fn test_sign_rejects_invalid_psbt() {
61+
let (cli, mut cmd_init) = setup_wallet_config();
62+
cmd_init.assert().success();
63+
64+
// Valid Base64 for "hello", which is not a serialized PSBT.
65+
cli.wallet_cmd(&["--wallet", WALLET_NAME, "sign", "aGVsbG8="])
66+
.assert()
67+
.code(1)
68+
.stderr(predicate::str::contains("PsbtError:"));
69+
}
70+
71+
#[test]
72+
fn test_finalize_psbt_rejects_invalid_base64() {
73+
let (cli, mut cmd_init) = setup_wallet_config();
74+
cmd_init.assert().success();
75+
76+
cli.wallet_cmd(&["--wallet", WALLET_NAME, "finalize_psbt", "!!!"])
77+
.assert()
78+
.code(1)
79+
.stderr(predicate::str::contains("Base64 decoding error:"));
80+
}
81+
82+
#[test]
83+
fn test_finalize_psbt_rejects_invalid_psbt() {
84+
let (cli, mut cmd_init) = setup_wallet_config();
85+
cmd_init.assert().success();
86+
87+
// Valid Base64 for "hello", which is not a serialized PSBT.
88+
cli.wallet_cmd(&["--wallet", WALLET_NAME, "finalize_psbt", "aGVsbG8="])
89+
.assert()
90+
.code(1)
91+
.stderr(predicate::str::contains("PsbtError:"));
92+
}
93+
94+
#[test]
95+
fn test_broadcast_rejects_invalid_base64() {
96+
let (cli, mut cmd_init) = setup_wallet_config();
97+
cmd_init.assert().success();
98+
99+
cli.wallet_cmd(&["--wallet", WALLET_NAME, "broadcast", "--psbt", "!!!"])
100+
.assert()
101+
.code(1)
102+
.stderr(predicate::str::contains("Base64 decoding error:"));
103+
}
104+
48105
#[test]
49106
fn test_new_and_unused_address() {
50107
let (cli, mut cmd_init) = setup_wallet_config();

0 commit comments

Comments
 (0)