Skip to content

Commit

Permalink
book: add NIP-01 python examples
Browse files Browse the repository at this point in the history
Added python example for NIP-01 including metadata event and deserialization.

Closes #434

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
RydalWater authored and yukibtc committed May 23, 2024
1 parent 17223a1 commit e658a1b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 2 additions & 0 deletions book/snippets/nostr/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from src.keys import generate, restore, vanity
from src.event.json import event_json
from src.event.builder import event_builder
from src.nip01 import nip01
from src.nip44 import nip44
from src.nip59 import nip59

Expand All @@ -13,6 +14,7 @@ def main():
vanity()
event_json()
event_builder()
nip01()
nip44()
nip59()

Expand Down
47 changes: 47 additions & 0 deletions book/snippets/nostr/python/src/nip01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from nostr_protocol import Keys, Metadata, EventBuilder
def nip01():
# Generate random keys
keys = Keys.generate()

# ANCHOR: create-event
# Create metadata object with desired content
metadata_content = Metadata()\
.set_name("TestName")\
.set_display_name("PyTestur")\
.set_about("This is a Test Account for Rust Nostr Python Bindings")\
.set_website("https://rust-nostr.org/")\
.set_picture("https://avatars.githubusercontent.com/u/123304603?s=200&v=4")\
.set_banner("https://nostr-resources.com/assets/images/cover.png")\
.set_nip05("[email protected]")

# Build metadata event and assign content
builder = EventBuilder.metadata(metadata_content)

# Signed event and print details
print("\nCreating Metadata Event:")
event = builder.to_event(keys)

print(" Event Details:")
print(f" Author : {event.author().to_bech32()}")
print(f" Kind : {event.kind().as_u16()}")
print(f" Content : {event.content()}")
print(f" Datetime : {event.created_at().to_human_datetime()}")
print(f" Signature : {event.signature()}")
print(f" Verify : {event.verify()}")
print(f" JSON : {event.as_json()}")
# ANCHOR_END: create-event

# ANCHOR: create-metadata
# Deserialize Metadata from event
print("\nDeserializing Metadata Event:")
metadata = Metadata().from_json(event.content())

print(" Metadata Details:")
print(f" Name : {metadata.get_name()}")
print(f" Display : {metadata.get_display_name()}")
print(f" About : {metadata.get_about()}")
print(f" Website : {metadata.get_website()}")
print(f" Picture : {metadata.get_picture()}")
print(f" Banner : {metadata.get_banner()}")
print(f" NIP05 : {metadata.get_nip05()}")
# ANCHOR_END: create-metadata
12 changes: 11 additions & 1 deletion book/src/nostr/06-nip01.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ For documentation on the available struct attributes, check out [the Metadata do
<div slot="title">Python</div>
<section>

TODO
Using the `Metadata` class to build the metadata object and the `EventBuilder` class to create a Metadata event.

```python,ignore
{{#include ../../snippets/nostr/python/src/nip01.py:create-event}}
```

Use the `Metadata` class to deserialize the content of an exsiting metadata event.

```python,ignore
{{#include ../../snippets/nostr/python/src/nip01.py:create-metadata}}
```

</section>

Expand Down

0 comments on commit e658a1b

Please sign in to comment.