Skip to main content

snarkvm_console_account/view_key/
try_from.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM 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
16use super::*;
17
18#[cfg(feature = "private_key")]
19impl<N: Network> TryFrom<PrivateKey<N>> for ViewKey<N> {
20    type Error = Error;
21
22    /// Initializes a new account view key 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")]
29impl<N: Network> TryFrom<&PrivateKey<N>> for ViewKey<N> {
30    type Error = Error;
31
32    /// Initializes a new account view key from an account private key.
33    fn try_from(private_key: &PrivateKey<N>) -> Result<Self, Self::Error> {
34        // Derive the compute key.
35        let compute_key = ComputeKey::try_from(private_key)?;
36        // Compute view_key := sk_sig + r_sig + sk_prf.
37        Ok(Self::from_scalar(private_key.sk_sig() + private_key.r_sig() + compute_key.sk_prf()))
38    }
39}
40
41#[cfg(feature = "private_key")]
42impl<N: Network> TryFrom<(&PrivateKey<N>, &ComputeKey<N>)> for ViewKey<N> {
43    type Error = Error;
44
45    /// Initializes a new account view key from an account private key.
46    fn try_from((private_key, compute_key): (&PrivateKey<N>, &ComputeKey<N>)) -> Result<Self, Self::Error> {
47        // Compute view_key := sk_sig + r_sig + sk_prf.
48        Ok(Self::from_scalar(private_key.sk_sig() + private_key.r_sig() + compute_key.sk_prf()))
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use snarkvm_console_network::MainnetV0;
56
57    type CurrentNetwork = MainnetV0;
58
59    const ITERATIONS: u64 = 1000;
60
61    #[test]
62    fn test_try_from() -> Result<()> {
63        let rng = &mut TestRng::default();
64
65        for _ in 0..ITERATIONS {
66            // Sample a new compute key and view key.
67            let private_key = PrivateKey::<CurrentNetwork>::new(rng)?;
68            let compute_key = ComputeKey::try_from(private_key)?;
69            let view_key = ViewKey::try_from(private_key)?;
70
71            // Check that the view key matches.
72            // Compute view_key := sk_sig + r_sig + sk_prf.
73            let candidate = ViewKey(private_key.sk_sig() + private_key.r_sig() + compute_key.sk_prf());
74            assert_eq!(view_key, candidate);
75
76            let view_key2 = ViewKey::try_from((&private_key, &compute_key))?;
77            assert_eq!(view_key2, candidate);
78        }
79        Ok(())
80    }
81}