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

52 lines
1.8 KiB
Python
Raw Normal View History

2019-10-07 10:08:26 +02:00
"""
This module is for generating ack payload
"""
2019-10-07 15:31:21 +02:00
2019-10-07 10:08:26 +02:00
from binascii import hexlify
from struct import pack
2018-04-06 14:42:57 +02:00
2017-09-30 11:19:44 +02:00
import helper_random
import highlevelcrypto
2017-09-30 11:19:44 +02:00
from addresses import encodeVarint
2018-04-06 12:51:29 +02:00
2017-09-30 11:19:44 +02:00
def genAckPayload(streamNumber=1, stealthLevel=0):
2019-10-07 15:31:21 +02: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 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
2019-10-07 15:31:21 +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