v0.5.2-pre.002
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// file: ks-wallet/src/wallet.rs
|
||||
// version: 10
|
||||
// version: 11
|
||||
|
||||
//! Local wallet storage and signing primitives.
|
||||
|
||||
@@ -46,17 +46,29 @@ pub struct WalletPolicy {
|
||||
pub lamport_spend_limit: std::option::Option<u64>,
|
||||
}
|
||||
|
||||
/// Non-secret description of a loaded wallet.
|
||||
/// Whether a runtime wallet is temporary or persistent.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum WalletPersistence {
|
||||
/// Wallet exists only in runtime memory.
|
||||
Temporary,
|
||||
/// Wallet originates from persistent local storage.
|
||||
Persistent,
|
||||
}
|
||||
|
||||
/// Minimal non-secret identity of a wallet.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct WalletSummary {
|
||||
/// Validated local alias.
|
||||
pub struct WalletIdentity {
|
||||
/// Validated logical alias.
|
||||
pub alias: crate::WalletAlias,
|
||||
/// Base58 Solana public key.
|
||||
pub public_key: std::string::String,
|
||||
/// Persistent keypair path when the wallet was loaded from local storage.
|
||||
pub storage_path: std::option::Option<std::path::PathBuf>,
|
||||
/// Whether the runtime wallet is temporary or persistent.
|
||||
pub persistence: crate::WalletPersistence,
|
||||
}
|
||||
|
||||
/// Backward-compatible name for the non-secret wallet identity.
|
||||
pub type WalletSummary = crate::WalletIdentity;
|
||||
|
||||
/// Solana keypair kept private inside the wallet boundary.
|
||||
pub struct TemporaryWallet {
|
||||
alias: crate::WalletAlias,
|
||||
@@ -70,7 +82,7 @@ impl std::fmt::Debug for crate::TemporaryWallet {
|
||||
.debug_struct("TemporaryWallet")
|
||||
.field("alias", &self.alias)
|
||||
.field("public_key", &self.public_key())
|
||||
.field("storage_path", &self.storage_path)
|
||||
.field("persistence", &self.persistence())
|
||||
.finish();
|
||||
}
|
||||
}
|
||||
@@ -85,15 +97,28 @@ impl crate::TemporaryWallet {
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns a non-secret summary suitable for logs, CLI output or application adapters.
|
||||
pub fn summary(&self) -> crate::WalletSummary {
|
||||
return crate::WalletSummary {
|
||||
/// Returns the minimal non-secret wallet identity.
|
||||
pub fn identity(&self) -> crate::WalletIdentity {
|
||||
return crate::WalletIdentity {
|
||||
alias: self.alias.clone(),
|
||||
public_key: self.public_key(),
|
||||
storage_path: self.storage_path.clone(),
|
||||
persistence: self.persistence(),
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the non-secret wallet identity using the historical API name.
|
||||
pub fn summary(&self) -> crate::WalletSummary {
|
||||
return self.identity();
|
||||
}
|
||||
|
||||
/// Returns whether this runtime wallet is temporary or persistent.
|
||||
pub fn persistence(&self) -> crate::WalletPersistence {
|
||||
if self.storage_path.is_some() {
|
||||
return crate::WalletPersistence::Persistent;
|
||||
}
|
||||
return crate::WalletPersistence::Temporary;
|
||||
}
|
||||
|
||||
/// Returns the wallet public key in base58 form.
|
||||
pub fn public_key(&self) -> std::string::String {
|
||||
return self.keypair.pubkey().to_string();
|
||||
@@ -443,7 +468,7 @@ mod tests {
|
||||
assert!(!signature.is_empty());
|
||||
assert_eq!(wallet.public_key(), wallet.as_signer().pubkey().to_string());
|
||||
assert_eq!(wallet.public_key(), wallet.as_sync_signer().pubkey().to_string());
|
||||
assert!(wallet.summary().storage_path.is_none());
|
||||
assert_eq!(wallet.summary().persistence, crate::WalletPersistence::Temporary);
|
||||
assert!(!format!("{wallet:?}").contains("secret"));
|
||||
}
|
||||
|
||||
@@ -464,10 +489,7 @@ mod tests {
|
||||
.await
|
||||
.unwrap_or_else(|error| panic!("unexpected load error: {error}"));
|
||||
assert_eq!(created.public_key(), loaded.public_key());
|
||||
assert_eq!(
|
||||
created.summary().storage_path,
|
||||
std::option::Option::Some(store.wallet_path(&alias))
|
||||
);
|
||||
assert_eq!(created.summary().persistence, crate::WalletPersistence::Persistent);
|
||||
let encoded = std::fs::read(store.wallet_path(&alias))
|
||||
.unwrap_or_else(|error| panic!("wallet file must be readable: {error}"));
|
||||
let keypair_bytes = serde_json::from_slice::<std::vec::Vec<u8>>(encoded.as_slice())
|
||||
|
||||
Reference in New Issue
Block a user