mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-22 23:25:22 +01:00
refactor: Remove nostr last checked methods from database trait and implementations
This commit is contained in:
committed by
thesimplekid
parent
214b75ac31
commit
cb87fefacd
@@ -17,6 +17,7 @@ use tracing::Level;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use url::Url;
|
||||
|
||||
mod nostr_storage;
|
||||
mod sub_commands;
|
||||
|
||||
const DEFAULT_WORK_DIR: &str = ".cdk-cli";
|
||||
@@ -188,6 +189,7 @@ async fn main() -> Result<()> {
|
||||
localstore,
|
||||
&mnemonic.to_seed_normalized(""),
|
||||
sub_command_args,
|
||||
&work_dir,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
37
crates/cdk-cli/src/nostr_storage.rs
Normal file
37
crates/cdk-cli/src/nostr_storage.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use cdk::nuts::PublicKey;
|
||||
use cdk::util::hex;
|
||||
|
||||
/// Stores the last checked time for a nostr key in a file
|
||||
pub async fn store_nostr_last_checked(
|
||||
work_dir: &Path,
|
||||
verifying_key: &PublicKey,
|
||||
last_checked: u32,
|
||||
) -> Result<()> {
|
||||
let key_hex = hex::encode(verifying_key.to_bytes());
|
||||
let file_path = work_dir.join(format!("nostr_last_checked_{}", key_hex));
|
||||
|
||||
fs::write(file_path, last_checked.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Gets the last checked time for a nostr key from a file
|
||||
pub async fn get_nostr_last_checked(
|
||||
work_dir: &Path,
|
||||
verifying_key: &PublicKey,
|
||||
) -> Result<Option<u32>> {
|
||||
let key_hex = hex::encode(verifying_key.to_bytes());
|
||||
let file_path = work_dir.join(format!("nostr_last_checked_{}", key_hex));
|
||||
|
||||
match fs::read_to_string(file_path) {
|
||||
Ok(content) => {
|
||||
let timestamp = content.trim().parse::<u32>()?;
|
||||
Ok(Some(timestamp))
|
||||
}
|
||||
Err(_) => Ok(None),
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -14,6 +15,8 @@ use clap::Args;
|
||||
use nostr_sdk::nips::nip04;
|
||||
use nostr_sdk::{Filter, Keys, Kind, Timestamp};
|
||||
|
||||
use crate::nostr_storage;
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct ReceiveSubCommand {
|
||||
/// Cashu Token
|
||||
@@ -40,6 +43,7 @@ pub async fn receive(
|
||||
localstore: Arc<dyn WalletDatabase<Err = cdk_database::Error> + Send + Sync>,
|
||||
seed: &[u8],
|
||||
sub_command_args: &ReceiveSubCommand,
|
||||
work_dir: &Path,
|
||||
) -> Result<()> {
|
||||
let mut signing_keys = Vec::new();
|
||||
|
||||
@@ -89,12 +93,19 @@ pub async fn receive(
|
||||
signing_keys.push(nostr_key.clone());
|
||||
|
||||
let relays = sub_command_args.relay.clone();
|
||||
let since = localstore
|
||||
.get_nostr_last_checked(&nostr_key.public_key())
|
||||
.await?;
|
||||
let since =
|
||||
nostr_storage::get_nostr_last_checked(work_dir, &nostr_key.public_key()).await?;
|
||||
|
||||
let tokens = nostr_receive(relays, nostr_key.clone(), since).await?;
|
||||
|
||||
// Store the current time as last checked
|
||||
nostr_storage::store_nostr_last_checked(
|
||||
work_dir,
|
||||
&nostr_key.public_key(),
|
||||
unix_time() as u32,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut total_amount = Amount::ZERO;
|
||||
for token_str in &tokens {
|
||||
match receive_token(
|
||||
@@ -116,9 +127,6 @@ pub async fn receive(
|
||||
}
|
||||
}
|
||||
|
||||
localstore
|
||||
.add_nostr_last_checked(nostr_key.public_key(), unix_time() as u32)
|
||||
.await?;
|
||||
total_amount
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user