use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::consensus::encode;
use bitcoin::hash_types::{BlockHash, TxMerkleNode, Txid};
-use bitcoin::hashes::hex::{FromHex, ToHex};
+use bitcoin::hashes::hex::FromHex;
use bitcoin::Transaction;
-use serde::Deserialize;
-
use serde_json;
use std::convert::From;
use std::convert::TryFrom;
use std::convert::TryInto;
+use std::str::FromStr;
use bitcoin::hashes::Hash;
+impl TryInto<serde_json::Value> for JsonResponse {
+ type Error = std::io::Error;
+ fn try_into(self) -> Result<serde_json::Value, std::io::Error> { Ok(self.0) }
+}
+
/// Conversion from `std::io::Error` into `BlockSourceError`.
impl From<std::io::Error> for BlockSourceError {
fn from(e: std::io::Error) -> BlockSourceError {
}
}
+/// Parses binary data as a block hash.
+impl TryInto<BlockHash> for BinaryResponse {
+ type Error = std::io::Error;
+
+ fn try_into(self) -> std::io::Result<BlockHash> {
+ BlockHash::from_slice(&self.0).map_err(|_|
+ std::io::Error::new(std::io::ErrorKind::InvalidData, "bad block hash length")
+ )
+ }
+}
+
/// Converts a JSON value into block header data. The JSON value may be an object representing a
/// block header or an array of such objects. In the latter case, the first object is converted.
impl TryInto<BlockHeaderData> for JsonResponse {
type Error = std::io::Error;
fn try_into(self) -> std::io::Result<BlockHeaderData> {
- let mut header = match self.0 {
+ let header = match self.0 {
serde_json::Value::Array(mut array) if !array.is_empty() => array.drain(..).next().unwrap(),
serde_json::Value::Object(_) => self.0,
_ => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "unexpected JSON type")),
}
// Add an empty previousblockhash for the genesis block.
- if let None = header.get("previousblockhash") {
- let hash: BlockHash = BlockHash::all_zeros();
- header.as_object_mut().unwrap().insert("previousblockhash".to_string(), serde_json::json!(hash.to_hex()));
- }
-
- match serde_json::from_value::<GetHeaderResponse>(header) {
- Err(_) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid header response")),
- Ok(response) => match response.try_into() {
- Err(_) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid header data")),
- Ok(header) => Ok(header),
- },
+ match header.try_into() {
+ Err(_) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid header data")),
+ Ok(header) => Ok(header),
}
}
}
-/// Response data from `getblockheader` RPC and `headers` REST requests.
-#[derive(Deserialize)]
-struct GetHeaderResponse {
- pub version: i32,
- pub merkleroot: String,
- pub time: u32,
- pub nonce: u32,
- pub bits: String,
- pub previousblockhash: String,
-
- pub chainwork: String,
- pub height: u32,
-}
+impl TryFrom<serde_json::Value> for BlockHeaderData {
+ type Error = ();
-/// Converts from `GetHeaderResponse` to `BlockHeaderData`.
-impl TryFrom<GetHeaderResponse> for BlockHeaderData {
- type Error = bitcoin::hashes::hex::Error;
+ fn try_from(response: serde_json::Value) -> Result<Self, ()> {
+ macro_rules! get_field { ($name: expr, $ty_access: tt) => {
+ response.get($name).ok_or(())?.$ty_access().ok_or(())?
+ } }
- fn try_from(response: GetHeaderResponse) -> Result<Self, bitcoin::hashes::hex::Error> {
Ok(BlockHeaderData {
header: BlockHeader {
- version: response.version,
- prev_blockhash: BlockHash::from_hex(&response.previousblockhash)?,
- merkle_root: TxMerkleNode::from_hex(&response.merkleroot)?,
- time: response.time,
- bits: u32::from_be_bytes(<[u8; 4]>::from_hex(&response.bits)?),
- nonce: response.nonce,
+ version: get_field!("version", as_i64).try_into().map_err(|_| ())?,
+ prev_blockhash: if let Some(hash_str) = response.get("previousblockhash") {
+ BlockHash::from_hex(hash_str.as_str().ok_or(())?).map_err(|_| ())?
+ } else { BlockHash::all_zeros() },
+ merkle_root: TxMerkleNode::from_hex(get_field!("merkleroot", as_str)).map_err(|_| ())?,
+ time: get_field!("time", as_u64).try_into().map_err(|_| ())?,
+ bits: u32::from_be_bytes(<[u8; 4]>::from_hex(get_field!("bits", as_str)).map_err(|_| ())?),
+ nonce: get_field!("nonce", as_u64).try_into().map_err(|_| ())?,
},
- chainwork: hex_to_uint256(&response.chainwork)?,
- height: response.height,
+ chainwork: hex_to_uint256(get_field!("chainwork", as_str)).map_err(|_| ())?,
+ height: get_field!("height", as_u64).try_into().map_err(|_| ())?,
})
}
}
}
}
+impl TryInto<BlockHash> for JsonResponse {
+ type Error = std::io::Error;
+
+ fn try_into(self) -> std::io::Result<BlockHash> {
+ match self.0.as_str() {
+ None => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON string")),
+ Some(hex_data) if hex_data.len() != 64 =>
+ Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid hash length")),
+ Some(hex_data) => BlockHash::from_str(hex_data)
+ .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid hex data")),
+ }
+ }
+}
+
+/// The REST `getutxos` endpoint retuns a whole pile of data we don't care about and one bit we do
+/// - whether the `hit bitmap` field had any entries. Thus we condense the result down into only
+/// that.
+pub(crate) struct GetUtxosResponse {
+ pub(crate) hit_bitmap_nonempty: bool
+}
+
+impl TryInto<GetUtxosResponse> for JsonResponse {
+ type Error = std::io::Error;
+
+ fn try_into(self) -> std::io::Result<GetUtxosResponse> {
+ let bitmap_str =
+ self.0.as_object().ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected an object"))?
+ .get("bitmap").ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "missing bitmap field"))?
+ .as_str().ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "bitmap should be an str"))?;
+ let mut hit_bitmap_nonempty = false;
+ for c in bitmap_str.chars() {
+ if c < '0' || c > '9' {
+ return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid byte"));
+ }
+ if c > '0' { hit_bitmap_nonempty = true; }
+ }
+ Ok(GetUtxosResponse { hit_bitmap_nonempty })
+ }
+}
+
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use bitcoin::blockdata::constants::genesis_block;
use bitcoin::hashes::Hash;
+ use bitcoin::hashes::hex::ToHex;
use bitcoin::network::constants::Network;
use serde_json::value::Number;
use serde_json::Value;
match TryInto::<BlockHeaderData>::try_into(response) {
Err(e) => {
assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
- assert_eq!(e.get_ref().unwrap().to_string(), "invalid header response");
+ assert_eq!(e.get_ref().unwrap().to_string(), "invalid header data");
},
Ok(_) => panic!("Expected error"),
}