Skip to content

Commit

Permalink
Remove needed for passsword to get logs
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Feb 13, 2024
1 parent 6b4191d commit df21fb8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion mutiny-core/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use hex_conservative::DisplayHex;
use lightning::util::logger::{Level, Logger, Record};
use log::*;

pub(crate) const LOGGING_KEY: &str = "logs";
pub const LOGGING_KEY: &str = "logs";

const MAX_LOG_ITEMS: usize = 10_000;

Expand Down
26 changes: 26 additions & 0 deletions mutiny-wasm/src/indexed_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use gloo_utils::format::JsValueSerdeExt;
use lightning::util::logger::Logger;
use lightning::{log_debug, log_error, log_trace};
use log::error;
use mutiny_core::logging::LOGGING_KEY;
use mutiny_core::storage::*;
use mutiny_core::vss::*;
use mutiny_core::*;
Expand Down Expand Up @@ -140,6 +141,31 @@ impl IndexedDbStorage {
Ok(res)
}

pub(crate) async fn get_logs() -> Result<Option<Vec<String>>, MutinyError> {
let indexed_db = Self::build_indexed_db_database().await?;
let tx = indexed_db
.transaction(&[WALLET_OBJECT_STORE_NAME], TransactionMode::ReadOnly)
.map_err(|e| {
MutinyError::read_err(
anyhow!("Failed to create indexed db transaction: {e}").into(),
)
})?;

let store = tx.store(WALLET_OBJECT_STORE_NAME).map_err(|e| {
MutinyError::read_err(anyhow!("Failed to create indexed db store: {e}").into())
})?;

let key = JsValue::from(LOGGING_KEY);
let read = store
.get(&key)
.await
.map_err(|_| MutinyError::read_err(MutinyStorageError::IndexedDBError))?;

let result: Option<Vec<String>> = read.into_serde()?;

Ok(result)
}

async fn save_to_indexed_db(
indexed_db: &Arc<RwLock<RexieContainer>>,
items: &[(String, Value)],
Expand Down
34 changes: 6 additions & 28 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ use mutiny_core::{
use mutiny_core::{logging::MutinyLogger, nostr::ProfileType};
use nostr::key::{FromSkStr, Secp256k1, SecretKey};
use nostr::{FromBech32, Keys, ToBech32};
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use std::{
collections::HashMap,
sync::atomic::{AtomicBool, Ordering},
};
use wasm_bindgen::prelude::*;

static INITIALIZED: once_cell::sync::Lazy<Mutex<bool>> =
Expand Down Expand Up @@ -1356,26 +1353,9 @@ impl MutinyWallet {

/// Exports the current state of the node manager to a json object.
#[wasm_bindgen]
pub async fn get_logs(
password: Option<String>,
) -> Result<JsValue /* Option<Vec<String>> */, MutinyJsError> {
let logger = Arc::new(MutinyLogger::default());
// TODO Password should not be required for logs
let cipher = password
.as_ref()
.filter(|p| !p.is_empty())
.map(|p| encryption_key_from_pass(p))
.transpose()?;
let storage = IndexedDbStorage::new(password, cipher, None, logger.clone()).await?;
let stop = Arc::new(AtomicBool::new(false));
let logger = Arc::new(MutinyLogger::with_writer(
stop.clone(),
storage.clone(),
None,
));
let res = JsValue::from_serde(&NodeManager::get_logs(storage, logger)?)?;
stop.swap(true, Ordering::Relaxed);
Ok(res)
pub async fn get_logs() -> Result<JsValue /* Option<Vec<String>> */, MutinyJsError> {
let logs = IndexedDbStorage::get_logs().await?;
Ok(JsValue::from_serde(&logs)?)
}

/// Get nostr wallet connect profiles
Expand Down Expand Up @@ -2269,7 +2249,7 @@ mod tests {

// sleep to make sure logs save
sleep(6_000).await;
let logs = MutinyWallet::get_logs(None).await.expect("should get logs");
let logs = MutinyWallet::get_logs().await.expect("should get logs");
let parsed_logs = js_to_option_vec_string(logs).expect("should parse logs");
assert!(parsed_logs.is_some());
assert!(!parsed_logs.clone().unwrap().is_empty());
Expand Down Expand Up @@ -2326,9 +2306,7 @@ mod tests {

// sleep to make sure logs save
sleep(6_000).await;
let logs = MutinyWallet::get_logs(password)
.await
.expect("should get logs");
let logs = MutinyWallet::get_logs().await.expect("should get logs");
let parsed_logs = js_to_option_vec_string(logs).expect("should parse logs");
assert!(parsed_logs.is_some());
assert!(!parsed_logs.clone().unwrap().is_empty());
Expand Down

0 comments on commit df21fb8

Please sign in to comment.