Skip to content

Use binary literals for compatibility to Python3 #2241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v0.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/bitmessageqt/bitmessage_icons_rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from PyQt4 import QtCore

qt_resource_data = "\
qt_resource_data = b"\
\x00\x00\x03\x66\
\x89\
\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\
Expand Down Expand Up @@ -1534,7 +1534,7 @@
\x82\
"

qt_resource_name = "\
qt_resource_name = b"\
\x00\x09\
\x0c\x78\x54\x88\
\x00\x6e\
Expand Down Expand Up @@ -1639,7 +1639,7 @@
\x00\x70\x00\x6e\x00\x67\
"

qt_resource_struct = "\
qt_resource_struct = b"\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\
\x00\x00\x00\x18\x00\x02\x00\x00\x00\x15\x00\x00\x00\x03\
Expand Down
18 changes: 9 additions & 9 deletions src/class_objectProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ def processpubkey(self, data):
'(within processpubkey) payloadLength less than 146.'
' Sanity check failed.')
readPosition += 4
pubSigningKey = '\x04' + data[readPosition:readPosition + 64]
pubSigningKey = b'\x04' + data[readPosition:readPosition + 64]
# Is it possible for a public key to be invalid such that trying to
# encrypt or sign with it will cause an error? If it is, it would
# be easiest to test them here.
readPosition += 64
pubEncryptionKey = '\x04' + data[readPosition:readPosition + 64]
pubEncryptionKey = b'\x04' + data[readPosition:readPosition + 64]
if len(pubEncryptionKey) < 65:
return logger.debug(
'publicEncryptionKey length less than 64. Sanity check'
Expand Down Expand Up @@ -350,9 +350,9 @@ def processpubkey(self, data):
' Sanity check failed.')
return
readPosition += 4
pubSigningKey = '\x04' + data[readPosition:readPosition + 64]
pubSigningKey = b'\x04' + data[readPosition:readPosition + 64]
readPosition += 64
pubEncryptionKey = '\x04' + data[readPosition:readPosition + 64]
pubEncryptionKey = b'\x04' + data[readPosition:readPosition + 64]
readPosition += 64
specifiedNonceTrialsPerByteLength = decodeVarint(
data[readPosition:readPosition + 10])[1]
Expand Down Expand Up @@ -507,9 +507,9 @@ def processmsg(self, data):
return
readPosition += sendersStreamNumberLength
readPosition += 4
pubSigningKey = '\x04' + decryptedData[readPosition:readPosition + 64]
pubSigningKey = b'\x04' + decryptedData[readPosition:readPosition + 64]
readPosition += 64
pubEncryptionKey = '\x04' + decryptedData[readPosition:readPosition + 64]
pubEncryptionKey = b'\x04' + decryptedData[readPosition:readPosition + 64]
readPosition += 64
if sendersAddressVersionNumber >= 3:
requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = \
Expand Down Expand Up @@ -854,10 +854,10 @@ def processbroadcast(self, data):
)
readPosition += sendersStreamLength
readPosition += 4
sendersPubSigningKey = '\x04' + \
sendersPubSigningKey = b'\x04' + \
decryptedData[readPosition:readPosition + 64]
readPosition += 64
sendersPubEncryptionKey = '\x04' + \
sendersPubEncryptionKey = b'\x04' + \
decryptedData[readPosition:readPosition + 64]
readPosition += 64
if sendersAddressVersion >= 3:
Expand Down Expand Up @@ -1047,7 +1047,7 @@ def ackDataHasAValidHeader(ackData):
if checksum != hashlib.sha512(payload).digest()[0:4]:
logger.info('ackdata checksum wrong. Not sending ackdata.')
return False
command = command.rstrip('\x00')
command = command.rstrip(b'\x00')
if command != 'object':
return False
return True
Expand Down
28 changes: 14 additions & 14 deletions src/class_singleWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def run(self):
for oldack in state.ackdataForWhichImWatching:
if len(oldack) == 32:
# attach legacy header, always constant (msg/1/1)
newack = '\x00\x00\x00\x02\x01\x01' + oldack
newack = b'\x00\x00\x00\x02\x01\x01' + oldack
state.ackdataForWhichImWatching[newack] = 0
sqlExecute(
'''UPDATE sent SET ackdata=? WHERE ackdata=? AND folder = 'sent' ''',
Expand Down Expand Up @@ -261,7 +261,7 @@ def doPOWForMyV2Pubkey(self, adressHash):
TTL = int(28 * 24 * 60 * 60 + helper_random.randomrandrange(-300, 300))
embeddedTime = int(time.time() + TTL)
payload = pack('>Q', (embeddedTime))
payload += '\x00\x00\x00\x01' # object type: pubkey
payload += b'\x00\x00\x00\x01' # object type: pubkey
payload += encodeVarint(addressVersionNumber) # Address version number
payload += encodeVarint(streamNumber)
# bitfield of features supported by me (see the wiki).
Expand Down Expand Up @@ -338,7 +338,7 @@ def sendOutOrStoreMyV3Pubkey(self, adressHash):
# expiresTime time.

payload = pack('>Q', (embeddedTime))
payload += '\x00\x00\x00\x01' # object type: pubkey
payload += b'\x00\x00\x00\x01' # object type: pubkey
payload += encodeVarint(addressVersionNumber) # Address version number
payload += encodeVarint(streamNumber)
# bitfield of features supported by me (see the wiki).
Expand Down Expand Up @@ -413,7 +413,7 @@ def sendOutOrStoreMyV4Pubkey(self, myAddress):
TTL = int(28 * 24 * 60 * 60 + helper_random.randomrandrange(-300, 300))
embeddedTime = int(time.time() + TTL)
payload = pack('>Q', (embeddedTime))
payload += '\x00\x00\x00\x01' # object type: pubkey
payload += b'\x00\x00\x00\x01' # object type: pubkey
payload += encodeVarint(addressVersionNumber) # Address version number
payload += encodeVarint(streamNumber)
dataToEncrypt = protocol.getBitfield(myAddress)
Expand Down Expand Up @@ -599,7 +599,7 @@ def sendBroadcast(self):
TTL = int(TTL + helper_random.randomrandrange(-300, 300))
embeddedTime = int(time.time() + TTL)
payload = pack('>Q', embeddedTime)
payload += '\x00\x00\x00\x03' # object type: broadcast
payload += b'\x00\x00\x00\x03' # object type: broadcast

if addressVersionNumber <= 3:
payload += encodeVarint(4) # broadcast version
Expand All @@ -615,7 +615,7 @@ def sendBroadcast(self):
tag = doubleHashOfAddressData[32:]
payload += tag
else:
tag = ''
tag = b''

dataToEncrypt = encodeVarint(addressVersionNumber)
dataToEncrypt += encodeVarint(streamNumber)
Expand Down Expand Up @@ -788,7 +788,7 @@ def sendMsg(self):
# We don't have the needed pubkey in the pubkeys table already.
else:
if toAddressVersionNumber <= 3:
toTag = ''
toTag = b''
else:
toTag = highlevelcrypto.double_sha512(
encodeVarint(toAddressVersionNumber)
Expand Down Expand Up @@ -1201,14 +1201,14 @@ def sendMsg(self):
'Not bothering to include ackdata because we are'
' sending to ourselves or a chan.'
)
fullAckPayload = ''
fullAckPayload = b''
elif not protocol.checkBitfield(
behaviorBitfield, protocol.BITFIELD_DOESACK):
self.logger.info(
'Not bothering to include ackdata because'
' the receiver said that they won\'t relay it anyway.'
)
fullAckPayload = ''
fullAckPayload = b''
else:
# The fullAckPayload is a normal msg protocol message
# with the proof of work already completed that the
Expand All @@ -1217,7 +1217,7 @@ def sendMsg(self):
ackdata, toStreamNumber, TTL)
payload += encodeVarint(len(fullAckPayload))
payload += fullAckPayload
dataToSign = pack('>Q', embeddedTime) + '\x00\x00\x00\x02' + \
dataToSign = pack('>Q', embeddedTime) + b'\x00\x00\x00\x02' + \
encodeVarint(1) + encodeVarint(toStreamNumber) + payload
signature = highlevelcrypto.sign(
dataToSign, privSigningKeyHex, self.digestAlg)
Expand All @@ -1227,7 +1227,7 @@ def sendMsg(self):
# We have assembled the data that will be encrypted.
try:
encrypted = highlevelcrypto.encrypt(
payload, "04" + hexlify(pubEncryptionKeyBase256)
payload, b"04" + hexlify(pubEncryptionKeyBase256)
)
except: # noqa:E722
self.logger.warning("highlevelcrypto.encrypt didn't work")
Expand All @@ -1247,7 +1247,7 @@ def sendMsg(self):
continue

