v0.2.6-pre.009

This commit is contained in:
2026-08-21 12:35:01 +02:00
parent 7d03825ec6
commit 2b7afab663
17 changed files with 970 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/app_state.rs
// version: 10
// version: 11
//! Shared backend state owned by the Wallet Desk Tauri application.
@@ -126,7 +126,7 @@ impl AppState {
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.008-wallet-import".to_owned(),
shell_phase: "pre.009-owner-metadata".to_owned(),
startup_diagnostic: runtime.startup_diagnostic.clone(),
transport_available_endpoint_count,
transport_clusters: self.transport_runtime.clusters(),
@@ -217,7 +217,7 @@ impl AppState {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
};
*session = crate::WalletSession::Owner { wallet_id: filename.clone(), path: destination, wallet: std::boxed::Box::new(owner) };
*session = crate::WalletSession::Owner { wallet_id: filename.clone(), path: destination, view_enabled, wallet: std::boxed::Box::new(owner) };
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = filename.as_str(), view_enabled, "Wallet created and OWNER session opened");
return std::result::Result::Ok(dto);
}
@@ -313,7 +313,7 @@ impl AppState {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
};
*session = crate::WalletSession::Owner { wallet_id: filename.clone(), path: destination, wallet: std::boxed::Box::new(owner) };
*session = crate::WalletSession::Owner { wallet_id: filename.clone(), path: destination, view_enabled, wallet: std::boxed::Box::new(owner) };
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_IMPORT, wallet_id = filename.as_str(), transfer_format = transfer_format.code(), view_enabled, "Wallet transfer imported and OWNER session opened");
return std::result::Result::Ok(dto);
}
@@ -483,6 +483,18 @@ impl AppState {
"Wallet unlock 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 {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
};
*session = crate::WalletSession::OwnerOperation { wallet_id, path, pubkey, view_enabled };
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_SESSION_INVALID,
"Wallet OWNER metadata operation is already in progress",
));
},
crate::WalletSession::View { wallet_id, path, wallet, .. } => {
drop(wallet);
(wallet_id, path)
@@ -595,7 +607,7 @@ impl AppState {
},
};
let dto = crate::view_projection(wallet_id.as_str(), view_enabled, configured_secret_candidate_count, &view);
*session = crate::WalletSession::View { wallet_id: wallet_id.clone(), path, wallet: std::boxed::Box::new(view) };
*session = crate::WalletSession::View { wallet_id: wallet_id.clone(), path, view_enabled, wallet: std::boxed::Box::new(view) };
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = wallet_id.as_str(), capability = "view", "Wallet VIEW session opened");
return std::result::Result::Ok(dto);
}
@@ -633,7 +645,7 @@ impl AppState {
},
};
let dto = crate::owner_projection(wallet_id.as_str(), view_enabled, configured_secret_candidate_count, &owner);
*session = crate::WalletSession::Owner { wallet_id: wallet_id.clone(), path, wallet: std::boxed::Box::new(owner) };
*session = crate::WalletSession::Owner { wallet_id: wallet_id.clone(), path, view_enabled, wallet: std::boxed::Box::new(owner) };
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = wallet_id.as_str(), capability = "owner", "Wallet OWNER session opened");
return std::result::Result::Ok(dto);
}
@@ -649,6 +661,178 @@ impl AppState {
};
}
/// Updates or clears the protected alias through the current OWNER handle.
pub(crate) async fn update_wallet_alias(&self, request: crate::WalletAliasUpdateRequestDto) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
let context = self.begin_owner_metadata_operation("alias_update");
let mut context = match context {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let alias = normalize_optional_text(request.alias);
let result = context.owner.update_alias(context.path.as_path(), alias).await;
return self.finish_owner_metadata_operation(context, result).await;
}
/// Appends one protected note through the current OWNER handle.
pub(crate) async fn add_wallet_note(&self, request: crate::WalletNoteAddRequestDto) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
let context = self.begin_owner_metadata_operation("note_add");
let mut context = match context {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let result = context.owner.add_note(context.path.as_path(), request.text).await;
let result = match result {
std::result::Result::Ok(_) => std::result::Result::Ok(()),
std::result::Result::Err(error) => std::result::Result::Err(error),
};
return self.finish_owner_metadata_operation(context, result).await;
}
/// Updates one protected note through the current OWNER handle.
pub(crate) async fn update_wallet_note(&self, request: crate::WalletNoteUpdateRequestDto) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
let context = self.begin_owner_metadata_operation("note_update");
let mut context = match context {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let result = context.owner.update_note(context.path.as_path(), request.note_id.as_str(), request.text).await;
return self.finish_owner_metadata_operation(context, result).await;
}
/// Deletes one protected note through the current OWNER handle.
pub(crate) async fn delete_wallet_note(&self, request: crate::WalletNoteDeleteRequestDto) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
let context = self.begin_owner_metadata_operation("note_delete");
let mut context = match context {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let result = context.owner.delete_note(context.path.as_path(), request.note_id.as_str()).await;
return self.finish_owner_metadata_operation(context, result).await;
}
fn begin_owner_metadata_operation(&self, operation: &'static str) -> ksp_core_lib::Result<OwnerMetadataContext> {
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_enabled, owner) = match previous {
crate::WalletSession::Owner { wallet_id, path, view_enabled, wallet } => (wallet_id, path, view_enabled, wallet),
other => {
*session = other;
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_AUTHORIZATION_REQUIRED,
"Wallet metadata administration requires an authorized OWNER session",
));
},
};
let pubkey = owner.pubkey().to_owned();
let context = OwnerMetadataContext { operation, owner, path: path.clone(), pubkey, view_enabled, wallet_id: wallet_id.clone() };
*session = crate::WalletSession::OwnerOperation { wallet_id, path, pubkey, view_enabled };
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = context.wallet_id.as_str(), operation, "Wallet OWNER metadata operation started");
return std::result::Result::Ok(context);
}
async fn finish_owner_metadata_operation(
&self,
context: OwnerMetadataContext,
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_owner_metadata_state_conflict(context).await;
return std::result::Result::Err(error);
}
self.restore_owner_after_metadata_failure(context);
return std::result::Result::Err(error);
}
return self.install_owner_after_metadata_success(context);
}
fn install_owner_after_metadata_success(&self, context: OwnerMetadataContext) -> ksp_core_lib::Result<crate::WalletAuthorizedDto> {
let OwnerMetadataContext { operation, owner, path, pubkey, view_enabled, wallet_id } = context;
let configured_secret_candidate_count = self.secret_candidate_count_or_zero(wallet_id.as_str());
let dto = crate::owner_projection(wallet_id.as_str(), view_enabled, configured_secret_candidate_count, &owner);
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::OwnerOperation {
wallet_id: reserved_wallet_id,
path: reserved_path,
pubkey: reserved_pubkey,
view_enabled: reserved_view_enabled,
} if reserved_wallet_id == wallet_id && reserved_path == path && reserved_pubkey == pubkey && reserved_view_enabled == view_enabled => {
*session = crate::WalletSession::Owner { wallet_id: wallet_id.clone(), path, view_enabled, wallet: owner };
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = wallet_id.as_str(), operation, "Wallet OWNER metadata operation completed");
return std::result::Result::Ok(dto);
},
other => {
*session = other;
drop(owner);
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_SESSION_INVALID,
"Wallet metadata completion no longer owns the selected OWNER session",
));
},
}
}
fn restore_owner_after_metadata_failure(&self, context: OwnerMetadataContext) {
let OwnerMetadataContext { operation, owner, path, pubkey, view_enabled, 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::OwnerOperation {
wallet_id: reserved_wallet_id,
path: reserved_path,
pubkey: reserved_pubkey,
view_enabled: reserved_view_enabled,
} if reserved_wallet_id == wallet_id && reserved_path == path && reserved_pubkey == pubkey && reserved_view_enabled == view_enabled => {
*session = crate::WalletSession::Owner { wallet_id: wallet_id.clone(), path, view_enabled, wallet: owner };
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_WALLET_SESSION, wallet_id = wallet_id.as_str(), operation, "Wallet OWNER metadata operation failed without invalidating the authenticated handle");
},
other => {
*session = other;
drop(owner);
},
}
}
async fn recover_owner_metadata_state_conflict(&self, context: OwnerMetadataContext) {
let OwnerMetadataContext { operation, owner, path, pubkey, view_enabled, wallet_id } = context;
drop(owner);
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::OwnerOperation {
wallet_id: reserved_wallet_id,
path: reserved_path,
pubkey: reserved_pubkey,
view_enabled: reserved_view_enabled,
} if reserved_wallet_id == wallet_id && reserved_path == path && reserved_pubkey == pubkey && reserved_view_enabled == view_enabled => {
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 OWNER metadata state conflict purged the stale handle and forced reauthorization");
},
other => *session = other,
}
}
/// 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();
@@ -702,10 +886,12 @@ 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::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::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",
))
},
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",
@@ -726,7 +912,10 @@ impl AppState {
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,
crate::WalletSession::NoSelection
| crate::WalletSession::Locked { .. }
| crate::WalletSession::OwnerOperation { .. }
| crate::WalletSession::PrivilegedOperation { .. } => false,
};
}
@@ -772,6 +961,15 @@ 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 OwnerMetadataContext {
operation: &'static str,
owner: std::boxed::Box<ksp_wallet_lib::WalletOwner>,
path: std::path::PathBuf,
pubkey: ksp_core_lib::Pubkey,
view_enabled: bool,
wallet_id: String,
}
struct WalletBalanceContext {
capability: &'static str,
pubkey: ksp_core_lib::Pubkey,