|
|
|
|
@@ -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>,
|
|
|
|
|
|