sub.py (928B)
1 # sub.py 2 import sys 3 import bech32 4 import coincurve 5 import json 6 import hashlib 7 import string 8 import os 9 10 # parse public key from input 11 if len(sys.argv) == 1: 12 raise ValueError("need single argument") 13 r = bech32.bech32_decode(sys.argv[1]) 14 if r[0] == None: 15 raise ValueError("Invalid npub key {}".format(sys.argv[1])) 16 17 # Can't use bech32.decode() directly because there is a data length check there that will always fail with nostr public keys. 18 # I guess the module was written with 160 bit bitcoin addresses in mind. 19 # So we manually have to convert the bits as in the function. 20 pubk_bytes = bytes(r[1]) 21 pubk_v = bech32.convertbits(pubk_bytes, 5, 8)[:-1] 22 pubk_bytes = bytes(pubk_v) 23 pubk_object = coincurve.PublicKeyXOnly(pubk_bytes) 24 pubk = pubk_object.format().hex() 25 26 sub = os.urandom(32).hex() 27 cmd = [ 28 "REQ", 29 sub, 30 { 31 "authors": [pubk], 32 "kinds": [1], 33 } 34 ] 35 print(json.dumps(cmd)) 36 37