manbytesgnu_site

Source files for manbytesgnu.org
git clone git://holbrook.no/manbytesgnu_site.git
Info | Log | Files | Refs

msg.py (1017B)


      1 # msg.py
      2 import coincurve
      3 import json
      4 import time
      5 import hashlib
      6 import string
      7 
      8 
      9 # generate secret for private key
     10 h = hashlib.sha256()
     11 h.update(b"foo")
     12 k = h.digest()
     13 
     14 # create private and public keys
     15 pk = coincurve.PrivateKey(k)
     16 pubk = pk.public_key_xonly.format().hex()
     17 
     18 # create the unsigned message data with the current timestamp
     19 t = int(time.time())
     20 msg = [
     21     0,
     22     pubk, 
     23     t,
     24     1,
     25     [],
     26     u"hello nostr \u00b6",
     27         ]
     28 v = json.dumps(msg, ensure_ascii=False, separators=(',',':'))
     29 
     30 # calculate the signature over the digest of the unsigned message
     31 h = hashlib.sha256()
     32 h.update(v.encode("utf-8"))
     33 z = h.digest()
     34 sig = pk.sign_schnorr(z)
     35 
     36 # assemble and output the message to be published, adding the digest and the signature
     37 payload = {
     38     "id": z.hex(),
     39     "pubkey": msg[1],
     40     "created_at": msg[2],
     41     "kind": msg[3],
     42     "tags": msg[4],
     43     "content": msg[5],
     44     "sig": sig.hex(),
     45         }
     46 cmd = [
     47         "EVENT",
     48         payload,
     49         ]
     50 print(json.dumps(cmd, ensure_ascii=False))