Skip to content

Commit

Permalink
book: add kotlin Event.kt snippet
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Apr 11, 2024
1 parent e99b721 commit 1b6872d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion book/snippets/nostr/justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test: rust python js
test: rust python js kotlin

rust:
cd rust && cargo build
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rust.nostr.snippets

import rust.nostr.protocol.*


// ANCHOR: generate
fun generate() {
val keys = Keys.generate();
Expand Down
2 changes: 1 addition & 1 deletion book/snippets/nostr/python/src/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions book/src/nostr/04_00-event.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
<div slot="title">Kotlin</div>
<section>

TODO
```kotlin
{{#include ../../snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Event.kt:json}}
```

</section>

Expand Down Expand Up @@ -84,7 +86,9 @@ A convenient way to compose events is by using the `EventBuilder`. It allow to c
<div slot="title">Kotlin</div>
<section>

TODO
```kotlin
{{#include ../../snippets/nostr/kotlin/shared/src/main/kotlin/rust/nostr/snippets/Event.kt:builder}}
```

</section>

Expand Down

0 comments on commit 1b6872d

Please sign in to comment.