Skip to content

Instantly share code, notes, and snippets.

@kdmukai
Last active December 20, 2022 16:48
Show Gist options
  • Save kdmukai/86844202a7343470a801379b07872cfc to your computer and use it in GitHub Desktop.
Save kdmukai/86844202a7343470a801379b07872cfc to your computer and use it in GitHub Desktop.
Trying (and failing) to reproduce Nostr NIP-26 delegation signature
"""
see: https://github.com/nostr-protocol/nips/blob/master/26.md
"""
import hashlib
from binascii import unhexlify
from embit import bip39
from embit.bip32 import HDKey
from embit.ec import PrivateKey
msg = "nostr:delegation:62903b1ff41559daf9ee98ef1ae67cc52f301bb5ce26d14baba3052f649c3f49:kind=1&created_at>1640995200"
msg_hash = hashlib.sha256(msg.encode("utf-8")).digest()
target_sig = "c33c88ba78ec3c760e49db591ac5f7b129e3887c8af7729795e85a0588007e5ac89b46549232d8f918eefd73e726cb450135314bfda419c030d0b6affe401ec1"
# EDIT: Ah, here's the problem. This is NOT the delegator's privkey. It's the delegator's pubkey.
pk = "86f0689bd48dcd19c67a19d994f938ee34f251d8c39976290955ff585f2db42e"
# Try to sign with simplified ec objs
privkey = PrivateKey(unhexlify(pk))
sig = privkey.schnorr_sign(msg_hash)
if sig.to_string() != target_sig:
print(f"Incorrect signature:\n{sig.to_string()}\n{target_sig}")
# Try to load via bip39
mnemonic = bip39.mnemonic_from_bytes(unhexlify(pk))
root = HDKey.from_seed(bip39.mnemonic_to_seed(mnemonic))
# Try to sign with the root privkey
sig = root.schnorr_sign(msg_hash)
if sig.to_string() != target_sig:
print(f"Incorrect signature:\n{sig.to_string()}\n{target_sig}")
# Try to sign with the nostr privkey
nostr_derivation_path = """m/44'/1237'/0'/0/0"""
nostr_root = root.derive(nostr_derivation_path)
sig = nostr_root.schnorr_sign(msg_hash)
if sig.to_string() != target_sig:
print(f"Incorrect signature:\n{sig.to_string()}\n{target_sig}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment