Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use bitcoin::{
};

use serde::Deserialize;
use serde::Deserializer;

#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct PrevOut {
Expand All @@ -19,6 +20,7 @@ pub struct PrevOut {

#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Vin {
#[serde(deserialize_with = "deserialize_txid")]
pub txid: Txid,
pub vout: u32,
// None if coinbase
Expand Down Expand Up @@ -68,6 +70,7 @@ pub struct BlockStatus {

#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Tx {
#[serde(deserialize_with = "deserialize_txid")]
pub txid: Txid,
pub version: i32,
pub locktime: u32,
Expand Down Expand Up @@ -198,3 +201,17 @@ where
.collect::<Result<Vec<Vec<u8>>, _>>()
.map_err(serde::de::Error::custom)
}

fn deserialize_txid<'de, D>(deserializer: D) -> Result<Txid, D::Error>
where
D: Deserializer<'de>,
{
use bitcoin::hashes::Hash;
use std::str::FromStr;

let s = String::deserialize(deserializer)?;
if s.is_empty() {
return Ok(Txid::all_zeros());
}
Txid::from_str(&s).map_err(serde::de::Error::custom)
}