Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl KaspaCli {
}

pub async fn try_new_arc(options: Options) -> Result<Arc<Self>> {
let wallet = Arc::new(Wallet::try_new(Wallet::local_store()?, Some(Resolver::default()), None)?);
let wallet = Arc::new(Wallet::try_new(Wallet::local_store(None)?, Some(Resolver::default()), None)?);

let kaspa_cli = Arc::new(KaspaCli {
term: Arc::new(Mutex::new(None)),
Expand Down
7 changes: 4 additions & 3 deletions wallet/core/src/storage/local/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl LocalStoreInner {

async fn try_load(wallet_secret: &Secret, folder: &str, args: OpenArgs) -> Result<Self> {
let filename = make_filename(&None, &args.filename);
let storage = Storage::try_new_with_folder(folder, &format!("{filename}.wallet"))?;
let storage = Storage::try_new_with_folder(folder, &filename)?;

let wallet = WalletStorage::try_load(&storage).await?;
let cache = Arc::new(RwLock::new(Cache::from_wallet(wallet, wallet_secret)?));
Expand Down Expand Up @@ -289,9 +289,10 @@ pub(crate) struct LocalStore {
}

impl LocalStore {
pub fn try_new(is_resident: bool) -> Result<Self> {
pub fn try_new(is_resident: bool, path: Option<PathBuf>) -> Result<Self> {
let location = if let Some(path) = path { Location::new(path.to_str().unwrap()) } else { Location::default() };
Ok(Self {
location: Arc::new(Mutex::new(Some(Arc::new(Location::default())))),
location: Arc::new(Mutex::new(Some(Arc::new(location)))),
inner: Arc::new(Mutex::new(None)),
is_resident,
batch: Arc::new(AtomicBool::new(false)),
Expand Down
10 changes: 6 additions & 4 deletions wallet/core/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
pub mod api;
pub mod args;
pub mod maps;

pub use args::*;
use std::path::PathBuf;

use crate::account::ScanNotifier;
use crate::api::traits::WalletApi;
Expand Down Expand Up @@ -123,18 +125,18 @@ pub struct Wallet {

impl Default for Wallet {
fn default() -> Self {
let storage = Wallet::local_store().expect("Unable to initialize local storage");
let storage = Wallet::local_store(None).expect("Unable to initialize local storage");
Wallet::try_new(storage, None, None).unwrap()
}
}

impl Wallet {
pub fn local_store() -> Result<Arc<dyn Interface>> {
Ok(Arc::new(LocalStore::try_new(false)?))
pub fn local_store(path: Option<PathBuf>) -> Result<Arc<dyn Interface>> {
Ok(Arc::new(LocalStore::try_new(false, path)?))
}

pub fn resident_store() -> Result<Arc<dyn Interface>> {
Ok(Arc::new(LocalStore::try_new(true)?))
Ok(Arc::new(LocalStore::try_new(true, None)?))
}

pub fn try_new(storage: Arc<dyn Interface>, resolver: Option<Resolver>, network_id: Option<NetworkId>) -> Result<Wallet> {
Expand Down
2 changes: 1 addition & 1 deletion wallet/core/src/wasm/wallet/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Wallet {
pub fn constructor(config: IWalletConfig) -> Result<Wallet> {
let WalletCtorArgs { resident, network_id, encoding, url, resolver } = WalletCtorArgs::try_from(JsValue::from(config))?;

let store = Arc::new(LocalStore::try_new(resident)?);
let store = Arc::new(LocalStore::try_new(resident, None)?);

let rpc_config = RpcConfig { url, resolver, encoding, network_id };

Expand Down
3 changes: 3 additions & 0 deletions wallet/daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ workflow-terminal.workspace = true
clap.workspace = true
futures-util.workspace = true

[dev-dependencies]
tempfile.workspace = true

[lints]
workspace = true
68 changes: 55 additions & 13 deletions wallet/daemon/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clap::{Arg, Command};
use kaspa_core::kaspad_env::version;
use std::io;
use std::net::SocketAddr;
use std::path::PathBuf;

pub struct Args {
pub password: String,
Expand All @@ -9,23 +11,26 @@ pub struct Args {
pub network_id: Option<String>,
pub listen_address: SocketAddr,
pub ecdsa: bool,
pub location: Option<PathBuf>,
}

impl Args {
pub fn parse() -> Self {
pub fn parse() -> Result<Self, Box<dyn std::error::Error>> {
let matches = cli().get_matches();

Args {
let key_file = matches.get_one::<PathBuf>("keys-file").cloned();
let (name, location) = parse_keys_file_arg(key_file)?;
Ok(Args {
password: matches.get_one::<String>("password").cloned().expect("Password argument is missing."),
name: matches.get_one::<String>("name").cloned(),
name,
rpc_server: matches.get_one::<String>("rpc-server").cloned(),
network_id: matches.get_one::<String>("network-id").cloned(),
listen_address: matches
.get_one::<SocketAddr>("listen-address")
.cloned()
.unwrap_or_else(|| "127.0.0.1:8082".parse().unwrap()),
ecdsa: matches.get_one::<bool>("ecdsa").cloned().unwrap_or(false),
}
location,
})
}
}

Expand All @@ -34,14 +39,6 @@ pub fn cli() -> Command {
.about(format!("{} (kaspawalletd) v{}", env!("CARGO_PKG_DESCRIPTION"), version()))
.version(env!("CARGO_PKG_VERSION"))
.arg(Arg::new("password").long("password").short('p').value_name("password").help("Path of password file").required(true))
.arg(
Arg::new("name")
.long("name")
.short('n')
.value_name("name")
.value_parser(clap::value_parser!(String))
.help("Name of wallet"),
)
.arg(
Arg::new("rpc-server")
.long("rpc-server")
Expand Down Expand Up @@ -72,4 +69,49 @@ pub fn cli() -> Command {
.value_parser(clap::value_parser!(bool))
.help("Use ecdsa for transactions broadcast"),
)
.arg(
Arg::new("keys-file")
.long("keys-file")
.short('f')
.value_name("keys-file")
.value_parser(clap::value_parser!(PathBuf))
.help("Keys file location"),
)
}

fn parse_keys_file_arg(keys_file: Option<PathBuf>) -> Result<(Option<String>, Option<PathBuf>), Box<dyn std::error::Error>> {
if let Some(keys_file) = keys_file {
if keys_file.is_dir() {
Ok((None, Some(keys_file)))
} else {
let name = keys_file
.file_name()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid wallet file path"))?
.to_str()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Wallet file path is not valid UTF-8"))?
.to_owned();
Ok((Some(name), keys_file.parent().map(|p| p.to_owned())))
}
} else {
Ok((None, None))
}
}

#[cfg(test)]
mod tests {
use super::*;
use tempfile::NamedTempFile;

#[test]
fn test_parse_keys_file() {
// arrange
let tmp_keys_file = NamedTempFile::new().unwrap();

// act
let (name, location) = parse_keys_file_arg(Some(tmp_keys_file.path().to_path_buf())).unwrap();

// assert
assert_eq!(name, Some(tmp_keys_file.path().file_name().unwrap().to_str().unwrap().to_owned()));
assert_eq!(location, Some(tmp_keys_file.path().parent().unwrap().to_path_buf()));
}
}
4 changes: 2 additions & 2 deletions wallet/daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use tonic::transport::Server;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
kaspa_core::log::init_logger(None, "");
let args = Args::parse();
let args = Args::parse()?;

let wallet = Arc::new(Wallet::try_new(Wallet::local_store()?, Some(Resolver::default()), None)?);
let wallet = Arc::new(Wallet::try_new(Wallet::local_store(args.location)?, Some(Resolver::default()), None)?);
wallet.clone().wallet_open(args.password.into(), args.name, false, false).await?;
info!("Wallet path: {}", wallet.store().location()?);

Expand Down
Loading