helper_ackPayload.py
1 """ 2 This module is for generating ack payload 3 """ 4 5 from binascii import hexlify 6 from struct import pack 7 8 from . import helper_random 9 from . import highlevelcrypto 10 from .addresses import encodeVarint 11 12 13 def genAckPayload(streamNumber=1, stealthLevel=0): 14 """ 15 Generate and return payload obj. 16 17 This function generates payload objects for message acknowledgements 18 Several stealth levels are available depending on the privacy needs; 19 a higher level means better stealth, but also higher cost (size+POW) 20 21 - level 0: a random 32-byte sequence with a message header appended 22 - level 1: a getpubkey request for a (random) dummy key hash 23 - level 2: a standard message, encrypted to a random pubkey 24 """ 25 if stealthLevel == 2: # Generate privacy-enhanced payload 26 # Generate a dummy privkey and derive the pubkey 27 dummyPubKeyHex = highlevelcrypto.privToPub( 28 hexlify(highlevelcrypto.randomBytes(32))) 29 # Generate a dummy message of random length 30 # (the smallest possible standard-formatted message is 234 bytes) 31 dummyMessage = highlevelcrypto.randomBytes( 32 helper_random.randomrandrange(234, 801)) 33 # Encrypt the message using standard BM encryption (ECIES) 34 ackdata = highlevelcrypto.encrypt(dummyMessage, dummyPubKeyHex) 35 acktype = 2 # message 36 version = 1 37 38 elif stealthLevel == 1: # Basic privacy payload (random getpubkey) 39 ackdata = highlevelcrypto.randomBytes(32) 40 acktype = 0 # getpubkey 41 version = 4 42 43 else: # Minimum viable payload (non stealth) 44 ackdata = highlevelcrypto.randomBytes(32) 45 acktype = 2 # message 46 version = 1 47 48 ackobject = pack('>I', acktype) + encodeVarint( 49 version) + encodeVarint(streamNumber) + ackdata 50 51 return ackobject