Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,21 @@ impl BlockchainClient {
pub async fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> {
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 } => {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 } => {
Expand Down Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -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!(
Expand Down
12 changes: 4 additions & 8 deletions src/handlers/offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,8 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for SignCommand {

fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
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,
Expand Down Expand Up @@ -708,10 +706,8 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for FinalizePsbtCommand {

fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
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,
Expand Down
8 changes: 2 additions & 6 deletions src/handlers/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,7 @@ impl AsyncAppCommand<AppContext<OnlineOperations<'_>>> 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(),
Expand Down Expand Up @@ -343,9 +341,7 @@ impl AsyncAppCommand<AppContext<OnlineOperations<'_>>> 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()?
Expand Down
57 changes: 57 additions & 0 deletions tests/integration/offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading