### Install python-nostr Source: https://github.com/jeffthibault/python-nostr/blob/main/README.md Installs the python-nostr library using pip. ```bash pip install nostr ``` -------------------------------- ### Install Test Dependencies Source: https://github.com/jeffthibault/python-nostr/blob/main/test/README.md Installs the necessary Python packages required for running the test suite from the specified requirements file. ```bash pip3 install -r test/requirements.txt ``` -------------------------------- ### Install Local Development Module Source: https://github.com/jeffthibault/python-nostr/blob/main/test/README.md Installs the python-nostr module in an editable mode, making it importable and visible for development and testing purposes from the repository root. ```bash pip3 install -e . ``` -------------------------------- ### Connect to Nostr Relays Source: https://github.com/jeffthibault/python-nostr/blob/main/README.md Establishes connections to specified Nostr relays. It disables SSL certificate verification for simplicity in the example. It also demonstrates how to retrieve and print any notices received from the relays. ```python import json import ssl import time from nostr.relay_manager import RelayManager relay_manager = RelayManager() relay_manager.add_relay("wss://nostr-pub.wellorder.net") relay_manager.add_relay("wss://relay.damus.io") relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE}) # NOTE: This disables ssl certificate verification time.sleep(1.25) # allow the connections to open while relay_manager.message_pool.has_notices(): notice_msg = relay_manager.message_pool.get_notice() print(notice_msg.content) relay_manager.close_connections() ``` -------------------------------- ### Run All Tests Source: https://github.com/jeffthibault/python-nostr/blob/main/test/README.md Executes the entire test suite for the python-nostr project using the pytest framework. This command should be run from the repository's root directory. ```bash pytest ``` -------------------------------- ### Reply to a Nostr Note Source: https://github.com/jeffthibault/python-nostr/blob/main/README.md Constructs and publishes a reply to an existing Nostr note. It creates an Event, adds 'e' and 'p' tags to reference the original note and author, signs the reply, and publishes it. ```python from nostr.event import Event reply = Event( content="Hey, that's a great point!", ) # create 'e' tag reference to the note you're replying to reply.add_event_ref(original_note_id) # create 'p' tag reference to the pubkey you're replying to reply.add_pubkey_ref(original_note_author_pubkey) private_key.sign_event(reply) relay_manager.publish_event(reply) ``` -------------------------------- ### Receive Events from Nostr Relays Source: https://github.com/jeffthibault/python-nostr/blob/main/README.md Subscribes to and receives events from Nostr relays based on specified filters. It sets up filters for authors and event kinds, opens connections, publishes a subscription request, and then processes incoming events. ```python import json import ssl import time from nostr.filter import Filter, Filters from nostr.event import Event, EventKind from nostr.relay_manager import RelayManager from nostr.message_type import ClientMessageType filters = Filters([Filter(authors=[], kinds=[EventKind.TEXT_NOTE])]) subscription_id = request = [ClientMessageType.REQUEST, subscription_id] request.extend(filters.to_json_array()) relay_manager = RelayManager() relay_manager.add_relay("wss://nostr-pub.wellorder.net") relay_manager.add_relay("wss://relay.damus.io") relay_manager.add_subscription(subscription_id, filters) relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE}) # NOTE: This disables ssl certificate verification time.sleep(1.25) # allow the connections to open message = json.dumps(request) relay_manager.publish_message(message) time.sleep(1) # allow the messages to send while relay_manager.message_pool.has_events(): event_msg = relay_manager.message_pool.get_event() print(event_msg.event.content) relay_manager.close_connections() ``` -------------------------------- ### Generate Nostr Key Source: https://github.com/jeffthibault/python-nostr/blob/main/README.md Generates a new private and public key pair for Nostr. Displays the private key in bech32 format and the public key in bech32 format. ```python from nostr.key import PrivateKey private_key = PrivateKey() public_key = private_key.public_key print(f"Private key: {private_key.bech32()}") print(f"Public key: {public_key.bech32()}") ``` -------------------------------- ### Publish Event to Nostr Relays Source: https://github.com/jeffthibault/python-nostr/blob/main/README.md Publishes a Nostr event to connected relays. This involves creating an Event object, signing it with a private key, and then publishing it through the RelayManager. A short delay is included to ensure the message is sent. ```python import json import ssl import time from nostr.event import Event from nostr.relay_manager import RelayManager from nostr.message_type import ClientMessageType from nostr.key import PrivateKey relay_manager = RelayManager() relay_manager.add_relay("wss://nostr-pub.wellorder.net") relay_manager.add_relay("wss://relay.damus.io") relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE}) # NOTE: This disables ssl certificate verification time.sleep(1.25) # allow the connections to open private_key = PrivateKey() event = Event("Hello Nostr") private_key.sign_event(event) relay_manager.publish_event(event) time.sleep(1) # allow the messages to send relay_manager.close_connections() ``` -------------------------------- ### Run Specific Test Source: https://github.com/jeffthibault/python-nostr/blob/main/test/README.md Executes a single, specific test function within a Python test file. This allows for highly targeted testing of individual functionalities. Run from the repository root. ```bash pytest test/test_this_file.py::test_this_specific_test ``` -------------------------------- ### Run Specific Test File Source: https://github.com/jeffthibault/python-nostr/blob/main/test/README.md Executes tests contained within a particular Python file. This is useful for focusing on a subset of tests during development or debugging. Run from the repository root. ```bash pytest test/test_this_file.py ``` -------------------------------- ### NIP-26 Delegation Source: https://github.com/jeffthibault/python-nostr/blob/main/README.md Implements NIP-26 delegation, allowing one Nostr key (delegator) to authorize another key (delegatee) to sign events on its behalf for a specified duration and event kind. It generates a delegation tag that can be used in event tags. ```python from nostr.delegation import Delegation from nostr.event import EventKind, Event from nostr.key import PrivateKey # Load your "identity" PK that you'd like to keep safely offline identity_pk = PrivateKey.from_nsec("nsec1...") # Create a new, disposable PK as the "delegatee" that can be "hot" in a Nostr client delegatee_pk = PrivateKey() # the "identity" PK will authorize "delegatee" to sign TEXT_NOTEs on its behalf for the next month delegation = Delegation( delegator_pubkey=identity_pk.public_key.hex(), delegatee_pubkey=delegatee_pk.public_key.hex(), event_kind=EventKind.TEXT_NOTE, duration_secs=30*24*60*60 ) identity_pk.sign_delegation(delegation) event = Event( "Hello, NIP-26!", tags=[delegation.get_tag()], ) delegatee_pk.sign_event(event) # ...normal broadcast steps... ``` -------------------------------- ### Send Encrypted Direct Message (DM) Source: https://github.com/jeffthibault/python-nostr/blob/main/README.md Sends an encrypted direct message to another Nostr user. It uses the EncryptedDirectMessage class, specifying the recipient's public key and the message content. The message is then signed and published. ```python from nostr.event import EncryptedDirectMessage dm = EncryptedDirectMessage( recipient_pubkey=recipient_pubkey, cleartext_content="Secret message!" ) private_key.sign_event(dm) relay_manager.publish_event(dm) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.