This repository has been archived on 2024-12-22. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-22/src/helper_ackPayload.py

43 lines
1.7 KiB
Python
Raw Normal View History

2017-09-30 11:19:44 +02:00
import highlevelcrypto
import helper_random
2018-04-06 12:51:29 +02:00
from binascii import hexlify
from struct import pack
2017-09-30 11:19:44 +02:00
from addresses import encodeVarint
# This function generates payload objects for message acknowledgements
2018-04-06 12:51:29 +02:00
# Several stealth levels are available depending on the privacy needs;
2017-09-30 11:19:44 +02:00
# 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
2018-04-06 12:51:29 +02:00
2017-09-30 11:19:44 +02:00
def genAckPayload(streamNumber=1, stealthLevel=0):
2018-04-06 12:51:29 +02:00
if (stealthLevel == 2): # Generate privacy-enhanced payload
2017-09-30 11:19:44 +02:00
# Generate a dummy privkey and derive the pubkey
2018-04-06 12:51:29 +02:00
dummyPubKeyHex = highlevelcrypto.privToPub(
hexlify(helper_random.randomBytes(32)))
2017-09-30 11:19:44 +02:00
# Generate a dummy message of random length
# (the smallest possible standard-formatted message is 234 bytes)
2018-04-06 12:51:29 +02:00
dummyMessage = helper_random.randomBytes(
helper_random.randomrandrange(234, 801))
2017-09-30 11:19:44 +02:00
# Encrypt the message using standard BM encryption (ECIES)
ackdata = highlevelcrypto.encrypt(dummyMessage, dummyPubKeyHex)
acktype = 2 # message
version = 1
2018-04-06 12:51:29 +02:00
elif (stealthLevel == 1): # Basic privacy payload (random getpubkey)
2017-09-30 11:19:44 +02: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 12:51:29 +02:00
ackobject = pack('>I', acktype) + encodeVarint(
version) + encodeVarint(streamNumber) + ackdata
2017-09-30 11:19:44 +02:00
return ackobject