snarkvm_console_account/address/
try_from.rsuse super::*;
#[cfg(feature = "private_key")]
impl<N: Network> TryFrom<PrivateKey<N>> for Address<N> {
type Error = Error;
fn try_from(private_key: PrivateKey<N>) -> Result<Self, Self::Error> {
Self::try_from(&private_key)
}
}
#[cfg(feature = "private_key")]
impl<N: Network> TryFrom<&PrivateKey<N>> for Address<N> {
type Error = Error;
fn try_from(private_key: &PrivateKey<N>) -> Result<Self, Self::Error> {
Self::try_from(ComputeKey::try_from(private_key)?)
}
}
#[cfg(feature = "compute_key")]
impl<N: Network> TryFrom<ComputeKey<N>> for Address<N> {
type Error = Error;
fn try_from(compute_key: ComputeKey<N>) -> Result<Self, Self::Error> {
Self::try_from(&compute_key)
}
}
#[cfg(feature = "compute_key")]
impl<N: Network> TryFrom<&ComputeKey<N>> for Address<N> {
type Error = Error;
fn try_from(compute_key: &ComputeKey<N>) -> Result<Self, Self::Error> {
Ok(compute_key.to_address())
}
}
#[cfg(feature = "view_key")]
impl<N: Network> TryFrom<ViewKey<N>> for Address<N> {
type Error = Error;
fn try_from(view_key: ViewKey<N>) -> Result<Self, Self::Error> {
Self::try_from(&view_key)
}
}
#[cfg(feature = "view_key")]
impl<N: Network> TryFrom<&ViewKey<N>> for Address<N> {
type Error = Error;
fn try_from(view_key: &ViewKey<N>) -> Result<Self, Self::Error> {
Ok(view_key.to_address())
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::MainnetV0;
type CurrentNetwork = MainnetV0;
const ITERATIONS: u64 = 1_000;
#[test]
fn test_try_from() -> Result<()> {
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let private_key = PrivateKey::<CurrentNetwork>::new(&mut rng)?;
let expected = Address::try_from(private_key)?;
let compute_key = ComputeKey::<CurrentNetwork>::try_from(private_key)?;
assert_eq!(expected, Address::try_from(compute_key)?);
let view_key = ViewKey::<CurrentNetwork>::try_from(private_key)?;
assert_eq!(expected, Address::try_from(view_key)?);
}
Ok(())
}
}