Skip to content

Commit

Permalink
book: fix python snippets and make them runnable
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Feb 27, 2025
1 parent 7ac5f34 commit 0227d10
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 9 deletions.
3 changes: 3 additions & 0 deletions book/snippets/python/src/event/builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# ANCHOR: full
import asyncio
from nostr_sdk import Keys, EventBuilder, Kind, Tag, NostrSigner, Timestamp


Expand Down Expand Up @@ -35,4 +36,6 @@ async def event_builder():

await sign_and_print(signer, builder3)

if __name__ == '__main__':
asyncio.run(event_builder())
# ANCHOR_END: full
3 changes: 3 additions & 0 deletions book/snippets/python/src/event/id.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ def event_id():
print(f" - Event ID: {event.id()}")
print(f" - Verify the ID & Signature: {event.verify()}")
# ANCHOR_END: access-verify

if __name__ == '__main__':
event_id()
3 changes: 3 additions & 0 deletions book/snippets/python/src/event/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ def event_json():
# ANCHOR_END: serialize

print(json)

if __name__ == '__main__':
event_json()
# ANCHOR_END: full
4 changes: 3 additions & 1 deletion book/snippets/python/src/event/kind.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import cast
from nostr_sdk import Kind, KindStandard, EventBuilder, Keys, Metadata


Expand Down Expand Up @@ -61,3 +60,6 @@ def kind():
kind = Kind(10001)
print(f" - Is {kind.as_u16()} relay replaceable?: {kind.is_replaceable()}")
# ANCHOR_END: kind-tests

if __name__ == '__main__':
kind()
3 changes: 3 additions & 0 deletions book/snippets/python/src/event/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ def tags():
print(f" - Tag1 (Title?) : {tag.kind().is_title()}")
print(f" - Tag1 (Summary?): {tag.kind().is_summary()}")
# ANCHOR_END: logical

if __name__ == '__main__':
tags()
8 changes: 6 additions & 2 deletions book/snippets/python/src/hello.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# ANCHOR: full
from nostr_sdk import Keys, Client, EventBuilder
import asyncio
from nostr_sdk import Keys, Client, EventBuilder, NostrSigner


async def hello():
# ANCHOR: client
keys = Keys.generate()
client = Client(keys)
signer = NostrSigner.keys(keys)
client = Client(signer)
# ANCHOR_END: client

# ANCHOR: connect
Expand All @@ -24,4 +26,6 @@ async def hello():
print(f"Not send to: {output.failed}")
# ANCHOR_END: output

if __name__ == '__main__':
asyncio.run(hello())
# ANCHOR_END: full
4 changes: 4 additions & 0 deletions book/snippets/python/src/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ def restore():
secret_key = SecretKey.parse("6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e")
keys = Keys(secret_key)
# ANCHOR_END: restore

if __name__ == '__main__':
generate()
restore()
3 changes: 3 additions & 0 deletions book/snippets/python/src/messages/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,6 @@ def client_message():
print(f" - JSON: {message.as_json()}")
print(f" - Negative Error Message: {message.as_enum().is_neg_msg()}")
# ANCHOR_END: neg-msg

if __name__ == '__main__':
client_message()
3 changes: 3 additions & 0 deletions book/snippets/python/src/messages/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,6 @@ def filters():
print(f" Event match for filter: {f.match_event(event)}")
print(f" Event2 match for filter: {f.match_event(event2)}")
# ANCHOR_END: other-match

if __name__ == '__main__':
filters()
3 changes: 3 additions & 0 deletions book/snippets/python/src/messages/relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ def relay_message():
print(f" - Negative Error Message: {message.as_enum().is_neg_msg()}")
print(f" - JSON: {message.as_json()}")
# ANCHOR_END: neg-msg

if __name__ == '__main__':
relay_message()
7 changes: 5 additions & 2 deletions book/snippets/python/src/nip01.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from nostr_sdk import Keys, Metadata, EventBuilder


def nip01():
# Generate random keys
keys = Keys.generate()

print()
# ANCHOR: create-event
# Create metadata object with desired content
metadata_content = Metadata()\
Expand Down Expand Up @@ -32,7 +33,6 @@ def nip01():
print(f" JSON : {event.as_json()}")
# ANCHOR_END: create-event

print()
# ANCHOR: create-metadata
# Deserialize Metadata from event
print("Deserializing Metadata Event:")
Expand All @@ -47,3 +47,6 @@ def nip01():
print(f" Banner : {metadata.get_banner()}")
print(f" NIP05 : {metadata.get_nip05()}")
# ANCHOR_END: create-metadata

if __name__ == '__main__':
nip01()
6 changes: 4 additions & 2 deletions book/snippets/python/src/nip05.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from nostr_sdk import Metadata, PublicKey, verify_nip05, get_nip05_profile


Expand All @@ -9,8 +10,6 @@ async def nip05():
.set_nip05("[email protected]")
# ANCHOR_END: set-metadata

print()

