Skip to content

Commit

Permalink
New Account Details View (#307)
Browse files Browse the repository at this point in the history
* adds account details view and remove account feature - WIP

* fixes hidding add account confirmation modal

* finishes concurrency fix for app_cfg file

* reduces notifications timeout

* improves remove all accounts confirmation messages
moves remove account texts to the language file

* translates remove account(s) texts

* translates get private key texts

* fixes lint errors
  • Loading branch information
soaresa authored Jun 5, 2024
1 parent 9322af4 commit 0b3d3e2
Show file tree
Hide file tree
Showing 41 changed files with 986 additions and 37,918 deletions.
1 change: 1 addition & 0 deletions dist/assets/index-13e3c917.css

Large diffs are not rendered by default.

27,808 changes: 0 additions & 27,808 deletions dist/assets/index-d5120c7d.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/assets/index-d5120c7d.js.map

This file was deleted.

9,792 changes: 0 additions & 9,792 deletions dist/assets/index-e225b5d2.css

This file was deleted.

2 changes: 2 additions & 0 deletions dist/assets/index-e32be568.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/assets/index-e32be568.js.map

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script type="module" crossorigin src="/assets/index-d5120c7d.js"></script>
<link rel="stylesheet" href="/assets/index-e225b5d2.css" />
<script type="module" crossorigin src="/assets/index-e32be568.js"></script>
<link rel="stylesheet" href="/assets/index-13e3c917.css">
</head>
<body></body>
<body>

</body>
</html>
Binary file added dist/libra.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ serde = { version = "1.0.137", features = ["derive"] }
serde_json = { version = "1.0.81", features = ["preserve_order"] }
log = "0.4.17"
tokio = { version = "1.21.0", features = ["full"] }
tokio-util = "0.7.11"
url = { version = "2.2.2", features = ["serde"] }

# only in carpe
Expand Down
8 changes: 4 additions & 4 deletions src-tauri/src/commands/networks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! networks to connect to
use crate::configs::{get_cfg, get_client};
use crate::configs::{get_cfg, get_client, CONFIG_MUTEX};

use crate::carpe_error::CarpeError;
use libra_types::exports::IndexResponse;
Expand All @@ -15,7 +15,7 @@ use url::Url;
#[tauri::command(async)]
pub async fn toggle_network(chain_id_str: &str) -> Result<NetworkPlaylist, CarpeError> {
let chain_id = NamedChain::from_str(chain_id_str)?;
let mut app_cfg = get_cfg()?;
let mut app_cfg = CONFIG_MUTEX.lock().await;
app_cfg.set_chain_id(chain_id);
app_cfg.save_file()?;
maybe_create_playlist(&mut app_cfg, chain_id).await.ok();
Expand Down Expand Up @@ -60,7 +60,7 @@ pub async fn get_metadata() -> Result<IndexResponse, CarpeError> {

#[tauri::command(async)]
pub async fn override_playlist(url: Url) -> Result<NetworkPlaylist, CarpeError> {
let mut app_cfg = get_cfg()?;
let mut app_cfg = CONFIG_MUTEX.lock().await;
let np = app_cfg.update_network_playlist(None, Some(url)).await?;
app_cfg.save_file()?;
Ok(np)
Expand All @@ -69,7 +69,7 @@ pub async fn override_playlist(url: Url) -> Result<NetworkPlaylist, CarpeError>
#[tauri::command(async)]
/// we want to elimated the entire playlist and use a single fullnode
pub async fn force_upstream(url: Url) -> Result<NetworkPlaylist, CarpeError> {
let mut app_cfg = get_cfg()?;
let mut app_cfg = CONFIG_MUTEX.lock().await;
let dummy_playlist = NetworkPlaylist {
chain_name: app_cfg.workspace.default_chain_id,
nodes: vec![HostProfile::new(url)],
Expand Down
12 changes: 6 additions & 6 deletions src-tauri/src/commands/preferences.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::configs::{default_config_path, legacy_config_path};
use crate::configs::{default_config_path, legacy_config_path, CONFIG_MUTEX};
use crate::migrate::{self, backup_legacy_dir, read_accounts};
use crate::{carpe_error::CarpeError, configs::get_cfg};
use anyhow::Context;
Expand All @@ -14,8 +14,8 @@ pub struct Preferences {

#[tauri::command(async)]
/// set the locale preference
pub fn set_preferences_locale(locale: String) -> Result<(), CarpeError> {
let mut app_cfg = get_cfg()?;
pub async fn set_preferences_locale(locale: String) -> Result<(), CarpeError> {
let mut app_cfg = CONFIG_MUTEX.lock().await;
let profile = app_cfg.get_profile_mut(None)?;

profile.locale = Some(locale);
Expand All @@ -33,8 +33,8 @@ pub fn get_miner_txs_cost() -> Result<TxCost, CarpeError> {

#[tauri::command(async)]
/// set the miner_txs_cost
pub fn set_miner_txs_cost(max_gas_unit_for_tx: u64) -> Result<(), CarpeError> {
let mut app_cfg = get_cfg()?;
pub async fn set_miner_txs_cost(max_gas_unit_for_tx: u64) -> Result<(), CarpeError> {
let mut app_cfg = CONFIG_MUTEX.lock().await;
app_cfg.tx_configs.miner_txs_cost = Some(TxCost {
max_gas_unit_for_tx,
coin_price_per_unit: 100,
Expand All @@ -57,7 +57,7 @@ pub fn debug_preferences_path() -> Result<PathBuf, CarpeError> {
#[tauri::command(async)]
/// refreshes statistics and returns the synced peers
pub async fn refresh_upstream_peer_stats() -> Result<Vec<Url>, CarpeError> {
let mut app_cfg = get_cfg()?;
let mut app_cfg = CONFIG_MUTEX.lock().await;

let np = app_cfg.refresh_network_profile_and_save(None).await?; // uses app_cfg.chain_info_chain_id
app_cfg.network_playlist = vec![np.clone()];
Expand Down
69 changes: 56 additions & 13 deletions src-tauri/src/commands/wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
};

use anyhow::{anyhow, Context, Error};
use configs::CONFIG_MUTEX;
use libra_txs::{submit_transaction::Sender, txs_cli_user::SetSlowTx};
use libra_types::{
exports::{AccountAddress, AuthenticationKey, Ed25519PrivateKey, ValidCryptoMaterialStringExt},
Expand Down Expand Up @@ -240,8 +241,7 @@ pub fn get_default_profile() -> Result<CarpeProfile, CarpeError> {

#[tauri::command(async)]
pub async fn refresh_accounts() -> Result<Vec<CarpeProfile>, CarpeError> {
// let mut all = Accounts::read_from_file()?;
let mut app_cfg = get_cfg()?;
let mut app_cfg = CONFIG_MUTEX.lock().await;

// while we are here check if the accounts are on chain
// under a different address than implied by authkey
Expand Down Expand Up @@ -310,15 +310,23 @@ pub async fn get_originating_address(

/// Switch tx profiles, change libra.yaml to use selected account
#[tauri::command(async)]
// IMPORTANT: don't return the profile, since it has keys
pub async fn switch_profile(account: AccountAddress) -> Result<CarpeProfile, CarpeError> {
let mut app_cfg = get_cfg()?;
let p = app_cfg.get_profile(Some(account.to_string()))?;
app_cfg.workspace.set_default(p.nickname.clone());
// IMPORTANT: don't return the profile, since it has keys
let mut app_cfg = CONFIG_MUTEX.lock().await;

// Clone the necessary data from the immutable borrow
let account_str = account.to_string();
let p_nickname = {
let p = app_cfg.get_profile(Some(account_str.clone()))?;
p.nickname.clone()
};

// Perform the mutable operation
app_cfg.workspace.set_default(p_nickname);
app_cfg.save_file()?;

// TODO: gross, fix upstream `app_cfg.rs` to prevent the borrow issues here
let profile = app_cfg.get_profile(Some(account.to_string()))?;
// Get the profile again after the mutable operation
let profile = app_cfg.get_profile(Some(account_str))?;

// Assign account note
let mut profiles: Vec<CarpeProfile> = vec![profile.into()];
Expand All @@ -328,16 +336,34 @@ pub async fn switch_profile(account: AccountAddress) -> Result<CarpeProfile, Car
}

// remove all accounts which are being tracked.
#[tauri::command]
pub fn remove_accounts() -> Result<String, CarpeError> {
#[tauri::command(async)]
pub async fn remove_accounts() -> Result<String, CarpeError> {
// Note: this only removes the account tracking, doesn't delete account on chain.
let mut cfg = configs::get_cfg()?;
cfg.user_profiles = vec![];
cfg.save_file()?;
let mut app_cfg = CONFIG_MUTEX.lock().await;
app_cfg.user_profiles = vec![];
app_cfg.save_file()?;
let _ = remove_legacy_accounts();
Ok("removed all accounts".to_owned())
}

// remove an account from the tracked accounts
#[tauri::command(async)]
pub async fn remove_account(account: AccountAddress) -> Result<String, CarpeError> {
let mut app_cfg = CONFIG_MUTEX.lock().await;
let acc_str = account.to_string().to_lowercase();
let index = app_cfg
.user_profiles
.iter()
.position(|p| p.account.to_string() == acc_str);
if let Some(i) = index {
app_cfg.user_profiles.remove(i);
app_cfg.save_file()?;
Ok(format!("removed account {}", acc_str))
} else {
Err(CarpeError::misc("account not found"))
}
}

pub fn danger_get_keys(mnemonic: String) -> Result<KeyChain, anyhow::Error> {
let keys = account_keys::get_keys_from_mnem(mnemonic)?;
Ok(keys)
Expand Down Expand Up @@ -396,6 +422,7 @@ pub async fn add_watch_account(
is_legacy: bool,
) -> Result<CarpeProfile, CarpeError> {
let authkey: AuthenticationKey = query::get_auth_key(address).await?;

if is_legacy {
let _ = add_legacy_accounts(authkey);
}
Expand Down Expand Up @@ -468,3 +495,19 @@ pub fn remove_legacy_accounts() -> Result<String, CarpeError> {
&db_path
)))
}

pub fn remove_legacy_account(authkey: AuthenticationKey) -> Result<String, CarpeError> {
let mut all = read_legacy_accounts()?;
let acc_str = authkey.to_string();
let index = all
.accounts
.iter()
.position(|p| p.authkey.to_string() == acc_str);
if let Some(i) = index {
all.accounts.remove(i);
let _ = update_legacy_accounts(&all);
Ok(format!("removed account {}", acc_str))
} else {
Err(CarpeError::misc("account not found"))
}
}
5 changes: 5 additions & 0 deletions src-tauri/src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use libra_types::{
};
use once_cell::sync::Lazy;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::sync::Mutex;

pub static CONFIG_MUTEX: Lazy<Arc<Mutex<AppCfg>>> =
Lazy::new(|| Arc::new(Mutex::new(get_cfg().unwrap())));

// Set up paths for the canary builds
#[cfg(feature = "carpe-canary")]
Expand Down
31 changes: 15 additions & 16 deletions src-tauri/src/configs_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,39 @@ use std::{fs, path::PathBuf};

use crate::configs::{self, get_cfg};
use crate::default_config_path;
use configs::CONFIG_MUTEX;
use libra_types::exports::{AccountAddress, AuthenticationKey};
use libra_types::legacy_types::app_cfg::get_nickname;
use libra_types::legacy_types::app_cfg::Profile;
use libra_types::{
exports::{AccountAddress, AuthenticationKey},
legacy_types::app_cfg::AppCfg,
};

/// For switching between profiles in the Account DB.
pub async fn set_account_profile(
account: AccountAddress,
authkey: AuthenticationKey,
) -> anyhow::Result<AppCfg> {
) -> anyhow::Result<()> {
let configs_exist = configs::is_initialized();
let mut cfg = match configs_exist {
let _cfg = match configs_exist {
true => configs::get_cfg()?,
false => configs::new_cfg()?,
};

// Lock the mutex before making any changes
let mut cfg_guard = CONFIG_MUTEX.lock().await;

// set as default profile
cfg.workspace.default_profile = Some(get_nickname(account));
cfg_guard.workspace.default_profile = Some(get_nickname(account));
let profile = Profile::new(authkey, account);
// add if we have not already
cfg.maybe_add_profile(profile)?;
cfg_guard.maybe_add_profile(profile)?;

cfg.workspace.node_home = default_config_path().to_path_buf();
cfg_guard.workspace.node_home = default_config_path().to_path_buf();

if !cfg.workspace.node_home.exists() {
fs::create_dir_all(&cfg.workspace.node_home)?;
fs::create_dir_all(cfg.get_block_dir(None)?)?;
if !cfg_guard.workspace.node_home.exists() {
fs::create_dir_all(&cfg_guard.workspace.node_home)?;
fs::create_dir_all(cfg_guard.get_block_dir(None)?)?;
}

cfg.save_file()?;

Ok(cfg)
cfg_guard.save_file()?;
Ok(())
}

/// helper to get local proofs
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ async fn main() {
commands::wallets::init_from_mnem,
commands::wallets::init_from_private_key,
commands::wallets::remove_accounts,
commands::wallets::remove_account,
commands::wallets::switch_profile,
commands::wallets::is_slow,
commands::wallets::set_slow_wallet,
Expand Down
2 changes: 2 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import Nav from './components/Nav.svelte'
import DebugCard from './components/dev/DebugCard.svelte'
import Wallet from './components/wallet/Wallet.svelte'
import AccountDetails from './components/wallet/AccountDetails.svelte'
// import Miner from './components/miner/Miner.svelte'
import Settings from './components/settings/Settings.svelte'
import DevMode from './components/dev/DevMode.svelte'
Expand Down Expand Up @@ -135,6 +136,7 @@
<Nav />
<div class="uk-background-muted uk-margin-large">
<Route path={routes.wallet} component={Wallet} primary={false} />
<Route path={routes.accountDetails} component={AccountDetails} primary={false} />
<!-- <Route path="/add-account" component={AddAccount} primary={false} /> -->
<Route path={routes.accountFromMnem} component={AccountCreate} primary={false} />
<Route path={routes.addWatchAccount} component={AddWatchAccount} primary={false} />
Expand Down
1 change: 1 addition & 0 deletions src/components/Nav.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
routes.keygen,
routes.accountFromMnem,
routes.addWatchAccount,
routes.accountDetails
]
const location_store = useLocation()
Expand Down
49 changes: 0 additions & 49 deletions src/components/dev/DebugGetPrivateKey.svelte

This file was deleted.

Loading

0 comments on commit 0b3d3e2

Please sign in to comment.