Skip to main content

snarkvm_console_account/view_key/
to_address.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
18impl<N: Network> ViewKey<N> {
19    /// Returns the address corresponding to the view key.
20    pub fn to_address(&self) -> Address<N> {
21        Address::new(N::g_scalar_multiply(self))
22    }
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use snarkvm_console_network::MainnetV0;
29
30    type CurrentNetwork = MainnetV0;
31
32    const ITERATIONS: u64 = 1000;
33
34    #[test]
35    fn test_try_from() -> Result<()> {
36        let rng = &mut TestRng::default();
37
38        for _ in 0..ITERATIONS {
39            // Sample a new view key and address.
40            let private_key = PrivateKey::<CurrentNetwork>::new(rng)?;
41            let view_key = ViewKey::try_from(private_key)?;
42            let address = Address::try_from(private_key)?;
43
44            assert_eq!(address, view_key.to_address());
45        }
46        Ok(())
47    }
48}