From: Sergi Delgado Segura Date: Fri, 6 Aug 2021 14:13:37 +0000 (+0200) Subject: Adds Txid to lighting-block-sync::convert X-Git-Tag: v0.0.101~31^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=f65d05c23db8174d49771fd1b6b8dd0efa72ddd4;p=rust-lightning Adds Txid to lighting-block-sync::convert --- diff --git a/lightning-block-sync/src/convert.rs b/lightning-block-sync/src/convert.rs index 37b2c4323..e8e1427bd 100644 --- a/lightning-block-sync/src/convert.rs +++ b/lightning-block-sync/src/convert.rs @@ -4,7 +4,7 @@ use crate::utils::hex_to_uint256; use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::consensus::encode; -use bitcoin::hash_types::{BlockHash, TxMerkleNode}; +use bitcoin::hash_types::{BlockHash, TxMerkleNode, Txid}; use bitcoin::hashes::hex::{ToHex, FromHex}; use serde::Deserialize; @@ -156,11 +156,37 @@ impl TryInto<(BlockHash, Option)> for JsonResponse { } } +impl TryInto for JsonResponse { + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + match self.0.as_str() { + None => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "expected JSON string", + )), + Some(hex_data) => match Vec::::from_hex(hex_data) { + Err(_) => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "invalid hex data", + )), + Ok(txid_data) => match encode::deserialize(&txid_data) { + Err(_) => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "invalid txid", + )), + Ok(txid) => Ok(txid), + }, + }, + } + } +} + #[cfg(test)] pub(crate) mod tests { use super::*; use bitcoin::blockdata::constants::genesis_block; use bitcoin::consensus::encode; + use bitcoin::hashes::Hash; use bitcoin::network::constants::Network; /// Converts from `BlockHeaderData` into a `GetHeaderResponse` JSON value. @@ -469,4 +495,50 @@ pub(crate) mod tests { }, } } + + #[test] + fn into_txid_from_json_response_with_unexpected_type() { + let response = JsonResponse(serde_json::json!({ "result": "foo" })); + match TryInto::::try_into(response) { + Err(e) => { + assert_eq!(e.kind(), std::io::ErrorKind::InvalidData); + assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON string"); + } + Ok(_) => panic!("Expected error"), + } + } + + #[test] + fn into_txid_from_json_response_with_invalid_hex_data() { + let response = JsonResponse(serde_json::json!("foobar")); + match TryInto::::try_into(response) { + Err(e) => { + assert_eq!(e.kind(), std::io::ErrorKind::InvalidData); + assert_eq!(e.get_ref().unwrap().to_string(), "invalid hex data"); + } + Ok(_) => panic!("Expected error"), + } + } + + #[test] + fn into_txid_from_json_response_with_invalid_txid_data() { + let response = JsonResponse(serde_json::json!("abcd")); + match TryInto::::try_into(response) { + Err(e) => { + assert_eq!(e.kind(), std::io::ErrorKind::InvalidData); + assert_eq!(e.get_ref().unwrap().to_string(), "invalid txid"); + } + Ok(_) => panic!("Expected error"), + } + } + + #[test] + fn into_txid_from_json_response_with_valid_txid_data() { + let target_txid = Txid::from_slice(&[1; 32]).unwrap(); + let response = JsonResponse(serde_json::json!(encode::serialize_hex(&target_txid))); + match TryInto::::try_into(response) { + Err(e) => panic!("Unexpected error: {:?}", e), + Ok(txid) => assert_eq!(txid, target_txid), + } + } }