From ba49d3289db3895ef4ca846ce2cc9cfa3f1ab98a Mon Sep 17 00:00:00 2001 From: Kashiko Koibumi Date: Sun, 26 May 2024 12:05:31 +0900 Subject: [PATCH] use binary literals for compatibility to Python3 --- src/bitmessageqt/bitmessage_icons_rc.py | 6 +++--- src/class_objectProcessor.py | 18 ++++++++-------- src/class_singleWorker.py | 28 ++++++++++++------------- src/helper_bitcoin.py | 12 +++++------ src/network/bmproto.py | 18 ++++++++-------- src/network/connectionpool.py | 2 +- src/network/downloadthread.py | 2 +- src/network/invthread.py | 8 +++---- src/network/tcp.py | 2 +- src/network/tls.py | 2 +- src/network/uploadthread.py | 2 +- src/protocol.py | 6 +++--- 12 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/bitmessageqt/bitmessage_icons_rc.py b/src/bitmessageqt/bitmessage_icons_rc.py index bb0a02c0..771fc947 100644 --- a/src/bitmessageqt/bitmessage_icons_rc.py +++ b/src/bitmessageqt/bitmessage_icons_rc.py @@ -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\ @@ -1534,7 +1534,7 @@ qt_resource_data = "\ \x82\ " -qt_resource_name = "\ +qt_resource_name = b"\ \x00\x09\ \x0c\x78\x54\x88\ \x00\x6e\ @@ -1639,7 +1639,7 @@ qt_resource_name = "\ \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\ diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index f9feb183..c3c2ce74 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -299,12 +299,12 @@ class objectProcessor(threading.Thread): '(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' @@ -350,9 +350,9 @@ class objectProcessor(threading.Thread): ' 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] @@ -507,9 +507,9 @@ class objectProcessor(threading.Thread): 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 = \ @@ -854,10 +854,10 @@ class objectProcessor(threading.Thread): ) 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: @@ -1047,7 +1047,7 @@ class objectProcessor(threading.Thread): 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 diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index 4f080fa8..d8f73f46 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -106,7 +106,7 @@ class singleWorker(StoppableThread): 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' ''', @@ -262,7 +262,7 @@ class singleWorker(StoppableThread): 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). @@ -339,7 +339,7 @@ class singleWorker(StoppableThread): # 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). @@ -414,7 +414,7 @@ class singleWorker(StoppableThread): 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) @@ -600,7 +600,7 @@ class singleWorker(StoppableThread): 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 @@ -616,7 +616,7 @@ class singleWorker(StoppableThread): tag = doubleHashOfAddressData[32:] payload += tag else: - tag = '' + tag = b'' dataToEncrypt = encodeVarint(addressVersionNumber) dataToEncrypt += encodeVarint(streamNumber) @@ -789,7 +789,7 @@ class singleWorker(StoppableThread): # 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) @@ -1201,14 +1201,14 @@ class singleWorker(StoppableThread): '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 @@ -1217,7 +1217,7 @@ class singleWorker(StoppableThread): 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) @@ -1227,7 +1227,7 @@ class singleWorker(StoppableThread): # 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") @@ -1247,7 +1247,7 @@ class singleWorker(StoppableThread): 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 / ( @@ -1429,7 +1429,7 @@ class singleWorker(StoppableThread): 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: @@ -1511,4 +1511,4 @@ class singleWorker(StoppableThread): payload = self._doPOWDefaults( payload, TTL, log_prefix='(For ack message)', log_time=True) - return protocol.CreatePacket('object', payload) + return protocol.CreatePacket(b'object', payload) diff --git a/src/helper_bitcoin.py b/src/helper_bitcoin.py index d4f1d105..a51eaf9c 100644 --- a/src/helper_bitcoin.py +++ b/src/helper_bitcoin.py @@ -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): @@ -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 diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 8e49935b..ecd5e27e 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -82,7 +82,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): """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) @@ -107,7 +107,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): 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) @@ -168,14 +168,14 @@ class BMProto(AdvancedDispatcher, ObjectTracker): """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])) @@ -477,7 +477,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): 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 @@ -531,12 +531,12 @@ class BMProto(AdvancedDispatcher, ObjectTracker): 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, diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index 11240506..fcac9e7e 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -381,7 +381,7 @@ class BMConnectionPool(object): 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) diff --git a/src/network/downloadthread.py b/src/network/downloadthread.py index 38b1c432..cffa8694 100644 --- a/src/network/downloadthread.py +++ b/src/network/downloadthread.py @@ -72,7 +72,7 @@ class DownloadThread(StoppableThread): 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) diff --git a/src/network/invthread.py b/src/network/invthread.py index 82a34e13..a7830a8c 100644 --- a/src/network/invthread.py +++ b/src/network/invthread.py @@ -90,15 +90,15 @@ class InvThread(StoppableThread): 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)): diff --git a/src/network/tcp.py b/src/network/tcp.py index 9a01920a..0ef719ec 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -221,7 +221,7 @@ class TCPConnection(BMProto, TLSDispatcher): '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 = {} diff --git a/src/network/tls.py b/src/network/tls.py index a3774b44..7d76c48e 100644 --- a/src/network/tls.py +++ b/src/network/tls.py @@ -34,7 +34,7 @@ else: # 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: diff --git a/src/network/uploadthread.py b/src/network/uploadthread.py index 96794769..7290d139 100644 --- a/src/network/uploadthread.py +++ b/src/network/uploadthread.py @@ -49,7 +49,7 @@ class UploadThread(StoppableThread): break try: payload.extend(protocol.CreatePacket( - 'object', state.Inventory[chunk].payload)) + b'object', state.Inventory[chunk].payload)) chunk_count += 1 except KeyError: i.antiIntersectionDelay() diff --git a/src/protocol.py b/src/protocol.py index e300b97a..d43931eb 100644 --- a/src/protocol.py +++ b/src/protocol.py @@ -173,7 +173,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 @@ -512,9 +512,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]