encryptedPayload = pack('>Q', embeddedTime)
encryptedPayload += '\x00\x00\x00\x02' # object type: msg
encryptedPayload += b'\x00\x00\x00\x02' # object type: msg
encryptedPayload += encodeVarint(1) # msg version
encryptedPayload += encodeVarint(toStreamNumber) + encrypted
target = 2 ** 64 / (
Expand Down Expand Up @@ -1429,7 +1429,7 @@ def requestPubKey(self, toAddress):
TTL = TTL + helper_random.randomrandrange(-300, 300)
embeddedTime = int(time.time() + TTL)
payload = pack('>Q', embeddedTime)
payload += '\x00\x00\x00\x00' # object type: getpubkey
payload += b'\x00\x00\x00\x00' # object type: getpubkey
payload += encodeVarint(addressVersionNumber)
payload += encodeVarint(streamNumber)
if addressVersionNumber <= 3:
Expand Down Expand Up @@ -1511,4 +1511,4 @@ def generateFullAckMessage(self, ackdata, _, TTL):
payload = self._doPOWDefaults(
payload, TTL, log_prefix='(For ack message)', log_time=True)

return protocol.CreatePacket('object', payload)
return protocol.CreatePacket(b'object', payload)
12 changes: 6 additions & 6 deletions src/helper_bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ def calculateBitcoinAddressFromPubkey(pubkey):
sha = hashlib.new('sha256')
sha.update(pubkey)
ripe.update(sha.digest())
ripeWithProdnetPrefix = '\x00' + ripe.digest()
ripeWithProdnetPrefix = b'\x00' + ripe.digest()

checksum = hashlib.sha256(hashlib.sha256(
ripeWithProdnetPrefix).digest()).digest()[:4]
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
numberOfZeroBytesOnBinaryBitcoinAddress = 0
while binaryBitcoinAddress[0] == '\x00':
while binaryBitcoinAddress[0] == b'\x00':
numberOfZeroBytesOnBinaryBitcoinAddress += 1
binaryBitcoinAddress = binaryBitcoinAddress[1:]
base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58)
return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
return b"1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded


