Skip to content

Commit e7f9283

Browse files
authored
Merge pull request #1006 from jharveyb/2026-07-fix-getrawtx-rest
bitcoind: fix raw TX fetching when using REST
2 parents 48b76b4 + b9e07a6 commit e7f9283

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/chain/bitcoind.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,9 +1354,12 @@ pub(crate) struct GetRawTransactionResponse(pub Transaction);
13541354
impl TryInto<GetRawTransactionResponse> for JsonResponse {
13551355
type Error = String;
13561356
fn try_into(self) -> Result<GetRawTransactionResponse, String> {
1357+
// Accept both the bare hex-encoded TX from RPC, and the REST response
1358+
// of a JSON object with the hex-encoded TX in the 'hex' field.
13571359
let tx = self
13581360
.0
13591361
.as_str()
1362+
.or_else(|| self.0.get("hex").and_then(|v| v.as_str()))
13601363
.ok_or("Failed to parse getrawtransaction response".to_string())
13611364
.and_then(|s| {
13621365
bitcoin::consensus::encode::deserialize_hex(s)
@@ -1640,6 +1643,26 @@ mod tests {
16401643
prop_assert_eq!(decoded.0, tx);
16411644
}
16421645

1646+
#[test]
1647+
fn prop_get_raw_transaction_response_json_object_roundtrip(tx in arbitrary_transaction()) {
1648+
let hex = bitcoin::consensus::encode::serialize_hex(&tx);
1649+
let vsize = tx.vsize();
1650+
let json_val = json!({
1651+
"version": tx.version,
1652+
"vsize": vsize,
1653+
"hex": hex,
1654+
});
1655+
1656+
let resp = JsonResponse(json_val);
1657+
let decoded: GetRawTransactionResponse = resp.try_into().unwrap();
1658+
1659+
prop_assert_eq!(decoded.0.compute_txid(), tx.compute_txid());
1660+
prop_assert_eq!(decoded.0.compute_wtxid(), tx.compute_wtxid());
1661+
1662+
prop_assert_eq!(decoded.0, tx);
1663+
}
1664+
1665+
16431666
#[test]
16441667
fn prop_fee_response_roundtrip(fee_rate in any::<f64>()) {
16451668
let fee_rate = fee_rate.abs();

tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ pub(crate) fn setup_bitcoind_and_electrsd() -> (BitcoinD, ElectrsD) {
330330
pub(crate) fn random_chain_source<'a>(
331331
bitcoind: &'a BitcoinD, electrsd: &'a ElectrsD,
332332
) -> TestChainSource<'a> {
333-
let r = rand::random_range(0..3);
333+
let r = rand::random_range(0..4);
334334
match r {
335335
0 => {
336336
println!("Randomly setting up Esplora chain syncing...");

0 commit comments

Comments
 (0)