From 455350f25b2a5526a93550c12f0663ed8e717ab4 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Fri, 15 Nov 2024 09:49:13 +0100 Subject: [PATCH] nostr: update `EventBuilder::new` fingerprint * Add `EventBuilder::tag` and `EventBuilder::tags` * Remove `tags` arg from various `EventBuilder` constructor Signed-off-by: Yuki Kishimoto --- CHANGELOG.md | 6 + .../python/examples/client-with-opts.py | 2 +- .../nostr-sdk-ffi/python/examples/client.py | 2 +- .../python/examples/event_builder.py | 5 +- .../nostr-sdk-ffi/python/examples/tags.py | 2 +- bindings/nostr-sdk-ffi/python/examples/tor.py | 2 +- .../src/protocol/event/builder.rs | 42 +-- .../src/protocol/event/builder.rs | 39 +- .../nostr-connect/examples/handle-auth-url.rs | 2 +- crates/nostr-database/examples/helper.rs | 23 +- crates/nostr-database/examples/memory.rs | 23 +- crates/nostr-lmdb/src/lib.rs | 55 +-- crates/nostr-sdk/README.md | 8 +- crates/nostr-sdk/examples/client.rs | 6 +- crates/nostr-sdk/examples/gossip.rs | 4 +- crates/nostr-sdk/examples/lmdb.rs | 2 +- crates/nostr-sdk/examples/nostr-connect.rs | 2 +- crates/nostr-sdk/examples/nostrdb.rs | 2 +- crates/nostr-sdk/examples/shutdown-on-drop.rs | 2 +- crates/nostr-sdk/src/client/mod.rs | 2 +- crates/nostr/README.md | 4 +- crates/nostr/examples/nip13.rs | 7 +- crates/nostr/src/event/builder.rs | 341 ++++++++---------- crates/nostr/src/event/mod.rs | 22 +- crates/nostr/src/nips/nip34.rs | 6 +- crates/nostr/src/nips/nip47.rs | 9 +- crates/nostr/src/nips/nip57.rs | 10 +- crates/nostr/src/nips/nip59.rs | 6 +- 28 files changed, 275 insertions(+), 361 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1873c9d4..d4ddea724 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,10 @@ * nostr: change `EventBuilder::gift_wrap` (and linked methods) args to take `extra_tags` instead of `expiration` ([erskingardner]) * nostr: change `EventBuilder::gift_wrap` (and linked methods) args to take an `EventBuilder` rumor instead of `UnsignedEvent` ([Yuki Kishimoto]) * nostr: change `EventBuilder::private_msg_rumor` arg to take `extra_tags` instead of `reply_to` ([Yuki Kishimoto]) +* nostr: remove `tags` arg from `EventBuilder::new` ([Yuki Kishimoto]) +* nostr: remove `tags` arg from `EventBuilder::text_note` ([Yuki Kishimoto]) +* nostr: remove `tags` arg from `EventBuilder::long_form_text_note` ([Yuki Kishimoto]) +* nostr: remove `tags` arg from `EventBuilder::job_request` ([Yuki Kishimoto]) * nostr: disable all default features except `std` ([Yuki Kishimoto]) * nostr: change `Timestamp::to_human_datetime` fingerprint ([Yuki Kishimoto]) * pool: switch from async to sync message sending for `Relay` ([Yuki Kishimoto]) @@ -68,6 +72,7 @@ * nostr: add NIP73 support ([Yuki Kishimoto]) * nostr: add `NostrSigner::backend` ([Yuki Kishimoto]) * nostr: add `EventBuilder::private_msg` ([Yuki Kishimoto]) +* nostr: add `EventBuilder::tag` and `EventBuilder::tags` ([Yuki Kishimoto]) * pool: add relay reconnection and disconnection unit tests ([Yuki Kishimoto]) * sdk: allow to specify relay pool notification channel size in `Options` ([Yuki Kishimoto]) * connect: add `NostrConnect::non_secure_set_user_public_key` ([Yuki Kishimoto]) @@ -87,6 +92,7 @@ ### Deprecated +* nostr: deprecate `EventBuilder::add_tags` ([Yuki Kishimoto]) * pool: deprecate `RelayPoolNotification::RelayStatus` variant ([Yuki Kishimoto]) * sdk: deprecate `Client::with_opts` ([Yuki Kishimoto]) * sdk: deprecate `Options::connection_timeout` ([Yuki Kishimoto]) diff --git a/bindings/nostr-sdk-ffi/python/examples/client-with-opts.py b/bindings/nostr-sdk-ffi/python/examples/client-with-opts.py index 040f474aa..9827c5a08 100644 --- a/bindings/nostr-sdk-ffi/python/examples/client-with-opts.py +++ b/bindings/nostr-sdk-ffi/python/examples/client-with-opts.py @@ -20,7 +20,7 @@ async def main(): keys = Keys.generate() print(keys.public_key().to_bech32()) - event = EventBuilder.text_note("Hello from rust-nostr Python bindings!", []).sign_with_keys(keys) + event = EventBuilder.text_note("Hello from rust-nostr Python bindings!").sign_with_keys(keys) event_id = await client.send_event(event) print("Event sent:") print(f" hex: {event_id.to_hex()}") diff --git a/bindings/nostr-sdk-ffi/python/examples/client.py b/bindings/nostr-sdk-ffi/python/examples/client.py index b1b2d0423..f8fbd2d1c 100644 --- a/bindings/nostr-sdk-ffi/python/examples/client.py +++ b/bindings/nostr-sdk-ffi/python/examples/client.py @@ -33,7 +33,7 @@ async def main(): # Mine a POW event and sign it with custom keys custom_keys = Keys.generate() print("Mining a POW text note...") - event = EventBuilder.text_note("Hello from rust-nostr Python bindings!", []).pow(20).sign_with_keys(custom_keys) + event = EventBuilder.text_note("Hello from rust-nostr Python bindings!").pow(20).sign_with_keys(custom_keys) output = await client.send_event(event) print("Event sent:") print(f" hex: {output.id.to_hex()}") diff --git a/bindings/nostr-sdk-ffi/python/examples/event_builder.py b/bindings/nostr-sdk-ffi/python/examples/event_builder.py index 0f7af0e4d..ad4cfd9fc 100644 --- a/bindings/nostr-sdk-ffi/python/examples/event_builder.py +++ b/bindings/nostr-sdk-ffi/python/examples/event_builder.py @@ -6,15 +6,14 @@ async def main(): keys = Keys.generate() # Build a text note - builder = EventBuilder.text_note("Note from rust-nostr python bindings", []) + builder = EventBuilder.text_note("Note from rust-nostr python bindings") event = await builder.sign(keys) print(event.as_json()) # Build a custom event kind = Kind(1234) content = "My custom content" - tags = [] - builder = EventBuilder(kind, content, tags) + builder = EventBuilder(kind, content) # Sign with generic signer event = await builder.sign(keys) diff --git a/bindings/nostr-sdk-ffi/python/examples/tags.py b/bindings/nostr-sdk-ffi/python/examples/tags.py index 825df7596..ca6205628 100644 --- a/bindings/nostr-sdk-ffi/python/examples/tags.py +++ b/bindings/nostr-sdk-ffi/python/examples/tags.py @@ -10,7 +10,7 @@ # OR tag = Tag.public_key(other_user_pk) -event = EventBuilder.text_note("New note from Rust Nostr python bindings", [tag]).sign_with_keys(keys) +event = EventBuilder.text_note("New note from Rust Nostr python bindings").tags([tag]).sign_with_keys(keys) print(event.as_json()) print("\nTags:") diff --git a/bindings/nostr-sdk-ffi/python/examples/tor.py b/bindings/nostr-sdk-ffi/python/examples/tor.py index b2758f0bd..2bef7fc56 100644 --- a/bindings/nostr-sdk-ffi/python/examples/tor.py +++ b/bindings/nostr-sdk-ffi/python/examples/tor.py @@ -19,7 +19,7 @@ async def main(): await client.add_relay("ws://2jsnlhfnelig5acq6iacydmzdbdmg7xwunm4xl6qwbvzacw4lwrjmlyd.onion") await client.connect() - event = EventBuilder.text_note("Hello from rust-nostr Python bindings!", []) + event = EventBuilder.text_note("Hello from rust-nostr Python bindings!") res = await client.send_event_builder(event) print("Event sent:") print(f" hex: {res.id.to_hex()}") diff --git a/bindings/nostr-sdk-ffi/src/protocol/event/builder.rs b/bindings/nostr-sdk-ffi/src/protocol/event/builder.rs index 6ffa0e720..ded126bdb 100644 --- a/bindings/nostr-sdk-ffi/src/protocol/event/builder.rs +++ b/bindings/nostr-sdk-ffi/src/protocol/event/builder.rs @@ -57,18 +57,19 @@ impl EventBuilder { async fn _none(&self) {} #[uniffi::constructor] - pub fn new(kind: &Kind, content: &str, tags: &[Arc]) -> Self { - let tags = tags.iter().map(|t| t.as_ref().deref().clone()); + pub fn new(kind: &Kind, content: &str) -> Self { Self { - inner: nostr::EventBuilder::new(**kind, content, tags), + inner: nostr::EventBuilder::new(**kind, content), } } /// Add tags - pub fn add_tags(self: Arc, tags: &[Arc]) -> Self { + /// + /// This method extend the current tags (if any). + pub fn tags(self: Arc, tags: &[Arc]) -> Self { let mut builder = unwrap_or_clone_arc(self); let tags = tags.iter().map(|t| t.as_ref().deref().clone()); - builder.inner = builder.inner.add_tags(tags); + builder.inner = builder.inner.tags(tags); builder } @@ -133,10 +134,9 @@ impl EventBuilder { /// /// #[uniffi::constructor] - pub fn text_note(content: &str, tags: &[Arc]) -> Self { - let tags = tags.iter().map(|t| t.as_ref().deref().clone()); + pub fn text_note(content: &str) -> Self { Self { - inner: nostr::EventBuilder::text_note(content, tags), + inner: nostr::EventBuilder::text_note(content), } } @@ -188,10 +188,9 @@ impl EventBuilder { /// /// #[uniffi::constructor] - pub fn long_form_text_note(content: &str, tags: &[Arc]) -> Self { - let tags = tags.iter().map(|t| t.as_ref().deref().clone()); + pub fn long_form_text_note(content: &str) -> Self { Self { - inner: nostr::EventBuilder::long_form_text_note(content, tags), + inner: nostr::EventBuilder::long_form_text_note(content), } } @@ -499,12 +498,9 @@ impl EventBuilder { /// /// #[uniffi::constructor] - pub fn job_request(kind: &Kind, tags: &[Arc]) -> Result { + pub fn job_request(kind: &Kind) -> Result { Ok(Self { - inner: nostr::EventBuilder::job_request( - **kind, - tags.iter().map(|t| t.as_ref().deref().clone()), - )?, + inner: nostr::EventBuilder::job_request(**kind)?, }) } @@ -607,18 +603,10 @@ impl EventBuilder { /// /// /// - #[uniffi::constructor(default(extra_tags = []))] - pub fn private_msg_rumor( - receiver: &PublicKey, - message: &str, - extra_tags: Vec>, - ) -> Self { + #[uniffi::constructor] + pub fn private_msg_rumor(receiver: &PublicKey, message: &str) -> Self { Self { - inner: nostr::EventBuilder::private_msg_rumor( - **receiver, - message, - extra_tags.into_iter().map(|t| t.as_ref().deref().clone()), - ), + inner: nostr::EventBuilder::private_msg_rumor(**receiver, message), } } diff --git a/bindings/nostr-sdk-js/src/protocol/event/builder.rs b/bindings/nostr-sdk-js/src/protocol/event/builder.rs index 67817e7e4..f0e10a057 100644 --- a/bindings/nostr-sdk-js/src/protocol/event/builder.rs +++ b/bindings/nostr-sdk-js/src/protocol/event/builder.rs @@ -49,18 +49,18 @@ impl From for JsEventBuilder { #[wasm_bindgen(js_class = EventBuilder)] impl JsEventBuilder { #[wasm_bindgen(constructor)] - pub fn new(kind: &JsKind, content: &str, tags: Vec) -> Self { + pub fn new(kind: &JsKind, content: &str) -> Self { Self { - inner: EventBuilder::new(**kind, content, tags.into_iter().map(|t| t.into())), + inner: EventBuilder::new(**kind, content), } } /// Add tags - #[wasm_bindgen(js_name = addTags)] - pub fn add_tags(self, tags: Vec) -> Self { - self.inner - .add_tags(tags.into_iter().map(|t| t.into())) - .into() + /// + /// This method extend the current tags (if any). + #[wasm_bindgen] + pub fn tags(self, tags: Vec) -> Self { + self.inner.tags(tags.into_iter().map(|t| t.into())).into() } /// Set a custom `created_at` UNIX timestamp @@ -132,9 +132,9 @@ impl JsEventBuilder { /// /// #[wasm_bindgen(js_name = textNote)] - pub fn text_note(content: &str, tags: Vec) -> Self { + pub fn text_note(content: &str) -> Self { Self { - inner: EventBuilder::text_note(content, tags.into_iter().map(|t| t.into())), + inner: EventBuilder::text_note(content), } } @@ -186,9 +186,9 @@ impl JsEventBuilder { /// /// #[wasm_bindgen(js_name = longFormTextNote)] - pub fn long_form_text_note(content: &str, tags: Vec) -> Self { + pub fn long_form_text_note(content: &str) -> Self { Self { - inner: EventBuilder::long_form_text_note(content, tags.into_iter().map(|t| t.into())), + inner: EventBuilder::long_form_text_note(content), } } @@ -469,10 +469,9 @@ impl JsEventBuilder { /// /// #[wasm_bindgen(js_name = jobRequest)] - pub fn job_request(kind: &JsKind, tags: Vec) -> Result { + pub fn job_request(kind: &JsKind) -> Result { Ok(Self { - inner: EventBuilder::job_request(**kind, tags.into_iter().map(|t| t.into())) - .map_err(into_err)?, + inner: EventBuilder::job_request(**kind).map_err(into_err)?, }) } @@ -616,17 +615,9 @@ impl JsEventBuilder { /// /// #[wasm_bindgen(js_name = privateMsgRumor)] - pub fn private_msg_rumor( - receiver: &JsPublicKey, - message: &str, - extra_tags: Option>, - ) -> Self { + pub fn private_msg_rumor(receiver: &JsPublicKey, message: &str) -> Self { Self { - inner: EventBuilder::private_msg_rumor( - **receiver, - message, - extra_tags.unwrap_or_default().into_iter().map(|t| t.inner), - ), + inner: EventBuilder::private_msg_rumor(**receiver, message), } } diff --git a/crates/nostr-connect/examples/handle-auth-url.rs b/crates/nostr-connect/examples/handle-auth-url.rs index c2117bf14..a40a3929f 100644 --- a/crates/nostr-connect/examples/handle-auth-url.rs +++ b/crates/nostr-connect/examples/handle-auth-url.rs @@ -36,7 +36,7 @@ async fn main() -> Result<()> { let content = connect.nip44_encrypt(&receiver, "Hi").await?; println!("Content: {content}"); - let event = EventBuilder::text_note("Testing rust-nostr", []) + let event = EventBuilder::text_note("Testing rust-nostr") .sign(&connect) .await?; println!("Event: {}", event.as_json()); diff --git a/crates/nostr-database/examples/helper.rs b/crates/nostr-database/examples/helper.rs index dbecc29f9..ff9d23f3b 100644 --- a/crates/nostr-database/examples/helper.rs +++ b/crates/nostr-database/examples/helper.rs @@ -27,17 +27,15 @@ async fn main() { let helper = DatabaseHelper::unbounded(); for i in 0..100_000 { - let event = EventBuilder::text_note(format!("Event #{i}"), []) + let event = EventBuilder::text_note(format!("Event #{i}")) .sign_with_keys(&keys_a) .unwrap(); helper.index_event(&event).await; - let event = EventBuilder::text_note( - format!("Reply to event #{i}"), - [Tag::event(event.id), Tag::public_key(event.pubkey)], - ) - .sign_with_keys(&keys_b) - .unwrap(); + let event = EventBuilder::text_note(format!("Reply to event #{i}")) + .tags([Tag::event(event.id), Tag::public_key(event.pubkey)]) + .sign_with_keys(&keys_b) + .unwrap(); helper.index_event(&event).await; } @@ -50,13 +48,10 @@ async fn main() { } for i in 0..500_000 { - let event = EventBuilder::new( - Kind::Custom(123), - "Custom with d tag", - [Tag::identifier(format!("myid{i}"))], - ) - .sign_with_keys(&keys_a) - .unwrap(); + let event = EventBuilder::new(Kind::Custom(123), "Custom with d tag") + .tag(Tag::identifier(format!("myid{i}"))) + .sign_with_keys(&keys_a) + .unwrap(); helper.index_event(&event).await; } diff --git a/crates/nostr-database/examples/memory.rs b/crates/nostr-database/examples/memory.rs index 0ff69287d..bd2ba0d68 100644 --- a/crates/nostr-database/examples/memory.rs +++ b/crates/nostr-database/examples/memory.rs @@ -33,17 +33,15 @@ async fn main() { let database = MemoryDatabase::with_opts(opts); for i in 0..100_000 { - let event = EventBuilder::text_note(format!("Event #{i}"), []) + let event = EventBuilder::text_note(format!("Event #{i}")) .sign_with_keys(&keys_a) .unwrap(); database.save_event(&event).await.unwrap(); - let event = EventBuilder::text_note( - format!("Reply to event #{i}"), - [Tag::event(event.id), Tag::public_key(event.pubkey)], - ) - .sign_with_keys(&keys_b) - .unwrap(); + let event = EventBuilder::text_note(format!("Reply to event #{i}")) + .tags([Tag::event(event.id), Tag::public_key(event.pubkey)]) + .sign_with_keys(&keys_b) + .unwrap(); database.save_event(&event).await.unwrap(); } @@ -56,13 +54,10 @@ async fn main() { } for i in 0..500_000 { - let event = EventBuilder::new( - Kind::Custom(123), - "Custom with d tag", - [Tag::identifier(format!("myid{i}"))], - ) - .sign_with_keys(&keys_a) - .unwrap(); + let event = EventBuilder::new(Kind::Custom(123), "Custom with d tag") + .tag(Tag::identifier(format!("myid{i}"))) + .sign_with_keys(&keys_a) + .unwrap(); database.save_event(&event).await.unwrap(); } diff --git a/crates/nostr-lmdb/src/lib.rs b/crates/nostr-lmdb/src/lib.rs index fb62ec445..242fbdfe3 100644 --- a/crates/nostr-lmdb/src/lib.rs +++ b/crates/nostr-lmdb/src/lib.rs @@ -211,12 +211,12 @@ mod tests { // Add some text notes events.push( - EventBuilder::text_note("Text Note A", []) + EventBuilder::text_note("Text Note A") .sign_with_keys(&keys_a) .unwrap(), ); events.push( - EventBuilder::text_note("Text Note B", []) + EventBuilder::text_note("Text Note B") .sign_with_keys(&keys_b) .unwrap(), ); @@ -239,22 +239,16 @@ mod tests { // Add some param replaceable events events.push( - EventBuilder::new( - Kind::ParameterizedReplaceable(33_333), - "", - [Tag::identifier("my-id-a")], - ) - .sign_with_keys(&keys_a) - .unwrap(), + EventBuilder::new(Kind::ParameterizedReplaceable(33_333), "") + .tag(Tag::identifier("my-id-a")) + .sign_with_keys(&keys_a) + .unwrap(), ); events.push( - EventBuilder::new( - Kind::ParameterizedReplaceable(33_333), - "", - [Tag::identifier("my-id-b")], - ) - .sign_with_keys(&keys_b) - .unwrap(), + EventBuilder::new(Kind::ParameterizedReplaceable(33_333), "") + .tag(Tag::identifier("my-id-b")) + .sign_with_keys(&keys_b) + .unwrap(), ); // Store @@ -289,7 +283,7 @@ mod tests { let added_events: usize = db.add_random_events().await; - let (_keys, expected_event) = db.add_event(EventBuilder::text_note("Test", [])).await; + let (_keys, expected_event) = db.add_event(EventBuilder::text_note("Test")).await; let event = db.event_by_id(&expected_event.id).await.unwrap().unwrap(); assert_eq!(event, expected_event); @@ -374,12 +368,9 @@ mod tests { let (keys, expected_event) = db .add_event( - EventBuilder::new( - Kind::ParameterizedReplaceable(33_333), - "", - [Tag::identifier("my-id-a")], - ) - .custom_created_at(now - Duration::from_secs(120)), + EventBuilder::new(Kind::ParameterizedReplaceable(33_333), "") + .tag(Tag::identifier("my-id-a")) + .custom_created_at(now - Duration::from_secs(120)), ) .await; let coordinate = Coordinate::new(Kind::from(33_333), keys.public_key).identifier("my-id-a"); @@ -398,12 +389,9 @@ mod tests { // Replace previous event let (new_expected_event, stored) = db .add_event_with_keys( - EventBuilder::new( - Kind::ParameterizedReplaceable(33_333), - "Test replace", - [Tag::identifier("my-id-a")], - ) - .custom_created_at(now), + EventBuilder::new(Kind::ParameterizedReplaceable(33_333), "Test replace") + .tag(Tag::identifier("my-id-a")) + .custom_created_at(now), &keys, ) .await; @@ -430,12 +418,9 @@ mod tests { // Trey to add param replaceable event with older timestamp (MUSTN'T be stored) let (_, stored) = db .add_event_with_keys( - EventBuilder::new( - Kind::ParameterizedReplaceable(33_333), - "Test replace 2", - [Tag::identifier("my-id-a")], - ) - .custom_created_at(now - Duration::from_secs(2000)), + EventBuilder::new(Kind::ParameterizedReplaceable(33_333), "Test replace 2") + .tag(Tag::identifier("my-id-a")) + .custom_created_at(now - Duration::from_secs(2000)), &keys, ) .await; diff --git a/crates/nostr-sdk/README.md b/crates/nostr-sdk/README.md index 644e661ed..0499a7e44 100644 --- a/crates/nostr-sdk/README.md +++ b/crates/nostr-sdk/README.md @@ -71,13 +71,13 @@ async fn main() -> Result<()> { client.set_metadata(&metadata).await?; // Publish a text note - let builder = EventBuilder::text_note("My first text note from rust-nostr!", []); + let builder = EventBuilder::text_note("My first text note from rust-nostr!"); client.send_event_builder(builder).await?; // Create a POW text note - let event: Event = EventBuilder::text_note("POW text note from nostr-sdk", []).pow(20).sign(&keys).await?; - client.send_event(event).await?; // Send to all relays - // client.send_event_to(["wss://relay.damus.io"], event).await?; // Send to specific relay + let builder = EventBuilder::text_note("POW text note from nostr-sdk").pow(20); + client.send_event_builder(builder).await?; // Send to all relays + // client.send_event_builder_to(["wss://relay.damus.io"], builder).await?; // Send to specific relay // --------- Zap! ------------- diff --git a/crates/nostr-sdk/examples/client.rs b/crates/nostr-sdk/examples/client.rs index b1866bb7c..4af58b7fd 100644 --- a/crates/nostr-sdk/examples/client.rs +++ b/crates/nostr-sdk/examples/client.rs @@ -18,18 +18,18 @@ async fn main() -> Result<()> { client.connect().await; // Publish a text note - let builder = EventBuilder::text_note("Hello world", []); + let builder = EventBuilder::text_note("Hello world"); let output = client.send_event_builder(builder).await?; println!("Event ID: {}", output.id().to_bech32()?); println!("Sent to: {:?}", output.success); println!("Not sent to: {:?}", output.failed); // Create a text note POW event to relays - let builder = EventBuilder::text_note("POW text note from rust-nostr", []).pow(20); + let builder = EventBuilder::text_note("POW text note from rust-nostr").pow(20); client.send_event_builder(builder).await?; // Send a text note POW event to specific relays - let builder = EventBuilder::text_note("POW text note from rust-nostr 16", []).pow(16); + let builder = EventBuilder::text_note("POW text note from rust-nostr 16").pow(16); client .send_event_builder_to(["wss://relay.damus.io", "wss://relay.rip"], builder) .await?; diff --git a/crates/nostr-sdk/examples/gossip.rs b/crates/nostr-sdk/examples/gossip.rs index ea6bf0a62..083c9b63f 100644 --- a/crates/nostr-sdk/examples/gossip.rs +++ b/crates/nostr-sdk/examples/gossip.rs @@ -29,8 +29,8 @@ async fn main() -> Result<()> { let builder = EventBuilder::text_note( "Hello world nostr:npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet", - [Tag::public_key(pubkey)], - ); + ) + .tag(Tag::public_key(pubkey)); let output = client.send_event_builder(builder).await?; println!("Event ID: {}", output.to_bech32()?); diff --git a/crates/nostr-sdk/examples/lmdb.rs b/crates/nostr-sdk/examples/lmdb.rs index d339e62ea..88d459676 100644 --- a/crates/nostr-sdk/examples/lmdb.rs +++ b/crates/nostr-sdk/examples/lmdb.rs @@ -23,7 +23,7 @@ async fn main() -> Result<()> { client.connect().await; // Publish a text note - let builder = EventBuilder::text_note("Hello world", []); + let builder = EventBuilder::text_note("Hello world"); client.send_event_builder(builder).await?; // Negentropy reconcile diff --git a/crates/nostr-sdk/examples/nostr-connect.rs b/crates/nostr-sdk/examples/nostr-connect.rs index 0794f90f3..c0468e6f1 100644 --- a/crates/nostr-sdk/examples/nostr-connect.rs +++ b/crates/nostr-sdk/examples/nostr-connect.rs @@ -36,7 +36,7 @@ async fn main() -> Result<()> { client.connect().await; // Publish events - let builder = EventBuilder::text_note("Testing rust-nostr NIP46 signer [bunker]", []); + let builder = EventBuilder::text_note("Testing rust-nostr NIP46 signer [bunker]"); let output = client.send_event_builder(builder).await?; println!("Published text note: {}\n", output.id()); diff --git a/crates/nostr-sdk/examples/nostrdb.rs b/crates/nostr-sdk/examples/nostrdb.rs index 3025255bc..06be6f6d8 100644 --- a/crates/nostr-sdk/examples/nostrdb.rs +++ b/crates/nostr-sdk/examples/nostrdb.rs @@ -23,7 +23,7 @@ async fn main() -> Result<()> { client.connect().await; // Publish a text note - let builder = EventBuilder::text_note("Hello world", []); + let builder = EventBuilder::text_note("Hello world"); client.send_event_builder(builder).await?; // Negentropy reconcile diff --git a/crates/nostr-sdk/examples/shutdown-on-drop.rs b/crates/nostr-sdk/examples/shutdown-on-drop.rs index ac210838d..d0999b382 100644 --- a/crates/nostr-sdk/examples/shutdown-on-drop.rs +++ b/crates/nostr-sdk/examples/shutdown-on-drop.rs @@ -26,7 +26,7 @@ async fn main() -> Result<()> { thread::sleep(Duration::from_secs(5)).await; - let builder = EventBuilder::text_note("Hello world", []); + let builder = EventBuilder::text_note("Hello world"); client.send_event_builder(builder).await?; thread::sleep(Duration::from_secs(5)).await; diff --git a/crates/nostr-sdk/src/client/mod.rs b/crates/nostr-sdk/src/client/mod.rs index 51f8f4442..b72ad6f46 100644 --- a/crates/nostr-sdk/src/client/mod.rs +++ b/crates/nostr-sdk/src/client/mod.rs @@ -1234,7 +1234,7 @@ impl Client { S: Into, I: IntoIterator, { - let builder = EventBuilder::text_note(content, tags); + let builder = EventBuilder::text_note(content).tags(tags); self.send_event_builder(builder).await } diff --git a/crates/nostr/README.md b/crates/nostr/README.md index 7f70c258b..f6d3bf413 100644 --- a/crates/nostr/README.md +++ b/crates/nostr/README.md @@ -44,10 +44,10 @@ fn main() -> Result<()> { let event: Event = EventBuilder::metadata(&metadata).sign_with_keys(&keys)?; // New text note - let event: Event = EventBuilder::text_note("Hello from rust-nostr", []).sign_with_keys(&keys)?; + let event: Event = EventBuilder::text_note("Hello from rust-nostr").sign_with_keys(&keys)?; // New POW text note - let event: Event = EventBuilder::text_note("My first POW text note from rust-nostr", []).pow(20).sign_with_keys(&keys)?; + let event: Event = EventBuilder::text_note("POW text note from rust-nostr").pow(20).sign_with_keys(&keys)?; // Convert client nessage to JSON let json = ClientMessage::event(event).as_json(); diff --git a/crates/nostr/examples/nip13.rs b/crates/nostr/examples/nip13.rs index 29532dc83..607da3aea 100644 --- a/crates/nostr/examples/nip13.rs +++ b/crates/nostr/examples/nip13.rs @@ -10,11 +10,10 @@ fn main() -> Result<()> { let difficulty = 20; // leading zero bits let msg_content = "This is a Nostr message with embedded proof-of-work"; - let builder = EventBuilder::text_note(msg_content, []); - // or - // let builder = EventBuilder::new(Kind::TextNote, msg_content, &[]); + let event: Event = EventBuilder::text_note(msg_content) + .pow(difficulty) + .sign_with_keys(&keys)?; - let event: Event = builder.pow(difficulty).sign_with_keys(&keys)?; println!("{}", event.as_json()); Ok(()) diff --git a/crates/nostr/src/event/builder.rs b/crates/nostr/src/event/builder.rs index f21b8d632..099b06df4 100644 --- a/crates/nostr/src/event/builder.rs +++ b/crates/nostr/src/event/builder.rs @@ -149,16 +149,15 @@ pub struct EventBuilder { } impl EventBuilder { - /// New [`EventBuilder`] + /// New event builder #[inline] - pub fn new(kind: Kind, content: S, tags: I) -> Self + pub fn new(kind: Kind, content: S) -> Self where S: Into, - I: IntoIterator, { Self { kind, - tags: tags.into_iter().collect(), + tags: Vec::new(), content: content.into(), custom_created_at: None, pow: None, @@ -166,7 +165,7 @@ impl EventBuilder { } /// Add tags - #[inline] + #[deprecated(since = "0.37.0", note = "Use `tags` instead")] pub fn add_tags(mut self, tags: I) -> Self where I: IntoIterator, @@ -175,6 +174,25 @@ impl EventBuilder { self } + /// Add tag + #[inline] + pub fn tag(mut self, tag: Tag) -> Self { + self.tags.push(tag); + self + } + + /// Add tags + /// + /// This method extend the current tags (if any). + #[inline] + pub fn tags(mut self, tags: I) -> Self + where + I: IntoIterator, + { + self.tags.extend(tags); + self + } + /// Set a custom `created_at` UNIX timestamp #[inline] pub fn custom_created_at(mut self, created_at: Timestamp) -> Self { @@ -315,7 +333,7 @@ impl EventBuilder { /// ``` #[inline] pub fn metadata(metadata: &Metadata) -> Self { - Self::new(Kind::Metadata, metadata.as_json(), []) + Self::new(Kind::Metadata, metadata.as_json()) } /// Relay list metadata @@ -328,7 +346,7 @@ impl EventBuilder { let tags = iter .into_iter() .map(|(url, metadata)| Tag::relay_metadata(url, metadata)); - Self::new(Kind::RelayList, "", tags) + Self::new(Kind::RelayList, "").tags(tags) } /// Text note @@ -339,15 +357,14 @@ impl EventBuilder { /// ```rust,no_run /// use nostr::EventBuilder; /// - /// let builder = EventBuilder::text_note("My first text note from rust-nostr!", []); + /// let builder = EventBuilder::text_note("My first text note from rust-nostr!"); /// ``` #[inline] - pub fn text_note(content: S, tags: I) -> Self + pub fn text_note(content: S) -> Self where S: Into, - I: IntoIterator, { - Self::new(Kind::TextNote, content, tags) + Self::new(Kind::TextNote, content) } /// Text note reply @@ -431,7 +448,7 @@ impl EventBuilder { ); // Compose event - Self::new(Kind::TextNote, content, tags) + Self::new(Kind::TextNote, content).tags(tags) } /// Comment @@ -568,7 +585,7 @@ impl EventBuilder { ); // Compose event - Self::new(Kind::Comment, content, tags) + Self::new(Kind::Comment, content).tags(tags) } /// Long-form text note (generally referred to as "articles" or "blog posts"). @@ -590,15 +607,14 @@ impl EventBuilder { /// Tag::hashtag("placeholder".to_string()), /// Tag::event(event_id), /// ]; - /// let builder = EventBuilder::long_form_text_note("My first text note from rust-nostr!", []); + /// let builder = EventBuilder::long_form_text_note("My first text note from rust-nostr!"); /// ``` #[inline] - pub fn long_form_text_note(content: S, tags: I) -> Self + pub fn long_form_text_note(content: S) -> Self where S: Into, - I: IntoIterator, { - Self::new(Kind::LongFormTextNote, content, tags) + Self::new(Kind::LongFormTextNote, content) } /// Contact/Follow list @@ -616,7 +632,7 @@ impl EventBuilder { uppercase: false, }) }); - Self::new(Kind::ContactList, "", tags) + Self::new(Kind::ContactList, "").tags(tags) } /// OpenTimestamps Attestations for Events @@ -628,17 +644,17 @@ impl EventBuilder { relay_url: Option, ) -> Result { let ots: String = nostr_ots::timestamp_event(&event_id.to_hex())?; - Ok(Self::new( - Kind::OpenTimestamps, - ots, - [Tag::from_standardized_without_cell(TagStandard::Event { - event_id, - relay_url, - marker: None, - public_key: None, - uppercase: false, - })], - )) + Ok( + Self::new(Kind::OpenTimestamps, ots).tags([Tag::from_standardized_without_cell( + TagStandard::Event { + event_id, + relay_url, + marker: None, + public_key: None, + uppercase: false, + }, + )]), + ) } /// Repost @@ -646,41 +662,33 @@ impl EventBuilder { /// pub fn repost(event: &Event, relay_url: Option) -> Self { if event.kind == Kind::TextNote { - Self::new( - Kind::Repost, - event.as_json(), - [ - Tag::from_standardized_without_cell(TagStandard::Event { - event_id: event.id, - relay_url, - marker: None, - // NOTE: not add public key since it's already included as `p` tag - public_key: None, - uppercase: false, - }), - Tag::public_key(event.pubkey), - ], - ) + Self::new(Kind::Repost, event.as_json()).tags([ + Tag::from_standardized_without_cell(TagStandard::Event { + event_id: event.id, + relay_url, + marker: None, + // NOTE: not add public key since it's already included as `p` tag + public_key: None, + uppercase: false, + }), + Tag::public_key(event.pubkey), + ]) } else { - Self::new( - Kind::GenericRepost, - event.as_json(), - [ - Tag::from_standardized_without_cell(TagStandard::Event { - event_id: event.id, - relay_url, - marker: None, - // NOTE: not add public key since it's already included as `p` tag - public_key: None, - uppercase: false, - }), - Tag::public_key(event.pubkey), - Tag::from_standardized_without_cell(TagStandard::Kind { - kind: event.kind, - uppercase: false, - }), - ], - ) + Self::new(Kind::GenericRepost, event.as_json()).tags([ + Tag::from_standardized_without_cell(TagStandard::Event { + event_id: event.id, + relay_url, + marker: None, + // NOTE: not add public key since it's already included as `p` tag + public_key: None, + uppercase: false, + }), + Tag::public_key(event.pubkey), + Tag::from_standardized_without_cell(TagStandard::Kind { + kind: event.kind, + uppercase: false, + }), + ]) } } @@ -709,7 +717,7 @@ impl EventBuilder { let middle: EventIdOrCoordinate = t.into(); middle.into() }); - Self::new(Kind::EventDeletion, reason.into(), tags) + Self::new(Kind::EventDeletion, reason.into()).tags(tags) } /// Add reaction (like/upvote, dislike/downvote or emoji) to an event @@ -747,7 +755,7 @@ impl EventBuilder { })); } - Self::new(Kind::Reaction, reaction, tags) + Self::new(Kind::Reaction, reaction).tags(tags) } /// Create new channel @@ -755,7 +763,7 @@ impl EventBuilder { /// #[inline] pub fn channel(metadata: &Metadata) -> Self { - Self::new(Kind::ChannelCreation, metadata.as_json(), []) + Self::new(Kind::ChannelCreation, metadata.as_json()) } /// Channel metadata @@ -767,17 +775,15 @@ impl EventBuilder { relay_url: Option, metadata: &Metadata, ) -> Self { - Self::new( - Kind::ChannelMetadata, - metadata.as_json(), - [Tag::from_standardized_without_cell(TagStandard::Event { + Self::new(Kind::ChannelMetadata, metadata.as_json()).tags([ + Tag::from_standardized_without_cell(TagStandard::Event { event_id: channel_id, relay_url: relay_url.map(|u| u.into()), marker: None, public_key: None, uppercase: false, - })], - ) + }), + ]) } /// Channel message @@ -788,26 +794,23 @@ impl EventBuilder { where S: Into, { - Self::new( - Kind::ChannelMessage, - content, - [Tag::from_standardized_without_cell(TagStandard::Event { + Self::new(Kind::ChannelMessage, content).tags([Tag::from_standardized_without_cell( + TagStandard::Event { event_id: channel_id, relay_url: Some(relay_url.into()), marker: Some(Marker::Root), public_key: None, uppercase: false, - })], - ) + }, + )]) } /// Hide message /// + /// The `message_id` must be the [`EventId`] of the kind `42`. + /// /// - pub fn hide_channel_msg( - message_id: EventId, // event id of kind 42 - reason: Option, - ) -> Self + pub fn hide_channel_msg(message_id: EventId, reason: Option) -> Self where S: Into, { @@ -815,11 +818,7 @@ impl EventBuilder { "reason": reason.map(|s| s.into()).unwrap_or_default(), }); - Self::new( - Kind::ChannelHideMessage, - content.to_string(), - [Tag::event(message_id)], - ) + Self::new(Kind::ChannelHideMessage, content.to_string()).tag(Tag::event(message_id)) } /// Mute channel user @@ -833,11 +832,7 @@ impl EventBuilder { "reason": reason.map(|s| s.into()).unwrap_or_default(), }); - Self::new( - Kind::ChannelMuteUser, - content.to_string(), - [Tag::public_key(public_key)], - ) + Self::new(Kind::ChannelMuteUser, content.to_string()).tag(Tag::public_key(public_key)) } /// Authentication of clients to relays @@ -848,14 +843,10 @@ impl EventBuilder { where S: Into, { - Self::new( - Kind::Authentication, - "", - [ - Tag::from_standardized_without_cell(TagStandard::Challenge(challenge.into())), - Tag::from_standardized_without_cell(TagStandard::Relay(relay.into())), - ], - ) + Self::new(Kind::Authentication, "").tags([ + Tag::from_standardized_without_cell(TagStandard::Challenge(challenge.into())), + Tag::from_standardized_without_cell(TagStandard::Relay(relay.into())), + ]) } /// Nostr Connect / Nostr Remote Signing @@ -871,8 +862,8 @@ impl EventBuilder { Ok(Self::new( Kind::NostrConnect, nip04::encrypt(sender_keys.secret_key(), &receiver_pubkey, msg.as_json())?, - [Tag::public_key(receiver_pubkey)], - )) + ) + .tag(Tag::public_key(receiver_pubkey))) } /// Live Event @@ -881,7 +872,7 @@ impl EventBuilder { #[inline] pub fn live_event(live_event: LiveEvent) -> Self { let tags: Vec = live_event.into(); - Self::new(Kind::LiveEvent, "", tags) + Self::new(Kind::LiveEvent, "").tags(tags) } /// Live Event Message @@ -896,18 +887,14 @@ impl EventBuilder { where S: Into, { - Self::new( - Kind::LiveEventMessage, - content, - [Tag::from_standardized_without_cell( - TagStandard::Coordinate { - coordinate: Coordinate::new(Kind::LiveEvent, live_event_host) - .identifier(live_event_id), - relay_url: relay_url.map(|u| u.into()), - uppercase: false, - }, - )], - ) + Self::new(Kind::LiveEventMessage, content).tag(Tag::from_standardized_without_cell( + TagStandard::Coordinate { + coordinate: Coordinate::new(Kind::LiveEvent, live_event_host) + .identifier(live_event_id), + relay_url: relay_url.map(|u| u.into()), + uppercase: false, + }, + )) } /// Reporting @@ -919,7 +906,7 @@ impl EventBuilder { I: IntoIterator, S: Into, { - Self::new(Kind::Reporting, content, tags) + Self::new(Kind::Reporting, content).tags(tags) } /// Create **public** zap request event @@ -956,7 +943,7 @@ impl EventBuilder { pub fn public_zap_request(data: ZapRequestData) -> Self { let message: String = data.message.clone(); let tags: Vec = data.into(); - Self::new(Kind::ZapRequest, message, tags) + Self::new(Kind::ZapRequest, message).tags(tags) } /// Zap Receipt @@ -1038,7 +1025,7 @@ impl EventBuilder { }, )); - Self::new(Kind::ZapReceipt, "", tags) + Self::new(Kind::ZapReceipt, "").tags(tags) } /// Badge definition @@ -1112,7 +1099,7 @@ impl EventBuilder { tags.push(thumb_tag); } - Self::new(Kind::BadgeDefinition, "", tags) + Self::new(Kind::BadgeDefinition, "").tags(tags) } /// Badge award @@ -1148,7 +1135,7 @@ impl EventBuilder { tags.extend(awarded_public_keys.into_iter().map(Tag::public_key)); // Build event - Ok(Self::new(Kind::BadgeAward, "", tags)) + Ok(Self::new(Kind::BadgeAward, "").tags(tags)) } /// Profile badges @@ -1229,24 +1216,21 @@ impl EventBuilder { } } - Ok(EventBuilder::new(Kind::ProfileBadges, "", tags)) + Ok(EventBuilder::new(Kind::ProfileBadges, "").tags(tags)) } /// Data Vending Machine (DVM) - Job Request /// /// - pub fn job_request(kind: Kind, tags: I) -> Result - where - I: IntoIterator, - { - if kind.is_job_request() { - Ok(Self::new(kind, "", tags)) - } else { - Err(Error::WrongKind { + pub fn job_request(kind: Kind) -> Result { + if !kind.is_job_request() { + return Err(Error::WrongKind { received: kind, expected: WrongKindError::Range(NIP90_JOB_REQUEST_RANGE), - }) + }); } + + Ok(Self::new(kind, "")) } /// Data Vending Machine (DVM) - Job Result @@ -1295,7 +1279,7 @@ impl EventBuilder { Tag::from_standardized_without_cell(TagStandard::Amount { millisats, bolt11 }), ]); - Ok(Self::new(kind, payload, tags)) + Ok(Self::new(kind, payload).tags(tags)) } /// Data Vending Machine (DVM) - Job Feedback @@ -1320,7 +1304,7 @@ impl EventBuilder { })); } - Self::new(Kind::JobFeedback, data.payload.unwrap_or_default(), tags) + Self::new(Kind::JobFeedback, data.payload.unwrap_or_default()).tags(tags) } /// File metadata @@ -1332,7 +1316,7 @@ impl EventBuilder { S: Into, { let tags: Vec = metadata.into(); - Self::new(Kind::FileMetadata, description.into(), tags) + Self::new(Kind::FileMetadata, description.into()).tags(tags) } /// HTTP Auth @@ -1341,7 +1325,7 @@ impl EventBuilder { #[inline] pub fn http_auth(data: HttpData) -> Self { let tags: Vec = data.into(); - Self::new(Kind::HttpAuth, "", tags) + Self::new(Kind::HttpAuth, "").tags(tags) } /// Set stall data @@ -1351,7 +1335,7 @@ impl EventBuilder { pub fn stall_data(data: StallData) -> Self { let content: String = data.as_json(); let tags: Vec = data.into(); - Self::new(Kind::SetStall, content, tags) + Self::new(Kind::SetStall, content).tags(tags) } /// Set product data @@ -1361,7 +1345,7 @@ impl EventBuilder { pub fn product_data(data: ProductData) -> Self { let content: String = data.as_json(); let tags: Vec = data.into(); - Self::new(Kind::SetProduct, content, tags) + Self::new(Kind::SetProduct, content).tags(tags) } /// Seal @@ -1413,7 +1397,8 @@ impl EventBuilder { // Push received public key tags.push(Tag::public_key(*receiver)); - Self::new(Kind::GiftWrap, content, tags) + Self::new(Kind::GiftWrap, content) + .tags(tags) .custom_created_at(Timestamp::tweaked(nip59::RANGE_RANDOM_TIMESTAMP_TWEAK)) .sign_with_keys(&keys) } @@ -1452,17 +1437,11 @@ impl EventBuilder { /// #[inline] #[cfg(feature = "nip59")] - pub fn private_msg_rumor(receiver: PublicKey, message: S, extra_tags: I) -> Self + pub fn private_msg_rumor(receiver: PublicKey, message: S) -> Self where S: Into, - I: IntoIterator, { - // Compose tags - let mut tags: Vec = extra_tags.into_iter().collect(); - tags.push(Tag::public_key(receiver)); - - // Compose builder - Self::new(Kind::PrivateDirectMessage, message, tags) + Self::new(Kind::PrivateDirectMessage, message).tags([Tag::public_key(receiver)]) } /// Private Direct message @@ -1481,7 +1460,7 @@ impl EventBuilder { S: Into, I: IntoIterator, { - let rumor: Self = Self::private_msg_rumor(receiver, message, rumor_extra_tags); + let rumor: Self = Self::private_msg_rumor(receiver, message).tags(rumor_extra_tags); Self::gift_wrap(signer, &receiver, rumor, []).await } @@ -1491,7 +1470,7 @@ impl EventBuilder { #[inline] pub fn mute_list(list: MuteList) -> Self { let tags: Vec = list.into(); - Self::new(Kind::MuteList, "", tags) + Self::new(Kind::MuteList, "").tags(tags) } /// Pinned notes @@ -1502,7 +1481,7 @@ impl EventBuilder { where I: IntoIterator, { - Self::new(Kind::PinList, "", ids.into_iter().map(Tag::event)) + Self::new(Kind::PinList, "").tags(ids.into_iter().map(Tag::event)) } /// Bookmarks @@ -1511,7 +1490,7 @@ impl EventBuilder { #[inline] pub fn bookmarks(list: Bookmarks) -> Self { let tags: Vec = list.into(); - Self::new(Kind::Bookmarks, "", tags) + Self::new(Kind::Bookmarks, "").tags(tags) } /// Communities @@ -1522,11 +1501,7 @@ impl EventBuilder { where I: IntoIterator, { - Self::new( - Kind::Communities, - "", - communities.into_iter().map(Tag::from), - ) + Self::new(Kind::Communities, "").tags(communities.into_iter().map(Tag::from)) } /// Public chats @@ -1537,7 +1512,7 @@ impl EventBuilder { where I: IntoIterator, { - Self::new(Kind::PublicChats, "", chat.into_iter().map(Tag::event)) + Self::new(Kind::PublicChats, "").tags(chat.into_iter().map(Tag::event)) } /// Blocked relays @@ -1548,9 +1523,7 @@ impl EventBuilder { where I: IntoIterator, { - Self::new( - Kind::BlockedRelays, - "", + Self::new(Kind::BlockedRelays, "").tags( relay .into_iter() .map(|r| Tag::from_standardized_without_cell(TagStandard::Relay(r))), @@ -1565,9 +1538,7 @@ impl EventBuilder { where I: IntoIterator, { - Self::new( - Kind::SearchRelays, - "", + Self::new(Kind::SearchRelays, "").tags( relay .into_iter() .map(|r| Tag::from_standardized_without_cell(TagStandard::Relay(r))), @@ -1580,7 +1551,7 @@ impl EventBuilder { #[inline] pub fn interests(list: Interests) -> Self { let tags: Vec = list.into(); - Self::new(Kind::Interests, "", tags) + Self::new(Kind::Interests, "").tags(tags) } /// Emojis @@ -1589,7 +1560,7 @@ impl EventBuilder { #[inline] pub fn emojis(list: Emojis) -> Self { let tags: Vec = list.into(); - Self::new(Kind::Emojis, "", tags) + Self::new(Kind::Emojis, "").tags(tags) } /// Follow set @@ -1601,9 +1572,7 @@ impl EventBuilder { I: IntoIterator, { let tags: Vec = vec![Tag::identifier(identifier)]; - Self::new( - Kind::FollowSet, - "", + Self::new(Kind::FollowSet, "").tags( tags.into_iter() .chain(public_keys.into_iter().map(Tag::public_key)), ) @@ -1618,9 +1587,7 @@ impl EventBuilder { I: IntoIterator, { let tags: Vec = vec![Tag::identifier(identifier)]; - Self::new( - Kind::RelaySet, - "", + Self::new(Kind::RelaySet, "").tags( tags.into_iter().chain( relays .into_iter() @@ -1638,7 +1605,7 @@ impl EventBuilder { { let mut tags: Vec = list.into(); tags.push(Tag::identifier(identifier)); - Self::new(Kind::BookmarkSet, "", tags) + Self::new(Kind::BookmarkSet, "").tags(tags) } /// Article Curation set @@ -1650,7 +1617,7 @@ impl EventBuilder { { let mut tags: Vec = list.into(); tags.push(Tag::identifier(identifier)); - Self::new(Kind::ArticlesCurationSet, "", tags) + Self::new(Kind::ArticlesCurationSet, "").tags(tags) } /// Videos Curation set @@ -1662,9 +1629,7 @@ impl EventBuilder { I: IntoIterator, { let tags: Vec = vec![Tag::identifier(identifier)]; - Self::new( - Kind::VideosCurationSet, - "", + Self::new(Kind::VideosCurationSet, "").tags( tags.into_iter() .chain(video.into_iter().map(Tag::coordinate)), ) @@ -1680,9 +1645,7 @@ impl EventBuilder { S: Into, { let tags: Vec = vec![Tag::identifier(identifier)]; - Self::new( - Kind::InterestSet, - "", + Self::new(Kind::InterestSet, "").tags( tags.into_iter() .chain(hashtags.into_iter().map(Tag::hashtag)), ) @@ -1697,13 +1660,11 @@ impl EventBuilder { I: IntoIterator, { let tags: Vec = vec![Tag::identifier(identifier)]; - Self::new( - Kind::EmojiSet, - "", - tags.into_iter().chain(emojis.into_iter().map(|(s, url)| { + Self::new(Kind::EmojiSet, "").tags(tags.into_iter().chain(emojis.into_iter().map( + |(s, url)| { Tag::from_standardized_without_cell(TagStandard::Emoji { shortcode: s, url }) - })), - ) + }, + ))) } /// Label @@ -1716,14 +1677,10 @@ impl EventBuilder { { let namespace: String = namespace.into(); let labels: Vec = labels.into_iter().chain([namespace.clone()]).collect(); - Self::new( - Kind::Label, - "", - [ - Tag::from_standardized_without_cell(TagStandard::LabelNamespace(namespace)), - Tag::from_standardized_without_cell(TagStandard::Label(labels)), - ], - ) + Self::new(Kind::Label, "").tags([ + Tag::from_standardized_without_cell(TagStandard::LabelNamespace(namespace)), + Tag::from_standardized_without_cell(TagStandard::Label(labels)), + ]) } /// Git Repository Announcement @@ -1768,7 +1725,7 @@ mod tests { .unwrap(), ); - let event = EventBuilder::text_note("hello", []) + let event = EventBuilder::text_note("hello") .sign_with_keys(&keys) .unwrap(); diff --git a/crates/nostr/src/event/mod.rs b/crates/nostr/src/event/mod.rs index 272960af3..51dc956e0 100644 --- a/crates/nostr/src/event/mod.rs +++ b/crates/nostr/src/event/mod.rs @@ -471,7 +471,7 @@ mod tests { #[cfg(feature = "std")] fn test_custom_kind() { let keys = Keys::generate(); - let e: Event = EventBuilder::new(Kind::Custom(123), "my content", []) + let e: Event = EventBuilder::new(Kind::Custom(123), "my content") .sign_with_keys(&keys) .unwrap(); @@ -487,10 +487,10 @@ mod tests { #[cfg(feature = "std")] fn test_event_expired() { let my_keys = Keys::generate(); - let event = - EventBuilder::text_note("my content", [Tag::expiration(Timestamp::from(1600000000))]) - .sign_with_keys(&my_keys) - .unwrap(); + let event = EventBuilder::text_note("my content") + .tags([Tag::expiration(Timestamp::from(1600000000))]) + .sign_with_keys(&my_keys) + .unwrap(); assert!(&event.is_expired()); } @@ -502,12 +502,10 @@ mod tests { let expiry_date: u64 = now.as_u64() * 2; let my_keys = Keys::generate(); - let event = EventBuilder::text_note( - "my content", - [Tag::expiration(Timestamp::from(expiry_date))], - ) - .sign_with_keys(&my_keys) - .unwrap(); + let event = EventBuilder::text_note("my content") + .tags([Tag::expiration(Timestamp::from(expiry_date))]) + .sign_with_keys(&my_keys) + .unwrap(); assert!(!&event.is_expired()); } @@ -516,7 +514,7 @@ mod tests { #[cfg(feature = "std")] fn test_event_without_expiration_tag() { let my_keys = Keys::generate(); - let event = EventBuilder::text_note("my content", []) + let event = EventBuilder::text_note("my content") .sign_with_keys(&my_keys) .unwrap(); assert!(!&event.is_expired()); diff --git a/crates/nostr/src/nips/nip34.rs b/crates/nostr/src/nips/nip34.rs index 9d13b1bf8..d14022885 100644 --- a/crates/nostr/src/nips/nip34.rs +++ b/crates/nostr/src/nips/nip34.rs @@ -115,7 +115,7 @@ impl GitRepositoryAnnouncement { tags.push(Tag::alt(GIT_REPO_ANNOUNCEMENT_ALT)); // Build - EventBuilder::new(Kind::GitRepoAnnouncement, "", tags) + EventBuilder::new(Kind::GitRepoAnnouncement, "").tags(tags) } } @@ -158,7 +158,7 @@ impl GitIssue { tags.push(Tag::alt(GIT_ISSUE_ALT)); // Build - EventBuilder::new(Kind::GitIssue, self.content, tags) + EventBuilder::new(Kind::GitIssue, self.content).tags(tags) } } @@ -315,6 +315,6 @@ impl GitPatch { tags.extend(self.maintainers.into_iter().map(Tag::public_key)); // Build - EventBuilder::new(Kind::GitPatch, content, tags) + EventBuilder::new(Kind::GitPatch, content).tags(tags) } } diff --git a/crates/nostr/src/nips/nip47.rs b/crates/nostr/src/nips/nip47.rs index 6d23fe089..4e6d6002b 100644 --- a/crates/nostr/src/nips/nip47.rs +++ b/crates/nostr/src/nips/nip47.rs @@ -521,12 +521,9 @@ impl Request { pub fn to_event(self, uri: &NostrWalletConnectURI) -> Result { let encrypted = nip04::encrypt(&uri.secret, &uri.public_key, self.as_json())?; let keys: Keys = Keys::new(uri.secret.clone()); - Ok(EventBuilder::new( - Kind::WalletConnectRequest, - encrypted, - [Tag::public_key(uri.public_key)], - ) - .sign_with_keys(&keys)?) + Ok(EventBuilder::new(Kind::WalletConnectRequest, encrypted) + .tag(Tag::public_key(uri.public_key)) + .sign_with_keys(&keys)?) } } diff --git a/crates/nostr/src/nips/nip57.rs b/crates/nostr/src/nips/nip57.rs index c31bd2d29..253fdbe79 100644 --- a/crates/nostr/src/nips/nip57.rs +++ b/crates/nostr/src/nips/nip57.rs @@ -273,7 +273,9 @@ pub fn anonymous_zap_request(data: ZapRequestData) -> Result { tags.push(Tag::from_standardized_without_cell(TagStandard::Anon { msg: None, })); - Ok(EventBuilder::new(Kind::ZapRequest, message, tags).sign_with_keys(&keys)?) + Ok(EventBuilder::new(Kind::ZapRequest, message) + .tags(tags) + .sign_with_keys(&keys)?) } /// Create **private** zap request @@ -307,7 +309,8 @@ where if let Some(event_id) = data.event_id { tags.push(Tag::event(event_id)); } - let msg: String = EventBuilder::new(Kind::ZapPrivateMessage, &data.message, tags) + let msg: String = EventBuilder::new(Kind::ZapPrivateMessage, &data.message) + .tags(tags) .sign_with_ctx(secp, rng, supplier, keys)? .as_json(); let msg: String = encrypt_private_zap_message(rng, &secret_key, &data.public_key, msg)?; @@ -318,7 +321,8 @@ where msg: Some(msg), })); let private_zap_keys: Keys = Keys::new_with_ctx(secp, secret_key); - Ok(EventBuilder::new(Kind::ZapRequest, "", tags) + Ok(EventBuilder::new(Kind::ZapRequest, "") + .tags(tags) .custom_created_at(created_at) .sign_with_ctx(secp, rng, supplier, &private_zap_keys)?) } diff --git a/crates/nostr/src/nips/nip59.rs b/crates/nostr/src/nips/nip59.rs index 757925e5d..dda87f76d 100644 --- a/crates/nostr/src/nips/nip59.rs +++ b/crates/nostr/src/nips/nip59.rs @@ -157,7 +157,7 @@ where .await?; // Compose builder - Ok(EventBuilder::new(Kind::Seal, content, []) + Ok(EventBuilder::new(Kind::Seal, content) .custom_created_at(Timestamp::tweaked(RANGE_RANDOM_TIMESTAMP_TWEAK))) } @@ -177,7 +177,7 @@ mod tests { .unwrap(); // Compose Gift Wrap event - let rumor: EventBuilder = EventBuilder::text_note("Test", []); + let rumor: EventBuilder = EventBuilder::text_note("Test"); let event: Event = EventBuilder::gift_wrap(&sender_keys, &receiver_keys.public_key(), rumor.clone(), []) .await @@ -189,7 +189,7 @@ mod tests { assert!(unwrapped.rumor.tags.is_empty()); assert!(extract_rumor(&sender_keys, &event).await.is_err()); - let event: Event = EventBuilder::text_note("", []) + let event: Event = EventBuilder::text_note("") .sign(&sender_keys) .await .unwrap();