PyBitmessage/src/helper_ackPayload.py

52 lines
1.8 KiB
Python
Raw Normal View History

2019-10-07 08:08:26 +00:00
"""
This module is for generating ack payload
"""
2019-10-07 13:31:21 +00:00
2019-10-07 08:08:26 +00:00
from binascii import hexlify
from struct import pack
2018-04-06 12:42:57 +00:00
2017-09-30 09:19:44 +00:00
import helper_random
import highlevelcrypto
2017-09-30 09:19:44 +00:00
from addresses import encodeVarint
2018-04-06 10:51:29 +00:00
2017-09-30 09:19:44 +00:00
def genAckPayload(streamNumber=1, stealthLevel=0):
2019-10-07 13:31:21 +00:00
"""
Generate and return payload obj.
This function generates payload objects for message acknowledgements
Several stealth levels are available depending on the privacy needs;
a higher level means better stealth, but also higher cost (size+POW)
- level 0: a random 32-byte sequence with a message header appended
- level 1: a getpubkey request for a (random) dummy key hash
- level 2: a standard message, encrypted to a random pubkey
"""
if stealthLevel == 2: # Generate privacy-enhanced payload
2017-09-30 09:19:44 +00:00
# Generate a dummy privkey and derive the pubkey
2018-04-06 10:51:29 +00:00
dummyPubKeyHex = highlevelcrypto.privToPub(
hexlify(helper_random.randomBytes(32)))
2017-09-30 09:19:44 +00:00
# Generate a dummy message of random length
# (the smallest possible standard-formatted message is 234 bytes)
2018-04-06 10:51:29 +00:00
dummyMessage = helper_random.randomBytes(
helper_random.randomrandrange(234, 801))
2017-09-30 09:19:44 +00:00
# Encrypt the message using standard BM encryption (ECIES)
ackdata = highlevelcrypto.encrypt(dummyMessage, dummyPubKeyHex)
acktype = 2 # message
version = 1
2019-10-07 13:31:21 +00:00
elif stealthLevel == 1: # Basic privacy payload (random getpubkey)
2017-09-30 09:19:44 +00:00
ackdata = helper_random.randomBytes(32)
acktype = 0 # getpubkey
version = 4
else: # Minimum viable payload (non stealth)
ackdata = helper_random.randomBytes(32)
acktype = 2 # message
version = 1
2018-04-06 10:51:29 +00:00
ackobject = pack('>I', acktype) + encodeVarint(
version) + encodeVarint(streamNumber) + ackdata
2017-09-30 09:19:44 +00:00
return ackobject