Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Switch to from env_logger to pretty_env_logger for colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Jan 15, 2024
1 parent 7553030 commit ab09091
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = ["profile"]

[workspace.dependencies]
log = "0.4.20"
env_logger = "0.10.1"
pretty_env_logger = "0.5.0"
derive_more = { version = "1.0.0-beta.6", features = ["debug", "display"] }
serde = { version = "1.0.193", features = ["derive"] }
serde_json = { version = "1.0.108", features = ["preserve_order"] }
Expand Down
2 changes: 1 addition & 1 deletion profile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum-as-inner = { workspace = true }
identified_vec = { workspace = true }
schemars = { workspace = true }
log = { workspace = true }
env_logger = { workspace = true }
pretty_env_logger = { workspace = true }
uniffi = { workspace = true, features = ["cli"] }
url = { git = "https://github.com/sajjon/rust-url", branch = "uniffi", features = [
"serde",
Expand Down
123 changes: 73 additions & 50 deletions profile/src/wallet/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::sync::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};

pub type HeadersList = IdentifiedVecVia<Header>;
impl Identifiable for Header {
Expand All @@ -18,10 +18,57 @@ pub struct Wallet {

impl Wallet {
/// Initializes logging
fn init_logging(&self) {
// env_logger::init();
// Hmm ^^ not needed? already initialized?
// env_logger::init should not be called after logger initialized: SetLoggerError(())
fn init_logging() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
pretty_env_logger::formatted_builder()
.filter_level(log::LevelFilter::Info)
.try_init()
.expect("Should be able to setup a logger.");
});
}

fn with_imported_profile(profile: Profile, secure_storage: Arc<dyn SecureStorage>) -> Self {
// Init WalletClient's storage
let wallet_client_storage = WalletClientStorage::new(secure_storage);

// Init wallet
let wallet = Self {
profile: RwLock::new(profile.clone()),
wallet_client_storage,
};

// Save new profile (also sets activeProfileID)
wallet.save_new_profile_or_panic(&profile);

wallet
}

fn new_load_profile_with_id(

Check warning on line 47 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L47

Added line #L47 was not covered by tests
profile_id: ProfileID,
wallet_client_storage: WalletClientStorage,
) -> Result<Self> {
// Form storage key
let profile_key = SecureStorageKey::ProfileSnapshot {
profile_id: profile_id.clone(),

Check warning on line 53 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L53

Added line #L53 was not covered by tests
};

// Load Profile from storage with key
let profile: Profile = wallet_client_storage.load_or(
profile_key,
CommonError::ProfileSnapshotNotFound(profile_id.clone()),

Check warning on line 59 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L57-L59

Added lines #L57 - L59 were not covered by tests
)?;

// Create wallet
let wallet = Self {
profile: RwLock::new(profile),

Check warning on line 64 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L64

Added line #L64 was not covered by tests
wallet_client_storage,
};

// Set active profile ID
wallet.save_active_profile_id(&profile_id)?;

Check warning on line 69 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L69

Added line #L69 was not covered by tests

Ok(wallet)

Check warning on line 71 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L71

Added line #L71 was not covered by tests
}
}

Expand All @@ -39,6 +86,10 @@ impl Wallet {
wallet_client_name: String,
secure_storage: Arc<dyn SecureStorage>,
) -> Result<Self> {
Wallet::init_logging();

Check warning on line 89 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L89

Added line #L89 was not covered by tests

log::info!("Instantiating Wallet by creating a new Profile from entropy (provided), for client: {}", wallet_client_model);

Check warning on line 91 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L91

Added line #L91 was not covered by tests

let entropy_32bytes = Hex32Bytes::from_vec(entropy)?;

Check warning on line 93 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L93

Added line #L93 was not covered by tests
let private_hd_factor_source =
PrivateHierarchicalDeterministicFactorSource::new_with_entropy(
Expand All @@ -50,7 +101,7 @@ impl Wallet {
private_hd_factor_source.clone(),
wallet_client_name.as_str(),

Check warning on line 102 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L101-L102

Added lines #L101 - L102 were not covered by tests
);
let wallet = Self::by_importing_profile(profile, secure_storage);
let wallet = Self::with_imported_profile(profile, secure_storage);
wallet.wallet_client_storage.save(
SecureStorageKey::DeviceFactorSourceMnemonic {
factor_source_id: private_hd_factor_source.factor_source.id.clone(),

Check warning on line 107 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L104-L107

Added lines #L104 - L107 were not covered by tests
Expand All @@ -63,25 +114,22 @@ impl Wallet {
/// Creates wallet by *importing* a Profile.
#[uniffi::constructor]
pub fn by_importing_profile(profile: Profile, secure_storage: Arc<dyn SecureStorage>) -> Self {
// Init WalletClient's storage
let wallet_client_storage = WalletClientStorage::new(secure_storage);
Wallet::init_logging();

// Init wallet
let wallet = Self {
profile: RwLock::new(profile.clone()),
wallet_client_storage,
};

// Save new profile (also sets activeProfileID)
wallet.save_new_profile_or_panic(&profile);
log::info!(
"Instantiating Wallet by importing a Profile with ID: {}",
profile.id()
);

// Init logging
wallet.init_logging();
wallet
Self::with_imported_profile(profile, secure_storage)
}

#[uniffi::constructor]
pub fn by_loading_profile(secure_storage: Arc<dyn SecureStorage>) -> Result<Self> {
Wallet::init_logging();

Check warning on line 129 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L128-L129

Added lines #L128 - L129 were not covered by tests

log::info!("Instantiating Wallet by loading the active Profile from storage");

Check warning on line 131 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L131

Added line #L131 was not covered by tests

// Init WalletClient's storage
let wallet_client_storage = WalletClientStorage::new(secure_storage);

Check warning on line 134 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L134

Added line #L134 was not covered by tests

Expand All @@ -99,39 +147,14 @@ impl Wallet {
profile_id: ProfileID,
secure_storage: Arc<dyn SecureStorage>,
) -> Result<Self> {
Self::new_load_profile_with_id(profile_id, WalletClientStorage::new(secure_storage))
}
}

impl Wallet {
fn new_load_profile_with_id(
profile_id: ProfileID,
wallet_client_storage: WalletClientStorage,
) -> Result<Self> {
// Form storage key
let profile_key = SecureStorageKey::ProfileSnapshot {
profile_id: profile_id.clone(),
};

// Load Profile from storage with key
let profile: Profile = wallet_client_storage.load_or(
profile_key,
CommonError::ProfileSnapshotNotFound(profile_id.clone()),
)?;

// Create wallet
let wallet = Self {
profile: RwLock::new(profile),
wallet_client_storage,
};
Wallet::init_logging();

Check warning on line 150 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L150

Added line #L150 was not covered by tests

// Set active profile ID
wallet.save_active_profile_id(&profile_id)?;

// Init logging
wallet.init_logging();
log::info!(

Check warning on line 152 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L152

Added line #L152 was not covered by tests
"Instantiating Wallet by loading the Profile with ID {} from storage",
profile_id
);

Ok(wallet)
Self::new_load_profile_with_id(profile_id, WalletClientStorage::new(secure_storage))

Check warning on line 157 in profile/src/wallet/wallet.rs

View check run for this annotation

Codecov / codecov/patch

profile/src/wallet/wallet.rs#L157

Added line #L157 was not covered by tests
}
}

Expand Down
1 change: 1 addition & 0 deletions profile/tests/uniffi/bindings/test_wallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ extension Wallet {
entropy: Data,
iPhoneName: String = Wallet.defaultIphoneName
) throws -> Wallet {
// Rust: `by_creating_new_profile_and_secrets_with_entropy`
try Wallet.byCreatingNewProfileAndSecretsWithEntropy(
entropy: entropy,
walletClientModel: .iphone,
Expand Down

0 comments on commit ab09091

Please sign in to comment.