c

c

世上本没有路
tg_channel
mastodon
pleroma

Python releases Nostr information.

The free flow and sharing of information is one of the greatest charms of the Internet.

In pursuit of a more open and free Internet, I have decided to enable a fragmented information publishing platform, which includes Nostr (Notes and Other Stuff Transmitted by Relays). Nostr is a protocol similar to email; its client, such as Damus, can be seen as a decentralized Twitter.

This article introduces the Nostr protocol and how to use Python to publish Nostr Post information.

If you want to follow the author of this article, please go to the Others page to view.

Introduction to Nostr#

Communication Process#

  • This protocol consists of two components: a client and a relay. The client is the user's social client, and the relay is the forwarding server.
  • Users do not need to register; they only need a key pair (public key + private key) and sign the information they want to send, then send it to a group of relays.
  • Then, your followers can subscribe to your information from these relays (by entering the public key).

Technical Summary#

  • Technically, Nostr uses WebSocket + JSON. The main instructions are as follows:

    • From the client to the relay, the main instructions are:

      • EVENT: Send an event, which can be extended to perform many actions, such as sending messages, deleting messages, migrating messages, creating channels, etc. It has good extensibility.
      • REQ: Used to request events and subscribe to updates. After receiving the REQ message, the relay will query its internal database and return events that match the filter. It will then store the filter and resend all future events received to the same WebSocket until the WebSocket is closed.
      • CLOSE: Used to stop the subscription requested by REQ.
    • From the relay to the client, the main instructions are:

      • EVENT: Used to send events requested by the client.
      • NOTICE: Used to send human-readable error messages or other information to the client.
  • Regarding the EVENT, here are several commonly used basic events:

    • 0: set_metadata: For example, user name, user avatar, user profile, etc.
    • 1: text_note: The content of the information the user wants to send.
    • 2: recommend_server: The URL of the relay that the user wants to recommend to followers (e.g., wss://somerelay.com).

Publishing Nostr Information with Python#

Goal#

  • Requirement: Provide a private key and a string of text, and use Python to publish Nostr plain text Post information.
  • Support as many platforms as possible.

Solution#

Use libraries.

jeffthibault/python-nostr: A Python library for Nostr was considered, but it was found to have a dependency on secp256k1, which is not friendly to the Windows system, so it was excluded.

After comparison, the following library was used:

holgern/pynostr: Python library for nostr

pip install pynostr[websocket-client]

Complete Code#

发布 Nostr Post 信息.py・GitHub, content:

import json
import ssl
import time
import uuid
from pynostr.event import Event
from pynostr.relay_manager import RelayManager
from pynostr.filters import FiltersList, Filters
from pynostr.message_type import ClientMessageType
from pynostr.key import PrivateKey

relay_manager = RelayManager(timeout=6)
relay_manager.add_relay("wss://nostr-pub.wellorder.net")
relay_manager.add_relay("wss://relay.damus.io")
private_key = PrivateKey.from_nsec("Your private key")

filters = FiltersList([Filters(authors=[private_key.public_key.hex()], limit=100)])
subscription_id = uuid.uuid1().hex
relay_manager.add_subscription_on_all_relays(subscription_id, filters)

event = Event("String message")
event.sign(private_key.hex())

relay_manager.publish_event(event)
relay_manager.run_sync()
time.sleep(5)
while relay_manager.message_pool.has_ok_notices():
    ok_msg = relay_manager.message_pool.get_ok_notice()
    print(ok_msg)
while relay_manager.message_pool.has_events():
    event_msg = relay_manager.message_pool.get_event()
    print(event_msg.event.to_dict())

References#

聊聊 nostr 和 审查 | 酷 壳 - CoolShell

And the links in the article.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.