From 921dc183f1b4d6fff859e59a3ead337c298c5b10 Mon Sep 17 00:00:00 2001 From: Ethan Tuttle Date: Tue, 31 Oct 2023 21:48:05 -0400 Subject: [PATCH] fix: text-note cli command but invalid sig --- fedimint-cli/src/client.rs | 71 +++++++------- .../src/nostr_subcommands/text_note.rs | 92 +++++++++---------- modules/resolvr-common/src/api.rs | 5 +- 3 files changed, 83 insertions(+), 85 deletions(-) diff --git a/fedimint-cli/src/client.rs b/fedimint-cli/src/client.rs index 2f01d2c2229..d52b7a2bdc3 100644 --- a/fedimint-cli/src/client.rs +++ b/fedimint-cli/src/client.rs @@ -3,7 +3,7 @@ use std::ffi; use std::str::FromStr; use std::time::{Duration, UNIX_EPOCH}; -use anyhow::{anyhow, bail, Context}; +use anyhow::{anyhow, bail, Context, Ok}; use bitcoin::{secp256k1, Network}; use bitcoin_hashes::hex; use bitcoin_hashes::hex::ToHex; @@ -24,7 +24,7 @@ use fedimint_ln_common::contracts::ContractId; use fedimint_mint_client::{MintClientExt, MintClientModule, OOBNotes}; use fedimint_wallet_client::{WalletClientExt, WalletClientModule, WithdrawState}; use futures::StreamExt; -use resolvr_client::ResolvrClientExt; +use nostr_sdk::Event; use serde::{Deserialize, Serialize}; use serde_json::json; use time::format_description::well_known::iso8601; @@ -482,7 +482,7 @@ pub async fn handle_command( } ClientCmd::Nostr { nostr_command } => { match &nostr_command { - NostrCommands::UpdateMetadata(sub_command_args) => { + NostrCommands::UpdateMetadata(_sub_command_args) => { todo!() // nostr_subcommands::update_metadata::update_metadata( // args.private_key, @@ -492,15 +492,12 @@ pub async fn handle_command( // ) } NostrCommands::TextNote(sub_command_args) => { - todo!() - // nostr_subcommands::text_note::broadcast_textnote( - // args.private_key, - // args.relays, - // args.difficulty_target, - // sub_command_args, - // ) + let id = + nostr_subcommands::text_note::broadcast_textnote(client, sub_command_args) + .await?; + Ok(serde_json::Value::String(id.to_string())) } - NostrCommands::RecommendRelay(sub_command_args) => { + NostrCommands::RecommendRelay(_sub_command_args) => { todo!() // nostr_subcommands::recommend_relay::recommend_relay( // args.private_key, @@ -509,7 +506,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::PublishContactListCsv(sub_command_args) => { + NostrCommands::PublishContactListCsv(_sub_command_args) => { todo!() // nostr_subcommands::publish_contactlist_csv::publish_contact_list_from_csv_file( // args.private_key, @@ -518,7 +515,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::SendDirectMessage(sub_command_args) => { + NostrCommands::SendDirectMessage(_sub_command_args) => { todo!() // nostr_subcommands::dm::send( // args.private_key, @@ -527,7 +524,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::DeleteEvent(sub_command_args) => { + NostrCommands::DeleteEvent(_sub_command_args) => { todo!() // nostr_subcommands::delete_event::delete( // args.private_key, @@ -536,7 +533,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::React(sub_command_args) => { + NostrCommands::React(_sub_command_args) => { todo!() // nostr_subcommands::react::react_to_event( // args.private_key, @@ -545,28 +542,28 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::ListEvents(sub_command_args) => { + NostrCommands::ListEvents(_sub_command_args) => { todo!() // nostr_subcommands::list_events::list_events(args.relays, // sub_command_args) } - NostrCommands::GenerateKeypair(sub_command_args) => { + NostrCommands::GenerateKeypair(_sub_command_args) => { todo!() - // nostr_subcommands::generate_keypair::get_new_keypair(sub_command_args) + // nostr_subcommands::generate_keypair::get_new_keypair(_sub_command_args) } - NostrCommands::ConvertKey(sub_command_args) => { + NostrCommands::ConvertKey(_sub_command_args) => { todo!() - // nostr_subcommands::convert_key::convert_key(sub_command_args) + // nostr_subcommands::convert_key::convert_key(_sub_command_args) } - NostrCommands::Nprofile(sub_command_args) => { + NostrCommands::Nprofile(_sub_command_args) => { todo!() - // nostr_subcommands::nprofile::nprofile(sub_command_args) + // nostr_subcommands::nprofile::nprofile(_sub_command_args) } - NostrCommands::Vanity(sub_command_args) => { + NostrCommands::Vanity(_sub_command_args) => { todo!() - // nostr_subcommands::vanity::vanity(sub_command_args) + // nostr_subcommands::vanity::vanity(_sub_command_args) } - NostrCommands::CreatePublicChannel(sub_command_args) => { + NostrCommands::CreatePublicChannel(_sub_command_args) => { todo!() // nostr_subcommands::create_public_channel::create_public_channel( // args.private_key, @@ -575,7 +572,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::SetChannelMetadata(sub_command_args) => { + NostrCommands::SetChannelMetadata(_sub_command_args) => { todo!() // nostr_subcommands::set_channel_metadata::set_channel_metadata( // args.private_key, @@ -584,7 +581,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::SendChannelMessage(sub_command_args) => { + NostrCommands::SendChannelMessage(_sub_command_args) => { todo!() // nostr_subcommands::send_channel_message::send_channel_message( // args.private_key, @@ -593,7 +590,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::HidePublicChannelMessage(sub_command_args) => { + NostrCommands::HidePublicChannelMessage(_sub_command_args) => { todo!() // nostr_subcommands::hide_public_channel_message::hide_public_channel_message( // args.private_key, @@ -602,7 +599,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::MutePublicKey(sub_command_args) => { + NostrCommands::MutePublicKey(_sub_command_args) => { todo!() // nostr_subcommands::mute_publickey::mute_publickey( // args.private_key, @@ -611,11 +608,11 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::BroadcastEvents(sub_command_args) => { + NostrCommands::BroadcastEvents(_sub_command_args) => { todo!() // nostr_subcommands::broadcast_events::broadcast_events(args.relays, sub_command_args) } - NostrCommands::CreateZapRequest(sub_command_args) => { + NostrCommands::CreateZapRequest(_sub_command_args) => { todo!() // nostr_subcommands::zap_request::create_zap_request( // args.private_key, @@ -623,7 +620,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::CreateZapReceipt(sub_command_args) => { + NostrCommands::CreateZapReceipt(_sub_command_args) => { todo!() // nostr_subcommands::zap_receipt::send_zap_receipt( // args.private_key, @@ -632,7 +629,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::CreateBadge(sub_command_args) => { + NostrCommands::CreateBadge(_sub_command_args) => { todo!() // nostr_subcommands::create_badge::create_badge( // args.private_key, @@ -641,7 +638,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::AwardBadge(sub_command_args) => { + NostrCommands::AwardBadge(_sub_command_args) => { todo!() // nostr_subcommands::award_badge::award_badge( // args.private_key, @@ -650,7 +647,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::ProfileBadges(sub_command_args) => { + NostrCommands::ProfileBadges(_sub_command_args) => { todo!() // nostr_subcommands::profile_badges::set_profile_badges( // args.private_key, @@ -659,7 +656,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::CustomEvent(sub_command_args) => { + NostrCommands::CustomEvent(_sub_command_args) => { todo!() // nostr_subcommands::custom_event::create_custom_event( // args.private_key, @@ -668,7 +665,7 @@ pub async fn handle_command( // sub_command_args, // ) } - NostrCommands::SetUserStatus(sub_command_args) => { + NostrCommands::SetUserStatus(_sub_command_args) => { todo!() // nostr_subcommands::user_status::set_user_status( // args.private_key, diff --git a/fedimint-cli/src/nostr_subcommands/text_note.rs b/fedimint-cli/src/nostr_subcommands/text_note.rs index a8c4a383bc5..a131c375831 100644 --- a/fedimint-cli/src/nostr_subcommands/text_note.rs +++ b/fedimint-cli/src/nostr_subcommands/text_note.rs @@ -3,9 +3,12 @@ use std::str::FromStr; use std::time::Duration; use clap::Args; -use nostr_sdk::prelude::*; +use fedimint_client::Client; +use nostr_sdk::secp256k1::XOnlyPublicKey; +use nostr_sdk::{Event, EventId, Tag, Timestamp, ToBech32}; +use resolvr_client::ResolvrClientExt; -use crate::utils::{create_client, handle_keys, parse_key}; +use crate::utils::parse_key; #[derive(Args, Clone, Debug)] pub struct TextNoteSubCommand { @@ -29,55 +32,52 @@ pub struct TextNoteSubCommand { hex: bool, } -pub fn broadcast_textnote( - private_key: Option, - relays: Vec, - difficulty_target: u8, +pub async fn broadcast_textnote( + client: Client, sub_command_args: &TextNoteSubCommand, -) -> Result<()> { - todo!() - // if relays.is_empty() { - // panic!("No relays specified, at least one relay is required!") - // } +) -> anyhow::Result { + // Set up tags + let mut tags: Vec = vec![]; - // let keys = handle_keys(private_key, sub_command_args.hex, true)?; - // let client = create_client(&keys, relays, difficulty_target)?; + // Subject tag (NIP-14) + if let Some(subject) = &sub_command_args.subject { + let subject_tag = Tag::Subject(subject.clone()); + tags.push(subject_tag); + } - // // Set up tags - // let mut tags: Vec = vec![]; + // Any p-tag + for ptag in sub_command_args.ptag.iter() { + // Parse pubkey to ensure we're sending hex keys + let pubkey_hex = parse_key(ptag.clone()).unwrap(); + let pubkey = XOnlyPublicKey::from_str(&pubkey_hex)?; + tags.push(Tag::PubKey(pubkey, None)); + } + // Any e-tag + for etag in sub_command_args.etag.iter() { + let event_id = EventId::from_hex(etag)?; + tags.push(Tag::Event(event_id, None, None)); + } + // Set expiration tag + if let Some(expiration) = sub_command_args.expiration { + let timestamp = Timestamp::now().add(Duration::from_secs(expiration)); + tags.push(Tag::Expiration(timestamp)); + } - // // Subject tag (NIP-14) - // if let Some(subject) = &sub_command_args.subject { - // let subject_tag = Tag::Subject(subject.clone()); - // tags.push(subject_tag); - // } + // sign nostrmint with front - // // Any p-tag - // for ptag in sub_command_args.ptag.iter() { - // // Parse pubkey to ensure we're sending hex keys - // let pubkey_hex = parse_key(ptag.clone())?; - // let pubkey = XOnlyPublicKey::from_str(&pubkey_hex)?; - // tags.push(Tag::PubKey(pubkey, None)); - // } - // // Any e-tag - // for etag in sub_command_args.etag.iter() { - // let event_id = EventId::from_hex(etag)?; - // tags.push(Tag::Event(event_id, None, None)); - // } - // // Set expiration tag - // if let Some(expiration) = sub_command_args.expiration { - // let timestamp = - // Timestamp::now().add(Duration::from_secs(expiration)); - // tags.push(Tag::Expiration(timestamp)); - // } + let pubkey = client.get_npub().await?; + let unsigned_event = + nostr_sdk::EventBuilder::new_text_note(sub_command_args.clone().content, &tags) + .to_unsigned_event(pubkey); + client.request_sign_event(unsigned_event.clone()).await?; - // // Publish event - // let event_id = client.publish_text_note(sub_command_args.content.clone(), - // &tags)?; if !sub_command_args.hex { - // println!("Published text note with id: {}", event_id.to_bech32()?); - // } else { - // println!("Published text note with id: {}", event_id.to_hex()); - // } + // then + // get signed event back - // Ok(()) + println!( + "Published text note with id: {}", + unsigned_event.id.to_bech32()? + ); + + Ok(unsigned_event.id) } diff --git a/modules/resolvr-common/src/api.rs b/modules/resolvr-common/src/api.rs index ae0a8df315a..6d4f1096856 100644 --- a/modules/resolvr-common/src/api.rs +++ b/modules/resolvr-common/src/api.rs @@ -2,12 +2,13 @@ use fedimint_core::api::{FederationApiExt, FederationResult, IModuleFederationAp use fedimint_core::module::ApiRequestErased; use fedimint_core::task::{MaybeSend, MaybeSync}; use fedimint_core::{apply, async_trait_maybe_send}; +use nostr_sdk::Event; use crate::UnsignedEvent; #[apply(async_trait_maybe_send!)] pub trait ResolvrFederationApi { - async fn request_sign_event(&self, unsigned_event: UnsignedEvent) -> FederationResult<()>; + async fn request_sign_event(&self, unsigned_event: UnsignedEvent) -> FederationResult; async fn get_npub(&self) -> FederationResult; } @@ -16,7 +17,7 @@ impl ResolvrFederationApi for T where T: IModuleFederationApi + MaybeSend + MaybeSync + 'static, { - async fn request_sign_event(&self, unsigned_event: UnsignedEvent) -> FederationResult<()> { + async fn request_sign_event(&self, unsigned_event: UnsignedEvent) -> FederationResult { self.request_current_consensus( "sign_event".to_string(), ApiRequestErased::new(unsigned_event),