Split off a few generic functions and Bitcoin related functions in seperate helper files
parent
138877f5f7
commit
8f81c35a6f
@ -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
|
@ -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
|
Loading…
Reference in New Issue