1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use 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::Testnet3;
type CurrentNetwork = Testnet3;
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(())
}
}