From 0837bc182f4b417c0c98e38a2390a2d5467df8e3 Mon Sep 17 00:00:00 2001 From: Kevin Abramczyk Date: Sun, 22 Nov 2026 14:05:08 -0500 Subject: [PATCH] fix(rpc): read chunked responses from the bitcoind RPC server `bitcoincore_rpc::Client::new` always uses jsonrpc's `simple_http` transport, which rejects responses sent with `Transfer-Encoding: chunked` ("The server replied with a chunked response which is not supported"). Servers that stream JSON-RPC responses chunked, such as libbitcoin-server, cannot be used with `--client-type rpc`. Build the RPC client over jsonrpc's `minreq_http` transport instead, which reads chunked and content-length responses. bitcoincore-rpc 0.19 already enables the `minreq_http` feature of jsonrpc, so no dependency changes. minreq requires a URL scheme while simple_http defaulted to http, so `http://` is prepended when the URL has none (e.g. `127.0.0.1:18443`). --- CHANGELOG.md | 1 + src/client.rs | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85926de9..7bb4c8d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details. - Gated `full_scan`'s `--scan-stop-gap` to the Electrum and Esplora backends that use it; RPC and CBF wallets now reject the flag instead of silently ignoring it, while a `STOP_GAP` environment variable keeps working for supported backends and no longer breaks unsupported ones - Changed `payjoin history` to return JSON instead of a rendered table - Fixed `wallet config --force` silently changing an existing wallet's network, which left the wallet unable to load; it is now rejected +- Fixed the `rpc` backend failing against servers that send chunked responses ## [4.0.0] diff --git a/src/client.rs b/src/client.rs index 8937178f..700cf113 100644 --- a/src/client.rs +++ b/src/client.rs @@ -248,6 +248,38 @@ fn esplora_proxy_url(proxy_opts: &ProxyOpts) -> Option { }) } +/// Create a bitcoind RPC client over the minreq HTTP transport, which reads +/// chunked responses. The default simple_http transport rejects them. +#[cfg(feature = "rpc")] +fn rpc_client( + url: &str, + auth: bdk_bitcoind_rpc::bitcoincore_rpc::Auth, +) -> Result { + use bdk_bitcoind_rpc::bitcoincore_rpc::{self, jsonrpc}; + + let rpc_error = + |e: jsonrpc::Error| Error::BitcoinCoreRpcError(bitcoincore_rpc::Error::JsonRpc(e)); + let (user, pass) = auth.get_user_pass()?; + + // simple_http accepts a url without a scheme, minreq requires one. + let url = if url.contains("://") { + url.to_string() + } else { + format!("http://{url}") + }; + + let mut builder = jsonrpc::minreq_http::Builder::new() + .url(&url) + .map_err(|e| rpc_error(e.into()))?; + if let Some(user) = user { + builder = builder.basic_auth(user, pass); + } + + Ok(bitcoincore_rpc::Client::from_jsonrpc( + jsonrpc::Client::with_transport(builder.build()), + )) +} + #[cfg(any( feature = "electrum", feature = "esplora", @@ -300,8 +332,7 @@ pub(crate) fn new_blockchain_client( wallet_opts.basic_auth.1.clone(), ), }; - let client = bdk_bitcoind_rpc::bitcoincore_rpc::Client::new(url, auth) - .map_err(Error::BitcoinCoreRpcError)?; + let client = rpc_client(url, auth)?; BlockchainClient::RpcClient { client: Box::new(client), }