Modernize the entire customer-merchant communication layer from deprecated NIP-04 (kind 4, AES-256-CBC) to NIP-17 private direct messages using NIP-44 v2 encryption (ChaCha20 + HMAC-SHA256) and NIP-59 gift wrapping (rumor/seal/gift-wrap protocol). No backwards compatibility retained. New modules: - nostr/nip44.py: NIP-44 v2 encryption verified against official spec vectors - nostr/nip59.py: NIP-59 gift wrap with wrap/unwrap convenience functions - tests/: 44 unit tests for NIP-44 and NIP-59 Key changes: - Subscription filters: kind 4 → kind 1059 gift wraps - Message handler: _handle_nip04_message → _handle_gift_wrap (unwrap + route) - send_dm/reply_to_structured_dm: NIP-59 gift wrap to recipient + self-archive - Merchant model: removed NIP-04 crypto methods (decrypt/encrypt/build_dm_event) - helpers.py: removed NIP-04 functions, kept Schnorr signing + key normalization - views_api.py: consolidated DM sending through send_dm() service function Reliability improvements: - Event deduplication via bounded LRU set in NostrClient - Subscription health monitor (resubscribes after 120s of silence) - Preserved 5-minute lenient time window from prior work Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
26 lines
825 B
Python
26 lines
825 B
Python
import coincurve
|
|
from bech32 import bech32_decode, convertbits
|
|
|
|
|
|
def sign_message_hash(private_key: str, hash_: bytes) -> str:
|
|
privkey = coincurve.PrivateKey(bytes.fromhex(private_key))
|
|
sig = privkey.sign_schnorr(hash_)
|
|
return sig.hex()
|
|
|
|
|
|
def normalize_public_key(pubkey: str) -> str:
|
|
if pubkey.startswith("npub1"):
|
|
_, decoded_data = bech32_decode(pubkey)
|
|
if not decoded_data:
|
|
raise ValueError("Public Key is not valid npub")
|
|
|
|
decoded_data_bits = convertbits(decoded_data, 5, 8, False)
|
|
if not decoded_data_bits:
|
|
raise ValueError("Public Key is not valid npub")
|
|
return bytes(decoded_data_bits).hex()
|
|
|
|
# check if valid hex
|
|
if len(pubkey) != 64:
|
|
raise ValueError("Public Key is not valid hex")
|
|
int(pubkey, 16)
|
|
return pubkey
|