From b63bfc9a34657c5767c507deb7c059e24dd22779 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 25 Jun 2024 09:06:47 +0100 Subject: refactor: replace keys with signer so that nip46 bunker signing can be added --- src/sub_commands/init.rs | 17 +++++++++-------- src/sub_commands/push.rs | 9 +++++---- src/sub_commands/send.rs | 32 ++++++++++++++++---------------- 3 files changed, 30 insertions(+), 28 deletions(-) (limited to 'src/sub_commands') diff --git a/src/sub_commands/init.rs b/src/sub_commands/init.rs index 4d1bdfb..4afe83c 100644 --- a/src/sub_commands/init.rs +++ b/src/sub_commands/init.rs @@ -59,7 +59,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { #[cfg(test)] let mut client = ::default(); - let (keys, user_ref) = login::launch( + let (signer, user_ref) = login::launch( &git_repo, &cli_args.nsec, &cli_args.password, @@ -68,8 +68,6 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { ) .await?; - client.set_keys(&keys).await; - let repo_ref = if let Ok(rep_ref) = repo_ref::fetch( &git_repo, root_commit.to_string(), @@ -180,7 +178,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { let mut maintainers_string = if !args.other_maintainers.is_empty() { [args.other_maintainers.clone()].concat().join(" ") } else if repo_ref.is_none() && repo_config_result.is_err() { - keys.public_key().to_bech32()? + signer.public_key().await?.to_bech32()? } else { let maintainers = if let Ok(config) = &repo_config_result { config.maintainers.clone() @@ -193,7 +191,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { .collect() } else { //unreachable - vec![keys.public_key().to_bech32()?] + vec![signer.public_key().await?.to_bech32()?] }; // add current user if not present if maintainers.iter().any(|m| { @@ -205,7 +203,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { }) { maintainers.join(" ") } else { - [maintainers, vec![keys.public_key().to_bech32()?]] + [maintainers, vec![signer.public_key().await?.to_bech32()?]] .concat() .join(" ") } @@ -231,7 +229,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { } // add current user incase removed if !maintainers.iter().any(|m| user_ref.public_key.eq(m)) { - maintainers.push(keys.public_key()); + maintainers.push(signer.public_key().await?); } break maintainers; } @@ -300,7 +298,10 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { relays: relays.clone(), maintainers: maintainers.clone(), } - .to_event(&keys)?; + .to_event(&signer) + .await?; + + client.set_signer(signer).await; send_events( &client, diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs index ade2ff8..92c1c18 100644 --- a/src/sub_commands/push.rs +++ b/src/sub_commands/push.rs @@ -148,7 +148,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { ahead.len() ); - let (keys, user_ref) = login::launch( + let (signer, user_ref) = login::launch( &git_repo, &cli_args.nsec, &cli_args.password, @@ -157,8 +157,6 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { ) .await?; - client.set_keys(&keys).await; - let mut patch_events: Vec = vec![]; for commit in &ahead { patch_events.push( @@ -167,7 +165,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { &root_commit, commit, Some(proposal_root_event.id), - &keys, + &signer, &repo_ref, patch_events.last().map(nostr::Event::id), None, @@ -175,11 +173,14 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { &None, &[], ) + .await .context("cannot make patch event from commit")?, ); } println!("pushing {} commits", ahead.len()); + client.set_signer(signer).await; + send_events( &client, patch_events, diff --git a/src/sub_commands/send.rs b/src/sub_commands/send.rs index 8971d8b..1d20e90 100644 --- a/src/sub_commands/send.rs +++ b/src/sub_commands/send.rs @@ -8,7 +8,7 @@ use nostr::{ nips::{nip01::Coordinate, nip10::Marker, nip19::Nip19}, EventBuilder, FromBech32, Tag, TagKind, ToBech32, UncheckedUrl, }; -use nostr_sdk::{hashes::sha1::Hash as Sha1Hash, TagStandard}; +use nostr_sdk::{hashes::sha1::Hash as Sha1Hash, NostrSigner, TagStandard}; use super::list::tag_value; #[cfg(not(test))] @@ -178,7 +178,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { } else { None }; - let (keys, user_ref) = login::launch( + let (signer, user_ref) = login::launch( &git_repo, &cli_args.nsec, &cli_args.password, @@ -187,7 +187,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { ) .await?; - client.set_keys(&keys).await; + client.set_signer(signer.clone()).await; let repo_ref = repo_ref::fetch( &git_repo, @@ -208,11 +208,12 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { cover_letter_title_description.clone(), &git_repo, &commits, - &keys, + &signer, &repo_ref, &root_proposal_id, &mention_tags, - )?; + ) + .await?; println!( "posting {} patch{} {} a covering letter...", @@ -576,11 +577,11 @@ async fn get_root_proposal_id_and_mentions_from_in_reply_to( pub static PATCH_KIND: u16 = 1617; #[allow(clippy::too_many_lines)] -pub fn generate_cover_letter_and_patch_events( +pub async fn generate_cover_letter_and_patch_events( cover_letter_title_description: Option<(String, String)>, git_repo: &Repo, commits: &[Sha1Hash], - keys: &nostr::Keys, + signer: &NostrSigner, repo_ref: &RepoRef, root_proposal_id: &Option, mentions: &[nostr::Tag], @@ -592,7 +593,7 @@ pub fn generate_cover_letter_and_patch_events( let mut events = vec![]; if let Some((title, description)) = cover_letter_title_description { - events.push(EventBuilder::new( + events.push(signer.sign_event_builder(EventBuilder::new( nostr::event::Kind::Custom(PATCH_KIND), format!( "From {} Mon Sep 17 00:00:00 2001\nSubject: [PATCH 0/{}] {title}\n\n{description}", @@ -655,8 +656,7 @@ pub fn generate_cover_letter_and_patch_events( .map(|pk| Tag::public_key(*pk)) .collect(), ].concat(), - ) - .to_event(keys) + )).await .context("failed to create cover-letter event")?); } @@ -667,7 +667,7 @@ pub fn generate_cover_letter_and_patch_events( &root_commit, commit, events.first().map(|event| event.id), - keys, + signer, repo_ref, events.last().map(nostr::Event::id), if events.is_empty() { @@ -695,6 +695,7 @@ pub fn generate_cover_letter_and_patch_events( root_proposal_id, if events.is_empty() { mentions } else { &[] }, ) + .await .context("failed to generate patch event")?, ); } @@ -864,12 +865,12 @@ pub fn patch_supports_commit_ids(event: &nostr::Event) -> bool { #[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_lines)] -pub fn generate_patch_event( +pub async fn generate_patch_event( git_repo: &Repo, root_commit: &Sha1Hash, commit: &Sha1Hash, thread_event_id: Option, - keys: &nostr::Keys, + signer: &nostr_sdk::NostrSigner, repo_ref: &RepoRef, parent_patch_event_id: Option, series_count: Option<(u64, u64)>, @@ -882,7 +883,7 @@ pub fn generate_patch_event( .context("failed to get parent commit")?; let relay_hint = repo_ref.relays.first().map(nostr::UncheckedUrl::from); - EventBuilder::new( + signer.sign_event_builder(EventBuilder::new( nostr::event::Kind::Custom(PATCH_KIND), git_repo .make_patch_from_commit(commit,&series_count) @@ -999,8 +1000,7 @@ pub fn generate_patch_event( ], ] .concat(), - ) - .to_event(keys) + )).await .context("failed to sign event") } // TODO -- cgit v1.2.3