# ANCHOR: verify-nip05
print("Verify NIP-05:")
nip_05 = "[email protected]"
Expand All @@ -30,3 +29,6 @@ async def nip05():
profile = await get_nip05_profile(nip_05)
print(f" {nip_05} Public key: {profile.public_key().to_bech32()}")
# ANCHOR_END: nip05-profile

if __name__ == '__main__':
asyncio.run(nip05())
3 changes: 3 additions & 0 deletions book/snippets/python/src/nip06.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ def nip06():
nsec = Keys.from_mnemonic(words, passphrase, account).secret_key().to_bech32()
print(f" Account #{account} bech32: {nsec}")
# ANCHOR_END: keys-from-seed-accounts-pass

if __name__ == '__main__':
nip06()
6 changes: 6 additions & 0 deletions book/snippets/python/src/nip19.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from nostr_sdk import Keys, EventBuilder, Nip19Profile, Nip19, Nip19Event, Coordinate, Kind


def nip19():
keys = Keys.generate()

Expand Down Expand Up @@ -57,3 +59,7 @@ def nip19():
decode_coord = Nip19.from_bech32(coord.to_bech32())
print(f" Coordinate (decoded): {decode_coord}")
# ANCHOR_END: nip19-naddr-decode


if __name__ == '__main__':
nip19()
3 changes: 3 additions & 0 deletions book/snippets/python/src/nip21.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ def nip21():
coord_bech32 = Coordinate.parse(coord_uri).to_bech32()
print(f" Coordinate (bech32): {coord_bech32}")
# ANCHOR_END: naddr

if __name__ == '__main__':
nip21()
4 changes: 4 additions & 0 deletions book/snippets/python/src/nip44.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from nostr_sdk import Keys, PublicKey, nip44_encrypt, nip44_decrypt, Nip44Version


def nip44():
print("\nEncrypting and Decrypting Messages (NIP-44):")
keys = Keys.generate()
Expand All @@ -11,3 +12,6 @@ def nip44():

plaintext = nip44_decrypt(keys.secret_key(), pk, ciphertext)
print(f" Decrypted: {plaintext}")

if __name__ == '__main__':
nip44()
4 changes: 4 additions & 0 deletions book/snippets/python/src/nip47.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# ANCHOR: full
import asyncio
from nostr_sdk import NostrWalletConnectUri, Nwc, PayInvoiceRequest, MakeInvoiceRequest


Expand Down Expand Up @@ -26,4 +27,7 @@ async def main():
result = await nwc.make_invoice(params)
print(f"Invoice: {result.invoice}")


if __name__ == '__main__':
asyncio.run(main())
# ANCHOR_END: full
4 changes: 4 additions & 0 deletions book/snippets/python/src/nip49.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ def decrypt():

print(f"Decrypted secret key: {secret_key.to_bech32()}")


if __name__ == '__main__':
encrypt()
decrypt()
# ANCHOR_END: full
6 changes: 5 additions & 1 deletion book/snippets/python/src/nip59.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from nostr_sdk import Keys, EventBuilder, Event, gift_wrap, UnwrappedGift, UnsignedEvent, NostrSigner


Expand All @@ -15,7 +16,7 @@ async def nip59():
rumor: UnsignedEvent = EventBuilder.text_note("Test").build(alice_keys.public_key())

# Build gift wrap with sender keys
gw: Event = await gift_wrap(alice_signer, bob_keys.public_key(), rumor, None)
gw: Event = await gift_wrap(alice_signer, bob_keys.public_key(), rumor)
print(f" Gift Wrap:\n{gw.as_json()}")

# Extract rumor from gift wrap with receiver keys
Expand All @@ -25,3 +26,6 @@ async def nip59():
unwrapped_rumor: UnsignedEvent = unwrapped_gift.rumor()
print(f" Sender: {sender.to_bech32()}")
print(f" Rumor: {unwrapped_rumor.as_json()}")

if __name__ == '__main__':
asyncio.run(nip59())
4 changes: 4 additions & 0 deletions book/snippets/python/src/nip65.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from nostr_sdk import EventBuilder, Tag, Kind, Keys, RelayMetadata


def nip65():
# Get Keys
keys = Keys.generate()
Expand Down Expand Up @@ -38,3 +39,6 @@ def nip65():
# Print event as json
print(f" Event: {event.as_json()}")
# ANCHOR_END: relay-metadata-custom

if __name__ == '__main__':
nip65()
4 changes: 3 additions & 1 deletion book/snippets/python/src/timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
def timestamps():
# Generate keys and Events
alice_keys = Keys.generate()
bob_keys = Keys.generate()

print()
print("Timestamps:")
Expand Down Expand Up @@ -36,3 +35,6 @@ def timestamps():
tag = Tag.expiration(timestamp)
print(f" Tag: {tag.as_standardized()}")
# ANCHOR_END: timestamp-tag

if __name__ == '__main__':
timestamps()

0 comments on commit 0227d10

Please sign in to comment.