From cc89387b3451837d94613628b1d0c711c01afc6d Mon Sep 17 00:00:00 2001 From: hitchhooker Date: Sat, 11 Jan 2025 16:46:43 +0700 Subject: [PATCH] fix: pending challenges work again --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/api.rs | 59 +++++++++++++++++++++++++++++++++-------------------- src/main.rs | 10 ++++----- 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 27b517c..f5e3e9a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12897,7 +12897,7 @@ dependencies = [ [[package]] name = "w3registrar" -version = "0.1.0" +version = "0.2.2" dependencies = [ "anyhow", "async-stream", diff --git a/Cargo.toml b/Cargo.toml index 416cb8f..f030a76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "w3registrar" -version = "0.1.0" +version = "0.2.2" edition = "2021" [dependencies] diff --git a/src/api.rs b/src/api.rs index e292dc6..d4e0e9d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1407,9 +1407,12 @@ impl RedisConnection { /// Returns pairs of [account_type, challenge_token] pub fn get_challenges(&mut self, wallet_id: &AccountId32) -> anyhow::Result>> { let wallet_id_str = serde_json::to_string(wallet_id)?; + println!("Wallet ID string: {}", wallet_id_str); - Ok(self - .get_accounts_from_status(wallet_id, VerifStatus::Pending) + let pending_accounts = self.get_accounts_from_status(wallet_id, VerifStatus::Pending); + println!("Pending accounts: {:?}", pending_accounts); + + Ok(pending_accounts .into_iter() .filter_map(|account| { let info = format!( @@ -1417,10 +1420,21 @@ impl RedisConnection { serde_json::to_string(&account).ok()?, wallet_id_str ); + println!("Checking challenge for info key: {}", info); - match self.get_challenge_token_from_account_info(&info).unwrap() { - Some(token) => Some(vec![account.account_type().to_string(), token.show()]), - None => None, + match self.get_challenge_token_from_account_info(&info) { + Ok(Some(token)) => { + println!("Found challenge token: {}", token.show()); + Some(vec![account.account_type().to_string(), token.show()]) + } + Ok(None) => { + println!("No challenge token found for account"); + None + } + Err(e) => { + println!("Error retrieving challenge token: {:?}", e); + None + } } }) .collect()) @@ -1439,7 +1453,7 @@ impl RedisConnection { if acc_state == VerifStatus::Done { match account { Account::Discord(_) => verif_state.discord = true, - Account::Display(_) => verif_state.display = true, + Account::Display(_) => verif_state.display_name = true, Account::Email(_) => verif_state.email = true, Account::Twitter(_) => verif_state.twitter = true, Account::Matrix(_) => verif_state.matrix = true, @@ -1482,7 +1496,7 @@ impl RedisConnection { wallet_id: &AccountId32, acc_type: &AccountType, ) -> anyhow::Result> { - // Helper closure to create account key + // helper closure to create account key let make_info = |account: &Account| -> anyhow::Result { Ok(format!( "{}:{}", @@ -1506,10 +1520,14 @@ impl RedisConnection { | (AccountType::Email, Account::Email(_)) | (AccountType::Matrix, Account::Matrix(_)) | (AccountType::Twitter, Account::Twitter(_)) + | (AccountType::Github, Account::Github(_)) + | (AccountType::Legal, Account::Legal(_)) + | (AccountType::Web, Account::Web(_)) + | (AccountType::PGPFingerprint, Account::PGPFingerprint(_)) ) }); - // If we found a matching account, get its token + // if we found a matching account, get its token match matching_account { Some(account) if matches!(account, Account::Display(_)) => { // NOTE:For display names, we only check they exists onchain @@ -1776,18 +1794,11 @@ impl RedisConnection { accounts: HashMap, ) -> anyhow::Result<()> { for (account, status) in accounts { - // we don't save a token for display fields, and we don't - // create tokens/challenges for them - if let Account::Discord(_) = account { - self.save_account(who, &account, None, VerifStatus::Done) - .await? - } else { - match status { - VerifStatus::Done => self.save_account(who, &account, None, status).await?, - VerifStatus::Pending => { - self.save_account(who, &account, Some(Token::generate().await), status) - .await?; - } + match status { + VerifStatus::Done => self.save_account(who, &account, None, status).await?, + VerifStatus::Pending => { + self.save_account(who, &account, Some(Token::generate().await), status) + .await?; } } } @@ -1813,7 +1824,11 @@ impl RedisConnection { info!("Saving account information to Redis"); debug!(token = ?token, "Challenge token details"); - let key = serde_json::to_string(&(&account, who))?; + let key = format!( + "{}:{}", + serde_json::to_string(&account)?, + serde_json::to_string(who)? + ); let mut cmd = redis::cmd("HSET"); cmd.arg(&key) @@ -1822,7 +1837,7 @@ impl RedisConnection { .arg("wallet_id") .arg(serde_json::to_string(who)?); - // this is here display_name + // filter display_name if let Some(token_value) = token { cmd.arg("token").arg(token_value.show()); } diff --git a/src/main.rs b/src/main.rs index 48e0d7d..50e28f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use tracing::{error, info}; #[tokio::main] async fn main() -> anyhow::Result<()> { tracing_subscriber::fmt() - .with_max_level(Level::INFO) + .with_max_level(Level::DEBUG) .with_line_number(true) .with_target(true) .with_span_events(FmtSpan::CLOSE) @@ -29,10 +29,10 @@ async fn main() -> anyhow::Result<()> { info!("Starting services..."); let matrix_handle = tokio::spawn(async { - //info!("Spawning matrix bot..."); - //if let Err(e) = matrix::start_bot().await { - // error!("Matrix bot error: {}", e); - //} + info!("Spawning matrix bot..."); + if let Err(e) = matrix::start_bot().await { + error!("Matrix bot error: {}", e); + } }); let node_handle = tokio::spawn(async {