remove nostr-sdk
Signed-off-by: kernelkind <kernelkind@gmail.com> Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
William Casarin
parent
e04c8821d5
commit
59818edd83
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::Error;
|
||||
use nostr_sdk::{prelude::Keys, PublicKey, SecretKey};
|
||||
use enostr::{Keypair, Pubkey, SecretKey};
|
||||
use poll_promise::Promise;
|
||||
use reqwest::{Request, Response};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -39,16 +39,16 @@ async fn parse_nip05_response(response: Response) -> Result<Nip05Result, Error>
|
||||
}
|
||||
}
|
||||
|
||||
fn get_pubkey_from_result(result: Nip05Result, user: String) -> Result<PublicKey, Error> {
|
||||
fn get_pubkey_from_result(result: Nip05Result, user: String) -> Result<Pubkey, Error> {
|
||||
match result.names.get(&user).to_owned() {
|
||||
Some(pubkey_str) => PublicKey::from_str(pubkey_str).map_err(|e| {
|
||||
Some(pubkey_str) => Pubkey::from_hex(pubkey_str).map_err(|e| {
|
||||
Error::Generic("Could not parse pubkey: ".to_string() + e.to_string().as_str())
|
||||
}),
|
||||
None => Err(Error::Generic("Could not find user in json.".to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_nip05_pubkey(id: &str) -> Result<PublicKey, Error> {
|
||||
async fn get_nip05_pubkey(id: &str) -> Result<Pubkey, Error> {
|
||||
let mut parts = id.split('@');
|
||||
|
||||
let user = match parts.next() {
|
||||
@@ -95,12 +95,12 @@ fn retrieving_nip05_pubkey(key: &str) -> bool {
|
||||
key.contains('@')
|
||||
}
|
||||
|
||||
pub fn perform_key_retrieval(key: &str) -> Promise<Result<Keys, LoginError>> {
|
||||
pub fn perform_key_retrieval(key: &str) -> Promise<Result<Keypair, LoginError>> {
|
||||
let key_string = String::from(key);
|
||||
Promise::spawn_async(async move { get_login_key(&key_string).await })
|
||||
}
|
||||
|
||||
/// Attempts to turn a string slice key from the user into a Nostr-Sdk Keys object.
|
||||
/// Attempts to turn a string slice key from the user into a Nostr-Sdk Keypair object.
|
||||
/// The `key` can be in any of the following formats:
|
||||
/// - Public Bech32 key (prefix "npub"): "npub1xyz..."
|
||||
/// - Private Bech32 key (prefix "nsec"): "nsec1xyz..."
|
||||
@@ -108,7 +108,7 @@ pub fn perform_key_retrieval(key: &str) -> Promise<Result<Keys, LoginError>> {
|
||||
/// - Private hex key: "5dab..."
|
||||
/// - NIP-05 address: "example@nostr.com"
|
||||
///
|
||||
pub async fn get_login_key(key: &str) -> Result<Keys, LoginError> {
|
||||
pub async fn get_login_key(key: &str) -> Result<Keypair, LoginError> {
|
||||
let tmp_key: &str = if let Some(stripped) = key.strip_prefix('@') {
|
||||
stripped
|
||||
} else {
|
||||
@@ -117,13 +117,15 @@ pub async fn get_login_key(key: &str) -> Result<Keys, LoginError> {
|
||||
|
||||
if retrieving_nip05_pubkey(tmp_key) {
|
||||
match get_nip05_pubkey(tmp_key).await {
|
||||
Ok(pubkey) => Ok(Keys::from_public_key(pubkey)),
|
||||
Ok(pubkey) => Ok(Keypair::only_pubkey(pubkey)),
|
||||
Err(e) => Err(LoginError::Nip05Failed(e.to_string())),
|
||||
}
|
||||
} else if let Ok(pubkey) = PublicKey::from_str(tmp_key) {
|
||||
Ok(Keys::from_public_key(pubkey))
|
||||
} 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(Keys::new(secret_key))
|
||||
Ok(Keypair::new(secret_key))
|
||||
} else {
|
||||
Err(LoginError::InvalidKey)
|
||||
}
|
||||
@@ -137,21 +139,23 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn test_pubkey_async() {
|
||||
let pubkey_str = "npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s";
|
||||
let expected_pubkey = PublicKey::from_str(pubkey_str).expect("Should not have errored.");
|
||||
let expected_pubkey =
|
||||
Pubkey::try_from_bech32_string(pubkey_str, false).expect("Should not have errored.");
|
||||
let login_key_result = get_login_key(pubkey_str).await;
|
||||
|
||||
assert_eq!(Ok(Keys::from_public_key(expected_pubkey)), login_key_result);
|
||||
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 expected_pubkey = PublicKey::from_str(pubkey_str).expect("Should not have errored.");
|
||||
let expected_pubkey =
|
||||
Pubkey::try_from_bech32_string(pubkey_str, false).expect("Should not have errored.");
|
||||
let login_key_result = perform_key_retrieval(pubkey_str);
|
||||
|
||||
promise_assert!(
|
||||
assert_eq,
|
||||
Ok(Keys::from_public_key(expected_pubkey)),
|
||||
Ok(Keypair::only_pubkey(expected_pubkey)),
|
||||
&login_key_result
|
||||
);
|
||||
}
|
||||
@@ -159,12 +163,12 @@ mod tests {
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_hex_pubkey() {
|
||||
let pubkey_str = "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245";
|
||||
let expected_pubkey = PublicKey::from_str(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);
|
||||
|
||||
promise_assert!(
|
||||
assert_eq,
|
||||
Ok(Keys::from_public_key(expected_pubkey)),
|
||||
Ok(Keypair::only_pubkey(expected_pubkey)),
|
||||
&login_key_result
|
||||
);
|
||||
}
|
||||
@@ -177,7 +181,7 @@ mod tests {
|
||||
|
||||
promise_assert!(
|
||||
assert_eq,
|
||||
Ok(Keys::new(expected_privkey)),
|
||||
Ok(Keypair::new(expected_privkey)),
|
||||
&login_key_result
|
||||
);
|
||||
}
|
||||
@@ -190,7 +194,7 @@ mod tests {
|
||||
|
||||
promise_assert!(
|
||||
assert_eq,
|
||||
Ok(Keys::new(expected_privkey)),
|
||||
Ok(Keypair::new(expected_privkey)),
|
||||
&login_key_result
|
||||
);
|
||||
}
|
||||
@@ -198,14 +202,16 @@ mod tests {
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_nip05() {
|
||||
let nip05_str = "damus@damus.io";
|
||||
let expected_pubkey =
|
||||
PublicKey::from_str("npub18m76awca3y37hkvuneavuw6pjj4525fw90necxmadrvjg0sdy6qsngq955")
|
||||
.expect("Should not have errored.");
|
||||
let expected_pubkey = Pubkey::try_from_bech32_string(
|
||||
"npub18m76awca3y37hkvuneavuw6pjj4525fw90necxmadrvjg0sdy6qsngq955",
|
||||
false,
|
||||
)
|
||||
.expect("Should not have errored.");
|
||||
let login_key_result = perform_key_retrieval(nip05_str);
|
||||
|
||||
promise_assert!(
|
||||
assert_eq,
|
||||
Ok(Keys::from_public_key(expected_pubkey)),
|
||||
Ok(Keypair::only_pubkey(expected_pubkey)),
|
||||
&login_key_result
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use crate::key_parsing::perform_key_retrieval;
|
||||
use crate::key_parsing::LoginError;
|
||||
use egui::{TextBuffer, TextEdit};
|
||||
use nostr_sdk::Keys;
|
||||
use enostr::Keypair;
|
||||
use poll_promise::Promise;
|
||||
|
||||
/// The UI view interface to log in to a nostr account.
|
||||
#[derive(Default)]
|
||||
pub struct LoginManager {
|
||||
login_key: String,
|
||||
promise_query: Option<(String, Promise<Result<Keys, LoginError>>)>,
|
||||
promise_query: Option<(String, Promise<Result<Keypair, LoginError>>)>,
|
||||
error: Option<LoginError>,
|
||||
key_on_error: Option<String>,
|
||||
}
|
||||
@@ -67,7 +67,7 @@ impl<'a> LoginManager {
|
||||
}
|
||||
|
||||
/// Whether to indicate to the user that a successful login occured
|
||||
pub fn check_for_successful_login(&mut self) -> Option<Keys> {
|
||||
pub fn check_for_successful_login(&mut self) -> Option<Keypair> {
|
||||
if let Some((_, promise)) = &mut self.promise_query {
|
||||
if promise.ready().is_some() {
|
||||
if let Some((_, promise)) = self.promise_query.take() {
|
||||
@@ -89,15 +89,16 @@ impl<'a> LoginManager {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use enostr::Pubkey;
|
||||
|
||||
use super::*;
|
||||
use nostr_sdk::PublicKey;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_retrieve_key() {
|
||||
let mut manager = LoginManager::new();
|
||||
let expected_str = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681";
|
||||
let expected_key = Keys::from_public_key(PublicKey::from_hex(expected_str).unwrap());
|
||||
let expected_key = Keypair::only_pubkey(Pubkey::from_hex(expected_str).unwrap());
|
||||
|
||||
let start_time = Instant::now();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user