Skip to content

Commit

Permalink
feat(login): login via nip46 QR code
Browse files Browse the repository at this point in the history
or nostrconnect url string which is a much better UX flow for nip46
  • Loading branch information
DanConwayDev committed Sep 24, 2024
1 parent 0acc976 commit 36e4fb9
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 67 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ nostr-sdk = "0.34.0"
nostr-signer = "0.34.0"
nostr-sqlite = "0.34.0"
passwords = "3.1.13"
qrcode = { version = "0.14.1", default-features = false }
scrypt = "0.11.0"
serde = { version = "1.0.181", features = ["derive"] }
serde_json = "1.0.105"
Expand Down
7 changes: 4 additions & 3 deletions src/bin/git_remote_nostr/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use anyhow::{anyhow, bail, Context, Result};
use auth_git2::GitAuthenticator;
use git2::{Progress, Repository};
use ngit::{
cli_interactor::count_lines_per_msg_vec,
git::{
nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol},
utils::check_ssh_keys,
Expand All @@ -23,9 +24,9 @@ use nostr::nips::nip19;
use nostr_sdk::{Event, ToBech32};

use crate::utils::{
count_lines_per_msg_vec, fetch_or_list_error_is_not_authentication_failure,
find_proposal_and_patches_by_branch_name, get_oids_from_fetch_batch, get_open_proposals,
get_read_protocols_to_try, join_with_and, set_protocol_preference, Direction,
fetch_or_list_error_is_not_authentication_failure, find_proposal_and_patches_by_branch_name,
get_oids_from_fetch_batch, get_open_proposals, get_read_protocols_to_try, join_with_and,
set_protocol_preference, Direction,
};

pub async fn run_fetch(
Expand Down
8 changes: 4 additions & 4 deletions src/bin/git_remote_nostr/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use git_events::{
generate_cover_letter_and_patch_events, generate_patch_event, get_commit_id_from_patch,
};
use ngit::{
cli_interactor::count_lines_per_msg_vec,
client::{self, get_event_from_cache_by_id},
git::{
self,
Expand All @@ -39,10 +40,9 @@ use crate::{
git::Repo,
list::list_from_remotes,
utils::{
count_lines_per_msg_vec, find_proposal_and_patches_by_branch_name, get_all_proposals,
get_remote_name_by_url, get_short_git_server_name, get_write_protocols_to_try,
join_with_and, push_error_is_not_authentication_failure, read_line,
set_protocol_preference, Direction,
find_proposal_and_patches_by_branch_name, get_all_proposals, get_remote_name_by_url,
get_short_git_server_name, get_write_protocols_to_try, join_with_and,
push_error_is_not_authentication_failure, read_line, set_protocol_preference, Direction,
},
};

Expand Down
14 changes: 0 additions & 14 deletions src/bin/git_remote_nostr/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,20 +384,6 @@ pub fn error_might_be_authentication_related(error: &anyhow::Error) -> bool {
false
}

fn count_lines_per_msg(width: u16, msg: &str, prefix_len: usize) -> usize {
if width == 0 {
return 1;
}
// ((msg_len+prefix) / width).ceil() implemented using Integer Arithmetic
((msg.chars().count() + prefix_len) + (width - 1) as usize) / width as usize
}

pub fn count_lines_per_msg_vec(width: u16, msgs: &[String], prefix_len: usize) -> usize {
msgs.iter()
.map(|msg| count_lines_per_msg(width, msg, prefix_len))
.sum()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
48 changes: 48 additions & 0 deletions src/lib/cli_interactor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password};
use indicatif::TermLike;
#[cfg(test)]
use mockall::*;

Expand Down Expand Up @@ -184,3 +185,50 @@ impl PromptMultiChoiceParms {
self
}
}

#[derive(Debug, Default)]
pub struct Printer {
printed_lines: Vec<String>,
}
impl Printer {
pub fn println(&mut self, line: String) {
eprintln!("{line}");
self.printed_lines.push(line);
}
pub fn println_with_custom_formatting(
&mut self,
line: String,
line_without_formatting: String,
) {
eprintln!("{line}");
self.printed_lines.push(line_without_formatting);
}
pub fn printlns(&mut self, lines: Vec<String>) {
for line in lines {
self.println(line);
}
}
pub fn clear_all(&mut self) {
let term = console::Term::stderr();
let _ = term.clear_last_lines(count_lines_per_msg_vec(
term.width(),
&self.printed_lines,
0,
));
self.printed_lines.drain(..);
}
}

pub fn count_lines_per_msg(width: u16, msg: &str, prefix_len: usize) -> usize {
if width == 0 {
return 1;
}
// ((msg_len+prefix) / width).ceil() implemented using Integer Arithmetic
((msg.chars().count() + prefix_len) + (width - 1) as usize) / width as usize
}

pub fn count_lines_per_msg_vec(width: u16, msgs: &[String], prefix_len: usize) -> usize {
msgs.iter()
.map(|msg| count_lines_per_msg(width, msg, prefix_len))
.sum()
}
16 changes: 16 additions & 0 deletions src/lib/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct Client {
fallback_relays: Vec<String>,
more_fallback_relays: Vec<String>,
blaster_relays: Vec<String>,
fallback_signer_relays: Vec<String>,
}

#[cfg_attr(test, automock)]
Expand All @@ -66,6 +67,7 @@ pub trait Connect {
fn get_fallback_relays(&self) -> &Vec<String>;
fn get_more_fallback_relays(&self) -> &Vec<String>;
fn get_blaster_relays(&self) -> &Vec<String>;
fn get_fallback_signer_relays(&self) -> &Vec<String>;
async fn send_event_to(
&self,
git_repo_path: &Path,
Expand Down Expand Up @@ -132,13 +134,21 @@ impl Connect for Client {
} else {
vec!["wss://nostr.mutinywallet.com".to_string()]
};

let fallback_signer_relays: Vec<String> = if std::env::var("NGITTEST").is_ok() {
vec!["ws://localhost:8051".to_string()]
} else {
vec!["wss://relay.nsec.app".to_string()]
};

Client {
client: nostr_sdk::ClientBuilder::new()
.opts(Options::new().relay_limits(RelayLimits::disable()))
.build(),
fallback_relays,
more_fallback_relays,
blaster_relays,
fallback_signer_relays,
}
}
fn new(opts: Params) -> Self {
Expand All @@ -153,6 +163,7 @@ impl Connect for Client {
fallback_relays: opts.fallback_relays,
more_fallback_relays: opts.more_fallback_relays,
blaster_relays: opts.blaster_relays,
fallback_signer_relays: opts.fallback_signer_relays,
}
}

Expand Down Expand Up @@ -198,6 +209,10 @@ impl Connect for Client {
&self.blaster_relays
}

fn get_fallback_signer_relays(&self) -> &Vec<String> {
&self.fallback_signer_relays
}

async fn send_event_to(
&self,
git_repo_path: &Path,
Expand Down Expand Up @@ -629,6 +644,7 @@ pub struct Params {
pub fallback_relays: Vec<String>,
pub more_fallback_relays: Vec<String>,
pub blaster_relays: Vec<String>,
pub fallback_signer_relays: Vec<String>,
}

fn get_dedup_events(relay_results: Vec<Result<Vec<nostr::Event>>>) -> Vec<Event> {
Expand Down
Loading

0 comments on commit 36e4fb9

Please sign in to comment.