From 8f81c35a6f48d26d2562cd9e12adde95ebae40fe Mon Sep 17 00:00:00 2001 From: DivineOmega Date: Fri, 21 Jun 2013 10:10:13 +0100 Subject: [PATCH] Split off a few generic functions and Bitcoin related functions in seperate helper files --- src/bitmessagemain.py | 94 ++++--------------------------------------- src/helper_bitcoin.py | 43 ++++++++++++++++++++ src/helper_generic.py | 33 +++++++++++++++ 3 files changed, 84 insertions(+), 86 deletions(-) create mode 100644 src/helper_bitcoin.py create mode 100644 src/helper_generic.py diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 8e336fdc..7f30fc64 100644 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -58,6 +58,8 @@ import helper_startup import helper_bootstrap import helper_inbox import helper_sent +import helper_generic +import helper_bitcoin # For each stream to which we connect, several outgoingSynSender threads # will exist and will collectively create 8 connections with peers. @@ -1131,7 +1133,7 @@ class receiveDataThread(threading.Thread): print 'ECDSA verify failed', err return shared.printLock.acquire() - print 'As a matter of intellectual curiosity, here is the Bitcoin address associated with the keys owned by the other person:', calculateBitcoinAddressFromPubkey(pubSigningKey), ' ..and here is the testnet address:', calculateTestnetAddressFromPubkey(pubSigningKey), '. The other person must take their private signing key from Bitmessage and import it into Bitcoin (or a service like Blockchain.info) for it to be of any use. Do not use this unless you know what you are doing.' + print 'As a matter of intellectual curiosity, here is the Bitcoin address associated with the keys owned by the other person:', helper_bitcoin.calculateBitcoinAddressFromPubkey(pubSigningKey), ' ..and here is the testnet address:', helper_bitcoin.calculateTestnetAddressFromPubkey(pubSigningKey), '. The other person must take their private signing key from Bitmessage and import it into Bitcoin (or a service like Blockchain.info) for it to be of any use. Do not use this unless you know what you are doing.' shared.printLock.release() # calculate the fromRipe. sha = hashlib.new('sha512') @@ -1867,7 +1869,7 @@ class receiveDataThread(threading.Thread): if data[28 + lengthOfNumberOfAddresses + (34 * i)] == '\x7F': print 'Ignoring IP address in loopback range:', hostFromAddrMessage continue - if isHostInPrivateIPRange(hostFromAddrMessage): + if helper_generic.isHostInPrivateIPRange(hostFromAddrMessage): print 'Ignoring IP address in private range:', hostFromAddrMessage continue timeSomeoneElseReceivedMessageFromThisNode, = unpack('>I', data[lengthOfNumberOfAddresses + ( @@ -2065,7 +2067,7 @@ class receiveDataThread(threading.Thread): for i in range(500): random.seed() HOST, = random.sample(shared.knownNodes[self.streamNumber], 1) - if isHostInPrivateIPRange(HOST): + if helper_generic.isHostInPrivateIPRange(HOST): continue addrsInMyStream[HOST] = shared.knownNodes[ self.streamNumber][HOST] @@ -2074,7 +2076,7 @@ class receiveDataThread(threading.Thread): random.seed() HOST, = random.sample(shared.knownNodes[ self.streamNumber * 2], 1) - if isHostInPrivateIPRange(HOST): + if helper_generic.isHostInPrivateIPRange(HOST): continue addrsInChildStreamLeft[HOST] = shared.knownNodes[ self.streamNumber * 2][HOST] @@ -2083,7 +2085,7 @@ class receiveDataThread(threading.Thread): random.seed() HOST, = random.sample(shared.knownNodes[ (self.streamNumber * 2) + 1], 1) - if isHostInPrivateIPRange(HOST): + if helper_generic.isHostInPrivateIPRange(HOST): continue addrsInChildStreamRight[HOST] = shared.knownNodes[ (self.streamNumber * 2) + 1][HOST] @@ -2421,71 +2423,6 @@ def isInSqlInventory(hash): return True -def convertIntToString(n): - a = __builtins__.hex(n) - if a[-1:] == 'L': - a = a[:-1] - if (len(a) % 2) == 0: - return a[2:].decode('hex') - else: - return ('0' + a[2:]).decode('hex') - - -def convertStringToInt(s): - return int(s.encode('hex'), 16) - - -# This function expects that pubkey begin with \x04 -def calculateBitcoinAddressFromPubkey(pubkey): - if len(pubkey) != 65: - print 'Could not calculate Bitcoin address from pubkey because function was passed a pubkey that was', len(pubkey), 'bytes long rather than 65.' - return "error" - ripe = hashlib.new('ripemd160') - sha = hashlib.new('sha256') - sha.update(pubkey) - ripe.update(sha.digest()) - ripeWithProdnetPrefix = '\x00' + ripe.digest() - - checksum = hashlib.sha256(hashlib.sha256( - ripeWithProdnetPrefix).digest()).digest()[:4] - binaryBitcoinAddress = ripeWithProdnetPrefix + checksum - numberOfZeroBytesOnBinaryBitcoinAddress = 0 - while binaryBitcoinAddress[0] == '\x00': - numberOfZeroBytesOnBinaryBitcoinAddress += 1 - binaryBitcoinAddress = binaryBitcoinAddress[1:] - base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) - return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded - - -def calculateTestnetAddressFromPubkey(pubkey): - if len(pubkey) != 65: - print 'Could not calculate Bitcoin address from pubkey because function was passed a pubkey that was', len(pubkey), 'bytes long rather than 65.' - return "error" - ripe = hashlib.new('ripemd160') - sha = hashlib.new('sha256') - sha.update(pubkey) - ripe.update(sha.digest()) - ripeWithProdnetPrefix = '\x6F' + ripe.digest() - - checksum = hashlib.sha256(hashlib.sha256( - ripeWithProdnetPrefix).digest()).digest()[:4] - binaryBitcoinAddress = ripeWithProdnetPrefix + checksum - numberOfZeroBytesOnBinaryBitcoinAddress = 0 - while binaryBitcoinAddress[0] == '\x00': - numberOfZeroBytesOnBinaryBitcoinAddress += 1 - binaryBitcoinAddress = binaryBitcoinAddress[1:] - base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) - return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded - - -def signal_handler(signal, frame): - if shared.safeConfigGetBoolean('bitmessagesettings', 'daemon'): - shared.doCleanShutdown() - sys.exit(0) - else: - print 'Unfortunately you cannot use Ctrl+C when running the UI because the UI captures the signal.' - - def connectToStream(streamNumber): selfInitiatedConnections[streamNumber] = {} if sys.platform[0:3] == 'win': @@ -2563,21 +2500,6 @@ def assembleVersionMessage(remoteHost, remotePort, myStreamNumber): datatosend = datatosend + hashlib.sha512(payload).digest()[0:4] return datatosend + payload - -def isHostInPrivateIPRange(host): - if host[:3] == '10.': - return True - if host[:4] == '172.': - if host[6] == '.': - if int(host[4:6]) >= 16 and int(host[4:6]) <= 31: - return True - if host[:8] == '192.168.': - return True - return False - - - - # This thread, of which there is only one, does the heavy lifting: # calculating POWs. @@ -4326,7 +4248,7 @@ if __name__ == "__main__": # is the application already running? If yes then exit. thisapp = singleton.singleinstance() - signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGINT, helper_generic.signal_handler) # signal.signal(signal.SIGINT, signal.SIG_DFL) # Check the Major version, the first element in the array diff --git a/src/helper_bitcoin.py b/src/helper_bitcoin.py new file mode 100644 index 00000000..9ac966d6 --- /dev/null +++ b/src/helper_bitcoin.py @@ -0,0 +1,43 @@ +import hashlib + +# This function expects that pubkey begin with \x04 +def calculateBitcoinAddressFromPubkey(pubkey): + if len(pubkey) != 65: + print 'Could not calculate Bitcoin address from pubkey because function was passed a pubkey that was', len(pubkey), 'bytes long rather than 65.' + return "error" + ripe = hashlib.new('ripemd160') + sha = hashlib.new('sha256') + sha.update(pubkey) + ripe.update(sha.digest()) + ripeWithProdnetPrefix = '\x00' + ripe.digest() + + checksum = hashlib.sha256(hashlib.sha256( + ripeWithProdnetPrefix).digest()).digest()[:4] + binaryBitcoinAddress = ripeWithProdnetPrefix + checksum + numberOfZeroBytesOnBinaryBitcoinAddress = 0 + while binaryBitcoinAddress[0] == '\x00': + numberOfZeroBytesOnBinaryBitcoinAddress += 1 + binaryBitcoinAddress = binaryBitcoinAddress[1:] + base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) + return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded + + +def calculateTestnetAddressFromPubkey(pubkey): + if len(pubkey) != 65: + print 'Could not calculate Bitcoin address from pubkey because function was passed a pubkey that was', len(pubkey), 'bytes long rather than 65.' + return "error" + ripe = hashlib.new('ripemd160') + sha = hashlib.new('sha256') + sha.update(pubkey) + ripe.update(sha.digest()) + ripeWithProdnetPrefix = '\x6F' + ripe.digest() + + checksum = hashlib.sha256(hashlib.sha256( + ripeWithProdnetPrefix).digest()).digest()[:4] + binaryBitcoinAddress = ripeWithProdnetPrefix + checksum + numberOfZeroBytesOnBinaryBitcoinAddress = 0 + while binaryBitcoinAddress[0] == '\x00': + numberOfZeroBytesOnBinaryBitcoinAddress += 1 + binaryBitcoinAddress = binaryBitcoinAddress[1:] + base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) + return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded diff --git a/src/helper_generic.py b/src/helper_generic.py new file mode 100644 index 00000000..99c1c2d3 --- /dev/null +++ b/src/helper_generic.py @@ -0,0 +1,33 @@ +import shared + +def convertIntToString(n): + a = __builtins__.hex(n) + if a[-1:] == 'L': + a = a[:-1] + if (len(a) % 2) == 0: + return a[2:].decode('hex') + else: + return ('0' + a[2:]).decode('hex') + + +def convertStringToInt(s): + return int(s.encode('hex'), 16) + + +def signal_handler(signal, frame): + if shared.safeConfigGetBoolean('bitmessagesettings', 'daemon'): + shared.doCleanShutdown() + sys.exit(0) + else: + print 'Unfortunately you cannot use Ctrl+C when running the UI because the UI captures the signal.' + +def isHostInPrivateIPRange(host): + if host[:3] == '10.': + return True + if host[:4] == '172.': + if host[6] == '.': + if int(host[4:6]) >= 16 and int(host[4:6]) <= 31: + return True + if host[:8] == '192.168.': + return True + return False