try_from.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use super::*; 17 18 #[cfg(feature = "private_key")] 19 impl<N: Network> TryFrom<PrivateKey<N>> for Address<N> { 20 type Error = Error; 21 22 /// Derives the account address from an account private key. 23 fn try_from(private_key: PrivateKey<N>) -> Result<Self, Self::Error> { 24 Self::try_from(&private_key) 25 } 26 } 27 28 #[cfg(feature = "private_key")] 29 impl<N: Network> TryFrom<&PrivateKey<N>> for Address<N> { 30 type Error = Error; 31 32 /// Derives the account address from an account private key. 33 fn try_from(private_key: &PrivateKey<N>) -> Result<Self, Self::Error> { 34 Self::try_from(ComputeKey::try_from(private_key)?) 35 } 36 } 37 38 #[cfg(feature = "compute_key")] 39 impl<N: Network> TryFrom<ComputeKey<N>> for Address<N> { 40 type Error = Error; 41 42 /// Derives the account address from an account compute key. 43 fn try_from(compute_key: ComputeKey<N>) -> Result<Self, Self::Error> { 44 Self::try_from(&compute_key) 45 } 46 } 47 48 #[cfg(feature = "compute_key")] 49 impl<N: Network> TryFrom<&ComputeKey<N>> for Address<N> { 50 type Error = Error; 51 52 /// Derives the account address from an account compute key. 53 fn try_from(compute_key: &ComputeKey<N>) -> Result<Self, Self::Error> { 54 Ok(compute_key.to_address()) 55 } 56 } 57 58 #[cfg(feature = "view_key")] 59 impl<N: Network> TryFrom<ViewKey<N>> for Address<N> { 60 type Error = Error; 61 62 /// Derives the account address from an account view key. 63 fn try_from(view_key: ViewKey<N>) -> Result<Self, Self::Error> { 64 Self::try_from(&view_key) 65 } 66 } 67 68 #[cfg(feature = "view_key")] 69 impl<N: Network> TryFrom<&ViewKey<N>> for Address<N> { 70 type Error = Error; 71 72 /// Derives the account address from an account view key. 73 fn try_from(view_key: &ViewKey<N>) -> Result<Self, Self::Error> { 74 Ok(view_key.to_address()) 75 } 76 } 77 78 #[cfg(test)] 79 mod tests { 80 use super::*; 81 use alphavm_console_network::MainnetV0; 82 83 type CurrentNetwork = MainnetV0; 84 85 const ITERATIONS: u64 = 1_000; 86 87 #[test] 88 fn test_try_from() -> Result<()> { 89 let mut rng = TestRng::default(); 90 91 for _ in 0..ITERATIONS { 92 // Sample a new address. 93 let private_key = PrivateKey::<CurrentNetwork>::new(&mut rng)?; 94 let expected = Address::try_from(private_key)?; 95 96 // Check the address derived from the compute key. 97 let compute_key = ComputeKey::<CurrentNetwork>::try_from(private_key)?; 98 assert_eq!(expected, Address::try_from(compute_key)?); 99 100 // Check the address derived from the view key. 101 let view_key = ViewKey::<CurrentNetwork>::try_from(private_key)?; 102 assert_eq!(expected, Address::try_from(view_key)?); 103 } 104 Ok(()) 105 } 106 }