Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init some nostr book sections #247

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion book/snippets/nostr/js/index.js
Original file line number Diff line number Diff line change
@@ -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();
keys.keys();

eventJson.eventJson();

eventBuilder.eventBuilder();
26 changes: 26 additions & 0 deletions book/snippets/nostr/js/src/event/builder.js
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions book/snippets/nostr/js/src/event/json.js
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions book/snippets/nostr/python/main.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 16 additions & 0 deletions book/snippets/nostr/python/src/event/builder.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions book/snippets/nostr/python/src/event/json.py
Original file line number Diff line number Diff line change
@@ -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}")
23 changes: 23 additions & 0 deletions book/snippets/nostr/rust/src/event/builder.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
13 changes: 13 additions & 0 deletions book/snippets/nostr/rust/src/event/json.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
2 changes: 2 additions & 0 deletions book/snippets/nostr/rust/src/event/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod builder;
pub mod json;
3 changes: 3 additions & 0 deletions book/snippets/nostr/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(unused_variables)]

use nostr::Result;

pub mod event;
pub mod keys;

fn main() -> Result<()> {
Expand Down
17 changes: 17 additions & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
* [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)
* [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)

Expand Down
2 changes: 2 additions & 0 deletions book/src/nostr/03-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Generate new random keys

To generate a new key pair use the `generate()` method:

<custom-tabs category="lang">

<div slot="title">Rust</div>
Expand Down
139 changes: 139 additions & 0 deletions book/src/nostr/04_00-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Event



## Serialize/deserialize to/from JSON

<custom-tabs category="lang">

<div slot="title">Rust</div>
<section>

```rust,ignore
{{#include ../../snippets/nostr/rust/src/event/json.rs}}
```

</section>

<div slot="title">Python</div>
<section>

```python,ignore
{{#include ../../snippets/nostr/python/src/event/json.py}}
```

</section>

<div slot="title">JavaScript</div>
<section>

```javascript,ignore
{{#include ../../snippets/nostr/js/src/event/json.js}}
```

</section>

<div slot="title">Kotlin</div>
<section>

TODO

</section>

<div slot="title">Swift</div>
<section>

TODO

</section>
</custom-tabs>

## Compose with event builder

A convenient way to compose events is by using the `EventBuilder`. It allow to compose `standard` and/or `custom` events.

<custom-tabs category="lang">

<div slot="title">Rust</div>
<section>

```rust,ignore
{{#include ../../snippets/nostr/rust/src/event/builder.rs}}
```

</section>

<div slot="title">Python</div>
<section>

```python,ignore
{{#include ../../snippets/nostr/python/src/event/builder.py}}
```

</section>

<div slot="title">JavaScript</div>
<section>

```javascript,ignore
{{#include ../../snippets/nostr/js/src/event/builder.js}}
```

</section>

<div slot="title">Kotlin</div>
<section>

TODO

</section>

<div slot="title">Swift</div>
<section>

TODO

</section>
</custom-tabs>

## Compose manually

TODO

<custom-tabs category="lang">

<div slot="title">Rust</div>
<section>

TODO

</section>

<div slot="title">Python</div>
<section>

TODO

</section>

<div slot="title">JavaScript</div>
<section>

TODO

</section>

<div slot="title">Kotlin</div>
<section>

TODO

</section>

<div slot="title">Swift</div>
<section>

TODO

</section>
</custom-tabs>
1 change: 1 addition & 0 deletions book/src/nostr/04_01-event-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Event ID
1 change: 1 addition & 0 deletions book/src/nostr/04_02-kind.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Kind
1 change: 1 addition & 0 deletions book/src/nostr/04_03-timestamp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Timestamp
1 change: 1 addition & 0 deletions book/src/nostr/04_04-tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Tag
1 change: 1 addition & 0 deletions book/src/nostr/05_00-messages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Messages
1 change: 1 addition & 0 deletions book/src/nostr/05_01-client-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Client Message
1 change: 1 addition & 0 deletions book/src/nostr/05_01_01-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Filter
1 change: 1 addition & 0 deletions book/src/nostr/05_02-relay-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Relay Message
1 change: 1 addition & 0 deletions book/src/nostr/06-nip05.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NIP-05
1 change: 1 addition & 0 deletions book/src/nostr/06-nip06.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NIP-06
1 change: 1 addition & 0 deletions book/src/nostr/06-nip07.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NIP-07
1 change: 1 addition & 0 deletions book/src/nostr/06-nip19.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NIP-19
1 change: 1 addition & 0 deletions book/src/nostr/06-nip21.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NIP-21
1 change: 1 addition & 0 deletions book/src/nostr/06-nip44.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NIP-44
1 change: 1 addition & 0 deletions book/src/nostr/06-nip65.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NIP-65
1 change: 1 addition & 0 deletions book/src/nostr/06-nips.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NIPs