Skip to content

fix(api): allow parsing empty TXIDs #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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)
}