Skip to content

Commit

Permalink
ffi(nostr-sdk): update python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 5, 2024
1 parent f6416e1 commit e25d405
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
39 changes: 33 additions & 6 deletions bindings/nostr-sdk-ffi/bindings-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,56 @@ pip install nostr-sdk
```

```python
from nostr_sdk import Keys, Client, EventBuilder, Filter
from nostr_sdk import Keys, Client, ClientSigner, EventBuilder, Filter, Metadata, Nip46Signer, init_logger, LogLevel
from datetime import timedelta
import time

# Init logger
init_logger(LogLevel.INFO)

# Initialize client without signer
# client = Client(None)

# Or, initialize with Keys signer
keys = Keys.generate()
print(keys.public_key().to_bech32())
signer = ClientSigner.KEYS(keys)
client = Client(signer)

client = Client(None)
# Or, initialize with NIP46 signer
# app_keys = Keys.generate()
# nip46 = Nip46Signer("wss://relay.damus.io", app_keys, None)
#signer = ClientSigner.NIP46(nip46)
# client = Client(signer)

# Add a single relay
client.add_relay("wss://relay.damus.io")

# Add multiple relays
client.add_relays(["wss://relay.damus.io", "wss://nos.lol"])

# Connect
client.connect()

# Send an event using the Client Signer
builder = EventBuilder.new_text_note("Test from Rust Nostr Python!", [])
client.send_event_builder(builder)
client.set_metadata(Metadata().set_name("Testing Rust Nostr"))

# Mine a POW event and sign it with custom keys
custom_keys = Keys.generate()
print("Mining a POW text note...")
event = EventBuilder.new_text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(keys, 20)
event = EventBuilder.new_text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(custom_keys, 20)
event_id = client.send_event(event)
print("Event sent:")
print(f" hex: {event_id.to_hex()}")
print(f" bech32: {event_id.to_bech32()}")

time.sleep(2.0)

# Get events from relays
print("Getting events from relays...")
filter = Filter().authors([keys.public_key().to_hex()])
events = client.get_events_of([filter], None)
filter = Filter().authors([keys.public_key(), custom_keys.public_key()])
events = client.get_events_of([filter], timedelta(seconds=10))
for event in events:
print(event.as_json())
```
Expand Down
36 changes: 31 additions & 5 deletions bindings/nostr-sdk-ffi/bindings-python/examples/client.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
from nostr_sdk import Keys, Client, EventBuilder, Filter
from nostr_sdk import Keys, Client, ClientSigner, EventBuilder, Filter, Metadata, Nip46Signer, init_logger, LogLevel
from datetime import timedelta
import time

# Init logger
init_logger(LogLevel.INFO)

# Initialize client without signer
# client = Client(None)

# Or, initialize with Keys signer
keys = Keys.generate()
print(keys.public_key().to_bech32())
signer = ClientSigner.KEYS(keys)
client = Client(signer)

client = Client(None)
# Or, initialize with NIP46 signer
# app_keys = Keys.generate()
# nip46 = Nip46Signer("wss://relay.damus.io", app_keys, None)
#signer = ClientSigner.NIP46(nip46)
# client = Client(signer)

# Add a single relay
client.add_relay("wss://relay.damus.io")

# Add multiple relays
client.add_relays(["wss://relay.damus.io", "wss://nos.lol"])

# Connect
client.connect()

# Send an event using the Client Signer
builder = EventBuilder.new_text_note("Test from Rust Nostr Python!", [])
client.send_event_builder(builder)
client.set_metadata(Metadata().set_name("Testing Rust Nostr"))

# Mine a POW event and sign it with custom keys
custom_keys = Keys.generate()
print("Mining a POW text note...")
event = EventBuilder.new_text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(keys, 20)
event = EventBuilder.new_text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(custom_keys, 20)
event_id = client.send_event(event)
print("Event sent:")
print(f" hex: {event_id.to_hex()}")
print(f" bech32: {event_id.to_bech32()}")

time.sleep(2.0)

# Get events from relays
print("Getting events from relays...")
filter = Filter().authors([keys.public_key()])
filter = Filter().authors([keys.public_key(), custom_keys.public_key()])
events = client.get_events_of([filter], timedelta(seconds=10))
for event in events:
print(event.as_json())

0 comments on commit e25d405

Please sign in to comment.