From 1b6872db8720fd49f4f0ad142c5ecacf30002186 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Thu, 11 Apr 2024 16:25:29 +0200 Subject: [PATCH] book: add kotlin `Event.kt` snippet Signed-off-by: Yuki Kishimoto --- book/snippets/nostr/justfile | 2 +- .../main/kotlin/rust/nostr/snippets/Event.kt | 37 +++++++++++++++++++ .../main/kotlin/rust/nostr/snippets/Keys.kt | 1 - book/snippets/nostr/python/src/keys.py | 2 +- book/src/nostr/04_00-event.md | 8 +++- 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 book/snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Event.kt diff --git a/book/snippets/nostr/justfile b/book/snippets/nostr/justfile index 28c539813..796e1b305 100644 --- a/book/snippets/nostr/justfile +++ b/book/snippets/nostr/justfile @@ -1,4 +1,4 @@ -test: rust python js +test: rust python js kotlin rust: cd rust && cargo build diff --git a/book/snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Event.kt b/book/snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Event.kt new file mode 100644 index 000000000..1aa1cf8d7 --- /dev/null +++ b/book/snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Event.kt @@ -0,0 +1,37 @@ +package rust.nostr.snippets + +import rust.nostr.protocol.* + +// ANCHOR: json +fun json() { + // Deserialize from json + val json = + "{\"content\":\"uRuvYr585B80L6rSJiHocw==?iv=oh6LVqdsYYol3JfFnXTbPA==\",\"created_at\":1640839235,\"id\":\"2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45\",\"kind\":4,\"pubkey\":\"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785\",\"sig\":\"a5d9290ef9659083c490b303eb7ee41356d8778ff19f2f91776c8dc4443388a64ffcf336e61af4c25c05ac3ae952d1ced889ed655b67790891222aaa15b99fdd\",\"tags\":[[\"p\",\"13adc511de7e1cfcf1c6b7f6365fb5a03442d7bcacf565ea57fa7770912c023d\"]]}" + val event = Event.fromJson(json) + + // Serialize as json + println(event.asJson()) +} +// ANCHOR_END: json + +// ANCHOR: builder +fun builder() { + val keys = Keys.generate(); + + // Compose custom event + val customEvent = EventBuilder(Kind(1111u), "", listOf()).toEvent(keys); + + // Compose text note + val textNoteEvent = EventBuilder.textNote("Hello", listOf()).toEvent(keys); + + // Compose reply to above text note + val replyEvent = EventBuilder.textNote("Reply to hello", listOf(Tag.event(textNoteEvent.id()))) + .toEvent(keys); + + // Compose POW event + val powEvent = + EventBuilder.textNote("Another reply with POW", listOf(Tag.event(textNoteEvent.id()))) + .toPowEvent(keys, 20u); + println(powEvent.asJson()) +} +// ANCHOR_END: builder \ No newline at end of file diff --git a/book/snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Keys.kt b/book/snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Keys.kt index 8f36d5f2d..2add6c50b 100644 --- a/book/snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Keys.kt +++ b/book/snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Keys.kt @@ -2,7 +2,6 @@ package rust.nostr.snippets import rust.nostr.protocol.* - // ANCHOR: generate fun generate() { val keys = Keys.generate(); diff --git a/book/snippets/nostr/python/src/keys.py b/book/snippets/nostr/python/src/keys.py index a5da80270..4a4593fbf 100644 --- a/book/snippets/nostr/python/src/keys.py +++ b/book/snippets/nostr/python/src/keys.py @@ -20,7 +20,7 @@ def generate(): # ANCHOR: restore def restore(): - keys = Keys.parse("hex or bech32 secret key") + keys = Keys.parse("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99") secret_key = SecretKey.from_hex("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e") keys = Keys(secret_key) diff --git a/book/src/nostr/04_00-event.md b/book/src/nostr/04_00-event.md index 887880787..7e70ee259 100644 --- a/book/src/nostr/04_00-event.md +++ b/book/src/nostr/04_00-event.md @@ -34,7 +34,9 @@
Kotlin
-TODO +```kotlin +{{#include ../../snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Event.kt:json}} +```
@@ -84,7 +86,9 @@ A convenient way to compose events is by using the `EventBuilder`. It allow to c
Kotlin
-TODO +```kotlin +{{#include ../../snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Event.kt:builder}} +```