53 lines
2.3 KiB
Rust
53 lines
2.3 KiB
Rust
// file: crates/ksp-app-wallet-desk/src/wallet_export.rs
|
|
// version: 1
|
|
|
|
//! OWNER-only native save-picker Wallet transfer export contracts.
|
|
|
|
use ts_rs::TS; // rust-rules: trait-import
|
|
|
|
/// OWNER export request containing only the explicit transfer format.
|
|
#[derive(serde::Deserialize, TS)]
|
|
#[serde(rename_all = "camelCase")]
|
|
#[ts(export, export_to = "../frontend/ts/bindings/ksp_app_wallet_desk/wallet_export/WalletExportRequestDto.ts")]
|
|
pub(crate) struct WalletExportRequestDto {
|
|
/// Secret-transfer encoding selected before the Rust-owned save picker opens.
|
|
pub(crate) format: crate::WalletTransferFormatDto,
|
|
}
|
|
|
|
/// Safe result returned after an OWNER keypair export completes.
|
|
#[derive(serde::Serialize, TS)]
|
|
#[serde(rename_all = "camelCase")]
|
|
#[ts(export, export_to = "../frontend/ts/bindings/ksp_app_wallet_desk/wallet_export/WalletExportResultDto.ts")]
|
|
pub(crate) struct WalletExportResultDto {
|
|
/// Basename only for the newly created export file; the parent path remains Rust-only.
|
|
pub(crate) destination_name: String,
|
|
/// Explicit transfer format used by the export.
|
|
pub(crate) format: crate::WalletTransferFormatDto,
|
|
/// Root-scoped native Wallet identifier that authorized the export.
|
|
pub(crate) wallet_id: String,
|
|
}
|
|
|
|
/// Builds a deterministic native-save default filename without exposing Wallet metadata or Pubkey.
|
|
#[must_use]
|
|
pub(crate) fn wallet_export_default_filename(wallet_id: &str, format: crate::WalletTransferFormatDto) -> String {
|
|
let stem = wallet_id.strip_suffix(crate::WALLET_FILE_SUFFIX).unwrap_or(wallet_id);
|
|
let stem = if stem.is_empty() { "wallet" } else { stem };
|
|
return format!("{stem}-solana-keypair.{}", format.default_extension());
|
|
}
|
|
|
|
/// Extracts one safe basename from a native save-picker destination.
|
|
pub(crate) fn wallet_export_destination_name(destination: &std::path::Path) -> ksp_core_lib::Result<String> {
|
|
let name = destination.file_name().and_then(std::ffi::OsStr::to_str);
|
|
return match name {
|
|
std::option::Option::Some(value) if !value.is_empty() => std::result::Result::Ok(value.to_owned()),
|
|
_ => std::result::Result::Err(ksp_core_lib::Error::new(
|
|
crate::ERROR_CODE_WALLET_EXPORT_DESTINATION_INVALID,
|
|
"Selected Wallet export destination has no usable local filename",
|
|
)),
|
|
};
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "../unit_tests/wallet_export.rs"]
|
|
mod tests;
|