v0.2.6-pre.007

This commit is contained in:
2026-08-21 10:50:36 +02:00
parent 4c9086e2f0
commit f11f0921b0
25 changed files with 871 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/app_state.rs
// version: 7
// version: 8
//! Shared backend state owned by the Wallet Desk Tauri application.
@@ -9,6 +9,7 @@ pub(crate) struct AppState {
logging_runtime: std::sync::Mutex<LoggingRuntimeState>,
splash_settings: crate::SplashSettings,
splash_sequence_started: std::sync::atomic::AtomicBool,
transport_runtime: crate::TransportRuntime,
wallet_config_startup: crate::WalletConfigStartup,
wallet_session: std::sync::Mutex<crate::WalletSession>,
}
@@ -31,6 +32,14 @@ impl AppState {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let transport_runtime = crate::initialize_transport(&config_management);
let transport_runtime = match transport_runtime {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => {
ksp_logging_lib::error!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_BOOTSTRAP, error_domain = error.code().domain(), error_code = error.code().code(), "Wallet Desk Config/Transport bootstrap failed");
return std::result::Result::Err(error);
},
};
let wallet_config_startup = crate::initialize_wallet_config(&config_management);
let wallet_config_startup = match wallet_config_startup {
std::result::Result::Ok(value) => value,
@@ -58,6 +67,7 @@ impl AppState {
}),
splash_settings,
splash_sequence_started: std::sync::atomic::AtomicBool::new(false),
transport_runtime,
wallet_config_startup,
wallet_session: std::sync::Mutex::new(crate::WalletSession::no_selection()),
});
@@ -90,20 +100,37 @@ impl AppState {
},
};
let _keep_guard_alive = &runtime.guard;
let transport_snapshot = self.transport_runtime.pool().snapshot();
let transport_available_endpoint_count = count_to_u32(transport_snapshot.available_endpoint_count(), "available Transport endpoints");
let transport_available_endpoint_count = match transport_available_endpoint_count {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let transport_endpoint_count = count_to_u32(transport_snapshot.endpoint_count(), "Transport endpoints");
let transport_endpoint_count = match transport_endpoint_count {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let resolved = self.wallet_config_startup.resolved();
let wallets_subdirectory = resolved.wallets_subdirectory().map(|value| return value.to_string_lossy().into_owned());
return std::result::Result::Ok(crate::RuntimeStatusDto {
application_version: env!("CARGO_PKG_VERSION").to_owned(),
active_composite_profile: self.wallet_config_startup.composite_profile_id().to_owned(),
active_logging_profile: runtime.active_profile_id.clone(),
active_transport_profile: self.transport_runtime.profile_id().to_owned(),
active_wallet_profile: resolved.profile_id().to_owned(),
config_document_count: document_count,
effective_wallets_directory: resolved.effective_wallets_directory().to_string_lossy().into_owned(),
effective_wallets_directory_created_on_startup: self.wallet_config_startup.effective_directory_created_on_startup(),
fallback_logging_active: runtime.fallback_active,
root_wallets_directory_created_on_startup: self.wallet_config_startup.root_directory_created_on_startup(),
shell_phase: "pre.006-wallet-unlock".to_owned(),
shell_phase: "pre.007-wallet-balance".to_owned(),
startup_diagnostic: runtime.startup_diagnostic.clone(),
transport_available_endpoint_count,
transport_clusters: self.transport_runtime.clusters(),
transport_endpoint_count,
transport_providers: self.transport_runtime.providers(),
transport_role: self.transport_runtime.role().as_str().to_owned(),
wallets_directory: resolved.wallets_directory().to_string_lossy().into_owned(),
wallets_subdirectory,
});
@@ -524,6 +551,87 @@ impl AppState {
};
}
/// Refreshes the native SOL balance for the currently authorized VIEW/OWNER Wallet.
pub(crate) async fn refresh_wallet_balance(&self) -> ksp_core_lib::Result<crate::WalletBalanceDto> {
let context = self.authorized_balance_context();
let context = match context {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let config = ksp_onchain_transport_lib::GetBalanceConfig::new(
std::option::Option::Some(ksp_onchain_transport_lib::SolanaCommitment::Confirmed),
std::option::Option::None,
);
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, transport_profile = self.transport_runtime.profile_id(), transport_role = self.transport_runtime.role().as_str(), "Wallet balance refresh started");
let result = self.transport_runtime.pool().get_balance(self.transport_runtime.role(), &context.pubkey, std::option::Option::Some(&config)).await;
let result = match result {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => {
ksp_logging_lib::warn!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, error_domain = error.code().domain(), error_code = error.code().code(), "Wallet balance refresh failed");
return std::result::Result::Err(error);
},
};
if !self.balance_context_is_current(&context) {
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_SESSION_INVALID,
"Wallet balance refresh completed after the authorized session changed",
));
}
let lamports = result.value();
let dto = crate::WalletBalanceDto {
api_version: result.context().api_version().map(str::to_owned),
lamports: lamports.to_string(),
slot: result.context().slot(),
sol: crate::format_lamports_as_sol(lamports),
transport_profile: self.transport_runtime.profile_id().to_owned(),
transport_role: self.transport_runtime.role().as_str().to_owned(),
wallet_id: context.wallet_id.clone(),
};
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, lamports, slot = dto.slot, "Wallet balance refresh succeeded");
return std::result::Result::Ok(dto);
}
fn authorized_balance_context(&self) -> ksp_core_lib::Result<WalletBalanceContext> {
let session = self.wallet_session.lock();
let session = match session {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
};
return match &*session {
crate::WalletSession::View { wallet_id, wallet, .. } => {
std::result::Result::Ok(WalletBalanceContext { capability: "view", pubkey: wallet.pubkey().to_owned(), wallet_id: wallet_id.clone() })
},
crate::WalletSession::Owner { wallet_id, wallet, .. } => {
std::result::Result::Ok(WalletBalanceContext { capability: "owner", pubkey: wallet.pubkey().to_owned(), wallet_id: wallet_id.clone() })
},
crate::WalletSession::PrivilegedOperation { .. } => std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_SESSION_INVALID,
"Wallet balance is unavailable while a privileged Wallet operation is running",
)),
crate::WalletSession::Locked { .. } | crate::WalletSession::NoSelection => std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_AUTHORIZATION_REQUIRED,
"Wallet balance requires an authorized VIEW or OWNER session",
)),
};
}
fn balance_context_is_current(&self, context: &WalletBalanceContext) -> bool {
let session = self.wallet_session.lock();
let session = match session {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return false,
};
return match &*session {
crate::WalletSession::View { wallet_id, wallet, .. } => {
context.capability == "view" && wallet_id == &context.wallet_id && wallet.pubkey() == &context.pubkey
},
crate::WalletSession::Owner { wallet_id, wallet, .. } => {
context.capability == "owner" && wallet_id == &context.wallet_id && wallet.pubkey() == &context.pubkey
},
crate::WalletSession::NoSelection | crate::WalletSession::Locked { .. } | crate::WalletSession::PrivilegedOperation { .. } => false,
};
}
/// Returns the resolved common splash timings captured during bootstrap.
#[must_use]
pub(crate) const fn splash_settings(&self) -> crate::SplashSettings {
@@ -562,6 +670,24 @@ fn configured_secret_missing_error() -> ksp_core_lib::Error {
return ksp_core_lib::Error::new(crate::ERROR_CODE_WALLET_SECRET_CANDIDATES_MISSING, "No effective Config-owned Wallet password candidate is available");
}
struct WalletBalanceContext {
capability: &'static str,
pubkey: ksp_core_lib::Pubkey,
wallet_id: String,
}
fn count_to_u32(value: usize, label: &'static str) -> ksp_core_lib::Result<u32> {
let converted = u32::try_from(value);
return match converted {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(error) => std::result::Result::Err(
ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "Wallet Desk runtime count exceeds DTO range")
.with_context("count_kind", label)
.with_source(error),
),
};
}
struct LoggingRuntimeState {
guard: ksp_logging_lib::LoggingGuard,
active_profile_id: std::option::Option<String>,

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/constants.rs
// version: 5
// version: 6
//! Logging targets, domains and composite component identifiers owned by Wallet Desk.
@@ -15,6 +15,8 @@ pub(crate) const TRACING_DOMAIN_BOOTSTRAP: &str = "wallet.bootstrap";
pub(crate) const TRACING_DOMAIN_FRONTEND: &str = "frontend";
/// Structured domain used by the Wallet Desk shell.
pub(crate) const TRACING_DOMAIN_SHELL: &str = "wallet.shell";
/// Structured domain used by Wallet Desk HTTP Transport and balance operations.
pub(crate) const TRACING_DOMAIN_TRANSPORT: &str = "wallet.transport";
/// Structured domain used while preparing Wallet filesystem roots.
pub(crate) const TRACING_DOMAIN_WALLET_CONFIG: &str = "wallet.config";
/// Structured domain used while enumerating and inspecting locked Wallet files.

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/dto_common.rs
// version: 3
// version: 4
//! Common Tauri DTOs shared by Wallet Desk shell commands.
@@ -41,6 +41,8 @@ pub(crate) struct RuntimeStatusDto {
pub(crate) active_composite_profile: String,
/// Active configured Logging profile, or `None` while the transient fallback runtime is active.
pub(crate) active_logging_profile: std::option::Option<String>,
/// Standard Transport profile selected by the active composite.
pub(crate) active_transport_profile: String,
/// Standard Wallet profile selected by the active composite.
pub(crate) active_wallet_profile: String,
/// Number of logical Config resources registered by the current application runtime.
@@ -57,6 +59,16 @@ pub(crate) struct RuntimeStatusDto {
pub(crate) shell_phase: String,
/// Safe startup diagnostic that caused fallback Logging, when applicable.
pub(crate) startup_diagnostic: std::option::Option<CommandErrorDto>,
/// Number of Transport endpoints currently eligible for normal routing.
pub(crate) transport_available_endpoint_count: u32,
/// Safe cluster labels configured by the selected Transport profile.
pub(crate) transport_clusters: std::vec::Vec<String>,
/// Total number of configured Transport endpoints.
pub(crate) transport_endpoint_count: u32,
/// Safe provider labels configured by the selected Transport profile.
pub(crate) transport_providers: std::vec::Vec<String>,
/// Logical Transport role used by Wallet Desk balance reads.
pub(crate) transport_role: String,
/// Resolved global Wallet root configured by `cfg.std.wallet`.
pub(crate) wallets_directory: String,
/// Optional profile-relative Wallet subdirectory selected by the active composite.

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/errors.rs
// version: 5
// version: 6
//! Application-local error codes for Wallet Desk composition and desktop runtime surfaces.
@@ -26,6 +26,9 @@ pub(crate) const ERROR_CODE_TAURI_WINDOW_MISSING: ksp_core_lib::ErrorCode = ksp_
/// A Tauri window show/focus/destroy/event operation failed.
pub(crate) const ERROR_CODE_TAURI_WINDOW_OPERATION_FAILED: ksp_core_lib::ErrorCode =
ksp_core_lib::ErrorCode::new("wallet_desk", "tauri_window_operation_failed");
/// A Wallet balance request requires an authorized VIEW or OWNER session.
pub(crate) const ERROR_CODE_WALLET_AUTHORIZATION_REQUIRED: ksp_core_lib::ErrorCode =
ksp_core_lib::ErrorCode::new("wallet_desk", "wallet_authorization_required");
/// The configured Wallet root or effective profile directory resolves to an unsupported filesystem object.
pub(crate) const ERROR_CODE_WALLET_DIRECTORY_INVALID: ksp_core_lib::ErrorCode = ksp_core_lib::ErrorCode::new("wallet_desk", "wallet_directory_invalid");
/// Wallet Desk could not inspect or create the configured Wallet directory tree.

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/lib.rs
// version: 6
// version: 7
//! Tauri desktop application shell for KSP Wallet management and inspection.
@@ -16,8 +16,10 @@ mod frontend_logging;
mod logging_runtime;
mod splash;
mod tauri;
mod transport_runtime;
mod tw_main;
mod tw_splash;
mod wallet_balance;
mod wallet_config;
mod wallet_inventory;
mod wallet_secrets;
@@ -50,6 +52,8 @@ pub(crate) use self::constants::TRACING_DOMAIN_BOOTSTRAP;
pub(crate) use self::constants::TRACING_DOMAIN_FRONTEND;
/// Structured domain used by the Wallet Desk shell.
pub(crate) use self::constants::TRACING_DOMAIN_SHELL;
/// Structured domain used by Wallet Desk HTTP Transport and balance operations.
pub(crate) use self::constants::TRACING_DOMAIN_TRANSPORT;
/// Structured domain used while preparing Wallet filesystem roots.
pub(crate) use self::constants::TRACING_DOMAIN_WALLET_CONFIG;
/// Structured domain used while enumerating and inspecting locked Wallet files.
@@ -96,6 +100,8 @@ pub(crate) use self::errors::ERROR_CODE_TAURI_RUNTIME_FAILED;
pub(crate) use self::errors::ERROR_CODE_TAURI_WINDOW_MISSING;
/// A Tauri window show/focus/destroy/event operation failed.
pub(crate) use self::errors::ERROR_CODE_TAURI_WINDOW_OPERATION_FAILED;
/// Wallet balance access requires an authorized VIEW or OWNER session.
pub(crate) use self::errors::ERROR_CODE_WALLET_AUTHORIZATION_REQUIRED;
/// The configured Wallet root or effective profile directory resolves to an unsupported filesystem object.
pub(crate) use self::errors::ERROR_CODE_WALLET_DIRECTORY_INVALID;
/// Wallet Desk could not inspect or create the configured Wallet directory tree.
@@ -120,6 +126,10 @@ pub(crate) use self::logging_runtime::launch_identity;
pub(crate) use self::splash::SplashOrderDto;
/// Runtime timings used by the common desk splash lifecycle.
pub(crate) use self::splash::SplashSettings;
/// Safe and executable Transport runtime retained by the application state.
pub(crate) use self::transport_runtime::TransportRuntime;
/// Resolves the composite-selected Transport profile and builds the executable HTTP pool.
pub(crate) use self::transport_runtime::initialize_transport;
/// Resolves the required main window or returns a typed error.
pub(crate) use self::tw_main::require_main_window;
/// Shows and focuses the main Wallet Desk window.
@@ -128,6 +138,10 @@ pub(crate) use self::tw_main::show_main_window;
pub(crate) use self::tw_splash::require_splash_window;
/// Starts the one-shot splash lifecycle after the splash frontend reports readiness.
pub(crate) use self::tw_splash::splash_frontend_ready_service;
/// Safe balance snapshot for the currently authorized Wallet.
pub(crate) use self::wallet_balance::WalletBalanceDto;
/// Formats exact lamports as a decimal SOL string without floating point.
pub(crate) use self::wallet_balance::format_lamports_as_sol;
/// Resolved Wallet Config and directory preparation status captured during application bootstrap.
pub(crate) use self::wallet_config::WalletConfigStartup;
/// Resolves the composite-selected Wallet Config and prepares its application-owned directory tree.

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/tauri.rs
// version: 4
// version: 5
//! Tauri runtime assembly for the KSP wallet desktop application.
@@ -44,6 +44,7 @@ fn configure_commands(builder: tauri::Builder<tauri::Wry>) -> tauri::Builder<tau
get_runtime_status,
list_wallets,
lock_wallet,
refresh_wallet_balance,
refresh_wallets,
select_wallet,
splash_frontend_ready,
@@ -108,6 +109,15 @@ fn get_runtime_status(state: tauri::State<'_, crate::AppState>) -> std::result::
};
}
#[tauri::command]
async fn refresh_wallet_balance(state: tauri::State<'_, crate::AppState>) -> std::result::Result<crate::WalletBalanceDto, crate::CommandErrorDto> {
let result = state.refresh_wallet_balance().await;
return match result {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(error) => std::result::Result::Err(crate::CommandErrorDto::from_error(&error)),
};
}
#[tauri::command]
async fn list_wallets(state: tauri::State<'_, crate::AppState>) -> std::result::Result<std::vec::Vec<crate::WalletInventoryEntryDto>, crate::CommandErrorDto> {
let root = state.wallet_inventory_root().to_path_buf();

View File

@@ -0,0 +1,97 @@
// file: crates/ksp-app-wallet-desk/src/transport_runtime.rs
// version: 1
//! Composite-selected HTTP Transport runtime owned by Wallet Desk.
/// Safe and executable Transport runtime retained by the application state.
pub(crate) struct TransportRuntime {
pool: ksp_onchain_transport_lib::HttpTransportPool,
profile_id: String,
role: ksp_onchain_transport_lib::HttpRoleName,
}
impl TransportRuntime {
/// Returns the shareable HTTP Transport pool.
#[must_use]
pub(crate) const fn pool(&self) -> &ksp_onchain_transport_lib::HttpTransportPool {
return &self.pool;
}
/// Returns the composite-selected Transport profile identifier.
#[must_use]
pub(crate) fn profile_id(&self) -> &str {
return self.profile_id.as_str();
}
/// Returns the logical role used by Wallet Desk read-only RPC calls.
#[must_use]
pub(crate) const fn role(&self) -> &ksp_onchain_transport_lib::HttpRoleName {
return &self.role;
}
/// Returns safe configured cluster labels without endpoint URLs.
#[must_use]
pub(crate) fn clusters(&self) -> std::vec::Vec<String> {
let mut values = self.pool.snapshot().endpoints().iter().map(|endpoint| return endpoint.cluster().to_owned()).collect::<std::vec::Vec<_>>();
values.sort();
values.dedup();
return values;
}
/// Returns safe configured provider labels without endpoint URLs.
#[must_use]
pub(crate) fn providers(&self) -> std::vec::Vec<String> {
let mut values = self.pool.snapshot().endpoints().iter().map(|endpoint| return endpoint.provider().to_owned()).collect::<std::vec::Vec<_>>();
values.sort();
values.dedup();
return values;
}
}
/// Resolves the composite-selected Transport profile and builds the executable HTTP pool.
pub(crate) fn initialize_transport(management: &ksp_config_lib::ConfigManagement) -> ksp_core_lib::Result<TransportRuntime> {
let environment = ksp_config_lib::ConfigEnvironment::load();
let environment = match environment {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let composite = crate::load_wallet_desk_composite(management);
let composite = match composite {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let profile = crate::required_composite_component_profile(&composite, crate::COMPOSITE_COMPONENT_ID_TRANSPORT, ksp_config_lib::FILE_ID_STD_TRANSPORT);
let profile = match profile {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let resolved = management.engine().resolve_transport_config_profile(&profile, &environment);
let resolved = match resolved {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let profile_id = resolved.profile_id().to_owned();
let pool = ksp_onchain_transport_lib::HttpTransportPool::new(resolved.into_settings());
let pool = match pool {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let role = ksp_onchain_transport_lib::HttpRoleName::new("default");
let descriptor = ksp_onchain_transport_lib::find_http_rpc_method("getBalance");
let descriptor = match descriptor {
std::option::Option::Some(value) => value,
std::option::Option::None => {
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_APP_STATE_INVALID,
"Wallet Desk Transport registry does not expose getBalance",
));
},
};
let selection = pool.select_for_method(&role, descriptor);
if let std::result::Result::Err(error) = selection {
return std::result::Result::Err(error);
}
let snapshot = pool.snapshot();
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, transport_profile = profile_id.as_str(), transport_role = role.as_str(), endpoint_count = snapshot.endpoint_count(), available_endpoint_count = snapshot.available_endpoint_count(), "initialized Wallet Desk HTTP Transport from composite-managed configuration");
return std::result::Result::Ok(TransportRuntime { pool, profile_id, role });
}

View File

@@ -0,0 +1,39 @@
// file: crates/ksp-app-wallet-desk/src/wallet_balance.rs
// version: 1
//! Safe Wallet balance projection produced by the typed HTTP Transport path.
use ts_rs::TS; // rust-rules: trait-import
/// Safe balance snapshot for the currently authorized Wallet.
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "../frontend/ts/bindings/ksp_app_wallet_desk/wallet_balance/WalletBalanceDto.ts")]
pub(crate) struct WalletBalanceDto {
/// Optional Solana RPC API version reported by the selected node.
pub(crate) api_version: std::option::Option<String>,
/// Exact decimal account balance in lamports, encoded as text to avoid JavaScript integer precision loss.
pub(crate) lamports: String,
/// RPC context slot associated with this balance.
pub(crate) slot: u64,
/// Exact decimal SOL presentation derived from lamports without floating point.
pub(crate) sol: String,
/// Composite-selected Transport profile used for the RPC.
pub(crate) transport_profile: String,
/// Logical Transport role used for the RPC.
pub(crate) transport_role: String,
/// Root-scoped Wallet identifier whose authorized Pubkey was queried.
pub(crate) wallet_id: String,
}
/// Formats exact lamports as a decimal SOL string with nine fractional digits.
#[must_use]
pub(crate) fn format_lamports_as_sol(lamports: u64) -> String {
let whole = lamports / 1_000_000_000;
let fractional = lamports % 1_000_000_000;
return format!("{whole}.{fractional:09}");
}
#[cfg(test)]
#[path = "../unit_tests/wallet_balance.rs"]
mod tests;