def calculateTestnetAddressFromPubkey(pubkey):
Expand All @@ -43,14 +43,14 @@ def calculateTestnetAddressFromPubkey(pubkey):
sha = hashlib.new('sha256')
sha.update(pubkey)
ripe.update(sha.digest())
ripeWithProdnetPrefix = '\x6F' + ripe.digest()
ripeWithProdnetPrefix = b'\x6F' + ripe.digest()

checksum = hashlib.sha256(hashlib.sha256(
ripeWithProdnetPrefix).digest()).digest()[:4]
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
numberOfZeroBytesOnBinaryBitcoinAddress = 0
while binaryBitcoinAddress[0] == '\x00':
while binaryBitcoinAddress[0] == b'\x00':
numberOfZeroBytesOnBinaryBitcoinAddress += 1
binaryBitcoinAddress = binaryBitcoinAddress[1:]
base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58)
return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
return b"1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
18 changes: 9 additions & 9 deletions src/network/bmproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def state_bm_header(self):
"""Process incoming header"""
self.magic, self.command, self.payloadLength, self.checksum = \
protocol.Header.unpack(self.read_buf[:protocol.Header.size])
self.command = self.command.rstrip('\x00')
self.command = self.command.rstrip(b'\x00')
if self.magic != protocol.magic:
# skip 1 byte in order to sync
self.set_state("bm_header", length=1)
Expand All @@ -107,7 +107,7 @@ def state_bm_command(self): # pylint: disable=too-many-branches
self.invalid = True
retval = True
if not self.fullyEstablished and self.command not in (
"error", "version", "verack"):
b"error", b"version", b"verack"):
logger.error(
'Received command %s before connection was fully'
' established, ignoring', self.command)
Expand Down Expand Up @@ -168,14 +168,14 @@ def decode_payload_node(self):
"""Decode node details from the payload"""
# protocol.checkIPAddress()
services, host, port = self.decode_payload_content("Q16sH")
if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
if host[0:12] == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
host = socket.inet_ntop(socket.AF_INET, str(host[12:16]))
elif host[0:6] == '\xfd\x87\xd8\x7e\xeb\x43':
elif host[0:6] == b'\xfd\x87\xd8\x7e\xeb\x43':
# Onion, based on BMD/bitcoind
host = base64.b32encode(host[6:]).lower() + ".onion"
host = base64.b32encode(host[6:]).lower() + b".onion"
else:
host = socket.inet_ntop(socket.AF_INET6, str(host))
if host == "":
if host == b"":
# This can happen on Windows systems which are not 64-bit
# compatible so let us drop the IPv6 address.
host = socket.inet_ntop(socket.AF_INET, str(host[12:16]))
Expand Down Expand Up @@ -477,7 +477,7 @@ def bm_command_portcheck(self):

def bm_command_ping(self):
"""Incoming ping, respond to it."""
self.append_write_buf(protocol.CreatePacket('pong'))
self.append_write_buf(protocol.CreatePacket(b'pong'))
return True

