deserialize witness from a list of hex strings
- ci(primitives): enable hex feature in CI build - from_hex() implemented more efficiently
This commit is contained in:
parent
c85648cfd4
commit
e0b627ea81
|
@ -9,6 +9,8 @@ use core::ops::Index;
|
||||||
|
|
||||||
#[cfg(feature = "arbitrary")]
|
#[cfg(feature = "arbitrary")]
|
||||||
use arbitrary::{Arbitrary, Unstructured};
|
use arbitrary::{Arbitrary, Unstructured};
|
||||||
|
#[cfg(feature = "hex")]
|
||||||
|
use hex::{error::HexToBytesError, FromHex};
|
||||||
use internals::compact_size;
|
use internals::compact_size;
|
||||||
use internals::slice::SliceExt;
|
use internals::slice::SliceExt;
|
||||||
use internals::wrap_debug::WrapDebug;
|
use internals::wrap_debug::WrapDebug;
|
||||||
|
@ -218,6 +220,21 @@ impl Witness {
|
||||||
let end = element_len as usize;
|
let end = element_len as usize;
|
||||||
Some(&slice[..end])
|
Some(&slice[..end])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new witness from a list of hex strings.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// This function will return an error if any of the hex strings are invalid.
|
||||||
|
#[cfg(feature = "hex")]
|
||||||
|
pub fn from_hex<I, T>(iter: I) -> Result<Self, HexToBytesError>
|
||||||
|
where
|
||||||
|
I: IntoIterator<Item = T>,
|
||||||
|
T: AsRef<str>,
|
||||||
|
{
|
||||||
|
let result: Vec<Vec<u8>> = iter.into_iter().map(|hex_str| Vec::from_hex(hex_str.as_ref())).collect::<Result<Vec<_>, _>>()?;
|
||||||
|
Ok(Self::from_slice(&result))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Correctness Requirements: value must always fit within u32
|
/// Correctness Requirements: value must always fit within u32
|
||||||
|
@ -840,4 +857,16 @@ mod test {
|
||||||
let json = serde_json::to_string(&witness).unwrap();
|
let json = serde_json::to_string(&witness).unwrap();
|
||||||
assert_eq!(json, r#"["007b4b","0206030708"]"#);
|
assert_eq!(json, r#"["007b4b","0206030708"]"#);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "hex")]
|
||||||
|
#[test]
|
||||||
|
fn test_from_hex() {
|
||||||
|
let hex_strings = [
|
||||||
|
"30440220703350f1c8be5b41b4cb03b3b680c4f3337f987514a6b08e16d5d9f81e9b5f72022018fb269ba5b82864c0e1edeaf788829eb332fe34a859cc1f99c4a02edfb5d0df01",
|
||||||
|
"0208689fe2cca52d8726cefaf274de8fa61d5faa5e1058ad35b49fb194c035f9a4",
|
||||||
|
];
|
||||||
|
|
||||||
|
let witness = Witness::from_hex(hex_strings).unwrap();
|
||||||
|
assert_eq!(witness.len(), 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue