Skip to content

Commit

Permalink
nostr: update EventBuilder::new fingerprint
Browse files Browse the repository at this point in the history
* Add `EventBuilder::tag` and `EventBuilder::tags`
* Remove `tags` arg from various `EventBuilder` constructor

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 15, 2024
1 parent cc8fb5b commit 455350f
Show file tree
Hide file tree
Showing 28 changed files with 275 additions and 361 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/python/examples/client-with-opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}")
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/python/examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}")
Expand Down
5 changes: 2 additions & 3 deletions bindings/nostr-sdk-ffi/python/examples/event_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/python/examples/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/python/examples/tor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}")
Expand Down
42 changes: 15 additions & 27 deletions bindings/nostr-sdk-ffi/src/protocol/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,19 @@ impl EventBuilder {
async fn _none(&self) {}

#[uniffi::constructor]
pub fn new(kind: &Kind, content: &str, tags: &[Arc<Tag>]) -> 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<Self>, tags: &[Arc<Tag>]) -> Self {
///
/// This method extend the current tags (if any).
pub fn tags(self: Arc<Self>, tags: &[Arc<Tag>]) -> 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
}

Expand Down Expand Up @@ -133,10 +134,9 @@ impl EventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/01.md>
#[uniffi::constructor]
pub fn text_note(content: &str, tags: &[Arc<Tag>]) -> 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),
}
}

Expand Down Expand Up @@ -188,10 +188,9 @@ impl EventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/23.md>
#[uniffi::constructor]
pub fn long_form_text_note(content: &str, tags: &[Arc<Tag>]) -> 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),
}
}

Expand Down Expand Up @@ -499,12 +498,9 @@ impl EventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/90.md>
#[uniffi::constructor]
pub fn job_request(kind: &Kind, tags: &[Arc<Tag>]) -> Result<Self> {
pub fn job_request(kind: &Kind) -> Result<Self> {
Ok(Self {
inner: nostr::EventBuilder::job_request(
**kind,
tags.iter().map(|t| t.as_ref().deref().clone()),
)?,
inner: nostr::EventBuilder::job_request(**kind)?,
})
}

Expand Down Expand Up @@ -607,18 +603,10 @@ impl EventBuilder {
/// </div>
///
/// <https://github.com/nostr-protocol/nips/blob/master/17.md>
#[uniffi::constructor(default(extra_tags = []))]
pub fn private_msg_rumor(
receiver: &PublicKey,
message: &str,
extra_tags: Vec<Arc<Tag>>,
) -> 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),
}
}

Expand Down
39 changes: 15 additions & 24 deletions bindings/nostr-sdk-js/src/protocol/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ impl From<EventBuilder> for JsEventBuilder {
#[wasm_bindgen(js_class = EventBuilder)]
impl JsEventBuilder {
#[wasm_bindgen(constructor)]
pub fn new(kind: &JsKind, content: &str, tags: Vec<JsTag>) -> 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<JsTag>) -> 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<JsTag>) -> Self {
self.inner.tags(tags.into_iter().map(|t| t.into())).into()
}

/// Set a custom `created_at` UNIX timestamp
Expand Down Expand Up @@ -132,9 +132,9 @@ impl JsEventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/01.md>
#[wasm_bindgen(js_name = textNote)]
pub fn text_note(content: &str, tags: Vec<JsTag>) -> 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),
}
}

Expand Down Expand Up @@ -186,9 +186,9 @@ impl JsEventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/23.md>
#[wasm_bindgen(js_name = longFormTextNote)]
pub fn long_form_text_note(content: &str, tags: Vec<JsTag>) -> 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),
}
}

Expand Down Expand Up @@ -469,10 +469,9 @@ impl JsEventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/90.md>
#[wasm_bindgen(js_name = jobRequest)]
pub fn job_request(kind: &JsKind, tags: Vec<JsTag>) -> Result<JsEventBuilder> {
pub fn job_request(kind: &JsKind) -> Result<JsEventBuilder> {
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)?,
})
}

Expand Down Expand Up @@ -616,17 +615,9 @@ impl JsEventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/17.md>
#[wasm_bindgen(js_name = privateMsgRumor)]
pub fn private_msg_rumor(
receiver: &JsPublicKey,
message: &str,
extra_tags: Option<Vec<JsTag>>,
) -> 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),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-connect/examples/handle-auth-url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
23 changes: 9 additions & 14 deletions crates/nostr-database/examples/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
23 changes: 9 additions & 14 deletions crates/nostr-database/examples/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down
Loading

0 comments on commit 455350f

Please sign in to comment.