mirror of
https://github.com/aljazceru/notedeck.git
synced 2025-12-24 03:24:21 +01:00
Revert "move login logic from promise to async fns"
This reverts commit baaa7cc05d.
Closes: https://github.com/damus-io/notedeck/pull/567
This commit is contained in:
committed by
William Casarin
parent
c3bbc6b977
commit
4bfa26fd8c
@@ -2,9 +2,9 @@ use std::collections::HashMap;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
|
use ehttp::{Request, Response};
|
||||||
use enostr::{Keypair, Pubkey, SecretKey};
|
use enostr::{Keypair, Pubkey, SecretKey};
|
||||||
use poll_promise::Promise;
|
use poll_promise::Promise;
|
||||||
use reqwest::{Request, Response};
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
@@ -32,13 +32,9 @@ pub struct Nip05Result {
|
|||||||
pub relays: Option<HashMap<String, Vec<String>>>,
|
pub relays: Option<HashMap<String, Vec<String>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn parse_nip05_response(response: Response) -> Result<Nip05Result, Error> {
|
fn parse_nip05_response(response: Response) -> Result<Nip05Result, Error> {
|
||||||
match response.bytes().await {
|
serde_json::from_slice::<Nip05Result>(&response.bytes)
|
||||||
Ok(bytes) => {
|
.map_err(|e| Error::Generic(e.to_string()))
|
||||||
serde_json::from_slice::<Nip05Result>(&bytes).map_err(|e| Error::Generic(e.to_string()))
|
|
||||||
}
|
|
||||||
Err(e) => Err(Error::Generic(e.to_string())),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_pubkey_from_result(result: Nip05Result, user: String) -> Result<Pubkey, Error> {
|
fn get_pubkey_from_result(result: Nip05Result, user: String) -> Result<Pubkey, Error> {
|
||||||
@@ -50,56 +46,70 @@ fn get_pubkey_from_result(result: Nip05Result, user: String) -> Result<Pubkey, E
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_nip05_pubkey(id: &str) -> Result<Pubkey, Error> {
|
fn get_nip05_pubkey(id: &str) -> Promise<Result<Pubkey, Error>> {
|
||||||
|
let (sender, promise) = Promise::new();
|
||||||
let mut parts = id.split('@');
|
let mut parts = id.split('@');
|
||||||
|
|
||||||
let user = match parts.next() {
|
let user = match parts.next() {
|
||||||
Some(user) => user,
|
Some(user) => user,
|
||||||
None => {
|
None => {
|
||||||
return Err(Error::Generic(
|
sender.send(Err(Error::Generic(
|
||||||
"Address does not contain username.".to_string(),
|
"Address does not contain username.".to_string(),
|
||||||
));
|
)));
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let host = match parts.next() {
|
let host = match parts.next() {
|
||||||
Some(host) => host,
|
Some(host) => host,
|
||||||
None => {
|
None => {
|
||||||
return Err(Error::Generic(
|
sender.send(Err(Error::Generic(
|
||||||
"Nip05 address does not contain host.".to_string(),
|
"Nip05 address does not contain host.".to_string(),
|
||||||
));
|
)));
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if parts.next().is_some() {
|
if parts.next().is_some() {
|
||||||
return Err(Error::Generic(
|
sender.send(Err(Error::Generic(
|
||||||
"Nip05 address contains extraneous parts.".to_string(),
|
"Nip05 address contains extraneous parts.".to_string(),
|
||||||
));
|
)));
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = format!("https://{host}/.well-known/nostr.json?name={user}");
|
let url = format!("https://{host}/.well-known/nostr.json?name={user}");
|
||||||
let request = Request::new(reqwest::Method::GET, url.parse().unwrap());
|
let request = Request::get(url);
|
||||||
let cloned_user = user.to_string();
|
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let cloned_user = user.to_string();
|
||||||
match client.execute(request).await {
|
ehttp::fetch(request, move |response: Result<Response, String>| {
|
||||||
Ok(resp) => match parse_nip05_response(resp).await {
|
let result = match response {
|
||||||
Ok(result) => match get_pubkey_from_result(result, cloned_user) {
|
Ok(resp) => parse_nip05_response(resp)
|
||||||
Ok(pubkey) => Ok(pubkey),
|
.and_then(move |result| get_pubkey_from_result(result, cloned_user)),
|
||||||
Err(e) => Err(Error::Generic(e.to_string())),
|
|
||||||
},
|
|
||||||
Err(e) => Err(Error::Generic(e.to_string())),
|
Err(e) => Err(Error::Generic(e.to_string())),
|
||||||
},
|
};
|
||||||
Err(e) => Err(Error::Generic(e.to_string())),
|
sender.send(result);
|
||||||
}
|
});
|
||||||
|
|
||||||
|
promise
|
||||||
}
|
}
|
||||||
|
|
||||||
fn retrieving_nip05_pubkey(key: &str) -> bool {
|
fn retrieving_nip05_pubkey(key: &str) -> bool {
|
||||||
key.contains('@')
|
key.contains('@')
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn perform_key_retrieval(key: &str) -> Promise<Result<Keypair, AcquireKeyError>> {
|
fn nip05_promise_wrapper(id: &str) -> Promise<Result<Keypair, AcquireKeyError>> {
|
||||||
let key_string = String::from(key);
|
let (sender, promise) = Promise::new();
|
||||||
Promise::spawn_async(async move { get_key(&key_string).await })
|
let original_promise = get_nip05_pubkey(id);
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let result = original_promise.block_and_take();
|
||||||
|
let transformed_result = match result {
|
||||||
|
Ok(public_key) => Ok(Keypair::only_pubkey(public_key)),
|
||||||
|
Err(e) => Err(AcquireKeyError::Nip05Failed(e.to_string())),
|
||||||
|
};
|
||||||
|
sender.send(transformed_result);
|
||||||
|
});
|
||||||
|
|
||||||
|
promise
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempts to turn a string slice key from the user into a Nostr-Sdk Keypair object.
|
/// Attempts to turn a string slice key from the user into a Nostr-Sdk Keypair object.
|
||||||
@@ -110,7 +120,7 @@ pub fn perform_key_retrieval(key: &str) -> Promise<Result<Keypair, AcquireKeyErr
|
|||||||
/// - Private hex key: "5dab..."
|
/// - Private hex key: "5dab..."
|
||||||
/// - NIP-05 address: "example@nostr.com"
|
/// - NIP-05 address: "example@nostr.com"
|
||||||
///
|
///
|
||||||
pub async fn get_key(key: &str) -> Result<Keypair, AcquireKeyError> {
|
pub fn perform_key_retrieval(key: &str) -> Promise<Result<Keypair, AcquireKeyError>> {
|
||||||
let tmp_key: &str = if let Some(stripped) = key.strip_prefix('@') {
|
let tmp_key: &str = if let Some(stripped) = key.strip_prefix('@') {
|
||||||
stripped
|
stripped
|
||||||
} else {
|
} else {
|
||||||
@@ -118,18 +128,19 @@ pub async fn get_key(key: &str) -> Result<Keypair, AcquireKeyError> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if retrieving_nip05_pubkey(tmp_key) {
|
if retrieving_nip05_pubkey(tmp_key) {
|
||||||
match get_nip05_pubkey(tmp_key).await {
|
nip05_promise_wrapper(tmp_key)
|
||||||
Ok(pubkey) => Ok(Keypair::only_pubkey(pubkey)),
|
|
||||||
Err(e) => Err(AcquireKeyError::Nip05Failed(e.to_string())),
|
|
||||||
}
|
|
||||||
} else if let Ok(pubkey) = Pubkey::try_from_bech32_string(tmp_key, true) {
|
|
||||||
Ok(Keypair::only_pubkey(pubkey))
|
|
||||||
} else if let Ok(pubkey) = Pubkey::try_from_hex_str_with_verify(tmp_key) {
|
|
||||||
Ok(Keypair::only_pubkey(pubkey))
|
|
||||||
} else if let Ok(secret_key) = SecretKey::from_str(tmp_key) {
|
|
||||||
Ok(Keypair::from_secret(secret_key))
|
|
||||||
} else {
|
} else {
|
||||||
Err(AcquireKeyError::InvalidKey)
|
let res = if let Ok(pubkey) = Pubkey::try_from_bech32_string(tmp_key, true) {
|
||||||
|
Ok(Keypair::only_pubkey(pubkey))
|
||||||
|
} else if let Ok(pubkey) = Pubkey::try_from_hex_str_with_verify(tmp_key) {
|
||||||
|
Ok(Keypair::only_pubkey(pubkey))
|
||||||
|
} else if let Ok(secret_key) = SecretKey::from_str(tmp_key) {
|
||||||
|
Ok(Keypair::from_secret(secret_key))
|
||||||
|
} else {
|
||||||
|
Err(AcquireKeyError::InvalidKey)
|
||||||
|
};
|
||||||
|
|
||||||
|
Promise::from_ready(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,18 +149,8 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::promise_assert;
|
use crate::promise_assert;
|
||||||
|
|
||||||
#[tokio::test]
|
#[test]
|
||||||
async fn test_pubkey_async() {
|
fn test_pubkey() {
|
||||||
let pubkey_str = "npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s";
|
|
||||||
let expected_pubkey =
|
|
||||||
Pubkey::try_from_bech32_string(pubkey_str, false).expect("Should not have errored.");
|
|
||||||
let login_key_result = get_key(pubkey_str).await;
|
|
||||||
|
|
||||||
assert_eq!(Ok(Keypair::only_pubkey(expected_pubkey)), login_key_result);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
||||||
async fn test_pubkey() {
|
|
||||||
let pubkey_str = "npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s";
|
let pubkey_str = "npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s";
|
||||||
let expected_pubkey =
|
let expected_pubkey =
|
||||||
Pubkey::try_from_bech32_string(pubkey_str, false).expect("Should not have errored.");
|
Pubkey::try_from_bech32_string(pubkey_str, false).expect("Should not have errored.");
|
||||||
@@ -162,8 +163,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[test]
|
||||||
async fn test_hex_pubkey() {
|
fn test_hex_pubkey() {
|
||||||
let pubkey_str = "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245";
|
let pubkey_str = "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245";
|
||||||
let expected_pubkey = Pubkey::from_hex(pubkey_str).expect("Should not have errored.");
|
let expected_pubkey = Pubkey::from_hex(pubkey_str).expect("Should not have errored.");
|
||||||
let login_key_result = perform_key_retrieval(pubkey_str);
|
let login_key_result = perform_key_retrieval(pubkey_str);
|
||||||
@@ -175,8 +176,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[test]
|
||||||
async fn test_privkey() {
|
fn test_privkey() {
|
||||||
let privkey_str = "nsec1g8wt3hlwjpa4827xylr3r0lccufxltyekhraexes8lqmpp2hensq5aujhs";
|
let privkey_str = "nsec1g8wt3hlwjpa4827xylr3r0lccufxltyekhraexes8lqmpp2hensq5aujhs";
|
||||||
let expected_privkey = SecretKey::from_str(privkey_str).expect("Should not have errored.");
|
let expected_privkey = SecretKey::from_str(privkey_str).expect("Should not have errored.");
|
||||||
let login_key_result = perform_key_retrieval(privkey_str);
|
let login_key_result = perform_key_retrieval(privkey_str);
|
||||||
@@ -188,8 +189,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[test]
|
||||||
async fn test_hex_privkey() {
|
fn test_hex_privkey() {
|
||||||
let privkey_str = "41dcb8dfee907b53abc627c711bff8c7126fac99b5c7dc9b303fc1b08557cce0";
|
let privkey_str = "41dcb8dfee907b53abc627c711bff8c7126fac99b5c7dc9b303fc1b08557cce0";
|
||||||
let expected_privkey = SecretKey::from_str(privkey_str).expect("Should not have errored.");
|
let expected_privkey = SecretKey::from_str(privkey_str).expect("Should not have errored.");
|
||||||
let login_key_result = perform_key_retrieval(privkey_str);
|
let login_key_result = perform_key_retrieval(privkey_str);
|
||||||
@@ -201,8 +202,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[test]
|
||||||
async fn test_nip05() {
|
fn test_nip05() {
|
||||||
let nip05_str = "damus@damus.io";
|
let nip05_str = "damus@damus.io";
|
||||||
let expected_pubkey = Pubkey::try_from_bech32_string(
|
let expected_pubkey = Pubkey::try_from_bech32_string(
|
||||||
"npub18m76awca3y37hkvuneavuw6pjj4525fw90necxmadrvjg0sdy6qsngq955",
|
"npub18m76awca3y37hkvuneavuw6pjj4525fw90necxmadrvjg0sdy6qsngq955",
|
||||||
@@ -217,4 +218,18 @@ mod tests {
|
|||||||
&login_key_result
|
&login_key_result
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_nip05_pubkey() {
|
||||||
|
let nip05_str = "damus@damus.io";
|
||||||
|
let expected_pubkey = Pubkey::try_from_bech32_string(
|
||||||
|
"npub18m76awca3y37hkvuneavuw6pjj4525fw90necxmadrvjg0sdy6qsngq955",
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
.expect("Should not have errored.");
|
||||||
|
let login_key_result = get_nip05_pubkey(nip05_str);
|
||||||
|
|
||||||
|
let res = login_key_result.block_and_take().expect("Should not error");
|
||||||
|
assert_eq!(expected_pubkey, res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ impl<'a> AcquireKeyState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether to indicate to the user that there is a network operation occuring
|
|
||||||
pub fn is_awaiting_network(&self) -> bool {
|
pub fn is_awaiting_network(&self) -> bool {
|
||||||
self.promise_query.is_some()
|
self.promise_query.is_some()
|
||||||
}
|
}
|
||||||
@@ -98,8 +97,8 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[test]
|
||||||
async fn test_retrieve_key() {
|
fn test_retrieve_key() {
|
||||||
let mut manager = AcquireKeyState::new();
|
let mut manager = AcquireKeyState::new();
|
||||||
let expected_str = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681";
|
let expected_str = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681";
|
||||||
let expected_key = Keypair::only_pubkey(Pubkey::from_hex(expected_str).unwrap());
|
let expected_key = Keypair::only_pubkey(Pubkey::from_hex(expected_str).unwrap());
|
||||||
@@ -141,6 +140,6 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
panic!("Test failed to get expected key.");
|
panic!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user