惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
Forbes - Security
Forbes - Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
人人都是产品经理
人人都是产品经理
Know Your Adversary
Know Your Adversary
Security Latest
Security Latest
雷峰网
雷峰网
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
GbyAI
GbyAI
Last Week in AI
Last Week in AI
Hacker News: Ask HN
Hacker News: Ask HN
U
Unit 42
S
SegmentFault 最新的问题
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
Arctic Wolf
Schneier on Security
Schneier on Security
C
CERT Recently Published Vulnerability Notes
I
Intezer
V
Visual Studio Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
Google Online Security Blog
Google Online Security Blog
N
Netflix TechBlog - Medium
I
InfoQ
T
Tor Project blog
腾讯CDC
T
Tenable Blog
Webroot Blog
Webroot Blog
Y
Y Combinator Blog
TaoSecurity Blog
TaoSecurity Blog
Google DeepMind News
Google DeepMind News
AI
AI
C
Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V2EX - 技术
V2EX - 技术
PCI Perspectives
PCI Perspectives
C
CXSECURITY Database RSS Feed - CXSecurity.com
G
GRAHAM CLULEY
S
Schneier on Security

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work Top 15 Reinforcement Learning Questions That Will Appear in Exams The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain
Sai · 2026-04-18 · via DEV Community

Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain

Every Nostr tutorial I read while wiring up my own agent's publisher loop wanted me to install some variant of nostr-sdk, python-nostr, nostr-tools, or a Rust crate wrapped in maturin. A few of those packages had half-broken BIP340 paths. A couple pinned to cryptography libraries that fight with pip install on Windows. The best of them — nostr-sdk — is genuinely great, but it is 40 MB of transitive dependencies for what is, at the end of the day, one SHA-256, one Schnorr signature, and a websocket send().

So I did what a lot of devs do when the ecosystem feels heavier than the problem: I opened the NIP-01 spec and wrote it myself. The whole publisher — keypair derivation, canonical event serialization, BIP340 signing, and fan-out to six relays — is 60 lines of Python and two dependencies you already have if you do any Bitcoin or Solana work: coincurve and websocket-client.

