transaction/hash.rs: Add FromStr implementation (issue #299)
Co-authored-by: Deirdre Connolly <durumcrustulum@gmail.com>
This commit is contained in:
parent
46cb7c02f2
commit
6465689555
|
|
@ -3,7 +3,10 @@ use std::fmt;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use proptest_derive::Arbitrary;
|
use proptest_derive::Arbitrary;
|
||||||
|
|
||||||
use crate::{serialization::ZcashSerialize, sha256d_writer::Sha256dWriter};
|
use crate::{
|
||||||
|
serialization::{SerializationError, ZcashSerialize},
|
||||||
|
sha256d_writer::Sha256dWriter,
|
||||||
|
};
|
||||||
|
|
||||||
use super::Transaction;
|
use super::Transaction;
|
||||||
|
|
||||||
|
|
@ -33,6 +36,19 @@ impl fmt::Debug for TransactionHash {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::str::FromStr for TransactionHash {
|
||||||
|
type Err = SerializationError;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
let mut bytes = [0; 32];
|
||||||
|
if hex::decode_to_slice(s, &mut bytes[..]).is_err() {
|
||||||
|
Err(SerializationError::Parse("hex decoding error"))
|
||||||
|
} else {
|
||||||
|
Ok(TransactionHash(bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
@ -51,7 +67,16 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{:?}", hash),
|
format!("{:?}", hash),
|
||||||
"TransactionHash(\"bf46b4b5030752fedac6f884976162bbfb29a9398f104a280b3e34d51b416631\")"
|
r#"TransactionHash("bf46b4b5030752fedac6f884976162bbfb29a9398f104a280b3e34d51b416631")"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn transactionhash_from_str() {
|
||||||
|
let hash:TransactionHash = "bf46b4b5030752fedac6f884976162bbfb29a9398f104a280b3e34d51b416631".parse().unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
format!("{:?}", hash),
|
||||||
|
r#"TransactionHash("bf46b4b5030752fedac6f884976162bbfb29a9398f104a280b3e34d51b416631")"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue