use crate::prelude::*;
pub fn random_bytes(number_of_bytes: u32) -> ExternResult<Bytes> {
HDK.with(|h| h.borrow().random_bytes(number_of_bytes))
}
pub trait TryFromRandom {
fn try_from_random() -> ExternResult<Self>
where
Self: Sized;
}
macro_rules! impl_try_from_random {
( $t:ty, $bytes:expr ) => {
impl TryFromRandom for $t {
fn try_from_random() -> $crate::prelude::ExternResult<Self> {
$crate::prelude::random_bytes($bytes as u32).map(|bytes| {
assert_eq!($bytes, bytes.len());
let mut inner = [0; $bytes];
inner.copy_from_slice(bytes.as_ref());
Self::from(inner)
})
}
}
};
}
impl_try_from_random!(
CapSecret,
holochain_zome_types::capability::CAP_SECRET_BYTES
);