dusk_wallet/wallet/
file.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use std::fmt;
8use std::path::{Path, PathBuf};
9use std::str::FromStr;
10
11/// Provides access to a secure wallet file
12pub trait SecureWalletFile {
13    /// Returns the path
14    fn path(&self) -> &WalletPath;
15    /// Returns the hashed password
16    fn pwd(&self) -> &[u8];
17}
18
19/// Wrapper around `PathBuf` for wallet paths
20#[derive(PartialEq, Eq, Hash, Debug, Clone)]
21pub struct WalletPath {
22    /// Path of the wallet file
23    pub wallet: PathBuf,
24    /// Directory of the profile
25    pub profile_dir: PathBuf,
26    /// Name of the network
27    pub network: Option<String>,
28}
29
30impl WalletPath {
31    /// Create wallet path from the path of "wallet.dat" file. The wallet.dat
32    /// file should be located in the profile folder, this function also
33    /// generates the profile folder from the passed argument
34    pub fn new(wallet: &Path) -> Self {
35        let wallet = wallet.to_path_buf();
36        // The wallet should be in the profile folder
37        let mut profile_dir = wallet.clone();
38
39        profile_dir.pop();
40
41        Self {
42            wallet,
43            profile_dir,
44            network: None,
45        }
46    }
47
48    /// Returns the filename of this path
49    pub fn name(&self) -> Option<String> {
50        // extract the name
51        let name = self.wallet.file_stem()?.to_str()?;
52        Some(String::from(name))
53    }
54
55    /// Returns current directory for this path
56    pub fn dir(&self) -> Option<PathBuf> {
57        self.wallet.parent().map(PathBuf::from)
58    }
59
60    /// Returns a reference to the `PathBuf` holding the path
61    pub fn inner(&self) -> &PathBuf {
62        &self.wallet
63    }
64
65    /// Sets the network name for different cache locations.
66    /// e.g, devnet, testnet, etc.
67    pub fn set_network_name(&mut self, network: Option<String>) {
68        self.network = network;
69    }
70
71    /// Generates dir for cache based on network specified
72    pub fn cache_dir(&self) -> PathBuf {
73        let mut cache = self.profile_dir.clone();
74
75        if let Some(network) = &self.network {
76            cache.push(format!("cache_{network}"));
77        } else {
78            cache.push("cache");
79        }
80
81        cache
82    }
83}
84
85impl FromStr for WalletPath {
86    type Err = crate::Error;
87
88    fn from_str(s: &str) -> Result<Self, Self::Err> {
89        let p = Path::new(s);
90
91        Ok(Self::new(p))
92    }
93}
94
95impl From<PathBuf> for WalletPath {
96    fn from(p: PathBuf) -> Self {
97        Self::new(&p)
98    }
99}
100
101impl From<&Path> for WalletPath {
102    fn from(p: &Path) -> Self {
103        Self::new(p)
104    }
105}
106
107impl fmt::Display for WalletPath {
108    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109        write!(
110            f,
111            "wallet path: {}\n\rprofile dir: {}\n\rnetwork: {}",
112            self.wallet.display(),
113            self.profile_dir.display(),
114            self.network.as_ref().unwrap_or(&"default".to_string())
115        )
116    }
117}