Skip to content

Commit 0757faa

Browse files
committed
bitcoind: fix raw TX fetching when using REST
1 parent abbed3e commit 0757faa

1 file changed

Lines changed: 23 additions & 0 deletions

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();

0 commit comments

Comments
 (0)