Skip to content

Commit

Permalink
Merge pull request zingolabs#594 from fluidvanadium/housekeeping
Browse files Browse the repository at this point in the history
add path helper functions to lightclient
  • Loading branch information
zancas authored Oct 15, 2023
2 parents 4107d65 + aef08e8 commit fbf4aee
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 12 deletions.
2 changes: 1 addition & 1 deletion zingocli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ pub fn startup(
false,
)?),
None => {
if config.wallet_exists() {
if config.wallet_path_exists() {
Arc::new(LightClient::read_wallet_from_disk(&config)?)
} else {
println!("Creating a new wallet");
Expand Down
20 changes: 20 additions & 0 deletions zingoconfig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- pub fn get_wallet_pathbuf

### Changed

- wallet_exists renamed -> wallet_path_exists

### Deprecated

### Removed
17 changes: 11 additions & 6 deletions zingoconfig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,24 @@ impl ZingoConfig {
.expect("Couldn't read configured server URI!")
.clone()
}
pub fn get_wallet_path(&self) -> Box<Path> {
pub fn get_wallet_pathbuf(&self) -> PathBuf {
let mut wallet_location = self.get_zingo_wallet_dir().into_path_buf();
wallet_location.push(&self.wallet_name);

wallet_location.into_boxed_path()
wallet_location
}

pub fn wallet_exists(&self) -> bool {
pub fn get_wallet_path(&self) -> Box<Path> {
self.get_wallet_pathbuf().into_boxed_path()
}
pub fn wallet_path_exists(&self) -> bool {
self.get_wallet_path().exists()
}
#[deprecated(since = "1.3.2", note = "this function was renamed for clarity")]
pub fn wallet_exists(&self) -> bool {
self.wallet_path_exists()
}

pub fn backup_existing_wallet(&self) -> Result<String, String> {
if !self.wallet_exists() {
if !self.wallet_path_exists() {
return Err(format!(
"Couldn't find existing wallet to backup. Looked in {:?}",
self.get_wallet_path().to_str()
Expand Down
19 changes: 19 additions & 0 deletions zingolib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- lightclient pub fn get_wallet_file_location
- lightclient pub fn get_wallet_dir_location

### Changed

### Deprecated

### Removed
3 changes: 3 additions & 0 deletions zingolib/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub enum ZingoLibError {
NoWalletLocation,
}
1 change: 1 addition & 0 deletions zingolib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod test_framework;
pub mod blaze;
pub mod commands;
pub mod compact_formats;
pub mod error;
pub mod grpc_connector;
pub mod lightclient;
pub mod wallet;
Expand Down
35 changes: 30 additions & 5 deletions zingolib/src/lightclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
syncdata::BlazeSyncData, trial_decryptions::TrialDecryptions, update_notes::UpdateNotes,
},
compact_formats::RawTransaction,
error::ZingoLibError,
grpc_connector::GrpcConnector,
wallet::{
data::{
Expand All @@ -32,7 +33,7 @@ use std::{
collections::HashMap,
fs::{remove_file, File},
io::{self, BufReader, Error, ErrorKind, Read, Write},
path::Path,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};
Expand Down Expand Up @@ -197,7 +198,7 @@ impl LightClient {
) -> io::Result<Self> {
#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
if !overwrite && config.wallet_exists() {
if !overwrite && config.wallet_path_exists() {
return Err(Error::new(
ErrorKind::AlreadyExists,
format!(
Expand Down Expand Up @@ -263,7 +264,7 @@ impl LightClient {
pub fn new(config: &ZingoConfig, latest_block: u64) -> io::Result<Self> {
#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
if config.wallet_exists() {
if config.wallet_path_exists() {
return Err(Error::new(
ErrorKind::AlreadyExists,
"Cannot create a new wallet from seed, because a wallet already exists",
Expand Down Expand Up @@ -307,7 +308,7 @@ impl LightClient {
}

pub fn read_wallet_from_disk(config: &ZingoConfig) -> io::Result<Self> {
let wallet_path = if config.wallet_exists() {
let wallet_path = if config.wallet_path_exists() {
config.get_wallet_path()
} else {
return Err(Error::new(
Expand Down Expand Up @@ -425,6 +426,7 @@ impl LightClient {
object! {
// Is this address ever different than the address in the containing struct
// this is the full UA.
//aha!!
"address" => om.recipient_ua.clone().unwrap_or(om.to_address.clone()),
"value" => om.value,
"memo" => LightWallet::memo_str(Some(om.memo.clone()))
Expand Down Expand Up @@ -894,7 +896,7 @@ impl LightClient {
log::debug!("target_os is not ios or android");

// Check if the file exists before attempting to delete
if self.config.wallet_exists() {
if self.config.wallet_path_exists() {
match remove_file(self.config.get_wallet_path()) {
Ok(_) => {
log::debug!("File deleted successfully!");
Expand Down Expand Up @@ -2055,6 +2057,29 @@ impl LightClient {

Ok(())
}

/// Some LightClients have a data dir in state. Mobile versions instead rely on a buffer and will return an error if this function is called.
/// ZingoConfig specifies both a wallet file and a directory containing it.
/// This function returns a PathBuf, the absolute path of the wallet file typically named zingo-wallet.dat
pub fn get_wallet_file_location(&self) -> Result<PathBuf, ZingoLibError> {
if let Some(mut loc) = self.config.wallet_dir.clone() {
loc.push(self.config.wallet_name.clone());
Ok(loc)
} else {
Err(ZingoLibError::NoWalletLocation)
}
}

/// Some LightClients have a data dir in state. Mobile versions instead rely on a buffer and will return an error if this function is called.
/// ZingoConfig specifies both a wallet file and a directory containing it.
/// This function returns a PathBuf, the absolute path of a directory which typically contains a wallet.dat file
pub fn get_wallet_dir_location(&self) -> Result<PathBuf, ZingoLibError> {
if let Some(loc) = self.config.wallet_dir.clone() {
Ok(loc)
} else {
Err(ZingoLibError::NoWalletLocation)
}
}
}
use serde_json::Value;

Expand Down

0 comments on commit fbf4aee

Please sign in to comment.