This post is the full working code, the exact traps I hit (coincurve's API is not what the docs front page suggests), and the reason Nostr's BIP340 choice actually makes this simpler than the average "sign a JWT" example you'll see on the average SaaS blog.

The wider context — an autonomous signal engine that publishes its own research to X, Mastodon, and Nostr — lives at cipher-starter and cipher-x402. The full publisher script this article is extracted from is in github.com/cryptomotifs/cipher-x402.

Why BIP340 Schnorr and not ECDSA

The Bitcoin ecosystem shipped BIP340 in 2020 as the standard Schnorr signature scheme for secp256k1. Nostr adopted it verbatim in NIP-01: a Nostr pubkey is an x-only 32-byte secp256k1 point, and a Nostr sig is a 64-byte BIP340 Schnorr signature over the SHA-256 of a canonical serialization of the event.

The practical consequence for a Python dev is: you do not want cryptography, you do not want ecdsa, you do not want pyca. Those all implement ECDSA, not BIP340 Schnorr, and BIP340 has non-trivial differences (deterministic nonce via auxiliary randomness, x-only keys with even-y normalization, tagged-hash domain separation). You want a library that wraps libsecp256k1 directly, because libsecp256k1 has a hardened, constant-time BIP340 implementation shipped by the Bitcoin Core team.

That library, in Python, is coincurve. As of 21.0.0 (October 2025 release) it exposes PrivateKey.sign_schnorr and PublicKey.verify_schnorr and both are thin wrappers over secp256k1_schnorrsig_sign32 / secp256k1_schnorrsig_verify in libsecp256k1.

pip install coincurve==21.0.0 websocket-client==1.8.0

That is the entire dependency surface. No Rust toolchain. No maturin. On Windows it installs a prebuilt wheel; on Linux it compiles libsecp256k1 in under ten seconds.

Generate or load a keypair

Nostr private keys are 32 random bytes. Public keys are 32 bytes — the x-coordinate of the secp256k1 point, with the y-coordinate normalized to even. coincurve.PrivateKey gives you the full point; you need to call .public_key.format(compressed=True)[1:] to drop the 0x02/0x03 prefix and keep only the 32 x-bytes, then verify the prefix was 0x02. If it was 0x03 (odd y), BIP340 says the private key must be negated before signing. Luckily coincurve.PrivateKey.sign_schnorr already handles that internally — it does the even-y normalization for you. But the pubkey you publish must still be the x-only form derived from the even-y point.

# nostr_keys.py
import os
from coincurve import PrivateKey

def new_keypair() -> tuple[str, str]:
    """Return (privkey_hex, pubkey_hex_xonly) — both 64-char lowercase hex."""
    sk = PrivateKey(os.urandom(32))
    sk_hex = sk.secret.hex()
    # x-only pubkey: strip the 0x02/0x03 byte from the compressed form
    pk_xonly = sk.public_key.format(compressed=True)[1:]
    return sk_hex, pk_xonly.hex()

def load_keypair(privkey_hex: str) -> tuple[PrivateKey, str]:
    sk = PrivateKey(bytes.fromhex(privkey_hex.strip()))
    pk_xonly = sk.public_key.format(compressed=True)[1:]
    return sk, pk_xonly.hex()

The trap I hit here: I originally did sk.public_key.format(compressed=False)[1:33] thinking "uncompressed minus the 0x04 prefix, first 32 bytes". That gives you the x-coordinate regardless of y-parity. But the signer is going to normalize the internal representation to even-y and then sign. If your stored x-only pubkey came from the odd-y point, the resulting signature is still valid (because BIP340 is x-only on the verification side too) — but you now have two different pubkeys floating around, and relays will reject note.pubkey != serialized_pubkey. Always derive the published pubkey from the compressed form and strip byte 0, because that path is consistent with what libsecp256k1 itself does during signing.

Canonical serialization — the one rule everyone gets wrong

NIP-01 specifies that the event id is the SHA-256 of a UTF-8 JSON array with a very specific shape:

[0, <pubkey_hex>, <created_at>, <kind>, <tags>, <content>]

  • No whitespace between elements.
  • Unicode escape sequences only for characters below 0x20 and the five mandatory escapes (\n, \", \\, \r, \t, \b, \f).
  • Everything above 0x20 — including non-ASCII — goes through as UTF-8 bytes, not \uXXXX.

Python's json.dumps by default does the opposite: it escapes all non-ASCII to \uXXXX. If you serialize with ensure_ascii=True (the default) and the content contains emoji, your event id is wrong and every relay silently drops the message.

import json

def serialize(pubkey_hex: str, created_at: int, kind: int, tags: list, content: str) -> bytes:
    payload = [0, pubkey_hex, created_at, kind, tags, content]
    return json.dumps(
        payload,
        ensure_ascii=False,
        separators=(",", ":"),
        sort_keys=False,
    ).encode("utf-8")

The separators=(",", ":") kills the spaces that json.dumps inserts by default. The ensure_ascii=False is the load-bearing flag — without it, your 🚀 becomes \ud83d\ude80 in the serialized form and the relay's own SHA-256 over the received JSON no longer matches the id you sent.

Sign the event

With the canonical bytes in hand, the id is just SHA-256 of those bytes, and the signature is BIP340 Schnorr over the same bytes.

import hashlib
import time
import uuid

def build_event(sk, pubkey_hex: str, kind: int, content: str, tags=None) -> dict:
    tags = tags or []
    created_at = int(time.time())
    serialized = serialize(pubkey_hex, created_at, kind, tags, content)
    event_id = hashlib.sha256(serialized).digest()
    sig = sk.sign_schnorr(event_id)  # 64 bytes, deterministic + aux-rand internally
    return {
        "id": event_id.hex(),
        "pubkey": pubkey_hex,
        "created_at": created_at,
        "kind": kind,
        "tags": tags,
        "content": content,
        "sig": sig.hex(),
    }

That is it. coincurve.PrivateKey.sign_schnorr(msg32) takes exactly 32 bytes (the hash, not the preimage), pulls 32 bytes of auxiliary randomness from the OS, and returns 64 bytes of BIP340 signature. The auxiliary randomness is part of the spec — BIP340 signatures are not purely deterministic, they are "synthetic nonce" — but libsecp256k1 handles that under the hood.

Publish to relays over websockets

Nostr relays speak a tiny JSON-over-WS protocol. To publish, you open a websocket, send ["EVENT", <event>], and wait for an ["OK", <event_id>, <accepted>, <message>] frame. Most relays ack within a second; some will close the connection without acking if they disagree with your event (rate limit, spam filter, proof-of-work requirement). You do not want your publisher to hang on a dead relay, so set a small timeout and fire-and-forget the rest.

import json
import websocket

RELAYS = [
    "wss://relay.damus.io",
    "wss://nos.lol",
    "wss://nostr.mom",
    "wss://nostr-pub.wellorder.net",
    "wss://relay.primal.net",
    "wss://offchain.pub",
    "wss://relay.snort.social",
]

def publish(event: dict, timeout: float = 4.0) -> list[tuple[str, bool]]:
    results = []
    payload = json.dumps(["EVENT", event])
    for url in RELAYS:
        try:
            ws = websocket.create_connection(url, timeout=timeout)
            ws.send(payload)
            # Optional: read one frame to confirm OK, but do not block the loop on it
            try:
                resp = ws.recv()
                ok = '"OK"' in resp and event["id"] in resp and "true" in resp.lower()
            except Exception:
                ok = True  # relay silently accepted
            ws.close()
            results.append((url, ok))
        except Exception as e:
            results.append((url, False))
    return results

In production I run the loop in a thread pool so the six relays go in parallel — shaves ~20 seconds when one relay is slow. A sequential version is fine for hourly cron.

The full 60-line publisher

Putting it all together, this is what the autonomous agent actually ships to production:

# nostr_publish.py — full working publisher in ~60 lines
import hashlib
import json
import os
import time
import websocket
from coincurve import PrivateKey

RELAYS = [
    "wss://relay.damus.io", "wss://nos.lol", "wss://nostr.mom",
    "wss://nostr-pub.wellorder.net", "wss://relay.primal.net",
    "wss://offchain.pub", "wss://relay.snort.social",
]

def load(privkey_hex):
    sk = PrivateKey(bytes.fromhex(privkey_hex.strip()))
    pk = sk.public_key.format(compressed=True)[1:].hex()
    return sk, pk

def serialize(pk, ts, kind, tags, content):
    return json.dumps(
        [0, pk, ts, kind, tags, content],
        ensure_ascii=False,
        separators=(",", ":"),
    ).encode("utf-8")

def build(sk, pk, content, kind=1, tags=None):
    tags = tags or []
    ts = int(time.time())
    msg = serialize(pk, ts, kind, tags, content)
    eid = hashlib.sha256(msg).digest()
    sig = sk.sign_schnorr(eid)
    return {
        "id": eid.hex(), "pubkey": pk, "created_at": ts,
        "kind": kind, "tags": tags, "content": content, "sig": sig.hex(),
    }

def publish(event, relays=RELAYS, timeout=4.0):
    payload = json.dumps(["EVENT", event])
    ok_count = 0
    for url in relays:
        try:
            ws = websocket.create_connection(url, timeout=timeout)
            ws.send(payload)
            try:
                resp = ws.recv()
                if '"OK"' in resp and event["id"] in resp:
                    ok_count += 1
            except Exception:
                ok_count += 1
            ws.close()
        except Exception:
            pass
    return ok_count

if __name__ == "__main__":
    sk_hex = open(os.environ["NOSTR_KEY_PATH"]).read().strip()
    sk, pk = load(sk_hex)
    evt = build(sk, pk, "hello from 60 lines of python")
    n = publish(evt)
    print(f"event_id={evt['id']} relays_ok={n}")

That is the whole thing. No Rust. No 40 MB dependency tree. One secp256k1 operation, one SHA-256, one websocket send.

Gotchas worth memorizing

The even-y trap. I already mentioned it — derive your x-only pubkey from public_key.format(compressed=True)[1:], not from the uncompressed first-32-bytes slice. Otherwise your pubkey field does not match what libsecp256k1 uses internally for signing.

ensure_ascii=False is mandatory. If you ever post content with emoji, accented characters, CJK, or the rupee symbol, the default json.dumps output will hash to the wrong id and every relay will reject the event with "bad signature" — even though your signature is perfectly valid. It is the event id that is wrong. Spend ten seconds adding the flag.

Relay timeouts. Some relays are chatty (Damus, Primal, Snort all reliably ack in <500 ms). Some are quiet. Some have stealth-banned specific pubkeys and will accept the socket but never ack. Do not put a 30-second read timeout in your loop — 3-5 seconds is plenty, and "no ack" is fine because other relays will carry the event.

Rate limits and PoW. Damus drops you after ~10 events per 10 seconds per IP — spread posts across relays or back off. A handful of relays (not in the list above) require NIP-13 proof-of-work; add a nonce tag mined until the id has N leading zero bits. For the seven relays listed above, no PoW is required.

Verify your own signature locally

If a relay rejects your event for "bad sig", the debugging path is to verify the signature locally against your own pubkey before you even open the websocket. That takes two lines:

from coincurve import PublicKey
PublicKey.from_xonly(bytes.fromhex(event["pubkey"])).verify_schnorr(
    bytes.fromhex(event["sig"]),
    bytes.fromhex(event["id"]),
)

If that returns True, the event is structurally correct and any relay rejection is a policy decision (rate limit, PoW, pubkey ban), not a cryptographic one. If it returns False, your serialization is wrong — almost certainly the ensure_ascii=False flag or a whitespace issue in separators.

Why this matters for anyone running autonomous agents

The CIPHER signal engine I am building — see cipher-x402 and the open playbook at cipher-starter — posts its own research to Nostr on a schedule. Nostr is a good fit for autonomous publishers specifically because there is no signup, no email verification, no CAPTCHA, no TOS. You generate a keypair, you publish. It is what email would have been if email had been invented by someone who hated spam as much as developers do.

If you are wiring up your own autonomous poster and you hit the same "which Nostr library is not abandoned this week" problem I hit, copy the 60 lines above. They work today on Python 3.11+ on Windows, macOS, and Linux, with nothing but coincurve and websocket-client. They will still work in five years because BIP340 is not changing and NIP-01 is a frozen spec.

Shipping an autonomous publisher on top of a properly specced protocol feels like what web development is supposed to feel like. No SDK, no platform, no 15% cut — just a hash, a signature, and a socket.


Sai (cryptomotifs) builds autonomous signal engines. The full publisher is open-source at github.com/cryptomotifs/cipher-x402. The broader solo-dev playbook is at cipher-starter and the live product at cipher-x402.vercel.app.