2023-07-25 03:42:52 +03:00
|
|
|
// Copyright 2023 The Grim Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-07-28 00:00:57 +03:00
|
|
|
use std::fs;
|
2023-07-25 03:42:52 +03:00
|
|
|
use std::path::PathBuf;
|
2023-07-28 00:00:57 +03:00
|
|
|
use grin_core::global::ChainTypes;
|
2023-07-25 03:42:52 +03:00
|
|
|
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use crate::{AppConfig, Settings};
|
2023-07-29 00:17:54 +03:00
|
|
|
use crate::wallet::Wallets;
|
2023-07-25 03:42:52 +03:00
|
|
|
|
|
|
|
/// Wallet configuration.
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
|
|
pub struct WalletConfig {
|
2023-07-28 00:00:57 +03:00
|
|
|
/// Chain type for current wallet.
|
|
|
|
chain_type: ChainTypes,
|
2023-07-25 03:42:52 +03:00
|
|
|
/// Identifier for a wallet.
|
2023-07-29 00:17:54 +03:00
|
|
|
pub(crate) id: i64,
|
|
|
|
/// Human-readable wallet name for ui.
|
|
|
|
pub(crate) name: String,
|
2023-07-25 03:42:52 +03:00
|
|
|
/// External node connection URL.
|
|
|
|
external_node_url: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wallet configuration file name.
|
|
|
|
const CONFIG_FILE_NAME: &'static str = "grim-wallet.toml";
|
|
|
|
|
|
|
|
impl WalletConfig {
|
|
|
|
/// Create wallet config.
|
2023-07-28 00:00:57 +03:00
|
|
|
pub fn create(name: String, external_node_url: Option<String>) -> WalletConfig {
|
|
|
|
let id = chrono::Utc::now().timestamp();
|
|
|
|
let chain_type = AppConfig::chain_type();
|
|
|
|
let config_path = Self::get_config_file_path(&chain_type, id);
|
|
|
|
|
|
|
|
let config = WalletConfig { chain_type, id, name, external_node_url };
|
2023-07-25 03:42:52 +03:00
|
|
|
Settings::write_to_file(&config, config_path);
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load config from provided wallet dir.
|
|
|
|
pub fn load(wallet_dir: PathBuf) -> Option<WalletConfig> {
|
|
|
|
let mut config_path: PathBuf = wallet_dir.clone();
|
|
|
|
config_path.push(CONFIG_FILE_NAME);
|
|
|
|
if let Ok(config) = Settings::read_from_file::<WalletConfig>(config_path) {
|
|
|
|
return Some(config)
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:00:57 +03:00
|
|
|
/// Get config file path for provided [`ChainTypes`] and wallet identifier.
|
|
|
|
fn get_config_file_path(chain_type: &ChainTypes, id: i64) -> PathBuf {
|
2023-07-29 00:17:54 +03:00
|
|
|
let mut config_path = Wallets::get_base_path(chain_type);
|
2023-07-28 00:00:57 +03:00
|
|
|
config_path.push(id.to_string());
|
|
|
|
// Create if the config path doesn't exist.
|
|
|
|
if !config_path.exists() {
|
|
|
|
let _ = fs::create_dir_all(config_path.clone());
|
|
|
|
}
|
2023-07-25 03:42:52 +03:00
|
|
|
config_path.push(CONFIG_FILE_NAME);
|
|
|
|
config_path
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:00:57 +03:00
|
|
|
/// Get current wallet data path.
|
|
|
|
pub fn get_data_path(&self) -> String {
|
|
|
|
let chain_type = AppConfig::chain_type();
|
2023-07-29 00:17:54 +03:00
|
|
|
let mut config_path = Wallets::get_base_path(&chain_type);
|
2023-07-28 00:00:57 +03:00
|
|
|
config_path.push(self.id.to_string());
|
|
|
|
config_path.to_str().unwrap().to_string()
|
|
|
|
}
|
|
|
|
|
2023-07-25 03:42:52 +03:00
|
|
|
/// Save wallet config.
|
|
|
|
fn save(&self) {
|
2023-07-28 00:00:57 +03:00
|
|
|
let config_path = Self::get_config_file_path(&self.chain_type, self.id);
|
2023-07-25 03:42:52 +03:00
|
|
|
Settings::write_to_file(self, config_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get external node connection URL.
|
|
|
|
pub fn get_external_node_url(&self) -> &Option<String> {
|
|
|
|
&self.external_node_url
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set external node connection URL.
|
|
|
|
pub fn set_external_node_url(&mut self, url: Option<String>) {
|
|
|
|
self.external_node_url = url;
|
|
|
|
self.save();
|
|
|
|
}
|
|
|
|
}
|