From ef05c3a1df3d4c365e2d9211d05b14d40a1faa10 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Fri, 24 May 2024 14:48:16 +0200 Subject: [PATCH 01/17] Add a macro for defining custom error types This will be useful when I go through and add descriptive errors to everything everywhere --- src/lib.rs | 3 +++ src/macros.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/macros.rs diff --git a/src/lib.rs b/src/lib.rs index 99760c1..3404532 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,6 @@ +#[macro_use] +mod macros; + pub mod constants; pub mod crypto; pub mod primitives; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..f964310 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,54 @@ +/// Generate an enum to be used as an error type. +/// +/// This will automatically generate the enum, but also implement `Display` with given format +/// strings and optionally indicate the source error for errors which wrap another error. +/// +/// Example usage: +/// ```ignore +/// make_error_type!(pub MyError { +/// Unknown; "Unknown error", +/// IncorrectLength(length: usize); "Incorrect length {length}, expected 123", +/// InvalidHexData(source: hex::FromHexError); "Cannot convert hex string: {source}"; source, +/// }); +/// ``` +macro_rules! make_error_type { + (@fmt_args $tname:ident) => { Self::$tname }; + (@fmt_args $tname:ident ( $($targn:ident),+ )) => { Self::$tname($($targn),+) }; + + (@fmt_source) => { None }; + (@fmt_source $sourcen:expr) => { Some($sourcen) }; + + ($(#[derive( $($derive:ident),+ )])? $vis:vis $name:ident { + $( $tname:ident $(( $($targn:ident : $targ:ty),+ ))? ; $tmsg:literal $(; $sourcen:expr )?),+ $(,)? + }) => { + $( #[derive( $($derive),+ )] )? + #[derive(Clone, std::fmt::Debug)] + $vis enum $name { + $( $tname $(( $($targ),+ ))? ),+ + } + + impl std::error::Error for $name { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + $( + make_error_type!(@fmt_args $tname $(( $($targn),+ ))?) + => + make_error_type!(@fmt_source $($sourcen)?) + ),+ + } + } + } + + impl std::fmt::Display for $name { + fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + $( + make_error_type!(@fmt_args $tname $(( $($targn),+ ))?) + => + write!(_f, $tmsg) + ),+ + } + } + } + }; +} From 1831f38c3e4bb8012efa6480acf98f811f6ab879 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Thu, 6 Jun 2024 12:02:26 +0200 Subject: [PATCH 02/17] support errors with per-variant attributes --- src/macros.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index f964310..799d8ba 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -18,13 +18,24 @@ macro_rules! make_error_type { (@fmt_source) => { None }; (@fmt_source $sourcen:expr) => { Some($sourcen) }; - ($(#[derive( $($derive:ident),+ )])? $vis:vis $name:ident { - $( $tname:ident $(( $($targn:ident : $targ:ty),+ ))? ; $tmsg:literal $(; $sourcen:expr )?),+ $(,)? - }) => { - $( #[derive( $($derive),+ )] )? - #[derive(Clone, std::fmt::Debug)] + ( + $( #[$attr:meta] )* + $vis:vis enum $name:ident { + $( $( + #[$tattr:meta] )* + $tname:ident $(( $($targn:ident : $targ:ty),+ ))? + ; $tmsg:literal + $( ; $sourcen:expr )? + ),+ $(,)? + } + ) => { + $( #[$attr] )* + #[derive(::std::fmt::Debug)] $vis enum $name { - $( $tname $(( $($targ),+ ))? ),+ + $( + $( #[$tattr] )* + $tname $(( $($targ),+ ))? + ),+ } impl std::error::Error for $name { From 1101dd42f2d694af3d86537ee981ac69625a8961 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Fri, 21 Jun 2024 14:40:59 +0200 Subject: [PATCH 03/17] macros: make_error_type supports struct-like enums as well as tuple enums --- src/macros.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 799d8ba..76ff100 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -5,16 +5,13 @@ /// /// Example usage: /// ```ignore -/// make_error_type!(pub MyError { +/// make_error_type!(pub enum MyError { /// Unknown; "Unknown error", /// IncorrectLength(length: usize); "Incorrect length {length}, expected 123", /// InvalidHexData(source: hex::FromHexError); "Cannot convert hex string: {source}"; source, /// }); /// ``` macro_rules! make_error_type { - (@fmt_args $tname:ident) => { Self::$tname }; - (@fmt_args $tname:ident ( $($targn:ident),+ )) => { Self::$tname($($targn),+) }; - (@fmt_source) => { None }; (@fmt_source $sourcen:expr) => { Some($sourcen) }; @@ -23,8 +20,10 @@ macro_rules! make_error_type { $vis:vis enum $name:ident { $( $( #[$tattr:meta] )* - $tname:ident $(( $($targn:ident : $targ:ty),+ ))? - ; $tmsg:literal + $tname:ident + $(( $( $( #[$t_tuple_arg_attr:meta] )* $t_tuple_arg_name:ident : $t_tuple_arg_ty:ty),+ $(,)? ))? + $({ $( $( #[$t_struct_arg_attr:meta] )* $t_struct_arg_name:ident : $t_struct_arg_ty:ty),+ $(,)? })? + ; $tmsg:literal $(( $($tmsgarg:expr),* ))? $( ; $sourcen:expr )? ),+ $(,)? } @@ -34,15 +33,20 @@ macro_rules! make_error_type { $vis enum $name { $( $( #[$tattr] )* - $tname $(( $($targ),+ ))? + $tname + $(( $( $( #[$t_tuple_arg_attr] )* $t_tuple_arg_ty),+ ))? + $({ $( $( #[$t_struct_arg_attr] )* $t_struct_arg_name : $t_struct_arg_ty),+ })? ),+ } impl std::error::Error for $name { + #[allow(unused_variables)] fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { $( - make_error_type!(@fmt_args $tname $(( $($targn),+ ))?) + Self::$tname + $(( $($t_tuple_arg_name),+ ))? + $({ $($t_struct_arg_name),+ })? => make_error_type!(@fmt_source $($sourcen)?) ),+ @@ -54,9 +58,11 @@ macro_rules! make_error_type { fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { $( - make_error_type!(@fmt_args $tname $(( $($targn),+ ))?) + Self::$tname + $(( $($t_tuple_arg_name),+ ))? + $({ $($t_struct_arg_name),+ })? => - write!(_f, $tmsg) + write!(_f, $tmsg $($(, $tmsgarg)*)? ) ),+ } } From 15bf1b7153cd9b1aff6db17f66218f3c6744cbb4 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Wed, 26 Jun 2024 10:25:45 +0200 Subject: [PATCH 04/17] add traits for generating placeholder values for use in tests --- src/utils/mod.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index adb6987..5c0840b 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -51,3 +51,68 @@ pub fn add_btreemap( }); m1 } + +/// A trait which indicates that it is possible to acquire a "placeholder" value +/// of a type, which can be used for test purposes. +#[cfg(test)] +pub trait Placeholder : Sized { + /// Gets a placeholder value of this type which can be used for test purposes. + fn placeholder() -> Self; + + /// Gets an array of placeholder values of this type which can be used for test purposes. + fn placeholder_array() -> [Self; N] { + core::array::from_fn(|_| Self::placeholder()) + } +} + +/// A trait which indicates that it is possible to acquire a "placeholder" value +/// of a type, which can be used for test purposes. These placeholder values are consistent +/// across program runs. +#[cfg(test)] +pub trait PlaceholderIndexed : Sized { + /// Gets a dummy valid of this type which can be used for test purposes. + /// + /// This allows acquiring multiple distinct placeholder values which are still consistent + /// between runs. + /// + /// ### Arguments + /// + /// * `index` - the index of the placeholder value to obtain. Two placeholder values generated + /// from the same index are guaranteed to be equal (even across multiple test runs, + /// so long as the value format doesn't change). + fn placeholder_indexed(index: u64) -> Self; + + /// Gets an array of placeholder values of this type which can be used for test purposes. + fn placeholder_array_indexed(base_index: u64) -> [Self; N] { + core::array::from_fn(|n| Self::placeholder_indexed(base_index.wrapping_add(n as u64))) + } +} + +#[cfg(test)] +impl Placeholder for T { + fn placeholder() -> Self { + Self::placeholder_indexed(0) + } +} + +/// Generates the given number of pseudorandom bytes based on the given seed. +/// +/// This is intended to be used in tests, where random but reproducible placeholder values are often +/// required. +/// +/// ### Arguments +/// +/// * `seed_parts` - the parts of the seed, which will be concatenated to form the RNG seed +#[cfg(test)] +pub fn placeholder_bytes(seed_parts: &[&[u8]]) -> [u8; N] { + // Use Shake-256 to generate an arbitrarily large number of random bytes based on the given seed. + let mut shake256 = sha3::Shake256::default(); + for slice in seed_parts { + sha3::digest::Update::update(&mut shake256, slice); + } + let mut reader = sha3::digest::ExtendableOutput::finalize_xof(shake256); + + let mut res = [0u8; N]; + sha3::digest::XofReader::read(&mut reader, &mut res); + res +} From 8d30367bcda9b9fdcc4338dda86197a17a03fa60 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Fri, 17 May 2024 23:20:50 +0200 Subject: [PATCH 05/17] utils: add dedicated modules for serializing and deserializing arrays --- src/utils/mod.rs | 1 + src/utils/serialize_utils.rs | 353 +++++++++++++++++++++++++++++++++++ 2 files changed, 354 insertions(+) create mode 100644 src/utils/serialize_utils.rs diff --git a/src/utils/mod.rs b/src/utils/mod.rs index adb6987..d7b7f3e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -8,6 +8,7 @@ use crate::primitives::asset::TokenAmount; pub mod druid_utils; pub mod error_utils; pub mod script_utils; +pub mod serialize_utils; pub mod test_utils; pub mod transaction_utils; diff --git a/src/utils/serialize_utils.rs b/src/utils/serialize_utils.rs new file mode 100644 index 0000000..5d211e2 --- /dev/null +++ b/src/utils/serialize_utils.rs @@ -0,0 +1,353 @@ +use std::any::TypeId; +use std::convert::TryFrom; +use std::fmt::Formatter; +use std::marker::PhantomData; + +use bincode::config::*; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::de::{SeqAccess, Visitor}; +use serde::ser::{SerializeTuple}; + +pub fn bincode_default() -> WithOtherTrailing, RejectTrailing> { + DefaultOptions::new() + .with_fixint_encoding() + .reject_trailing_bytes() +} + +pub fn bincode_compact() -> WithOtherTrailing, RejectTrailing> { + DefaultOptions::new() + .with_varint_encoding() + .reject_trailing_bytes() +} + +/// A codec for fixed-size arrays. +pub mod fixed_array_codec { + use super::*; + + pub fn serialize( + values: &[T; N], + serializer: S, + ) -> Result { + if TypeId::of::() == TypeId::of::() && serializer.is_human_readable() { + // We're serializing a byte array for a human-readable format, make it a hex string + vec_codec::serialize(values, serializer) + } else { + // Serialize the array as a tuple, to avoid adding a length prefix + let mut tuple = serializer.serialize_tuple(N)?; + for e in values { + tuple.serialize_element(e)?; + } + tuple.end() + } + } + + pub fn deserialize<'de, T: Deserialize<'de> + 'static, D: Deserializer<'de>, const N: usize>( + deserializer: D, + ) -> Result<[T; N], D::Error> { + if TypeId::of::() == TypeId::of::() && deserializer.is_human_readable() { + // We're deserializing a byte array for a human-readable format, we'll accept two different + // representations: + // - A hexadecimal string + // - An array of byte literals (this format should never be produced by the serializer + // for human-readable formats, but it was in the past, so we'll still support reading + // it for backwards-compatibility). + vec_to_fixed_array(vec_codec::deserialize(deserializer)?) + } else { + // We're deserializing a binary format, read the array as a tuple + // (to avoid adding a length prefix) + + struct FixedArrayVisitor(PhantomData); + impl<'de, T: Deserialize<'de>, const N: usize> Visitor<'de> for FixedArrayVisitor { + type Value = [T; N]; + + fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result { + write!(formatter, "a sequence") + } + + fn visit_seq>(self, mut seq: A) -> Result { + let mut vec = Vec::with_capacity(N); + while let Some(val) = seq.next_element::()? { + vec.push(val) + } + vec_to_fixed_array(vec) + } + } + + deserializer.deserialize_tuple(N, FixedArrayVisitor(Default::default())) + } + } +} + +/// A codec for variable-length `Vec`s. +pub mod vec_codec { + use super::*; + + pub fn serialize( + values: &[T], + serializer: S, + ) -> Result { + if TypeId::of::() == TypeId::of::() && serializer.is_human_readable() { + // We're serializing a byte array for a human-readable format, make it a hex string + let bytes = unsafe { std::slice::from_raw_parts(values.as_ptr() as *const u8, values.len()) }; + serializer.serialize_str(&hex::encode(bytes)) + } else { + // Serialize the array as a length-prefixed sequence + values.serialize(serializer) + } + } + + pub fn deserialize<'de, T: Deserialize<'de> + 'static, D: Deserializer<'de>>( + deserializer: D, + ) -> Result, D::Error> { + if TypeId::of::() == TypeId::of::() && deserializer.is_human_readable() { + // We're deserializing a byte array for a human-readable format, we'll accept two different + // representations: + // - A hexadecimal string + // - An array of byte literals (this format should never be produced by the serializer + // for human-readable formats, but it was in the past, so we'll still support reading + // it for backwards-compatibility). + + struct HexStringOrBytesVisitor(); + impl<'de> Visitor<'de> for HexStringOrBytesVisitor { + type Value = Vec; + + fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result { + formatter.write_str("hex string or byte array") + } + + fn visit_str(self, value: &str) -> Result { + hex::decode(value).map_err(E::custom) + } + + fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de> { + let mut vec = Vec::new(); + while let Some(elt) = seq.next_element::()? { + vec.push(elt); + } + Ok(vec) + } + } + + Ok(deserializer.deserialize_any(HexStringOrBytesVisitor())?.into_iter() + // This is a hack to convert the Vec into a Vec, even though we already know + // that T = u8. This could be done in a much nicer way if trait specialization were + // a thing, but unfortunately it's still only available on nightly :( + .map(|b| unsafe { std::mem::transmute_copy::(&b) }) + .collect::>()) + } else { + // Read a length-prefixed sequence as a Vec + >::deserialize(deserializer) + } + } +} + +fn vec_to_fixed_array( + vec: Vec, +) -> Result<[T; N], E> { + <[T; N]>::try_from(vec) + .map_err(|vec| E::custom(format!("expected exactly {} elements, but read {}", N, vec.len()))) +} + +/*---- TESTS ----*/ + +#[cfg(test)] +mod tests { + use std::fmt::Debug; + use bincode::Options; + + use serde::{Deserialize, Serialize}; + use serde::de::DeserializeOwned; + use super::*; + + fn repeat(orig: &str, n: usize) -> String { + let mut res = String::with_capacity(orig.len() * n); + for _ in 0..n { + res.push_str(orig) + } + res + } + + fn test_bin_codec( + options: fn() -> O, + obj: T, + expect: &str, + ) { + let bytes = options().serialize(&obj).unwrap(); + assert_eq!(hex::encode(&bytes), expect); + assert_eq!(options().deserialize::(&bytes).unwrap(), obj); + } + + fn test_json_codec( + obj: T, + expect: &str, + ) { + let json = serde_json::to_string(&obj).unwrap(); + assert_eq!(json, expect); + assert_eq!(serde_json::from_str::(&json).unwrap(), obj); + } + + fn test_json_deserialize( + obj: T, + json: &str, + ) { + assert_eq!(serde_json::from_str::(&json).unwrap(), obj); + } + + macro_rules! test_fixed_array { + ($n:literal) => { + test_bin_codec(bincode_default, [VAL; $n], &repeat(HEX, $n)); + test_json_codec([VAL; $n], &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + macro_rules! test_fixed_array_wrapper { + ($e:ty, $t:ident, $n:literal) => { + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct $t([$e; $n]); + test_bin_codec(bincode_default, $t([VAL; $n]), &repeat(HEX, $n)); + test_json_codec($t([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + #[test] + fn test_fixed_u32_arrays() { + const VAL : u32 = 0xDEADBEEF; + const HEX : &str = "efbeadde"; + + test_fixed_array!(0); + test_fixed_array!(1); + test_fixed_array!(32); + + test_fixed_array_wrapper!(u32, FixedArrayWrapper0, 0); + test_fixed_array_wrapper!(u32, FixedArrayWrapper1, 1); + test_fixed_array_wrapper!(u32, FixedArrayWrapper32, 32); + + macro_rules! test_fixed_array_wrapper_codec { + ($t:ident, $n:literal) => { + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct $t(#[serde(with = "fixed_array_codec")] [u32; $n]); + test_bin_codec(bincode_default, $t([VAL; $n]), &repeat(HEX, $n)); + test_json_codec($t([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper0, 0); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper1, 1); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper32, 32); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper33, 33); + } + + #[test] + fn test_fixed_u8_arrays() { + const VAL : u8 = 123; + const HEX : &str = "7b"; + + test_fixed_array!(0); + test_fixed_array!(1); + test_fixed_array!(32); + + test_fixed_array_wrapper!(u8, FixedArrayWrapper0, 0); + test_fixed_array_wrapper!(u8, FixedArrayWrapper1, 1); + test_fixed_array_wrapper!(u8, FixedArrayWrapper32, 32); + + macro_rules! test_fixed_array_wrapper_codec { + ($t:ident, $n:literal) => { + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct $t(#[serde(with = "fixed_array_codec")] [u8; $n]); + test_bin_codec(bincode_default, $t([VAL; $n]), &repeat(HEX, $n)); + test_json_codec($t([VAL; $n]), &format!("\"{}\"", hex::encode(&[VAL; $n].to_vec()))); + test_json_deserialize($t([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper0, 0); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper1, 1); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper32, 32); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper33, 33); + } + + fn size_to_hex_default(n: usize) -> String { + hex::encode(bincode_default().serialize(&n).unwrap()) + } + + macro_rules! test_vec { + ($n:literal) => { + test_bin_codec(bincode_default, [VAL; $n].to_vec(), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_json_codec([VAL; $n].to_vec(), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + macro_rules! test_vec_wrapper { + ($n:literal) => { + test_bin_codec(bincode_default, VecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_json_codec(VecWrapper([VAL; $n].to_vec()), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + #[test] + fn test_u32_vecs() { + const VAL : u32 = 0xDEADBEEF; + const HEX : &str = "efbeadde"; + + test_vec!(0); + test_vec!(1); + test_vec!(32); + test_vec!(33); + + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct VecWrapper(Vec); + + test_vec_wrapper!(0); + test_vec_wrapper!(1); + test_vec_wrapper!(32); + test_vec_wrapper!(33); + + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct CodecVecWrapper(#[serde(with = "vec_codec")] Vec); + macro_rules! test_vec_wrapper_codec { + ($n:literal) => { + test_bin_codec(bincode_default, CodecVecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_json_codec(CodecVecWrapper([VAL; $n].to_vec()), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + test_vec_wrapper_codec!(0); + test_vec_wrapper_codec!(1); + test_vec_wrapper_codec!(32); + test_vec_wrapper_codec!(33); + } + + #[test] + fn test_u8_vecs() { + const VAL : u8 = 123; + const HEX : &str = "7b"; + + test_vec!(0); + test_vec!(1); + test_vec!(32); + test_vec!(33); + + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct VecWrapper(Vec); + + test_vec_wrapper!(0); + test_vec_wrapper!(1); + test_vec_wrapper!(32); + test_vec_wrapper!(33); + + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct CodecVecWrapper(#[serde(with = "vec_codec")] Vec); + macro_rules! test_vec_wrapper_codec { + ($n:literal) => { + test_bin_codec(bincode_default, CodecVecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_json_codec(CodecVecWrapper([VAL; $n].to_vec()), &format!("\"{}\"", hex::encode(&[VAL; $n].to_vec()))); + test_json_deserialize(CodecVecWrapper([VAL; $n].to_vec()), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + test_vec_wrapper_codec!(0); + test_vec_wrapper_codec!(1); + test_vec_wrapper_codec!(32); + test_vec_wrapper_codec!(33); + } +} From 7097e54d4342bcdc1123769240ea227b1bbfdb51 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Wed, 29 May 2024 12:56:17 +0200 Subject: [PATCH 06/17] Add a struct for representing fixed-length byte arrays This allows other structs which wrap a fixed-length byte array to simply use it as a field, and not have to deal with any of the details of formatting or JSON hex (de)serialization. --- src/utils/serialize_utils.rs | 146 +++++++++++++++++++++++++++++++++-- 1 file changed, 141 insertions(+), 5 deletions(-) diff --git a/src/utils/serialize_utils.rs b/src/utils/serialize_utils.rs index 5d211e2..bd199fc 100644 --- a/src/utils/serialize_utils.rs +++ b/src/utils/serialize_utils.rs @@ -1,7 +1,9 @@ use std::any::TypeId; -use std::convert::TryFrom; -use std::fmt::Formatter; +use std::convert::{TryFrom, TryInto}; +use std::fmt; use std::marker::PhantomData; +use std::ops::{Deref, DerefMut}; +use std::str::FromStr; use bincode::config::*; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -20,6 +22,104 @@ pub fn bincode_compact() -> WithOtherTrailing( + #[serde(with = "fixed_array_codec")] + [u8; N], +); + +impl FixedByteArray { + pub fn new(arr: [u8; N]) -> Self { + Self(arr) + } +} + +impl AsRef<[u8]> for FixedByteArray { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} + +impl AsMut<[u8]> for FixedByteArray { + fn as_mut(&mut self) -> &mut [u8] { + &mut self.0 + } +} + +impl Deref for FixedByteArray { + type Target = [u8; N]; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for FixedByteArray { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl From<[u8; N]> for FixedByteArray { + fn from(value: [u8; N]) -> Self { + Self(value) + } +} + +impl TryFrom<&[u8]> for FixedByteArray { + type Error = std::array::TryFromSliceError; + + fn try_from(value: &[u8]) -> Result { + value.try_into().map(Self) + } +} + +impl TryFrom<&Vec> for FixedByteArray { + type Error = std::array::TryFromSliceError; + + fn try_from(value: &Vec) -> Result { + value.as_slice().try_into().map(Self) + } +} + +impl fmt::LowerHex for FixedByteArray { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // This is hacky because we can't make an array of type [u8; {N * 2}] due to + // generic parameters not being allowed in constant expressions on stable rust + assert_eq!(std::mem::size_of::<[u16; N]>(), std::mem::size_of::<[u8; N]>() * 2); + let mut buf = [0u16; N]; + let slice = unsafe { std::slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, N * 2) }; + hex::encode_to_slice(&self.0, slice).unwrap(); + f.write_str(std::str::from_utf8(slice).unwrap()) + } +} + +impl fmt::Display for FixedByteArray { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::LowerHex::fmt(self, f) + } +} + +impl fmt::Debug for FixedByteArray { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "FixedByteArray<{N}>({self:x})") + } +} + +impl FromStr for FixedByteArray { + type Err = hex::FromHexError; + + fn from_str(s: &str) -> Result { + let mut buf = [0u8; N]; + hex::decode_to_slice(s, &mut buf)?; + Ok(Self(buf)) + } +} + /// A codec for fixed-size arrays. pub mod fixed_array_codec { use super::*; @@ -60,7 +160,7 @@ pub mod fixed_array_codec { impl<'de, T: Deserialize<'de>, const N: usize> Visitor<'de> for FixedArrayVisitor { type Value = [T; N]; - fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!(formatter, "a sequence") } @@ -111,7 +211,7 @@ pub mod vec_codec { impl<'de> Visitor<'de> for HexStringOrBytesVisitor { type Value = Vec; - fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("hex string or byte array") } @@ -152,7 +252,7 @@ fn vec_to_fixed_array( #[cfg(test)] mod tests { - use std::fmt::Debug; + use std::fmt::{Debug, Display}; use bincode::Options; use serde::{Deserialize, Serialize}; @@ -193,6 +293,15 @@ mod tests { assert_eq!(serde_json::from_str::(&json).unwrap(), obj); } + fn test_display_fromstr( + obj: T, + expect: &str, + ) { + let string = obj.to_string(); + assert_eq!(string, expect); + assert_eq!(::from_str(&string).ok().unwrap(), obj); + } + macro_rules! test_fixed_array { ($n:literal) => { test_bin_codec(bincode_default, [VAL; $n], &repeat(HEX, $n)); @@ -350,4 +459,31 @@ mod tests { test_vec_wrapper_codec!(32); test_vec_wrapper_codec!(33); } + + #[test] + fn test_fixed_byte_array() { + const VAL : u8 = 123; + const HEX : &str = "7b"; + + macro_rules! test_fixed_byte_array { + ($n:literal) => { + test_bin_codec(bincode_default, FixedByteArray::<$n>([VAL; $n]), &repeat(HEX, $n)); + test_json_codec(FixedByteArray::<$n>([VAL; $n]), &format!("\"{}\"", repeat(HEX, $n))); + test_json_deserialize(FixedByteArray::<$n>([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + test_display_fromstr(FixedByteArray::<$n>([VAL; $n]), &repeat(HEX, $n)); + assert_eq!(format!("{:x}", FixedByteArray::<$n>([VAL; $n])), repeat(HEX, $n)); + assert_eq!( + format!("{:?}", FixedByteArray::<$n>([VAL; $n])), + format!("FixedByteArray<{}>({})", $n, repeat(HEX, $n))); + assert_eq!( + format!("{:x?}", FixedByteArray::<$n>([VAL; $n])), + format!("FixedByteArray<{}>({})", $n, repeat(HEX, $n))); + }; + } + + test_fixed_byte_array!(0); + test_fixed_byte_array!(1); + test_fixed_byte_array!(32); + test_fixed_byte_array!(33); + } } From 4be53f5a1ed14aac6a73ed340b2ee826799fd81f Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Wed, 29 May 2024 12:56:17 +0200 Subject: [PATCH 07/17] serialize_utils: implement From<&[u8; N]> for FixedByteArray --- src/utils/serialize_utils.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/utils/serialize_utils.rs b/src/utils/serialize_utils.rs index bd199fc..af72618 100644 --- a/src/utils/serialize_utils.rs +++ b/src/utils/serialize_utils.rs @@ -70,6 +70,12 @@ impl From<[u8; N]> for FixedByteArray { } } +impl From<&[u8; N]> for FixedByteArray { + fn from(value: &[u8; N]) -> Self { + Self(*value) + } +} + impl TryFrom<&[u8]> for FixedByteArray { type Error = std::array::TryFromSliceError; From 37b51faa04ec9830e96193fd38943b6bd64a3fe0 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Thu, 11 Jul 2024 12:22:30 +0200 Subject: [PATCH 08/17] macros: add traits for generating simple enums --- src/macros.rs | 141 +++++++++++++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 38 +++++++++++++ 2 files changed, 179 insertions(+) diff --git a/src/macros.rs b/src/macros.rs index 76ff100..99f75b3 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -69,3 +69,144 @@ macro_rules! make_error_type { } }; } + +/// Generate an enum where each variant represents a unique named value. +/// +/// This will automatically generate the enum, but also implement the following traits: +/// * `ToName` and `FromName` with the variant names +/// * `Display` with the variant names +/// +/// Additionally, a constant array containing every enum variant will be generated with the +/// given name and visibility. +/// +/// Example usage: +/// ```ignore +/// make_trivial_enum!(pub enum MyError { +/// Hello, +/// World, +/// } +/// all_variants=pub(crate) ALL_VARIANTS); +/// ``` +macro_rules! make_trivial_enum { + ( + @impl_toname_fromname_display + $ename:ident { + $( $vname:ident ),+ + } + $all_variants_vis:vis $all_variants:ident + ) => { + impl $ename { + #[allow(unused)] + $all_variants_vis const $all_variants : &'static [Self] = + &[ $( Self::$vname ),+ ]; + } + + impl crate::utils::ToName for $ename { + fn to_name(&self) -> &'static str { + match self { + $( Self::$vname => stringify!($vname) ),+ + } + } + } + + impl crate::utils::FromName for $ename { + const ALL_NAMES: &'static [&'static str] = &[ $( stringify!($vname) ),+ ]; + + fn from_name(name: &str) -> Result { + match name { + $( stringify!($vname) => Ok(Self::$vname), )+ + _ => Err(name), + } + } + } + + impl std::fmt::Display for $ename { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str(crate::utils::ToName::to_name(self)) + } + } + }; + + ( + $( #[$eattr:meta] )* + $evis:vis enum $ename:ident { + $( + $( #[$vattr:meta] )* + $vname:ident, + )+ + } + all_variants=$all_variants_vis:vis $all_variants:ident + ) => { + $( #[$eattr] )* + $evis enum $ename { + $( $( #[$vattr] )* $vname ),+ + } + + make_trivial_enum!( + @impl_toname_fromname_display $ename { + $( $vname ),+ + } + $all_variants_vis $all_variants); + }; +} + +/// Generate an enum where each variant simply wraps a numeric ordinal number. +/// +/// This will automatically generate the enum, but also implement the following traits: +/// * `ToOrdinal` and `FromOrdinal` with the given ordinal numbers +/// * `ToName` and `FromName` with the variant names +/// * `Display` with the variant names +/// +/// Additionally, a constant array containing every enum variant will be generated with the +/// given name and visibility. +/// +/// Example usage: +/// ```ignore +/// make_ordinal_enum!(pub enum MyError { +/// Hello = 2, +/// World = 3, +/// } +/// all_variants=pub(crate) ALL_VARIANTS); +/// ``` +macro_rules! make_ordinal_enum { + ( + $( #[$eattr:meta] )* + $evis:vis enum $ename:ident { + $( + $( #[$vattr:meta] )* + $vname:ident = $vord:literal, + )+ + } + all_variants=$all_variants_vis:vis $all_variants:ident + ) => { + $( #[$eattr] )* + $evis enum $ename { + $( $( #[$vattr] )* $vname = $vord, )+ + } + + make_trivial_enum!( + @impl_toname_fromname_display $ename { + $( $vname ),+ + } + $all_variants_vis $all_variants); + + impl crate::utils::ToOrdinal for $ename { + fn to_ordinal(&self) -> u32 { + match self { + $( Self::$vname => $vord ),+ + } + } + } + + impl crate::utils::FromOrdinal for $ename { + const ALL_ORDINALS: &'static [u32] = &[ $( $vord ),+ ]; + + fn from_ordinal(ordinal: u32) -> Result { + match ordinal { + $( $vord => Ok(Self::$vname), )+ + _ => Err(ordinal), + } + } + } + }; +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index adb6987..fad88ff 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -51,3 +51,41 @@ pub fn add_btreemap( }); m1 } + +/// A trait which indicates that a type can be represented by an ordinal number. +pub trait ToOrdinal { + /// Gets the ordinal number from a value. + fn to_ordinal(&self) -> u32; +} + +/// A trait which indicates that a type can be instantiated from an ordinal number. +pub trait FromOrdinal : Sized { + /// A slice containing every valid ordinal number. + const ALL_ORDINALS : &'static [u32]; + + /// Gets the value corresponding to the given ordinal number. + /// + /// ### Arguments + /// + /// * `ordinal` - The ordinal number + fn from_ordinal(ordinal: u32) -> Result; +} + +/// A trait which indicates that a type can be represented by a string name. +pub trait ToName { + /// Gets a value's string name. + fn to_name(&self) -> &'static str; +} + +/// A trait which indicates that a type can be instantiated from a string name. +pub trait FromName : Sized { + /// A slice containing every valid name. + const ALL_NAMES : &'static [&'static str]; + + /// Gets the value corresponding to the given name. + /// + /// ### Arguments + /// + /// * `name` - The name + fn from_name(name: &str) -> Result; +} \ No newline at end of file From 87f48d6d3e5512b35bdbc498f29cdeffaab08294 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Mon, 3 Jun 2024 09:38:17 +0200 Subject: [PATCH 09/17] always use fully-qualified name for bincode (de)serialize functions this makes it clear which serialization format is being used where, as we are going to be changing this up soon --- src/primitives/block.rs | 7 +++---- src/primitives/transaction.rs | 11 ----------- src/script/interface_ops.rs | 2 -- src/script/lang.rs | 2 -- src/utils/script_utils.rs | 2 -- src/utils/transaction_utils.rs | 10 +++------- 6 files changed, 6 insertions(+), 28 deletions(-) diff --git a/src/primitives/block.rs b/src/primitives/block.rs index 72e07e9..3397424 100644 --- a/src/primitives/block.rs +++ b/src/primitives/block.rs @@ -4,7 +4,6 @@ use crate::crypto::sha3_256::{self, Sha3_256}; use crate::crypto::sign_ed25519::PublicKey; use crate::primitives::asset::Asset; use crate::primitives::transaction::{Transaction, TxIn, TxOut}; -use bincode::{deserialize, serialize}; use bytes::Bytes; use serde::{Deserialize, Serialize}; use std::convert::TryInto; @@ -82,7 +81,7 @@ impl Block { /// Sets the internal number of bits based on length pub fn set_bits(&mut self) { - let bytes = Bytes::from(match serialize(&self) { + let bytes = Bytes::from(match bincode::serialize(&self) { Ok(bytes) => bytes, Err(e) => { warn!("Failed to serialize block: {:?}", e); @@ -94,7 +93,7 @@ impl Block { /// Checks whether a block has hit its maximum size pub fn is_full(&self) -> bool { - let bytes = Bytes::from(match serialize(&self) { + let bytes = Bytes::from(match bincode::serialize(&self) { Ok(bytes) => bytes, Err(e) => { warn!("Failed to serialize block: {:?}", e); @@ -143,7 +142,7 @@ pub fn gen_random_hash() -> String { /// /// * `transactions` - Transactions to construct a merkle tree for pub fn build_hex_txs_hash(transactions: &[String]) -> String { - let txs = match serialize(transactions) { + let txs = match bincode::serialize(transactions) { Ok(bytes) => bytes, Err(e) => { warn!("Failed to serialize transactions: {:?}", e); diff --git a/src/primitives/transaction.rs b/src/primitives/transaction.rs index 08435fd..e1d7236 100644 --- a/src/primitives/transaction.rs +++ b/src/primitives/transaction.rs @@ -8,8 +8,6 @@ use crate::primitives::{ use crate::script::lang::Script; use crate::script::{OpCodes, StackEntry}; use crate::utils::is_valid_amount; -use bincode::serialize; -use bytes::Bytes; use serde::{Deserialize, Serialize}; use std::fmt; @@ -205,15 +203,6 @@ impl Transaction { } } - /// Get the total transaction size in bytes - pub fn get_total_size(&self) -> usize { - let bytes = match serialize(self) { - Ok(bytes) => bytes, - Err(_) => vec![], - }; - bytes.len() - } - /// Gets the create asset assigned to this transaction, if it exists fn get_create_asset(&self) -> Option<&Asset> { let is_create = self.inputs.len() == 1 diff --git a/src/script/interface_ops.rs b/src/script/interface_ops.rs index 7b9875b..ba2a63c 100644 --- a/src/script/interface_ops.rs +++ b/src/script/interface_ops.rs @@ -11,8 +11,6 @@ use crate::utils::error_utils::*; use crate::utils::transaction_utils::{ construct_address, construct_address_temp, construct_address_v0, }; -use bincode::de; -use bincode::serialize; use bytes::Bytes; use hex::encode; use std::collections::BTreeMap; diff --git a/src/script/lang.rs b/src/script/lang.rs index 259465e..2498e5c 100644 --- a/src/script/lang.rs +++ b/src/script/lang.rs @@ -8,8 +8,6 @@ use crate::script::interface_ops::*; use crate::script::{OpCodes, StackEntry}; use crate::utils::error_utils::*; use crate::utils::transaction_utils::{construct_address, construct_address_for}; -use bincode::serialize; -use bytes::Bytes; use hex::encode; use serde::{Deserialize, Serialize}; use tracing::{error, warn}; diff --git a/src/utils/script_utils.rs b/src/utils/script_utils.rs index a2cd085..fca79da 100644 --- a/src/utils/script_utils.rs +++ b/src/utils/script_utils.rs @@ -15,8 +15,6 @@ use crate::utils::transaction_utils::{ construct_address, construct_tx_hash, construct_tx_in_out_signable_hash, construct_tx_in_signable_asset_hash, construct_tx_in_signable_hash, }; -use bincode::serialize; -use bytes::Bytes; use hex::encode; use ring::error; use std::collections::{BTreeMap, BTreeSet}; diff --git a/src/utils/transaction_utils.rs b/src/utils/transaction_utils.rs index 8e9fd99..9a96af1 100644 --- a/src/utils/transaction_utils.rs +++ b/src/utils/transaction_utils.rs @@ -6,7 +6,6 @@ use crate::primitives::druid::{DdeValues, DruidExpectation}; use crate::primitives::transaction::*; use crate::script::lang::Script; use crate::script::{OpCodes, StackEntry}; -use bincode::serialize; use std::collections::BTreeMap; use tracing::debug; @@ -21,10 +20,7 @@ pub struct ReceiverInfo { /// /// * `script` - Script to build address for pub fn construct_p2sh_address(script: &Script) -> String { - let bytes = match serialize(script) { - Ok(bytes) => bytes, - Err(_) => vec![], - }; + let bytes = bincode::serialize(script).unwrap(); let mut addr = hex::encode(sha3_256::digest(&bytes)); addr.insert(ZERO, P2SH_PREPEND as char); addr.truncate(STANDARD_ADDRESS_LENGTH); @@ -344,7 +340,7 @@ pub fn update_utxo_set(current_utxo: &mut BTreeMap) { /// /// * `tx` - Transaction to hash pub fn construct_tx_hash(tx: &Transaction) -> String { - let bytes = match serialize(tx) { + let bytes = match bincode::serialize(tx) { Ok(bytes) => bytes, Err(_) => vec![], }; @@ -1179,7 +1175,7 @@ mod tests { ..Default::default() }]; - let bytes = match serialize(&tx_ins) { + let bytes = match bincode::serialize(&tx_ins) { Ok(bytes) => bytes, Err(_) => vec![], }; From feef369594a8cc6d7c4f6ca673999f6de17b3f8d Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Thu, 6 Jun 2024 14:17:56 +0200 Subject: [PATCH 10/17] Upgrade to bincode 2 This will enable us to keep JSON serialization separate from the binary encoding, and also be much more explicit about the exact binary representation of objects. --- Cargo.lock | 20 ++++++++++++-- Cargo.toml | 2 +- src/primitives/block.rs | 11 ++++---- src/utils/serialize_utils.rs | 50 ++++++++++++++++++---------------- src/utils/transaction_utils.rs | 6 ++-- 5 files changed, 54 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b6575dd..009c39b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -108,13 +108,23 @@ checksum = "0ea22880d78093b0cbe17c89f64a7d457941e65759157ec6cb31a31d652b05e5" [[package]] name = "bincode" -version = "1.3.3" +version = "2.0.0-rc.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +checksum = "f11ea1a0346b94ef188834a65c068a03aec181c94896d481d7a0a40d85b0ce95" dependencies = [ + "bincode_derive", "serde", ] +[[package]] +name = "bincode_derive" +version = "2.0.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e30759b3b99a1b802a7a3aa21c85c3ded5c28e1c83170d82d70f08bbf7f3e4c" +dependencies = [ + "virtue", +] + [[package]] name = "bindgen" version = "0.65.1" @@ -1068,6 +1078,12 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "virtue" +version = "0.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dcc60c0624df774c82a0ef104151231d37da4962957d691c011c852b2473314" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 5c6fd2e..ee03c21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ exclude = ["**/tests/**", "**/examples/**", "**/benchmarks/**", "docs/**", ".hoo [dependencies] actix-rt = "2.8.0" base64 = "0.20.0" -bincode = "1.3.3" +bincode = { version = "2.0.0-rc.3", features = ["serde"] } bytes = "1.4.0" colored = { version = "2.1.0", optional = true } hex = "0.4.3" diff --git a/src/primitives/block.rs b/src/primitives/block.rs index 3397424..18cb611 100644 --- a/src/primitives/block.rs +++ b/src/primitives/block.rs @@ -81,25 +81,25 @@ impl Block { /// Sets the internal number of bits based on length pub fn set_bits(&mut self) { - let bytes = Bytes::from(match bincode::serialize(&self) { + let bytes = match bincode::serde::encode_to_vec(&self, bincode::config::legacy()) { Ok(bytes) => bytes, Err(e) => { warn!("Failed to serialize block: {:?}", e); return; } - }); + }; self.header.bits = bytes.len(); } /// Checks whether a block has hit its maximum size pub fn is_full(&self) -> bool { - let bytes = Bytes::from(match bincode::serialize(&self) { + let bytes = match bincode::serde::encode_to_vec(&self, bincode::config::legacy()) { Ok(bytes) => bytes, Err(e) => { warn!("Failed to serialize block: {:?}", e); return false; } - }); + }; bytes.len() >= MAX_BLOCK_SIZE } @@ -142,7 +142,8 @@ pub fn gen_random_hash() -> String { /// /// * `transactions` - Transactions to construct a merkle tree for pub fn build_hex_txs_hash(transactions: &[String]) -> String { - let txs = match bincode::serialize(transactions) { + // TODO: This is bad, it won't produce the same result on 32-bit systems + let txs = match bincode::serde::encode_to_vec(transactions, bincode::config::legacy()) { Ok(bytes) => bytes, Err(e) => { warn!("Failed to serialize transactions: {:?}", e); diff --git a/src/utils/serialize_utils.rs b/src/utils/serialize_utils.rs index af72618..39eb7ea 100644 --- a/src/utils/serialize_utils.rs +++ b/src/utils/serialize_utils.rs @@ -5,21 +5,24 @@ use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; use std::str::FromStr; -use bincode::config::*; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::de::{SeqAccess, Visitor}; use serde::ser::{SerializeTuple}; -pub fn bincode_default() -> WithOtherTrailing, RejectTrailing> { - DefaultOptions::new() - .with_fixint_encoding() - .reject_trailing_bytes() +/// Implements `Write` by simply counting the number of bytes written to it. +pub struct ByteCountingWriter { + pub count: usize, } -pub fn bincode_compact() -> WithOtherTrailing, RejectTrailing> { - DefaultOptions::new() - .with_varint_encoding() - .reject_trailing_bytes() +impl std::io::Write for ByteCountingWriter { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.count += buf.len(); + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } } /// Simple wrapper around a fixed-length byte array. @@ -259,7 +262,6 @@ fn vec_to_fixed_array( #[cfg(test)] mod tests { use std::fmt::{Debug, Display}; - use bincode::Options; use serde::{Deserialize, Serialize}; use serde::de::DeserializeOwned; @@ -273,14 +275,14 @@ mod tests { res } - fn test_bin_codec( - options: fn() -> O, + fn test_bin_codec( + config: fn() -> C, obj: T, expect: &str, ) { - let bytes = options().serialize(&obj).unwrap(); + let bytes = bincode::serde::encode_to_vec(&obj, config()).unwrap(); assert_eq!(hex::encode(&bytes), expect); - assert_eq!(options().deserialize::(&bytes).unwrap(), obj); + assert_eq!(bincode::serde::decode_from_slice::(&bytes, config()).unwrap().0, obj); } fn test_json_codec( @@ -310,7 +312,7 @@ mod tests { macro_rules! test_fixed_array { ($n:literal) => { - test_bin_codec(bincode_default, [VAL; $n], &repeat(HEX, $n)); + test_bin_codec(bincode::config::legacy, [VAL; $n], &repeat(HEX, $n)); test_json_codec([VAL; $n], &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); }; } @@ -319,7 +321,7 @@ mod tests { ($e:ty, $t:ident, $n:literal) => { #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] struct $t([$e; $n]); - test_bin_codec(bincode_default, $t([VAL; $n]), &repeat(HEX, $n)); + test_bin_codec(bincode::config::legacy, $t([VAL; $n]), &repeat(HEX, $n)); test_json_codec($t([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); }; } @@ -341,7 +343,7 @@ mod tests { ($t:ident, $n:literal) => { #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] struct $t(#[serde(with = "fixed_array_codec")] [u32; $n]); - test_bin_codec(bincode_default, $t([VAL; $n]), &repeat(HEX, $n)); + test_bin_codec(bincode::config::legacy, $t([VAL; $n]), &repeat(HEX, $n)); test_json_codec($t([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); }; } @@ -369,7 +371,7 @@ mod tests { ($t:ident, $n:literal) => { #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] struct $t(#[serde(with = "fixed_array_codec")] [u8; $n]); - test_bin_codec(bincode_default, $t([VAL; $n]), &repeat(HEX, $n)); + test_bin_codec(bincode::config::legacy, $t([VAL; $n]), &repeat(HEX, $n)); test_json_codec($t([VAL; $n]), &format!("\"{}\"", hex::encode(&[VAL; $n].to_vec()))); test_json_deserialize($t([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); }; @@ -382,19 +384,19 @@ mod tests { } fn size_to_hex_default(n: usize) -> String { - hex::encode(bincode_default().serialize(&n).unwrap()) + hex::encode(&(n as u64).to_le_bytes()) } macro_rules! test_vec { ($n:literal) => { - test_bin_codec(bincode_default, [VAL; $n].to_vec(), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_bin_codec(bincode::config::legacy, [VAL; $n].to_vec(), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); test_json_codec([VAL; $n].to_vec(), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); }; } macro_rules! test_vec_wrapper { ($n:literal) => { - test_bin_codec(bincode_default, VecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_bin_codec(bincode::config::legacy, VecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); test_json_codec(VecWrapper([VAL; $n].to_vec()), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); }; } @@ -421,7 +423,7 @@ mod tests { struct CodecVecWrapper(#[serde(with = "vec_codec")] Vec); macro_rules! test_vec_wrapper_codec { ($n:literal) => { - test_bin_codec(bincode_default, CodecVecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_bin_codec(bincode::config::legacy, CodecVecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); test_json_codec(CodecVecWrapper([VAL; $n].to_vec()), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); }; } @@ -454,7 +456,7 @@ mod tests { struct CodecVecWrapper(#[serde(with = "vec_codec")] Vec); macro_rules! test_vec_wrapper_codec { ($n:literal) => { - test_bin_codec(bincode_default, CodecVecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_bin_codec(bincode::config::legacy, CodecVecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); test_json_codec(CodecVecWrapper([VAL; $n].to_vec()), &format!("\"{}\"", hex::encode(&[VAL; $n].to_vec()))); test_json_deserialize(CodecVecWrapper([VAL; $n].to_vec()), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); }; @@ -473,7 +475,7 @@ mod tests { macro_rules! test_fixed_byte_array { ($n:literal) => { - test_bin_codec(bincode_default, FixedByteArray::<$n>([VAL; $n]), &repeat(HEX, $n)); + test_bin_codec(bincode::config::legacy, FixedByteArray::<$n>([VAL; $n]), &repeat(HEX, $n)); test_json_codec(FixedByteArray::<$n>([VAL; $n]), &format!("\"{}\"", repeat(HEX, $n))); test_json_deserialize(FixedByteArray::<$n>([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); test_display_fromstr(FixedByteArray::<$n>([VAL; $n]), &repeat(HEX, $n)); diff --git a/src/utils/transaction_utils.rs b/src/utils/transaction_utils.rs index 9a96af1..fbc89e3 100644 --- a/src/utils/transaction_utils.rs +++ b/src/utils/transaction_utils.rs @@ -20,7 +20,7 @@ pub struct ReceiverInfo { /// /// * `script` - Script to build address for pub fn construct_p2sh_address(script: &Script) -> String { - let bytes = bincode::serialize(script).unwrap(); + let bytes = bincode::serde::encode_to_vec(script, bincode::config::legacy()).unwrap(); let mut addr = hex::encode(sha3_256::digest(&bytes)); addr.insert(ZERO, P2SH_PREPEND as char); addr.truncate(STANDARD_ADDRESS_LENGTH); @@ -340,7 +340,7 @@ pub fn update_utxo_set(current_utxo: &mut BTreeMap) { /// /// * `tx` - Transaction to hash pub fn construct_tx_hash(tx: &Transaction) -> String { - let bytes = match bincode::serialize(tx) { + let bytes = match bincode::serde::encode_to_vec(tx, bincode::config::legacy()) { Ok(bytes) => bytes, Err(_) => vec![], }; @@ -1175,7 +1175,7 @@ mod tests { ..Default::default() }]; - let bytes = match bincode::serialize(&tx_ins) { + let bytes = match bincode::serde::encode_to_vec(&tx_ins, bincode::config::legacy()) { Ok(bytes) => bytes, Err(_) => vec![], }; From 36409d524f2589c1777af3fb234b861c73c47d4f Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Thu, 11 Jul 2024 12:51:44 +0200 Subject: [PATCH 11/17] serialize_utils: add a bunch of helper methods --- src/utils/serialize_utils.rs | 116 +++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 4 deletions(-) diff --git a/src/utils/serialize_utils.rs b/src/utils/serialize_utils.rs index 39eb7ea..9053c02 100644 --- a/src/utils/serialize_utils.rs +++ b/src/utils/serialize_utils.rs @@ -1,20 +1,32 @@ use std::any::TypeId; use std::convert::{TryFrom, TryInto}; use std::fmt; +use std::io::Write; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; use std::str::FromStr; +use bincode::{BorrowDecode, Decode, Encode}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::de::{SeqAccess, Visitor}; use serde::ser::{SerializeTuple}; /// Implements `Write` by simply counting the number of bytes written to it. +#[derive(Copy, Clone, Debug)] pub struct ByteCountingWriter { pub count: usize, } -impl std::io::Write for ByteCountingWriter { +impl ByteCountingWriter { + /// Creates a new `ByteCountingWriter` with a `count` of 0. + pub fn new() -> Self { + Self { + count: 0, + } + } +} + +impl Write for ByteCountingWriter { fn write(&mut self, buf: &[u8]) -> std::io::Result { self.count += buf.len(); Ok(buf.len()) @@ -29,7 +41,7 @@ impl std::io::Write for ByteCountingWriter { /// /// This can be formatted to and parsed from a hexadecimal string using `Display` and `FromStr`. /// When serialized as JSON, it is also represented as a hexadecimal string. -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Encode, Decode)] pub struct FixedByteArray( #[serde(with = "fixed_array_codec")] [u8; N], @@ -129,7 +141,7 @@ impl FromStr for FixedByteArray { } } -/// A codec for fixed-size arrays. +/// A serde codec for fixed-size arrays. pub mod fixed_array_codec { use super::*; @@ -187,7 +199,7 @@ pub mod fixed_array_codec { } } -/// A codec for variable-length `Vec`s. +/// A serde codec for variable-length `Vec`s. pub mod vec_codec { use super::*; @@ -257,6 +269,102 @@ fn vec_to_fixed_array( .map_err(|vec| E::custom(format!("expected exactly {} elements, but read {}", N, vec.len()))) } +/// Encodes an object into a `Vec` using bincode 2's standard configuration. +/// +/// This allows using the turbofish operator to explicitly specify the encode type without also +/// having to specify the config type. +/// +/// ### Arguments +/// +/// * `value` - the value to encode +#[inline(always)] +pub fn bincode_encode_to_vec_standard( + value: &T, +) -> Result, bincode::error::EncodeError> { + bincode::encode_to_vec(value, bincode::config::standard()) +} + +/// Encodes an object into the given `Write` using bincode 2's standard configuration. +/// +/// This allows using the turbofish operator to explicitly specify the encode type without also +/// having to specify the config type. +/// +/// ### Arguments +/// +/// * `value` - the value to encode +/// * `write` - the `Write` to encode into +#[inline(always)] +pub fn bincode_encode_to_write_standard( + value: &T, + write: &mut impl Write, +) -> Result { + bincode::encode_into_std_write(value, write, bincode::config::standard()) +} + +/// Calculates the encoded size of the given object using bincode 2's standard configuration. +/// +/// ### Arguments +/// +/// * `value` - the value to encode +#[inline(always)] +pub fn bincode_encoded_size_standard( + value: &T, +) -> Result { + let mut writer = ByteCountingWriter::new(); + bincode::encode_into_std_write(value, &mut writer, bincode::config::standard())?; + Ok(writer.count) +} + +/// Decodes an object from a slice using bincode 2's standard configuration. +/// +/// This allows using the turbofish operator to explicitly specify the decode type without also +/// having to specify the config type. +/// +/// ### Arguments +/// +/// * `slice` - the slice to decode from +#[inline(always)] +pub fn bincode_decode_from_slice_standard( + slice: &[u8], +) -> Result<(T, usize), bincode::error::DecodeError> { + bincode::decode_from_slice(slice, bincode::config::standard()) +} + +/// Decodes an object from a slice using bincode 2's standard configuration. +/// +/// This allows using the turbofish operator to explicitly specify the decode type without also +/// having to specify the config type. +/// +/// ### Arguments +/// +/// * `slice` - the slice to decode from +pub fn bincode_decode_from_slice_standard_full( + slice: &[u8], +) -> Result { + let (result, read_bytes) = bincode_decode_from_slice_standard::(slice)?; + if read_bytes == slice.len() { + Ok(result) + } else { + Err(bincode::error::DecodeError::OtherString( + format!("{} bytes left over after decoding", slice.len() - read_bytes))) + } +} + +/// Decodes an object from a slice using bincode 2's standard configuration. +/// +/// This allows using the turbofish operator to explicitly specify the decode type without also +/// having to specify the config type. +/// +/// ### Arguments +/// +/// * `slice` - the slice to decode from +#[inline(always)] +pub fn bincode_borrow_decode_from_slice_standard<'a, T: BorrowDecode<'a>>( + slice: &'a [u8], +) -> Result<(T, usize), bincode::error::DecodeError> { + bincode::borrow_decode_from_slice(slice, bincode::config::standard()) +} + /*---- TESTS ----*/ #[cfg(test)] From 0e148be1945cb8427cf61a947d1efcf9ae14bfce Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Thu, 11 Jul 2024 16:41:31 +0200 Subject: [PATCH 12/17] PlaceholderIndexed -> PlaceholderSeed --- src/utils/mod.rs | 49 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 5c0840b..aa17b53 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -69,7 +69,33 @@ pub trait Placeholder : Sized { /// of a type, which can be used for test purposes. These placeholder values are consistent /// across program runs. #[cfg(test)] -pub trait PlaceholderIndexed : Sized { +pub trait PlaceholderSeed: Sized + PartialEq { + /// Gets a dummy valid of this type which can be used for test purposes. + /// + /// This allows acquiring multiple distinct placeholder values which are still consistent + /// between runs. + /// + /// ### Arguments + /// + /// * `seed_parts` - the parts of the seed for the placeholder value to obtain. Two placeholder + /// values generated from the same seed are guaranteed to be equal (even + /// across multiple test runs, so long as the value format doesn't change). + fn placeholder_seed_parts<'a>(seed_parts: impl IntoIterator) -> Self; + + /// Gets a dummy valid of this type which can be used for test purposes. + /// + /// This allows acquiring multiple distinct placeholder values which are still consistent + /// between runs. + /// + /// ### Arguments + /// + /// * `seed` - the seed for the placeholder value to obtain. Two placeholder + /// values generated from the same seed are guaranteed to be equal (even + /// across multiple test runs, so long as the value format doesn't change). + fn placeholder_seed(seed: impl AsRef<[u8]>) -> Self { + Self::placeholder_seed_parts([ seed.as_ref() ]) + } + /// Gets a dummy valid of this type which can be used for test purposes. /// /// This allows acquiring multiple distinct placeholder values which are still consistent @@ -80,18 +106,27 @@ pub trait PlaceholderIndexed : Sized { /// * `index` - the index of the placeholder value to obtain. Two placeholder values generated /// from the same index are guaranteed to be equal (even across multiple test runs, /// so long as the value format doesn't change). - fn placeholder_indexed(index: u64) -> Self; + fn placeholder_indexed(index: u64) -> Self { + Self::placeholder_seed_parts([ index.to_le_bytes().as_slice() ]) + } + + /// Gets an array of placeholder values of this type which can be used for test purposes. + fn placeholder_array_seed(seed: impl AsRef<[u8]>) -> [Self; N] { + core::array::from_fn(|n| Self::placeholder_seed_parts( + [ seed.as_ref(), &(n as u64).to_le_bytes() ] + )) + } /// Gets an array of placeholder values of this type which can be used for test purposes. fn placeholder_array_indexed(base_index: u64) -> [Self; N] { - core::array::from_fn(|n| Self::placeholder_indexed(base_index.wrapping_add(n as u64))) + Self::placeholder_array_seed(base_index.to_le_bytes()) } } #[cfg(test)] -impl Placeholder for T { +impl Placeholder for T { fn placeholder() -> Self { - Self::placeholder_indexed(0) + ::placeholder_seed_parts([]) } } @@ -104,7 +139,9 @@ impl Placeholder for T { /// /// * `seed_parts` - the parts of the seed, which will be concatenated to form the RNG seed #[cfg(test)] -pub fn placeholder_bytes(seed_parts: &[&[u8]]) -> [u8; N] { +pub fn placeholder_bytes<'a, const N: usize>( + seed_parts: impl IntoIterator +) -> [u8; N] { // Use Shake-256 to generate an arbitrarily large number of random bytes based on the given seed. let mut shake256 = sha3::Shake256::default(); for slice in seed_parts { From dcfa232e8f1519191b9700fc9a892b2d7b478a1a Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Thu, 11 Jul 2024 11:18:24 +0200 Subject: [PATCH 13/17] crypto: add a dedicated wrapper struct for SHA3-256 hashes --- src/crypto.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index 38b51d0..03a3d90 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -308,18 +308,69 @@ pub mod pbkdf2 { } pub mod sha3_256 { + use std::convert::{TryFrom, TryInto}; + use std::fmt::{Display, Formatter}; + use std::ops::Deref; + use std::str::FromStr; + pub use sha3::digest::Output; pub use sha3::Digest; pub use sha3::Sha3_256; - pub fn digest(data: &[u8]) -> Output { - Sha3_256::digest(data) + pub const HASH_LEN : usize = 256 / 8; + + /// A SHA3-256 hash. + #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)] + pub struct Hash( + [u8; HASH_LEN], + ); + + impl Hash { + pub fn from_slice(slice: &[u8]) -> Option { + Some(Self(slice.try_into().ok()?)) + } + } + + impl Display for Hash { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + f.write_str(&hex::encode(&self.0)) + } + } + + impl FromStr for Hash { + type Err = hex::FromHexError; + + fn from_str(s: &str) -> Result { + let mut buf = [0u8; HASH_LEN]; + match hex::decode_to_slice(s, &mut buf) { + Ok(_) => Ok(Self(buf)), + Err(e) => Err(e), + } + } + } + + impl AsRef<[u8]> for Hash { + fn as_ref(&self) -> &[u8] { + &self.0 + } + } + + impl Deref for Hash { + type Target = [u8; HASH_LEN]; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + pub fn digest(data: &[u8]) -> Hash { + Hash(Sha3_256::digest(data).try_into().unwrap()) } - pub fn digest_all<'a>(data: impl Iterator) -> Output { + pub fn digest_all<'a>(data: impl Iterator) -> Hash { let mut hasher = Sha3_256::new(); data.for_each(|v| hasher.update(v)); - hasher.finalize() + Hash(hasher.finalize().try_into().unwrap()) } } From 1a7fd05c14a51b9ee3910166c45a02e383b138b4 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Wed, 29 May 2024 14:10:09 +0200 Subject: [PATCH 14/17] Implement all crypto structs using FixedByteArray --- src/crypto.rs | 173 +++++++++++++++----------------------------------- 1 file changed, 52 insertions(+), 121 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index 03a3d90..9dd27e3 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -1,9 +1,42 @@ pub use ring; -use std::convert::TryInto; use tracing::warn; +use crate::utils::serialize_utils::FixedByteArray; + +macro_rules! fixed_bytes_wrapper { + ($vis:vis struct $name:ident, $n:expr, $doc:literal) => { + #[doc = $doc] + #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] + $vis struct $name(crate::utils::serialize_utils::FixedByteArray<$n>); + + impl $name { + pub fn from_slice(slice: &[u8]) -> Option { + Some(Self(slice.try_into().ok()?)) + } + } + + impl AsRef<[u8]> for $name { + fn as_ref(&self) -> &[u8] { + self.0.as_ref() + } + } + + impl std::fmt::Display for $name { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + std::fmt::Display::fmt(&self.0, f) + } + } + + impl std::str::FromStr for $name { + type Err = as std::str::FromStr>::Err; + + fn from_str(s: &str) -> Result { + as std::str::FromStr>::from_str(s).map(Self) + } + } + }; +} pub mod sign_ed25519 { - use super::deserialize_slice; pub use ring::signature::Ed25519KeyPair as SecretKeyBase; use ring::signature::KeyPair; pub use ring::signature::Signature as SignatureBase; @@ -21,53 +54,14 @@ pub mod sign_ed25519 { const SIGNATURE_LEN: usize = ELEM_LEN + SCALAR_LEN; pub const ED25519_SIGNATURE_LEN: usize = SIGNATURE_LEN; - /// Signature data - /// We used sodiumoxide serialization before (treated it as slice with 64 bit length prefix). - #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] - pub struct Signature( - #[serde(serialize_with = "<[_]>::serialize")] - #[serde(deserialize_with = "deserialize_slice")] - [u8; ED25519_SIGNATURE_LEN], - ); - - impl Signature { - pub fn from_slice(slice: &[u8]) -> Option { - Some(Self(slice.try_into().ok()?)) - } - } - - impl AsRef<[u8]> for Signature { - fn as_ref(&self) -> &[u8] { - self.0.as_ref() - } - } - - /// Public key data - /// We used sodiumoxide serialization before (treated it as slice with 64 bit length prefix). - #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] - pub struct PublicKey( - #[serde(serialize_with = "<[_]>::serialize")] - #[serde(deserialize_with = "deserialize_slice")] - [u8; ED25519_PUBLIC_KEY_LEN], - ); - - impl PublicKey { - pub fn from_slice(slice: &[u8]) -> Option { - Some(Self(slice.try_into().ok()?)) - } - } - - impl AsRef<[u8]> for PublicKey { - fn as_ref(&self) -> &[u8] { - self.0.as_ref() - } - } + fixed_bytes_wrapper!(pub struct Signature, ED25519_SIGNATURE_LEN, "Signature data"); + fixed_bytes_wrapper!(pub struct PublicKey, ED25519_PUBLIC_KEY_LEN, "Public key data"); /// PKCS8 encoded secret key pair /// We used sodiumoxide serialization before (treated it as slice with 64 bit length prefix). /// Slice and vector are serialized the same. #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] - pub struct SecretKey(Vec); + pub struct SecretKey(#[serde(with = "crate::utils::serialize_utils::vec_codec")] Vec); impl SecretKey { pub fn from_slice(slice: &[u8]) -> Option { @@ -91,7 +85,7 @@ pub mod sign_ed25519 { Ok(secret) => secret, Err(_) => { warn!("Invalid secret key"); - return Signature([0; ED25519_SIGNATURE_LEN]); + return Signature([0; ED25519_SIGNATURE_LEN].into()); } }; @@ -99,7 +93,7 @@ pub mod sign_ed25519 { Ok(signature) => signature, Err(_) => { warn!("Invalid signature"); - return Signature([0; ED25519_SIGNATURE_LEN]); + return Signature([0; ED25519_SIGNATURE_LEN].into()); } }; Signature(signature) @@ -135,7 +129,7 @@ pub mod sign_ed25519 { Ok(pkcs8) => pkcs8, Err(_) => { warn!("Failed to generate secret key base for pkcs8"); - return (PublicKey([0; ED25519_PUBLIC_KEY_LEN]), SecretKey(vec![])); + return (PublicKey([0; ED25519_PUBLIC_KEY_LEN].into()), SecretKey(vec![])); } }; @@ -143,7 +137,7 @@ pub mod sign_ed25519 { Ok(secret) => secret, Err(_) => { warn!("Invalid secret key base"); - return (PublicKey([0; ED25519_PUBLIC_KEY_LEN]), SecretKey(vec![])); + return (PublicKey([0; ED25519_PUBLIC_KEY_LEN].into()), SecretKey(vec![])); } }; @@ -151,7 +145,7 @@ pub mod sign_ed25519 { Ok(pub_key_gen) => pub_key_gen, Err(_) => { warn!("Invalid public key generation"); - return (PublicKey([0; ED25519_PUBLIC_KEY_LEN]), SecretKey(vec![])); + return (PublicKey([0; ED25519_PUBLIC_KEY_LEN].into()), SecretKey(vec![])); } }; let public = PublicKey(pub_key_gen); @@ -159,7 +153,7 @@ pub mod sign_ed25519 { Some(secret) => secret, None => { warn!("Invalid secret key"); - return (PublicKey([0; ED25519_PUBLIC_KEY_LEN]), SecretKey(vec![])); + return (PublicKey([0; ED25519_PUBLIC_KEY_LEN].into()), SecretKey(vec![])); } }; @@ -169,7 +163,7 @@ pub mod sign_ed25519 { pub mod secretbox_chacha20_poly1305 { // Use key and nonce separately like rust-tls does - use super::{deserialize_slice, generate_random}; + use super::generate_random; pub use ring::aead::LessSafeKey as KeyBase; pub use ring::aead::Nonce as NonceBase; pub use ring::aead::NONCE_LEN; @@ -179,45 +173,8 @@ pub mod secretbox_chacha20_poly1305 { pub const KEY_LEN: usize = 256 / 8; - /// key data - #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] - pub struct Key( - #[serde(serialize_with = "<[_]>::serialize")] - #[serde(deserialize_with = "deserialize_slice")] - [u8; KEY_LEN], - ); - - impl Key { - pub fn from_slice(slice: &[u8]) -> Option { - Some(Self(slice.try_into().ok()?)) - } - } - - impl AsRef<[u8]> for Key { - fn as_ref(&self) -> &[u8] { - self.0.as_ref() - } - } - - /// Nonce data - #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] - pub struct Nonce( - #[serde(serialize_with = "<[_]>::serialize")] - #[serde(deserialize_with = "deserialize_slice")] - [u8; NONCE_LEN], - ); - - impl Nonce { - pub fn from_slice(slice: &[u8]) -> Option { - Some(Self(slice.try_into().ok()?)) - } - } - - impl AsRef<[u8]> for Nonce { - fn as_ref(&self) -> &[u8] { - self.0.as_ref() - } - } + fixed_bytes_wrapper!(pub struct Key, KEY_LEN, "Key data"); + fixed_bytes_wrapper!(pub struct Nonce, NONCE_LEN, "Nonce data"); pub fn seal(mut plain_text: Vec, nonce: &Nonce, key: &Key) -> Option> { let key = get_keybase(key)?; @@ -249,7 +206,7 @@ pub mod secretbox_chacha20_poly1305 { } fn get_noncebase(nonce: &Nonce) -> NonceBase { - NonceBase::assume_unique_for_key(nonce.0) + NonceBase::assume_unique_for_key(*nonce.0) } pub fn gen_key() -> Key { @@ -262,7 +219,7 @@ pub mod secretbox_chacha20_poly1305 { } pub mod pbkdf2 { - use super::{deserialize_slice, generate_random}; + use super::generate_random; use ring::pbkdf2::{derive, PBKDF2_HMAC_SHA256}; use serde::{Deserialize, Serialize}; use std::convert::TryInto; @@ -272,24 +229,7 @@ pub mod pbkdf2 { pub const SALT_LEN: usize = 256 / 8; pub const OPSLIMIT_INTERACTIVE: u32 = 100_000; - #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] - pub struct Salt( - #[serde(serialize_with = "<[_]>::serialize")] - #[serde(deserialize_with = "deserialize_slice")] - [u8; SALT_LEN], - ); - - impl Salt { - pub fn from_slice(slice: &[u8]) -> Option { - Some(Self(slice.try_into().ok()?)) - } - } - - impl AsRef<[u8]> for Salt { - fn as_ref(&self) -> &[u8] { - self.0.as_ref() - } - } + fixed_bytes_wrapper!(pub struct Salt, SALT_LEN, "Salt data"); pub fn derive_key(key: &mut [u8], passwd: &[u8], salt: &Salt, iterations: u32) { let iterations = match NonZeroU32::new(iterations) { @@ -374,16 +314,7 @@ pub mod sha3_256 { } } -fn deserialize_slice<'de, D: serde::Deserializer<'de>, const N: usize>( - deserializer: D, -) -> Result<[u8; N], D::Error> { - let value: &[u8] = serde::Deserialize::deserialize(deserializer)?; - value - .try_into() - .map_err(|_| serde::de::Error::custom("Invalid array in deserialization".to_string())) -} - -pub fn generate_random() -> [u8; N] { +fn generate_random() -> FixedByteArray { let mut value: [u8; N] = [0; N]; use ring::rand::SecureRandom; @@ -393,5 +324,5 @@ pub fn generate_random() -> [u8; N] { Err(_) => warn!("Failed to generate random bytes"), }; - value + FixedByteArray::new(value) } From 74aa8207646f3b4280284da2576af0eb2b68948c Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Thu, 11 Jul 2024 12:03:16 +0200 Subject: [PATCH 15/17] crypto: improve keypair generation Also implemented PlaceholderIndexed for all crypto structs --- src/crypto.rs | 163 ++++++++++++++++++++++++++------------------------ 1 file changed, 85 insertions(+), 78 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index 9dd27e3..7e57887 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -1,6 +1,5 @@ pub use ring; use tracing::warn; -use crate::utils::serialize_utils::FixedByteArray; macro_rules! fixed_bytes_wrapper { ($vis:vis struct $name:ident, $n:expr, $doc:literal) => { @@ -33,6 +32,15 @@ macro_rules! fixed_bytes_wrapper { as std::str::FromStr>::from_str(s).map(Self) } } + + #[cfg(test)] + impl crate::utils::PlaceholderSeed for $name { + fn placeholder_seed_parts<'a>(seed_parts: impl IntoIterator) -> Self { + Self(crate::utils::placeholder_bytes( + [ concat!(stringify!($name), ":").as_bytes() ].iter().copied().chain(seed_parts) + ).into()) + } + } }; } @@ -44,9 +52,7 @@ pub mod sign_ed25519 { pub use ring::signature::{ED25519, ED25519_PUBLIC_KEY_LEN}; use serde::{Deserialize, Serialize}; use std::convert::TryInto; - use tracing::warn; - - pub type PublicKeyBase = ::PublicKey; + use crate::crypto::generate_random; // Constants copied from the ring library const SCALAR_LEN: usize = 32; @@ -54,6 +60,8 @@ pub mod sign_ed25519 { const SIGNATURE_LEN: usize = ELEM_LEN + SCALAR_LEN; pub const ED25519_SIGNATURE_LEN: usize = SIGNATURE_LEN; + pub const ED25519_SEED_LEN: usize = 32; + fixed_bytes_wrapper!(pub struct Signature, ED25519_SIGNATURE_LEN, "Signature data"); fixed_bytes_wrapper!(pub struct PublicKey, ED25519_PUBLIC_KEY_LEN, "Public key data"); @@ -64,8 +72,49 @@ pub mod sign_ed25519 { pub struct SecretKey(#[serde(with = "crate::utils::serialize_utils::vec_codec")] Vec); impl SecretKey { + /// Constructs a `SecretKey` from the given PKCS8 document. + /// + /// ### Arguments + /// + /// * `slice` - a slice containing the encoded PKCS8 document pub fn from_slice(slice: &[u8]) -> Option { - Some(Self(slice.to_vec())) + match SecretKeyBase::from_pkcs8(slice) { + Ok(_) => Some(Self(slice.to_vec())), + Err(_) => None, + } + } + + /// Gets the public key corresponding to this secret key. + pub fn get_public_key(&self) -> PublicKey { + let keypair = SecretKeyBase::from_pkcs8(&self.0) + .expect("SecretKey contains invalid PKCS8 document?!?"); + + PublicKey::from_slice(keypair.public_key().as_ref()) + .expect("Keypair public key length is invalid?!?") + } + } + + impl From for SecretKey { + fn from(value: ring::pkcs8::Document) -> Self { + Self(value.as_ref().to_vec()) + } + } + + #[cfg(test)] + impl crate::utils::PlaceholderSeed for SecretKey { + fn placeholder_seed_parts<'a>(seed_parts: impl IntoIterator) -> Self { + gen_keypair_from_seed(&crate::utils::placeholder_bytes( + [ "SecretKey:".as_bytes() ].iter().copied().chain(seed_parts) + )).1 + } + } + + #[cfg(test)] + impl crate::utils::PlaceholderSeed for (PublicKey, SecretKey) { + fn placeholder_seed_parts<'a>(seed_parts: impl IntoIterator) -> Self { + gen_keypair_from_seed(&crate::utils::placeholder_bytes( + [ "(PublicKey, SecretKey):".as_bytes() ].iter().copied().chain(seed_parts) + )) } } @@ -81,83 +130,41 @@ pub mod sign_ed25519 { } pub fn sign_detached(msg: &[u8], sk: &SecretKey) -> Signature { - let secret = match SecretKeyBase::from_pkcs8(sk.as_ref()) { - Ok(secret) => secret, - Err(_) => { - warn!("Invalid secret key"); - return Signature([0; ED25519_SIGNATURE_LEN].into()); - } - }; + let keypair = SecretKeyBase::from_pkcs8(sk.as_ref()) + .expect("Invalid PKCS8 secret key?!?"); - let signature = match secret.sign(msg).as_ref().try_into() { - Ok(signature) => signature, - Err(_) => { - warn!("Invalid signature"); - return Signature([0; ED25519_SIGNATURE_LEN].into()); - } - }; + let signature = keypair.sign(msg).as_ref().try_into() + .expect("Invalid signature?!?"); Signature(signature) } - pub fn verify_append(sm: &[u8], pk: &PublicKey) -> bool { - if sm.len() > ED25519_SIGNATURE_LEN { - let start = sm.len() - ED25519_SIGNATURE_LEN; - let sig = Signature(match sm[start..].try_into() { - Ok(sig) => sig, - Err(_) => { - warn!("Invalid signature"); - return false; - } - }); - let msg = &sm[..start]; - verify_detached(&sig, msg, pk) - } else { - false - } - } - - pub fn sign_append(msg: &[u8], sk: &SecretKey) -> Vec { - let sig = sign_detached(msg, sk); - let mut sm = msg.to_vec(); - sm.extend_from_slice(sig.as_ref()); - sm + /// Generates a completely random Ed25519 keypair. + pub fn gen_keypair() -> (PublicKey, SecretKey) { + let seed = generate_random(); + gen_keypair_from_seed(&seed) } - pub fn gen_keypair() -> (PublicKey, SecretKey) { - let rand = ring::rand::SystemRandom::new(); - let pkcs8 = match SecretKeyBase::generate_pkcs8(&rand) { - Ok(pkcs8) => pkcs8, - Err(_) => { - warn!("Failed to generate secret key base for pkcs8"); - return (PublicKey([0; ED25519_PUBLIC_KEY_LEN].into()), SecretKey(vec![])); - } + /// Generates an Ed25519 keypair based on the given seed. + /// + /// ### Arguments + /// + /// * `seed` - the seed to generate the keypair from + fn gen_keypair_from_seed(seed: &[u8; ED25519_SEED_LEN]) -> (PublicKey, SecretKey) { + let rand = ring::test::rand::FixedSliceSequenceRandom { + bytes: &[ seed ], + current: core::cell::UnsafeCell::new(0), }; - let secret = match SecretKeyBase::from_pkcs8(pkcs8.as_ref()) { - Ok(secret) => secret, - Err(_) => { - warn!("Invalid secret key base"); - return (PublicKey([0; ED25519_PUBLIC_KEY_LEN].into()), SecretKey(vec![])); - } - }; + let pkcs8 = SecretKeyBase::generate_pkcs8(&rand) + .expect("Failed to generate secret key base for pkcs8"); - let pub_key_gen = match secret.public_key().as_ref().try_into() { - Ok(pub_key_gen) => pub_key_gen, - Err(_) => { - warn!("Invalid public key generation"); - return (PublicKey([0; ED25519_PUBLIC_KEY_LEN].into()), SecretKey(vec![])); - } - }; - let public = PublicKey(pub_key_gen); - let secret = match SecretKey::from_slice(pkcs8.as_ref()) { - Some(secret) => secret, - None => { - warn!("Invalid secret key"); - return (PublicKey([0; ED25519_PUBLIC_KEY_LEN].into()), SecretKey(vec![])); - } - }; + let keypair = SecretKeyBase::from_pkcs8(pkcs8.as_ref()) + .expect("Generated PKCS8 document is invalid?!?"); - (public, secret) + let public_key = PublicKey(keypair.public_key().as_ref().try_into() + .expect("Generated keypair contains an invalid public key?!?")); + let secret_key = pkcs8.into(); + (public_key, secret_key) } } @@ -210,11 +217,11 @@ pub mod secretbox_chacha20_poly1305 { } pub fn gen_key() -> Key { - Key(generate_random()) + Key(generate_random().into()) } pub fn gen_nonce() -> Nonce { - Nonce(generate_random()) + Nonce(generate_random().into()) } } @@ -243,12 +250,12 @@ pub mod pbkdf2 { } pub fn gen_salt() -> Salt { - Salt(generate_random()) + Salt(generate_random().into()) } } pub mod sha3_256 { - use std::convert::{TryFrom, TryInto}; + use std::convert::TryInto; use std::fmt::{Display, Formatter}; use std::ops::Deref; use std::str::FromStr; @@ -314,7 +321,7 @@ pub mod sha3_256 { } } -fn generate_random() -> FixedByteArray { +fn generate_random() -> [u8; N] { let mut value: [u8; N] = [0; N]; use ring::rand::SecureRandom; @@ -324,5 +331,5 @@ fn generate_random() -> FixedByteArray { Err(_) => warn!("Failed to generate random bytes"), }; - FixedByteArray::new(value) + value } From 675e0eba0f2b3f8a4d6da72b9f1e7d12cd262f2d Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Fri, 12 Jul 2024 11:18:52 +0200 Subject: [PATCH 16/17] crypto: add tests for object serialization --- src/crypto.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/crypto.rs b/src/crypto.rs index 7e57887..25e2bd6 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -333,3 +333,82 @@ fn generate_random() -> [u8; N] { value } + +#[cfg(test)] +mod test { + use std::fmt::{Debug, Display}; + use std::str::FromStr; + use serde::Serialize; + use serde::de::DeserializeOwned; + use crate::utils::PlaceholderSeed; + use super::*; + + fn test_placeholders_different_seed() { + let [v0, v1] = W::placeholder_array_seed::<2>([]); + assert_eq!(v0, v0); + assert_eq!(v1, v1); + assert_ne!(v0, v1); + } + + fn test_fixed_bytes_wrapper + PlaceholderSeed + Serialize + DeserializeOwned>( + expected_placeholder_hex: &str, + ) { + let placeholder = W::placeholder_seed([]); + assert_eq!(placeholder.to_string(), expected_placeholder_hex); + assert_eq!(W::from_str(expected_placeholder_hex).unwrap(), placeholder); + + let expected_json = format!("\"{}\"", expected_placeholder_hex); + assert_eq!(serde_json::to_string(&placeholder).unwrap(), expected_json); + assert_eq!(serde_json::from_str::(&expected_json).unwrap(), placeholder); + + test_placeholders_different_seed::(); + } + + #[test] + fn test_ed25519_signature() { + test_fixed_bytes_wrapper::( + "9c4e2259fc9b47b4c4cf672c7436dc16ace2970955a002b69a495ca96d9dfaf026dbee622284a1cf306a1189af8a462d2ea498d10f14b637c848168b0ba698a7", + ); + } + + #[test] + fn test_ed25519_public_key() { + test_fixed_bytes_wrapper::( + "1d67f7de4c59192568f8e0381fcd1eb9ce044568e4670038e0b42e421540b4f6", + ); + } + + #[test] + fn test_ed25519_secret_key() { + assert_eq!(hex::encode(sign_ed25519::SecretKey::placeholder_seed([])), + "3053020101300506032b6570042204203651dccde39be8697d8e0690acd90e3b8ce7f596c5f205fbd0b3b3e3a68629e1a12303210092bc778f74110b3fcbcf8a4df71ed9a33c62faa8d01417d381745ef700ef6b73"); + + test_placeholders_different_seed::(); + } + + #[test] + fn test_ed25519_keypair() { + test_placeholders_different_seed::<(sign_ed25519::PublicKey, sign_ed25519::SecretKey)>(); + } + + #[test] + fn test_chacha20_key() { + test_fixed_bytes_wrapper::( + "d2678ac6abff79fa16d2a8f762a3c33b227a519ac1830aee33a19605b7f9cd35", + ); + } + + #[test] + fn test_chacha20_nonce() { + test_fixed_bytes_wrapper::( + "b0980a0a073d6b828fb48ad5", + ); + } + + #[test] + fn test_pbkdf2_salt() { + test_fixed_bytes_wrapper::( + "81c4a8cde605d6b51857eb6ebaead0de98cf254d4855725db7aec45a98699e9c", + ); + } +} From 305f3407a304622101b12e33d5470016e1ea5f83 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Thu, 11 Jul 2024 13:12:46 +0200 Subject: [PATCH 17/17] Add dedicated structs for addresses --- src/primitives/address.rs | 337 ++++++++++++++++++++++++++++++++++++++ src/primitives/mod.rs | 1 + 2 files changed, 338 insertions(+) create mode 100644 src/primitives/address.rs diff --git a/src/primitives/address.rs b/src/primitives/address.rs new file mode 100644 index 0000000..c164995 --- /dev/null +++ b/src/primitives/address.rs @@ -0,0 +1,337 @@ +use std::fmt::{Display, Formatter}; +use std::ops::Deref; +use std::str::FromStr; +use bincode::{Decode, Encode}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use crate::crypto::sha3_256; +use crate::crypto::sign_ed25519::PublicKey; +use crate::utils::serialize_utils::FixedByteArray; + +const STANDARD_ADDRESS_BYTES : usize = sha3_256::HASH_LEN; + +/// A standard 32-byte address. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] +struct StandardAddress(FixedByteArray); + +impl StandardAddress { + /// Creates a new address out of the given SHA3-256 `Hash`. + fn new(hash: sha3_256::Hash) -> Self { + Self(hash.deref().into()) + } +} + +#[cfg(test)] +impl crate::utils::PlaceholderSeed for StandardAddress { + fn placeholder_seed_parts<'a>(seed_parts: impl IntoIterator) -> Self { + Self(crate::utils::placeholder_bytes(seed_parts).into()) + } +} + +impl Display for StandardAddress { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + f.write_str(&hex::encode(self.as_ref())) + } +} + +impl FromStr for StandardAddress { + type Err = hex::FromHexError; + + fn from_str(s: &str) -> Result { + s.parse().map(Self) + } +} + +impl AsRef<[u8]> for StandardAddress { + fn as_ref(&self) -> &[u8] { + self.0.as_ref() + } +} + +make_error_type!( +#[derive(PartialEq)] +pub enum ParseAddressError { + BadPrefix(address: String); "Address \"{address}\" has unknown prefix", + ParseFailed(cause: hex::FromHexError); "{cause}"; cause, +}); + +macro_rules! standard_address_type { + ($doc:literal, $name:ident, $prefix:literal $(, wrap: $anyname:ident)?) => { + #[doc = $doc] + #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] + pub struct $name { standard_address: StandardAddress } + + impl $name { + #[doc = "Gets the SHA3-256 hash which this address wraps"] + pub fn get_hash(&self) -> sha3_256::Hash { + sha3_256::Hash::from_slice(self.standard_address.as_ref()).unwrap() + } + } + + $( impl $name { + #[doc = "Wraps this address into an `AnyAddress`"] + pub fn wrap(self) -> AnyAddress { + AnyAddress::$anyname(self) + } + } + + impl From<$name> for AnyAddress { + fn from(value: $name) -> Self { + value.wrap() + } + } )? + + impl From for $name { + fn from(standard_address: StandardAddress) -> Self { + Self { standard_address } + } + } + + #[cfg(test)] + impl crate::utils::PlaceholderSeed for $name { + fn placeholder_seed_parts<'a>(seed_parts: impl IntoIterator) -> Self { + StandardAddress::placeholder_seed_parts( + [ concat!(stringify!($name), ":").as_bytes() ].iter().cloned().chain(seed_parts) + ).into() + } + } + + impl Display for $name { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + write!(f, concat!($prefix, "{}"), self.standard_address) + } + } + + impl FromStr for $name { + type Err = ParseAddressError; + + fn from_str(s: &str) -> Result { + if let Some(s) = s.strip_prefix($prefix) { + StandardAddress::from_str(s).map(Self::from).map_err(ParseAddressError::ParseFailed) + } else { + Err(ParseAddressError::BadPrefix(s.to_string())) + } + } + } + }; +} + +standard_address_type!("The type of address used for P2PKH outputs", P2PKHAddress, "", wrap: P2PKH); + +impl P2PKHAddress { + /// Creates a new P2PKH address from the hash of the given public key. + pub fn from_pubkey(public_key: &PublicKey) -> Self { + StandardAddress::new(sha3_256::digest(public_key.as_ref())).into() + } +} + +//standard_address_type!("The type of address used for P2SH outputs", P2SHAddress, "H", wrap: P2SH); + +standard_address_type!("The type of address used for DRUID input expectations outputs", TxInsAddress, ""); + +impl TxInsAddress { + /// Creates a new TxIns address from the hash of the given public key. + pub fn from_hash(hash: sha3_256::Hash) -> Self { + StandardAddress::new(hash).into() + } +} + +/// Wrapper enum representing an address of any type. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] +pub enum AnyAddress { + Burn, + P2PKH(P2PKHAddress), + //P2SH(P2SHAddress), +} + +impl AnyAddress { + /// Identifies which sort of address this is. + pub fn sort(&self) -> AddressSort { + match self { + Self::P2PKH(_) => AddressSort::P2PKH, + Self::Burn => AddressSort::Burn, + } + } +} + +impl Display for AnyAddress { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + match self { + AnyAddress::Burn => f.write_str("BURN"), + AnyAddress::P2PKH(address) => address.fmt(f), + //AnyAddress::P2SH(address) => address.fmt(f), + } + } +} + +impl FromStr for AnyAddress { + type Err = ParseAddressError; + + fn from_str(s: &str) -> Result { + if let Ok(addr) = P2PKHAddress::from_str(s) { + Ok(Self::P2PKH(addr)) + //} else if let Ok(addr) = P2SHAddress::from_str(s) { + // Ok(Self::P2SH(addr)) + } else if s == "BURN" { + Ok(Self::Burn) + } else { + Err(ParseAddressError::BadPrefix(s.to_string())) + } + } +} + +impl Serialize for AnyAddress { + fn serialize(&self, serializer: S) -> Result { + assert!(serializer.is_human_readable(), "serializer must be human-readable!"); + serializer.serialize_str(&self.to_string()) + } +} + +impl<'de> Deserialize<'de> for AnyAddress { + fn deserialize>(deserializer: D) -> Result { + assert!(deserializer.is_human_readable(), "deserializer must be human-readable!"); + + let text : String = serde::Deserialize::deserialize(deserializer)?; + text.parse().map_err(::custom) + } +} + +#[cfg(test)] +impl crate::utils::Placeholder for AnyAddress { + fn placeholder() -> Self { + Self::P2PKH(P2PKHAddress::placeholder()) + } +} + +make_trivial_enum!( +#[doc = "The different kinds of addresses."] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum AddressSort { + Burn, + P2PKH, + //P2SH, +} +all_variants=ALL_SORTS); + +/*---- TESTS ----*/ + +#[cfg(test)] +mod tests { + use crate::utils::PlaceholderSeed; + use super::*; + + #[test] + fn test_p2pkh_address() { + let pks : [PublicKey; 2] = PlaceholderSeed::placeholder_array_indexed(1); + + let pk_hashes = pks.each_ref().map(|pk| sha3_256::digest(pk.as_ref())); + let addresses = pks.each_ref().map(P2PKHAddress::from_pubkey); + + // The two addresses are generated from different public keys, so they should be distinct + assert_ne!(addresses[0], addresses[1]); + + // The string representation of each address should be equal to the hex-encoded pubkey hash + assert_eq!( + pk_hashes.each_ref().map(sha3_256::Hash::to_string), + addresses.each_ref().map(P2PKHAddress::to_string)); + + // Converting an address to a string and parsing it should give the same address + assert_eq!( + addresses.each_ref() + .map(|address| P2PKHAddress::from_str(&address.to_string()).unwrap()), + addresses); + } + + #[test] + fn test_p2pkh_address_parse() { + // Valid + assert_eq!( + P2PKHAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381d"), + Ok(StandardAddress::new(sha3_256::digest(b"jeff")).into())); + + // Valid (upper-case hex chars) + assert_eq!( + P2PKHAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381D"), + Ok(StandardAddress::new(sha3_256::digest(b"jeff")).into())); + + // Too short (even) + assert_eq!( + P2PKHAddress::from_str(""), + Err(ParseAddressError::ParseFailed(hex::FromHexError::InvalidStringLength))); + assert_eq!( + P2PKHAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb038"), + Err(ParseAddressError::ParseFailed(hex::FromHexError::InvalidStringLength))); + + // Too short (odd) + assert_eq!( + P2PKHAddress::from_str("0"), + Err(ParseAddressError::ParseFailed(hex::FromHexError::OddLength))); + assert_eq!( + P2PKHAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381"), + Err(ParseAddressError::ParseFailed(hex::FromHexError::OddLength))); + + // Too long (even) + assert_eq!( + P2PKHAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381d00"), + Err(ParseAddressError::ParseFailed(hex::FromHexError::InvalidStringLength))); + + // Too long (odd) + assert_eq!( + P2PKHAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381d0"), + Err(ParseAddressError::ParseFailed(hex::FromHexError::OddLength))); + + // Non-hex chars + assert_eq!( + P2PKHAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381Z"), + Err(ParseAddressError::ParseFailed(hex::FromHexError::InvalidHexCharacter { + c: 'Z', + index: 63, + }))); + } + + #[test] + fn test_any_address_parse() { + // Valid + assert_eq!( + AnyAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381d"), + Ok(AnyAddress::P2PKH(StandardAddress::new(sha3_256::digest(b"jeff")).into()))); + assert_eq!( + AnyAddress::from_str("BURN"), + Ok(AnyAddress::Burn)); + + // Valid (upper-case hex chars) + assert_eq!( + AnyAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381D"), + Ok(AnyAddress::P2PKH(StandardAddress::new(sha3_256::digest(b"jeff")).into()))); + + // Too short (even) + assert_eq!( + AnyAddress::from_str(""), + Err(ParseAddressError::BadPrefix("".to_owned()))); + assert_eq!( + AnyAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb038"), + Err(ParseAddressError::BadPrefix("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb038".to_owned()))); + + // Too short (odd) + assert_eq!( + AnyAddress::from_str("0"), + Err(ParseAddressError::BadPrefix("0".to_owned()))); + assert_eq!( + AnyAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381"), + Err(ParseAddressError::BadPrefix("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381".to_owned()))); + + // Too long (even) + assert_eq!( + AnyAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381d00"), + Err(ParseAddressError::BadPrefix("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381d00".to_owned()))); + + // Too long (odd) + assert_eq!( + AnyAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381d0"), + Err(ParseAddressError::BadPrefix("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381d0".to_owned()))); + + // Non-hex chars + assert_eq!( + AnyAddress::from_str("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381Z"), + Err(ParseAddressError::BadPrefix("00f4381fd2f762b0b8532a4a4993be444f75bc8e57bf672c58effabeedb0381Z".to_owned()))); + } +} diff --git a/src/primitives/mod.rs b/src/primitives/mod.rs index bcde86f..1aca0ef 100644 --- a/src/primitives/mod.rs +++ b/src/primitives/mod.rs @@ -1,3 +1,4 @@ +pub mod address; pub mod asset; pub mod block; pub mod druid;