Skip to content

Commit e658a1b

Browse files
RydalWateryukibtc
authored andcommitted
book: add NIP-01 python examples
Added python example for NIP-01 including metadata event and deserialization. Closes #434 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 17223a1 commit e658a1b

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

book/snippets/nostr/python/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from src.keys import generate, restore, vanity
44
from src.event.json import event_json
55
from src.event.builder import event_builder
6+
from src.nip01 import nip01
67
from src.nip44 import nip44
78
from src.nip59 import nip59
89

@@ -13,6 +14,7 @@ def main():
1314
vanity()
1415
event_json()
1516
event_builder()
17+
nip01()
1618
nip44()
1719
nip59()
1820

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from nostr_protocol import Keys, Metadata, EventBuilder
2+
def nip01():
3+
# Generate random keys
4+
keys = Keys.generate()
5+
6+
# ANCHOR: create-event
7+
# Create metadata object with desired content
8+
metadata_content = Metadata()\
9+
.set_name("TestName")\
10+
.set_display_name("PyTestur")\
11+
.set_about("This is a Test Account for Rust Nostr Python Bindings")\
12+
.set_website("https://rust-nostr.org/")\
13+
.set_picture("https://avatars.githubusercontent.com/u/123304603?s=200&v=4")\
14+
.set_banner("https://nostr-resources.com/assets/images/cover.png")\
15+
.set_nip05("[email protected]")
16+
17+
# Build metadata event and assign content
18+
builder = EventBuilder.metadata(metadata_content)
19+
20+
# Signed event and print details
21+
print("\nCreating Metadata Event:")
22+
event = builder.to_event(keys)
23+
24+
print(" Event Details:")
25+
print(f" Author : {event.author().to_bech32()}")
26+
print(f" Kind : {event.kind().as_u16()}")
27+
print(f" Content : {event.content()}")
28+
print(f" Datetime : {event.created_at().to_human_datetime()}")
29+
print(f" Signature : {event.signature()}")
30+
print(f" Verify : {event.verify()}")
31+
print(f" JSON : {event.as_json()}")
32+
# ANCHOR_END: create-event
33+
34+
# ANCHOR: create-metadata
35+
# Deserialize Metadata from event
36+
print("\nDeserializing Metadata Event:")
37+
metadata = Metadata().from_json(event.content())
38+
39+
print(" Metadata Details:")
40+
print(f" Name : {metadata.get_name()}")
41+
print(f" Display : {metadata.get_display_name()}")
42+
print(f" About : {metadata.get_about()}")
43+
print(f" Website : {metadata.get_website()}")
44+
print(f" Picture : {metadata.get_picture()}")
45+
print(f" Banner : {metadata.get_banner()}")
46+
print(f" NIP05 : {metadata.get_nip05()}")
47+
# ANCHOR_END: create-metadata

book/src/nostr/06-nip01.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ For documentation on the available struct attributes, check out [the Metadata do
2828
<div slot="title">Python</div>
2929
<section>
3030

31-
TODO
31+
Using the `Metadata` class to build the metadata object and the `EventBuilder` class to create a Metadata event.
32+
33+
```python,ignore
34+
{{#include ../../snippets/nostr/python/src/nip01.py:create-event}}
35+
```
36+
37+
Use the `Metadata` class to deserialize the content of an exsiting metadata event.
38+
39+
```python,ignore
40+
{{#include ../../snippets/nostr/python/src/nip01.py:create-metadata}}
41+
```
3242

3343
</section>
3444

0 commit comments

Comments
 (0)