diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-11 13:08:35 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-11 22:09:21 +0000 |
| commit | 694325fa25e281b1a4c9d7f275f1a8e0f1ad7abf (patch) | |
| tree | 3351c9e1fb1057bb61fb3fe74291b3462b17c054 /src/lib/login | |
| parent | 35ad07180e418931dd499f799d704672d27ff35b (diff) | |
chore: bump rust-nostr v0.36
bump all rust-nostr packages to latest issued version.
there have been some breaking changes to nip46 and this applies
these changes.
Diffstat (limited to 'src/lib/login')
| -rw-r--r-- | src/lib/login/mod.rs | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/src/lib/login/mod.rs b/src/lib/login/mod.rs index d774426..148b9d7 100644 --- a/src/lib/login/mod.rs +++ b/src/lib/login/mod.rs | |||
| @@ -7,11 +7,11 @@ use nostr::{ | |||
| 7 | nips::{nip05, nip46::NostrConnectURI}, | 7 | nips::{nip05, nip46::NostrConnectURI}, |
| 8 | PublicKey, | 8 | PublicKey, |
| 9 | }; | 9 | }; |
| 10 | use nostr_connect::client::NostrConnect; | ||
| 10 | use nostr_sdk::{ | 11 | use nostr_sdk::{ |
| 11 | Alphabet, FromBech32, JsonUtil, Keys, Kind, NostrSigner, SingleLetterTag, Timestamp, ToBech32, | 12 | Alphabet, FromBech32, JsonUtil, Keys, Kind, NostrSigner, SingleLetterTag, Timestamp, ToBech32, |
| 12 | Url, | 13 | Url, |
| 13 | }; | 14 | }; |
| 14 | use nostr_signer::Nip46Signer; | ||
| 15 | use qrcode::QrCode; | 15 | use qrcode::QrCode; |
| 16 | use tokio::sync::{oneshot, Mutex}; | 16 | use tokio::sync::{oneshot, Mutex}; |
| 17 | 17 | ||
| @@ -45,7 +45,7 @@ pub async fn launch( | |||
| 45 | #[cfg(not(test))] client: Option<&Client>, | 45 | #[cfg(not(test))] client: Option<&Client>, |
| 46 | change_user: bool, | 46 | change_user: bool, |
| 47 | silent: bool, | 47 | silent: bool, |
| 48 | ) -> Result<(NostrSigner, UserRef)> { | 48 | ) -> Result<(Arc<dyn NostrSigner>, UserRef)> { |
| 49 | if let Ok(signer) = match get_signer_without_prompts( | 49 | if let Ok(signer) = match get_signer_without_prompts( |
| 50 | git_repo, | 50 | git_repo, |
| 51 | bunker_uri, | 51 | bunker_uri, |
| @@ -86,7 +86,7 @@ pub async fn launch( | |||
| 86 | .password(PromptPasswordParms::default().with_prompt("password")) | 86 | .password(PromptPasswordParms::default().with_prompt("password")) |
| 87 | .context("failed to get password input from interactor.password")?; | 87 | .context("failed to get password input from interactor.password")?; |
| 88 | if let Ok(keys) = get_keys_with_password(git_repo, &password) { | 88 | if let Ok(keys) = get_keys_with_password(git_repo, &password) { |
| 89 | break Ok(NostrSigner::Keys(keys)); | 89 | break Ok(Arc::new(keys) as Arc<dyn NostrSigner>); |
| 90 | } | 90 | } |
| 91 | eprintln!("incorrect password"); | 91 | eprintln!("incorrect password"); |
| 92 | } | 92 | } |
| @@ -101,7 +101,7 @@ pub async fn launch( | |||
| 101 | // get user ref | 101 | // get user ref |
| 102 | let user_ref = get_user_details( | 102 | let user_ref = get_user_details( |
| 103 | &signer | 103 | &signer |
| 104 | .public_key() | 104 | .get_public_key() |
| 105 | .await | 105 | .await |
| 106 | .context("cannot get public key from signer")?, | 106 | .context("cannot get public key from signer")?, |
| 107 | client, | 107 | client, |
| @@ -139,15 +139,13 @@ async fn get_signer_without_prompts( | |||
| 139 | nsec: &Option<String>, | 139 | nsec: &Option<String>, |
| 140 | password: &Option<String>, | 140 | password: &Option<String>, |
| 141 | save_local: bool, | 141 | save_local: bool, |
| 142 | ) -> Result<NostrSigner> { | 142 | ) -> Result<Arc<dyn NostrSigner>> { |
| 143 | if let Some(nsec) = nsec { | 143 | if let Some(nsec) = nsec { |
| 144 | Ok(NostrSigner::Keys(get_keys_from_nsec( | 144 | Ok(Arc::new(get_keys_from_nsec( |
| 145 | git_repo, nsec, password, save_local, | 145 | git_repo, nsec, password, save_local, |
| 146 | )?)) | 146 | )?)) |
| 147 | } else if let Some(password) = password { | 147 | } else if let Some(password) = password { |
| 148 | Ok(NostrSigner::Keys(get_keys_with_password( | 148 | Ok(Arc::new(get_keys_with_password(git_repo, password)?)) |
| 149 | git_repo, password, | ||
| 150 | )?)) | ||
| 151 | } else if let Some(bunker_uri) = bunker_uri { | 149 | } else if let Some(bunker_uri) = bunker_uri { |
| 152 | if let Some(bunker_app_key) = bunker_app_key { | 150 | if let Some(bunker_app_key) = bunker_app_key { |
| 153 | let signer = get_nip46_signer_from_uri_and_key(bunker_uri, bunker_app_key) | 151 | let signer = get_nip46_signer_from_uri_and_key(bunker_uri, bunker_app_key) |
| @@ -156,7 +154,7 @@ async fn get_signer_without_prompts( | |||
| 156 | if save_local { | 154 | if save_local { |
| 157 | save_to_git_config( | 155 | save_to_git_config( |
| 158 | git_repo, | 156 | git_repo, |
| 159 | &signer.public_key().await?.to_bech32()?, | 157 | &signer.get_public_key().await?.to_bech32()?, |
| 160 | &None, | 158 | &None, |
| 161 | &Some((bunker_uri.to_string(),bunker_app_key.to_string())), | 159 | &Some((bunker_uri.to_string(),bunker_app_key.to_string())), |
| 162 | false, | 160 | false, |
| @@ -286,11 +284,14 @@ fn get_keys_with_password(git_repo: &Repo, password: &str) -> Result<nostr::Keys | |||
| 286 | .context("failed to decrypt stored nsec key with provided password") | 284 | .context("failed to decrypt stored nsec key with provided password") |
| 287 | } | 285 | } |
| 288 | 286 | ||
| 289 | async fn get_nip46_signer_from_uri_and_key(uri: &str, app_key: &str) -> Result<NostrSigner> { | 287 | async fn get_nip46_signer_from_uri_and_key( |
| 288 | uri: &str, | ||
| 289 | app_key: &str, | ||
| 290 | ) -> Result<Arc<dyn NostrSigner>> { | ||
| 290 | let term = console::Term::stderr(); | 291 | let term = console::Term::stderr(); |
| 291 | term.write_line("connecting to remote signer...")?; | 292 | term.write_line("connecting to remote signer...")?; |
| 292 | let uri = NostrConnectURI::parse(uri)?; | 293 | let uri = NostrConnectURI::parse(uri)?; |
| 293 | let signer = NostrSigner::nip46(Nip46Signer::new( | 294 | let signer = Arc::new(NostrConnect::new( |
| 294 | uri, | 295 | uri, |
| 295 | nostr::Keys::from_str(app_key).context("invalid app key")?, | 296 | nostr::Keys::from_str(app_key).context("invalid app key")?, |
| 296 | Duration::from_secs(10 * 60), | 297 | Duration::from_secs(10 * 60), |
| @@ -302,7 +303,7 @@ async fn get_nip46_signer_from_uri_and_key(uri: &str, app_key: &str) -> Result<N | |||
| 302 | 303 | ||
| 303 | async fn get_signer_with_git_config_nsec_or_bunker_without_prompts( | 304 | async fn get_signer_with_git_config_nsec_or_bunker_without_prompts( |
| 304 | git_repo: &Repo, | 305 | git_repo: &Repo, |
| 305 | ) -> Result<NostrSigner> { | 306 | ) -> Result<Arc<dyn NostrSigner>> { |
| 306 | if let Ok(local_nsec) = &git_repo | 307 | if let Ok(local_nsec) = &git_repo |
| 307 | .get_git_config_item("nostr.nsec", Some(false)) | 308 | .get_git_config_item("nostr.nsec", Some(false)) |
| 308 | .context("failed get local git config")? | 309 | .context("failed get local git config")? |
| @@ -311,7 +312,7 @@ async fn get_signer_with_git_config_nsec_or_bunker_without_prompts( | |||
| 311 | if local_nsec.contains("ncryptsec") { | 312 | if local_nsec.contains("ncryptsec") { |
| 312 | bail!("git global config item nostr.nsec is an ncryptsec") | 313 | bail!("git global config item nostr.nsec is an ncryptsec") |
| 313 | } | 314 | } |
| 314 | Ok(NostrSigner::Keys( | 315 | Ok(Arc::new( |
| 315 | nostr::Keys::from_str(local_nsec).context("invalid nsec parameter")?, | 316 | nostr::Keys::from_str(local_nsec).context("invalid nsec parameter")?, |
| 316 | )) | 317 | )) |
| 317 | } else if let Ok((uri, app_key)) = get_git_config_bunker_uri_and_app_key(git_repo, Some(false)) | 318 | } else if let Ok((uri, app_key)) = get_git_config_bunker_uri_and_app_key(git_repo, Some(false)) |
| @@ -325,7 +326,7 @@ async fn get_signer_with_git_config_nsec_or_bunker_without_prompts( | |||
| 325 | if global_nsec.contains("ncryptsec") { | 326 | if global_nsec.contains("ncryptsec") { |
| 326 | bail!("git global config item nostr.nsec is an ncryptsec") | 327 | bail!("git global config item nostr.nsec is an ncryptsec") |
| 327 | } | 328 | } |
| 328 | Ok(NostrSigner::Keys( | 329 | Ok(Arc::new( |
| 329 | nostr::Keys::from_str(global_nsec).context("invalid nsec parameter")?, | 330 | nostr::Keys::from_str(global_nsec).context("invalid nsec parameter")?, |
| 330 | )) | 331 | )) |
| 331 | } else if let Ok((uri, app_key)) = get_git_config_bunker_uri_and_app_key(git_repo, Some(true)) { | 332 | } else if let Ok((uri, app_key)) = get_git_config_bunker_uri_and_app_key(git_repo, Some(true)) { |
| @@ -358,7 +359,7 @@ async fn fresh_login( | |||
| 358 | #[cfg(test)] client: Option<&MockConnect>, | 359 | #[cfg(test)] client: Option<&MockConnect>, |
| 359 | #[cfg(not(test))] client: Option<&Client>, | 360 | #[cfg(not(test))] client: Option<&Client>, |
| 360 | always_save: bool, | 361 | always_save: bool, |
| 361 | ) -> Result<(NostrSigner, UserRef)> { | 362 | ) -> Result<(Arc<dyn NostrSigner>, UserRef)> { |
| 362 | let app_key = Keys::generate(); | 363 | let app_key = Keys::generate(); |
| 363 | let app_key_secret = app_key.secret_key().to_secret_hex(); | 364 | let app_key_secret = app_key.secret_key().to_secret_hex(); |
| 364 | let relays = if let Some(client) = client { | 365 | let relays = if let Some(client) = client { |
| @@ -403,13 +404,13 @@ async fn fresh_login( | |||
| 403 | if offline { | 404 | if offline { |
| 404 | return; | 405 | return; |
| 405 | } | 406 | } |
| 406 | if let Ok(nip46_signer) = Nip46Signer::new( | 407 | if let Ok(nostr_connect) = NostrConnect::new( |
| 407 | nostr_connect_url.clone(), | 408 | nostr_connect_url.clone(), |
| 408 | app_key.clone(), | 409 | app_key.clone(), |
| 409 | Duration::from_secs(10 * 60), | 410 | Duration::from_secs(10 * 60), |
| 410 | None, | 411 | None, |
| 411 | ) { | 412 | ) { |
| 412 | let signer = NostrSigner::nip46(nip46_signer); | 413 | let signer: Arc<dyn NostrSigner> = Arc::new(nostr_connect); |
| 413 | if let Ok(pub_key) = fetch_public_key(&signer).await { | 414 | if let Ok(pub_key) = fetch_public_key(&signer).await { |
| 414 | let mut printer_locked = printer_clone.lock().await; | 415 | let mut printer_locked = printer_clone.lock().await; |
| 415 | printer_locked.clear_all(); | 416 | printer_locked.clear_all(); |
| @@ -437,7 +438,7 @@ async fn fresh_login( | |||
| 437 | let (signer, public_key) = { | 438 | let (signer, public_key) = { |
| 438 | if let Ok(Some((signer, public_key))) = rx.await { | 439 | if let Ok(Some((signer, public_key))) = rx.await { |
| 439 | let bunker_url = NostrConnectURI::Bunker { | 440 | let bunker_url = NostrConnectURI::Bunker { |
| 440 | signer_public_key: public_key, | 441 | remote_signer_public_key: public_key, |
| 441 | relays: relays.clone(), | 442 | relays: relays.clone(), |
| 442 | secret: None, | 443 | secret: None, |
| 443 | }; | 444 | }; |
| @@ -455,7 +456,7 @@ async fn fresh_login( | |||
| 455 | let mut public_key: Option<PublicKey> = None; | 456 | let mut public_key: Option<PublicKey> = None; |
| 456 | // prompt for nsec | 457 | // prompt for nsec |
| 457 | let mut prompt = "login with nsec / bunker url / nostr address"; | 458 | let mut prompt = "login with nsec / bunker url / nostr address"; |
| 458 | let signer = loop { | 459 | let signer: Arc<dyn NostrSigner> = loop { |
| 459 | let input = Interactor::default() | 460 | let input = Interactor::default() |
| 460 | .input(PromptInputParms::default().with_prompt(prompt)) | 461 | .input(PromptInputParms::default().with_prompt(prompt)) |
| 461 | .context("failed to get nsec input from interactor")?; | 462 | .context("failed to get nsec input from interactor")?; |
| @@ -463,7 +464,7 @@ async fn fresh_login( | |||
| 463 | if let Err(error) = save_keys(git_repo, &keys, always_save) { | 464 | if let Err(error) = save_keys(git_repo, &keys, always_save) { |
| 464 | eprintln!("{error}"); | 465 | eprintln!("{error}"); |
| 465 | } | 466 | } |
| 466 | break NostrSigner::Keys(keys); | 467 | break Arc::new(keys); |
| 467 | } | 468 | } |
| 468 | let uri = if let Ok(uri) = NostrConnectURI::parse(&input) { | 469 | let uri = if let Ok(uri) = NostrConnectURI::parse(&input) { |
| 469 | uri | 470 | uri |
| @@ -501,7 +502,7 @@ async fn fresh_login( | |||
| 501 | let public_key = if let Some(public_key) = public_key { | 502 | let public_key = if let Some(public_key) = public_key { |
| 502 | public_key | 503 | public_key |
| 503 | } else { | 504 | } else { |
| 504 | signer.public_key().await? | 505 | signer.get_public_key().await? |
| 505 | }; | 506 | }; |
| 506 | (signer, public_key) | 507 | (signer, public_key) |
| 507 | } | 508 | } |
| @@ -561,7 +562,7 @@ pub async fn fetch_nip46_uri_from_nip05(nip05: &str) -> Result<NostrConnectURI> | |||
| 561 | bail!("nip05 provider isn't configured for remote login") | 562 | bail!("nip05 provider isn't configured for remote login") |
| 562 | } | 563 | } |
| 563 | Ok(NostrConnectURI::Bunker { | 564 | Ok(NostrConnectURI::Bunker { |
| 564 | signer_public_key: profile.public_key, | 565 | remote_signer_public_key: profile.public_key, |
| 565 | relays: profile.nip46, | 566 | relays: profile.nip46, |
| 566 | secret: None, | 567 | secret: None, |
| 567 | }) | 568 | }) |