This repository has been archived on 2024-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-20/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
"""
2021-12-09 17:44:24 +01: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(
2021-12-09 17:44:24 +01:00
hexlify(highlevelcrypto.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)
2021-12-09 17:44:24 +01:00
dummyMessage = highlevelcrypto.randomBytes(
2018-04-06 12:51:29 +02:00
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
2021-12-09 17:44:24 +01:00
elif stealthLevel == 1: # Basic privacy payload (random getpubkey)
ackdata = highlevelcrypto.randomBytes(32)
2017-09-30 11:19:44 +02:00
acktype = 0 # getpubkey
version = 4
else: # Minimum viable payload (non stealth)
2021-12-09 17:44:24 +01:00
ackdata = highlevelcrypto.randomBytes(32)
2017-09-30 11:19:44 +02:00
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