v0.2.6-pre.010-fix.001

This commit is contained in:
2026-08-21 14:53:50 +02:00
parent 4dcdbfaa55
commit 6dcd278459
13 changed files with 280 additions and 45 deletions

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/app_state.rs
// version: 13
// version: 14
//! Shared backend state owned by the Wallet Desk Tauri application.
@@ -483,6 +483,18 @@ impl AppState {
"Wallet unlock operation is already in progress",
));
},
crate::WalletSession::ViewOperation { wallet_id, path, pubkey } => {
let session = self.wallet_session.lock();
let mut session = match session {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
};
*session = crate::WalletSession::ViewOperation { wallet_id, path, pubkey };
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_SESSION_INVALID,
"Wallet VIEW privileged operation is already in progress",
));
},
crate::WalletSession::OwnerOperation { wallet_id, path, pubkey, view_enabled } => {
let session = self.wallet_session.lock();
let mut session = match session {
@@ -722,23 +734,146 @@ impl AppState {
return self.finish_owner_operation(context, result).await;
}
/// Rotates the enabled VIEW password from the current OWNER session without requiring the previous VIEW password.
/// Rotates the enabled VIEW password from either VIEW self-service or OWNER administration.
pub(crate) async fn rotate_view_password(&self, request: crate::WalletPasswordRotationRequestDto) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
let context = self.begin_owner_operation("view_password_rotate");
let owner_context = self.begin_owner_operation("view_password_rotate");
if let std::result::Result::Ok(mut context) = owner_context {
if !context.view_enabled {
self.restore_owner_after_operation_failure(context);
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_VIEW_DISABLED,
"Wallet VIEW capability is disabled and cannot rotate a VIEW password",
));
}
let password = ksp_wallet_lib::ViewPassword::new(request.password);
let result = context.owner.rotate_view_password(context.path.as_path(), password).await;
return self.finish_owner_operation(context, result).await;
}
let context = self.begin_view_operation("view_password_self_rotate");
let mut context = match context {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
if !context.view_enabled {
self.restore_owner_after_operation_failure(context);
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_VIEW_DISABLED,
"Wallet VIEW capability is disabled and cannot rotate a VIEW password",
));
}
let password = ksp_wallet_lib::ViewPassword::new(request.password);
let result = context.owner.rotate_view_password(context.path.as_path(), password).await;
return self.finish_owner_operation(context, result).await;
let result = context.view.rotate_view_password(context.path.as_path(), password).await;
return self.finish_view_operation(context, result).await;
}
fn begin_view_operation(&self, operation: &'static str) -> ksp_core_lib::Result<ViewOperationContext> {
let session = self.wallet_session.lock();
let mut session = match session {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
};
let previous = std::mem::replace(&mut *session, crate::WalletSession::no_selection());
let (wallet_id, path, view) = match previous {
crate::WalletSession::View { wallet_id, path, wallet } => (wallet_id, path, wallet),
other => {
*session = other;
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_AUTHORIZATION_REQUIRED,
"Wallet VIEW self-service operation requires an authorized VIEW session",
));
},
};
let pubkey = view.pubkey().to_owned();
let context = ViewOperationContext { operation, path: path.clone(), pubkey, view, wallet_id: wallet_id.clone() };
*session = crate::WalletSession::ViewOperation { wallet_id, path, pubkey };
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = context.wallet_id.as_str(), operation, "Wallet VIEW privileged operation started");
return std::result::Result::Ok(context);
}
async fn finish_view_operation(
&self,
context: ViewOperationContext,
operation_result: ksp_core_lib::Result<()>,
) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
if let std::result::Result::Err(error) = operation_result {
if error.code() == ksp_wallet_lib::ERROR_CODE_STATE_CONFLICT {
self.recover_view_state_conflict(context).await;
return std::result::Result::Err(error);
}
self.restore_view_after_operation_failure(context);
return std::result::Result::Err(error);
}
return self.install_view_after_operation_success(context);
}
fn install_view_after_operation_success(&self, context: ViewOperationContext) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
let ViewOperationContext { operation, path, pubkey, view, wallet_id } = context;
let configured_secret_candidate_count = self.secret_candidate_count_or_zero(wallet_id.as_str());
let dto = crate::view_projection(wallet_id.as_str(), true, configured_secret_candidate_count, &view);
let session = self.wallet_session.lock();
let mut session = match session {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
};
let previous = std::mem::replace(&mut *session, crate::WalletSession::no_selection());
match previous {
crate::WalletSession::ViewOperation { wallet_id: reserved_wallet_id, path: reserved_path, pubkey: reserved_pubkey }
if reserved_wallet_id == wallet_id && reserved_path == path && reserved_pubkey == pubkey =>
{
*session = crate::WalletSession::View { wallet_id: wallet_id.clone(), path, wallet: view };
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = wallet_id.as_str(), operation, "Wallet VIEW privileged operation completed");
return std::result::Result::Ok(dto);
},
other => {
*session = other;
drop(view);
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_SESSION_INVALID,
"Wallet VIEW operation completion no longer owns the selected session",
));
},
}
}
fn restore_view_after_operation_failure(&self, context: ViewOperationContext) {
let ViewOperationContext { operation, path, pubkey, view, wallet_id } = context;
let session = self.wallet_session.lock();
let mut session = match session {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return,
};
let previous = std::mem::replace(&mut *session, crate::WalletSession::no_selection());
match previous {
crate::WalletSession::ViewOperation { wallet_id: reserved_wallet_id, path: reserved_path, pubkey: reserved_pubkey }
if reserved_wallet_id == wallet_id && reserved_path == path && reserved_pubkey == pubkey =>
{
*session = crate::WalletSession::View { wallet_id: wallet_id.clone(), path, wallet: view };
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = wallet_id.as_str(), operation, "Wallet VIEW privileged operation failed without invalidating the authenticated handle");
},
other => {
*session = other;
drop(view);
},
}
}
async fn recover_view_state_conflict(&self, context: ViewOperationContext) {
let ViewOperationContext { operation, path, pubkey, view, wallet_id } = context;
drop(view);
let inspected = ksp_wallet_lib::inspect_locked_wallet_file_v1(path.as_path()).await;
let session = self.wallet_session.lock();
let mut session = match session {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return,
};
let previous = std::mem::replace(&mut *session, crate::WalletSession::no_selection());
match previous {
crate::WalletSession::ViewOperation { wallet_id: reserved_wallet_id, path: reserved_path, pubkey: reserved_pubkey }
if reserved_wallet_id == wallet_id && reserved_path == path && reserved_pubkey == pubkey =>
{
if let std::result::Result::Ok(locked_info) = inspected {
*session = crate::WalletSession::Locked { wallet_id: wallet_id.clone(), path, locked_info };
}
ksp_logging_lib::warn!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = wallet_id.as_str(), operation, "Wallet VIEW state conflict purged the stale handle and forced reauthorization");
},
other => {
*session = other;
return;
},
}
}
fn begin_owner_operation(&self, operation: &'static str) -> ksp_core_lib::Result<OwnerOperationContext> {
@@ -920,7 +1055,7 @@ impl AppState {
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::OwnerOperation { .. } | crate::WalletSession::PrivilegedOperation { .. } => {
crate::WalletSession::ViewOperation { .. } | crate::WalletSession::OwnerOperation { .. } | 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",
@@ -948,6 +1083,7 @@ impl AppState {
},
crate::WalletSession::NoSelection
| crate::WalletSession::Locked { .. }
| crate::WalletSession::ViewOperation { .. }
| crate::WalletSession::OwnerOperation { .. }
| crate::WalletSession::PrivilegedOperation { .. } => false,
};
@@ -995,6 +1131,14 @@ 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 ViewOperationContext {
operation: &'static str,
path: std::path::PathBuf,
pubkey: ksp_core_lib::Pubkey,
view: std::boxed::Box<ksp_wallet_lib::WalletView>,
wallet_id: String,
}
struct OwnerOperationContext {
operation: &'static str,
owner: std::boxed::Box<ksp_wallet_lib::WalletOwner>,

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/lib.rs
// version: 10
// version: 11
//! Tauri desktop application shell for KSP Wallet management and inspection.
@@ -200,7 +200,7 @@ pub(crate) use self::wallet_metadata::WalletNoteUpdateRequestDto;
pub(crate) use self::wallet_secrets::discover_wallet_secret_candidates;
/// Returns the configured Wallet password candidate count without exposing their names or values.
pub(crate) use self::wallet_secrets::wallet_secret_candidate_count;
/// New credential request moved frontend -> Rust only for one explicit OWNER-authorized rotation.
/// New credential request moved frontend -> Rust only for one explicit VIEW/OWNER-authorized rotation.
pub(crate) use self::wallet_security::WalletPasswordRotationRequestDto;
/// Authorized OWNER projection exposed only after successful creation.
pub(crate) use self::wallet_session::WalletAuthorizedDto;

View File

@@ -1,7 +1,7 @@
// file: crates/ksp-app-wallet-desk/src/wallet_security.rs
// version: 1
// version: 2
//! OWNER-only credential rotation contracts for Wallet Desk.
//! VIEW/OWNER credential rotation contracts for Wallet Desk.
use ts_rs::TS; // rust-rules: trait-import

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/wallet_session.rs
// version: 6
// version: 7
//! Durable root-scoped Wallet session lifecycle for Wallet Desk.
@@ -14,7 +14,7 @@ pub(crate) enum WalletSessionStateDto {
NoSelection,
/// One Wallet is selected but remains locked.
Locked,
/// One explicit unlock operation is running in Rust.
/// One explicit privileged Wallet operation is running in Rust.
PrivilegedOperation,
/// One Wallet is open with VIEW capability.
ViewOpen,
@@ -150,6 +150,15 @@ pub(crate) enum WalletSession {
/// Authorized VIEW handle retaining metadata capability only in Rust.
wallet: std::boxed::Box<ksp_wallet_lib::WalletView>,
},
/// One VIEW-authorized self-service mutation temporarily owns the handle outside the session mutex.
ViewOperation {
/// Native Wallet filename / root-scoped identifier.
wallet_id: String,
/// Full root-scoped filesystem path retained only in Rust.
path: std::path::PathBuf,
/// Authorized Solana identity used only to reject stale completion.
pubkey: ksp_core_lib::Pubkey,
},
/// One Wallet kept open with OWNER capability.
Owner {
/// Native Wallet filename / root-scoped identifier.
@@ -208,6 +217,11 @@ impl WalletSession {
state: WalletSessionStateDto::ViewOpen,
wallet_id: std::option::Option::Some(wallet_id.clone()),
},
Self::ViewOperation { wallet_id, .. } => WalletSessionDto {
filename: std::option::Option::Some(wallet_id.clone()),
state: WalletSessionStateDto::PrivilegedOperation,
wallet_id: std::option::Option::Some(wallet_id.clone()),
},
Self::Owner { wallet_id, .. } => WalletSessionDto {
filename: std::option::Option::Some(wallet_id.clone()),
state: WalletSessionStateDto::OwnerOpen,