From 9eb97e037161fdfa3bdc11b678c346cdaf582f53 Mon Sep 17 00:00:00 2001 From: Rusted Moon Date: Thu, 11 Jan 2024 12:50:20 +0100 Subject: [PATCH 1/3] book: Init Event and Messages sections --- book/src/SUMMARY.md | 8 ++++++++ book/src/nostr/04_00-event.md | 1 + book/src/nostr/04_01-event-id.md | 1 + book/src/nostr/04_02-kind.md | 1 + book/src/nostr/04_03-timestamp.md | 1 + book/src/nostr/04_04-tag.md | 1 + book/src/nostr/05_00-messages.md | 1 + book/src/nostr/05_01-client-message.md | 1 + book/src/nostr/05_02-relay-message.md | 1 + 9 files changed, 16 insertions(+) create mode 100644 book/src/nostr/04_00-event.md create mode 100644 book/src/nostr/04_01-event-id.md create mode 100644 book/src/nostr/04_02-kind.md create mode 100644 book/src/nostr/04_03-timestamp.md create mode 100644 book/src/nostr/04_04-tag.md create mode 100644 book/src/nostr/05_00-messages.md create mode 100644 book/src/nostr/05_01-client-message.md create mode 100644 book/src/nostr/05_02-relay-message.md diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 81c108224..bbf211e3c 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -7,6 +7,14 @@ * [Nostr](./nostr/01-index.md) * [Installation](./nostr/02-installation.md) * [Keys](./nostr/03-keys.md) + * [Event](./nostr/04_00-event.md) + * [Event ID](./nostr/04_01-event-id.md) + * [Kind](./nostr/04_02-kind.md) + * [Timestamp](./nostr/04_03-timestamp.md) + * [Tag](./nostr/04_04-tag.md) + * [Messages](./nostr/05_00-messages.md) + * [Client Message](./nostr/05_01-client-message.md) + * [Relay Message](./nostr/05_02-relay-message.md) * [Nostr SDK](./nostr-sdk/01-index.md) * [Installation](./nostr-sdk/02-installation.md) diff --git a/book/src/nostr/04_00-event.md b/book/src/nostr/04_00-event.md new file mode 100644 index 000000000..bdd2b86ed --- /dev/null +++ b/book/src/nostr/04_00-event.md @@ -0,0 +1 @@ +# Event diff --git a/book/src/nostr/04_01-event-id.md b/book/src/nostr/04_01-event-id.md new file mode 100644 index 000000000..ae49f3455 --- /dev/null +++ b/book/src/nostr/04_01-event-id.md @@ -0,0 +1 @@ +# Event ID diff --git a/book/src/nostr/04_02-kind.md b/book/src/nostr/04_02-kind.md new file mode 100644 index 000000000..d90208d86 --- /dev/null +++ b/book/src/nostr/04_02-kind.md @@ -0,0 +1 @@ +# Kind diff --git a/book/src/nostr/04_03-timestamp.md b/book/src/nostr/04_03-timestamp.md new file mode 100644 index 000000000..1ec5b908c --- /dev/null +++ b/book/src/nostr/04_03-timestamp.md @@ -0,0 +1 @@ +# Timestamp diff --git a/book/src/nostr/04_04-tag.md b/book/src/nostr/04_04-tag.md new file mode 100644 index 000000000..acfeba49c --- /dev/null +++ b/book/src/nostr/04_04-tag.md @@ -0,0 +1 @@ +# Tag diff --git a/book/src/nostr/05_00-messages.md b/book/src/nostr/05_00-messages.md new file mode 100644 index 000000000..c22dce549 --- /dev/null +++ b/book/src/nostr/05_00-messages.md @@ -0,0 +1 @@ +# Messages \ No newline at end of file diff --git a/book/src/nostr/05_01-client-message.md b/book/src/nostr/05_01-client-message.md new file mode 100644 index 000000000..6c106d01c --- /dev/null +++ b/book/src/nostr/05_01-client-message.md @@ -0,0 +1 @@ +# Client Message \ No newline at end of file diff --git a/book/src/nostr/05_02-relay-message.md b/book/src/nostr/05_02-relay-message.md new file mode 100644 index 000000000..5bf48f1f6 --- /dev/null +++ b/book/src/nostr/05_02-relay-message.md @@ -0,0 +1 @@ +# Relay Message \ No newline at end of file From 2be32c73f664c6414d6b33a38835ff7c60a56313 Mon Sep 17 00:00:00 2001 From: Rusted Moon Date: Fri, 12 Jan 2024 12:53:49 +0100 Subject: [PATCH 2/3] book: add `Event` section examples --- book/snippets/nostr/js/index.js | 8 +- book/snippets/nostr/js/src/event/builder.js | 26 ++++ book/snippets/nostr/js/src/event/json.js | 16 ++ book/snippets/nostr/python/main.py | 5 + .../nostr/python/src/event/builder.py | 16 ++ book/snippets/nostr/python/src/event/json.py | 10 ++ book/snippets/nostr/rust/src/event/builder.rs | 23 +++ book/snippets/nostr/rust/src/event/json.rs | 13 ++ book/snippets/nostr/rust/src/event/mod.rs | 2 + book/snippets/nostr/rust/src/main.rs | 3 + book/src/nostr/03-keys.md | 2 + book/src/nostr/04_00-event.md | 138 ++++++++++++++++++ 12 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 book/snippets/nostr/js/src/event/builder.js create mode 100644 book/snippets/nostr/js/src/event/json.js create mode 100644 book/snippets/nostr/python/src/event/builder.py create mode 100644 book/snippets/nostr/python/src/event/json.py create mode 100644 book/snippets/nostr/rust/src/event/builder.rs create mode 100644 book/snippets/nostr/rust/src/event/json.rs create mode 100644 book/snippets/nostr/rust/src/event/mod.rs diff --git a/book/snippets/nostr/js/index.js b/book/snippets/nostr/js/index.js index 2acf1b768..8f15ac855 100644 --- a/book/snippets/nostr/js/index.js +++ b/book/snippets/nostr/js/index.js @@ -1,4 +1,10 @@ const keys = require("./src/keys"); +const eventJson = require("./src/event/json"); +const eventBuilder = require("./src/event/builder"); // Test keys -keys.keys(); \ No newline at end of file +keys.keys(); + +eventJson.eventJson(); + +eventBuilder.eventBuilder(); \ No newline at end of file diff --git a/book/snippets/nostr/js/src/event/builder.js b/book/snippets/nostr/js/src/event/builder.js new file mode 100644 index 000000000..d4b9dcc0c --- /dev/null +++ b/book/snippets/nostr/js/src/event/builder.js @@ -0,0 +1,26 @@ +const { Keys, loadWasmSync, EventBuilder, Tag } = require("@rust-nostr/nostr"); + +function eventBuilder() { + // Load WASM + loadWasmSync(); + + let keys = Keys.generate(); + + // Compose custom event + let customEvent = new EventBuilder(1111, "", []).toEvent(keys); + + // Compose text note + let textnoteEvent = EventBuilder.textNote("Hello", []).toEvent(keys); + + // Compose reply to above text note + let replyEvent = + EventBuilder.textNote("Reply to hello", [Tag.parse(["e", textnoteEvent.id.toHex()])]) + .toEvent(keys); + + // Compose POW event + let powEvent = + EventBuilder.textNote("Another reply with POW", [Tag.parse(["e", textnoteEvent.id.toHex()])]) + .toPowEvent(keys, 20); +} + +module.exports.eventBuilder = eventBuilder; \ No newline at end of file diff --git a/book/snippets/nostr/js/src/event/json.js b/book/snippets/nostr/js/src/event/json.js new file mode 100644 index 000000000..681f2833c --- /dev/null +++ b/book/snippets/nostr/js/src/event/json.js @@ -0,0 +1,16 @@ +const { loadWasmSync, Event } = require("@rust-nostr/nostr"); + +function eventJson() { + // Load WASM + loadWasmSync(); + + // Deserialize from json + let json1 = '{"content":"uRuvYr585B80L6rSJiHocw==?iv=oh6LVqdsYYol3JfFnXTbPA==","created_at":1640839235,"id":"2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45","kind":4,"pubkey":"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785","sig":"a5d9290ef9659083c490b303eb7ee41356d8778ff19f2f91776c8dc4443388a64ffcf336e61af4c25c05ac3ae952d1ced889ed655b67790891222aaa15b99fdd","tags":[["p","13adc511de7e1cfcf1c6b7f6365fb5a03442d7bcacf565ea57fa7770912c023d"]]}' + let event = Event.fromJson(json1) + + // Serialize as json + let json2 = event.asJson() + console.log(json2); +} + +module.exports.eventJson = eventJson; \ No newline at end of file diff --git a/book/snippets/nostr/python/main.py b/book/snippets/nostr/python/main.py index beb76c214..7bcb74875 100755 --- a/book/snippets/nostr/python/main.py +++ b/book/snippets/nostr/python/main.py @@ -1,8 +1,13 @@ #!/usr/bin/env python from src.keys import keys +from src.event.json import event_json +from src.event.builder import event_builder + def main(): keys() + event_json() + event_builder() main() \ No newline at end of file diff --git a/book/snippets/nostr/python/src/event/builder.py b/book/snippets/nostr/python/src/event/builder.py new file mode 100644 index 000000000..ef26c131c --- /dev/null +++ b/book/snippets/nostr/python/src/event/builder.py @@ -0,0 +1,16 @@ +from nostr_protocol import * + +def event_builder(): + keys = Keys.generate() + + # Compose custom event + custom_event = EventBuilder(1111, "", []).to_event(keys) + + # Compose text note + textnote_event = EventBuilder.new_text_note("Hello", []).to_event(keys) + + # Compose reply to above text note + reply_event = EventBuilder.new_text_note("Reply to hello", [Tag.event(textnote_event.id())]).to_event(keys) + + # Compose POW event + pow_event = EventBuilder.new_text_note("Another reply with POW", [Tag.event(textnote_event.id())]).to_pow_event(keys, 20) diff --git a/book/snippets/nostr/python/src/event/json.py b/book/snippets/nostr/python/src/event/json.py new file mode 100644 index 000000000..661e21097 --- /dev/null +++ b/book/snippets/nostr/python/src/event/json.py @@ -0,0 +1,10 @@ +from nostr_protocol import * + +def event_json(): + # Deserialize from json + json = '{"content":"uRuvYr585B80L6rSJiHocw==?iv=oh6LVqdsYYol3JfFnXTbPA==","created_at":1640839235,"id":"2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45","kind":4,"pubkey":"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785","sig":"a5d9290ef9659083c490b303eb7ee41356d8778ff19f2f91776c8dc4443388a64ffcf336e61af4c25c05ac3ae952d1ced889ed655b67790891222aaa15b99fdd","tags":[["p","13adc511de7e1cfcf1c6b7f6365fb5a03442d7bcacf565ea57fa7770912c023d"]]}' + event = Event.from_json(json) + + # Serialize as json + json = event.as_json() + print(f"{json}") \ No newline at end of file diff --git a/book/snippets/nostr/rust/src/event/builder.rs b/book/snippets/nostr/rust/src/event/builder.rs new file mode 100644 index 000000000..b3a34d074 --- /dev/null +++ b/book/snippets/nostr/rust/src/event/builder.rs @@ -0,0 +1,23 @@ +use nostr::prelude::*; + +pub fn event() -> Result<()> { + let keys = Keys::generate(); + + // Compose custom event + let custom_event = EventBuilder::new(Kind::Custom(1111), "", []).to_event(&keys)?; + + // Compose text note + let textnote_event = EventBuilder::new_text_note("Hello", []).to_event(&keys)?; + + // Compose reply to above text note + let reply_event = + EventBuilder::new_text_note("Reply to hello", [Tag::event(textnote_event.id)]) + .to_event(&keys)?; + + // Compose POW event + let pow_event = + EventBuilder::new_text_note("Another reply with POW", [Tag::event(textnote_event.id)]) + .to_pow_event(&keys, 20)?; + + Ok(()) +} diff --git a/book/snippets/nostr/rust/src/event/json.rs b/book/snippets/nostr/rust/src/event/json.rs new file mode 100644 index 000000000..f53aec6be --- /dev/null +++ b/book/snippets/nostr/rust/src/event/json.rs @@ -0,0 +1,13 @@ +use nostr::prelude::*; + +pub fn event() -> Result<()> { + // Deserialize from json + let json = r#"{"content":"uRuvYr585B80L6rSJiHocw==?iv=oh6LVqdsYYol3JfFnXTbPA==","created_at":1640839235,"id":"2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45","kind":4,"pubkey":"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785","sig":"a5d9290ef9659083c490b303eb7ee41356d8778ff19f2f91776c8dc4443388a64ffcf336e61af4c25c05ac3ae952d1ced889ed655b67790891222aaa15b99fdd","tags":[["p","13adc511de7e1cfcf1c6b7f6365fb5a03442d7bcacf565ea57fa7770912c023d"]]}"#; + let event = Event::from_json(json)?; + + // Serialize as json + let json = event.as_json(); + println!("{json}"); + + Ok(()) +} diff --git a/book/snippets/nostr/rust/src/event/mod.rs b/book/snippets/nostr/rust/src/event/mod.rs new file mode 100644 index 000000000..d57e846e2 --- /dev/null +++ b/book/snippets/nostr/rust/src/event/mod.rs @@ -0,0 +1,2 @@ +pub mod builder; +pub mod json; diff --git a/book/snippets/nostr/rust/src/main.rs b/book/snippets/nostr/rust/src/main.rs index 2cebfbe49..87941639e 100644 --- a/book/snippets/nostr/rust/src/main.rs +++ b/book/snippets/nostr/rust/src/main.rs @@ -1,5 +1,8 @@ +#![allow(unused_variables)] + use nostr::Result; +pub mod event; pub mod keys; fn main() -> Result<()> { diff --git a/book/src/nostr/03-keys.md b/book/src/nostr/03-keys.md index 22250be59..ed2cf212c 100644 --- a/book/src/nostr/03-keys.md +++ b/book/src/nostr/03-keys.md @@ -2,6 +2,8 @@ ## Generate new random keys +To generate a new key pair use the `generate()` method: +
Rust
diff --git a/book/src/nostr/04_00-event.md b/book/src/nostr/04_00-event.md index bdd2b86ed..5ea513dd6 100644 --- a/book/src/nostr/04_00-event.md +++ b/book/src/nostr/04_00-event.md @@ -1 +1,139 @@ # Event + + + +## Serialize/deserialize to/from JSON + + + +
Rust
+
+ +```rust,ignore +{{#include ../../snippets/nostr/rust/src/event/json.rs}} +``` + +
+ +
Python
+
+ +```python,ignore +{{#include ../../snippets/nostr/python/src/event/json.py}} +``` + +
+ +
JavaScript
+
+ +```javascript,ignore +{{#include ../../snippets/nostr/js/src/event/json.js}} +``` + +
+ +
Kotlin
+
+ +TODO + +
+ +
Swift
+
+ +TODO + +
+
+ +## Compose with event builder + +A convenient way to compose events is by using the `EventBuilder`. It allow to compose `standard` and/or `custom` events. + + + +
Rust
+
+ +```rust,ignore +{{#include ../../snippets/nostr/rust/src/event/builder.rs}} +``` + +
+ +
Python
+
+ +```python,ignore +{{#include ../../snippets/nostr/python/src/event/builder.py}} +``` + +
+ +
JavaScript
+
+ +```javascript,ignore +{{#include ../../snippets/nostr/js/src/event/builder.js}} +``` + +
+ +
Kotlin
+
+ +TODO + +
+ +
Swift
+
+ +TODO + +
+
+ +## Compose manually + +TODO + + + +
Rust
+
+ +TODO + +
+ +
Python
+
+ +TODO + +
+ +
JavaScript
+
+ +TODO + +
+ +
Kotlin
+
+ +TODO + +
+ +
Swift
+
+ +TODO + +
+
\ No newline at end of file From 0f7969487869f12d560fc075541dad55b35809cb Mon Sep 17 00:00:00 2001 From: Rusted Moon Date: Thu, 18 Jan 2024 11:21:44 +0100 Subject: [PATCH 3/3] book: add NIPs section --- book/src/SUMMARY.md | 9 +++++++++ book/src/nostr/05_01_01-filter.md | 1 + book/src/nostr/06-nip05.md | 1 + book/src/nostr/06-nip06.md | 1 + book/src/nostr/06-nip07.md | 1 + book/src/nostr/06-nip19.md | 1 + book/src/nostr/06-nip21.md | 1 + book/src/nostr/06-nip44.md | 1 + book/src/nostr/06-nip65.md | 1 + book/src/nostr/06-nips.md | 1 + 10 files changed, 18 insertions(+) create mode 100644 book/src/nostr/05_01_01-filter.md create mode 100644 book/src/nostr/06-nip05.md create mode 100644 book/src/nostr/06-nip06.md create mode 100644 book/src/nostr/06-nip07.md create mode 100644 book/src/nostr/06-nip19.md create mode 100644 book/src/nostr/06-nip21.md create mode 100644 book/src/nostr/06-nip44.md create mode 100644 book/src/nostr/06-nip65.md create mode 100644 book/src/nostr/06-nips.md diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index bbf211e3c..80e4f37d2 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -14,7 +14,16 @@ * [Tag](./nostr/04_04-tag.md) * [Messages](./nostr/05_00-messages.md) * [Client Message](./nostr/05_01-client-message.md) + * [Filter](./nostr/05_01_01-filter.md) * [Relay Message](./nostr/05_02-relay-message.md) + * [NIPs](./nostr/06-nips.md) + * [NIP-05](./nostr/06-nip05.md) + * [NIP-06](./nostr/06-nip06.md) + * [NIP-07](./nostr/06-nip07.md) + * [NIP-19](./nostr/06-nip19.md) + * [NIP-21](./nostr/06-nip21.md) + * [NIP-44](./nostr/06-nip44.md) + * [NIP-65](./nostr/06-nip65.md) * [Nostr SDK](./nostr-sdk/01-index.md) * [Installation](./nostr-sdk/02-installation.md) diff --git a/book/src/nostr/05_01_01-filter.md b/book/src/nostr/05_01_01-filter.md new file mode 100644 index 000000000..8cc4cd046 --- /dev/null +++ b/book/src/nostr/05_01_01-filter.md @@ -0,0 +1 @@ +# Filter diff --git a/book/src/nostr/06-nip05.md b/book/src/nostr/06-nip05.md new file mode 100644 index 000000000..7100fa1c5 --- /dev/null +++ b/book/src/nostr/06-nip05.md @@ -0,0 +1 @@ +# NIP-05 diff --git a/book/src/nostr/06-nip06.md b/book/src/nostr/06-nip06.md new file mode 100644 index 000000000..93a4fdc26 --- /dev/null +++ b/book/src/nostr/06-nip06.md @@ -0,0 +1 @@ +# NIP-06 diff --git a/book/src/nostr/06-nip07.md b/book/src/nostr/06-nip07.md new file mode 100644 index 000000000..f68480429 --- /dev/null +++ b/book/src/nostr/06-nip07.md @@ -0,0 +1 @@ +# NIP-07 diff --git a/book/src/nostr/06-nip19.md b/book/src/nostr/06-nip19.md new file mode 100644 index 000000000..bede780a1 --- /dev/null +++ b/book/src/nostr/06-nip19.md @@ -0,0 +1 @@ +# NIP-19 diff --git a/book/src/nostr/06-nip21.md b/book/src/nostr/06-nip21.md new file mode 100644 index 000000000..bd575fb8b --- /dev/null +++ b/book/src/nostr/06-nip21.md @@ -0,0 +1 @@ +# NIP-21 diff --git a/book/src/nostr/06-nip44.md b/book/src/nostr/06-nip44.md new file mode 100644 index 000000000..79e0d8f13 --- /dev/null +++ b/book/src/nostr/06-nip44.md @@ -0,0 +1 @@ +# NIP-44 diff --git a/book/src/nostr/06-nip65.md b/book/src/nostr/06-nip65.md new file mode 100644 index 000000000..bc68d43cd --- /dev/null +++ b/book/src/nostr/06-nip65.md @@ -0,0 +1 @@ +# NIP-65 diff --git a/book/src/nostr/06-nips.md b/book/src/nostr/06-nips.md new file mode 100644 index 000000000..9daf692ea --- /dev/null +++ b/book/src/nostr/06-nips.md @@ -0,0 +1 @@ +# NIPs