@staticmethod
Expand Down Expand Up @@ -531,12 +531,12 @@ def bm_command_version(self):
if not self.peerValidityChecks():
# ABORT afterwards
return True
self.append_write_buf(protocol.CreatePacket('verack'))
self.append_write_buf(protocol.CreatePacket(b'verack'))
self.verackSent = True
ua_valid = re.match(
r'^/[a-zA-Z]+:[0-9]+\.?[\w\s\(\)\./:;-]*/$', self.userAgent)
if not ua_valid:
self.userAgent = '/INVALID:0/'
self.userAgent = b'/INVALID:0/'
if not self.isOutbound:
self.append_write_buf(protocol.assembleVersionMessage(
self.destination.host, self.destination.port,
Expand Down
2 changes: 1 addition & 1 deletion src/network/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def loop(self): # pylint: disable=too-many-branches,too-many-statements
minTx -= 300 - 20
if i.lastTx < minTx:
if i.fullyEstablished:
i.append_write_buf(protocol.CreatePacket('ping'))
i.append_write_buf(protocol.CreatePacket(b'ping'))
else:
i.close_reason = "Timeout (%is)" % (
time.time() - i.lastTx)
Expand Down
2 changes: 1 addition & 1 deletion src/network/downloadthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def run(self):
if not chunkCount:
continue
payload[0:0] = addresses.encodeVarint(chunkCount)
i.append_write_buf(protocol.CreatePacket('getdata', payload))
i.append_write_buf(protocol.CreatePacket(b'getdata', payload))
self.logger.debug(
'%s:%i Requesting %i objects',
i.destination.host, i.destination.port, chunkCount)
Expand Down
8 changes: 4 additions & 4 deletions src/network/invthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ def run(self): # pylint: disable=too-many-branches
if fluffs:
random.shuffle(fluffs)
connection.append_write_buf(protocol.CreatePacket(
'inv',
b'inv',
addresses.encodeVarint(
len(fluffs)) + ''.join(fluffs)))
len(fluffs)) + b''.join(fluffs)))
if stems:
random.shuffle(stems)
connection.append_write_buf(protocol.CreatePacket(
'dinv',
b'dinv',
addresses.encodeVarint(
len(stems)) + ''.join(stems)))
len(stems)) + b''.join(stems)))

invQueue.iterate()
for _ in range(len(chunk)):
Expand Down
2 changes: 1 addition & 1 deletion src/network/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def sendChunk():
'Sending huge inv message with %i objects to just this'
' one peer', objectCount)
self.append_write_buf(protocol.CreatePacket(
'inv', addresses.encodeVarint(objectCount) + payload))
b'inv', addresses.encodeVarint(objectCount) + payload))

# Select all hashes for objects in this stream.
bigInvList = {}
Expand Down
2 changes: 1 addition & 1 deletion src/network/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
# ciphers
if (
ssl.OPENSSL_VERSION_NUMBER >= 0x10100000
and not ssl.OPENSSL_VERSION.startswith(b"LibreSSL")
and not ssl.OPENSSL_VERSION.startswith("LibreSSL")
):
sslProtocolCiphers = "AECDH-AES256-SHA@SECLEVEL=0"
else:
Expand Down
2 changes: 1 addition & 1 deletion src/network/uploadthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def run(self):
break
try:
payload.extend(protocol.CreatePacket(
'object', state.Inventory[chunk].payload))
b'object', state.Inventory[chunk].payload))
chunk_count += 1
except KeyError:
i.antiIntersectionDelay()
Expand Down
6 changes: 3 additions & 3 deletions src/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def checkIPAddress(host, private=False):
return checkIPv4Address(host[12:], hostStandardFormat, private)
elif host[0:6] == b'\xfd\x87\xd8\x7e\xeb\x43':
# Onion, based on BMD/bitcoind
hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion"
hostStandardFormat = base64.b32encode(host[6:]).lower() + b".onion"
if private:
return False
return hostStandardFormat
Expand Down Expand Up @@ -511,9 +511,9 @@ def decryptAndCheckPubkeyPayload(data, address):
readPosition = 0
# bitfieldBehaviors = decryptedData[readPosition:readPosition + 4]
readPosition += 4
pubSigningKey = '\x04' + decryptedData[readPosition:readPosition + 64]
pubSigningKey = b'\x04' + decryptedData[readPosition:readPosition + 64]
readPosition += 64
pubEncryptionKey = '\x04' + decryptedData[readPosition:readPosition + 64]
pubEncryptionKey = b'\x04' + decryptedData[readPosition:readPosition + 64]
readPosition += 64
specifiedNonceTrialsPerByteLength = decodeVarint(
decryptedData[readPosition:readPosition + 10])[1]
Expand Down