Resolve codacy through issues

Fix codacy errors and added random helper methods

Fix line too long pep8 error for chained methods

Formatting files pep8)
This commit is contained in:
Mahendra 2018-03-17 17:39:40 +05:30
parent 20b5c3552f
commit 18c19cd618
No known key found for this signature in database
GPG Key ID: A672D8FAAEE398B3
5 changed files with 1288 additions and 564 deletions

View File

@ -1,11 +1,15 @@
import hashlib import hashlib
from struct import *
from pyelliptic import arithmetic
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
#from debug import logger from struct import *
from pyelliptic import arithmetic
# from debug import logger
# There is another copy of this function in Bitmessagemain.py
#There is another copy of this function in Bitmessagemain.py
def convertIntToString(n): def convertIntToString(n):
a = __builtins__.hex(n) a = __builtins__.hex(n)
if a[-1:] == 'L': if a[-1:] == 'L':
@ -13,12 +17,14 @@ def convertIntToString(n):
if (len(a) % 2) == 0: if (len(a) % 2) == 0:
return unhexlify(a[2:]) return unhexlify(a[2:])
else: else:
return unhexlify('0'+a[2:]) return unhexlify('0' + a[2:])
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def encodeBase58(num, alphabet=ALPHABET): def encodeBase58(num, alphabet=ALPHABET):
"""Encode a number in Base X """Encode a number in Base X.
`num`: The number to encode `num`: The number to encode
`alphabet`: The alphabet to use for encoding `alphabet`: The alphabet to use for encoding
@ -29,14 +35,15 @@ def encodeBase58(num, alphabet=ALPHABET):
base = len(alphabet) base = len(alphabet)
while num: while num:
rem = num % base rem = num % base
#print 'num is:', num # print 'num is:', num
num = num // base num = num // base
arr.append(alphabet[rem]) arr.append(alphabet[rem])
arr.reverse() arr.reverse()
return ''.join(arr) return ''.join(arr)
def decodeBase58(string, alphabet=ALPHABET): def decodeBase58(string, alphabet=ALPHABET):
"""Decode a Base X encoded string into the number """Decode a Base X encoded string into the number.
Arguments: Arguments:
- `string`: The encoded string - `string`: The encoded string
@ -44,73 +51,91 @@ def decodeBase58(string, alphabet=ALPHABET):
""" """
base = len(alphabet) base = len(alphabet)
num = 0 num = 0
try: try:
for char in string: for char in string:
num *= base num *= base
num += alphabet.index(char) num += alphabet.index(char)
except: except BaseException:
#character not found (like a space character or a 0) # character not found (like a space character or a 0)
return 0 return 0
return num return num
def encodeVarint(integer): def encodeVarint(integer):
if integer < 0: if integer < 0:
logger.error('varint cannot be < 0') logger.error('varint cannot be < 0')
raise SystemExit raise SystemExit
if integer < 253: if integer < 253:
return pack('>B',integer) return pack('>B', integer)
if integer >= 253 and integer < 65536: if integer >= 253 and integer < 65536:
return pack('>B',253) + pack('>H',integer) return pack('>B', 253) + pack('>H', integer)
if integer >= 65536 and integer < 4294967296: if integer >= 65536 and integer < 4294967296:
return pack('>B',254) + pack('>I',integer) return pack('>B', 254) + pack('>I', integer)
if integer >= 4294967296 and integer < 18446744073709551616: if integer >= 4294967296 and integer < 18446744073709551616:
return pack('>B',255) + pack('>Q',integer) return pack('>B', 255) + pack('>Q', integer)
if integer >= 18446744073709551616: if integer >= 18446744073709551616:
logger.error('varint cannot be >= 18446744073709551616') logger.error('varint cannot be >= 18446744073709551616')
raise SystemExit raise SystemExit
class varintDecodeError(Exception): class varintDecodeError(Exception):
pass pass
def decodeVarint(data): def decodeVarint(data):
""" """Decode an encoded varint.
Decodes an encoded varint to an integer and returns it.
Per protocol v3, the encoded value must be encoded with Decode an encoded varint to an integer and returns it.
the minimum amount of data possible or else it is malformed. Per protocol v3, the encoded value must be encoded with
the minimum amount of data possible or else it is malformed.
Returns a tuple: (theEncodedValue, theSizeOfTheVarintInBytes) Returns a tuple: (theEncodedValue, theSizeOfTheVarintInBytes)
""" """
if len(data) == 0: if len(data) == 0:
return (0,0) return (0, 0)
firstByte, = unpack('>B',data[0:1]) firstByte, = unpack('>B', data[0:1])
if firstByte < 253: if firstByte < 253:
# encodes 0 to 252 # encodes 0 to 252
return (firstByte,1) #the 1 is the length of the varint return (firstByte, 1) # the 1 is the length of the varint
if firstByte == 253: if firstByte == 253:
# encodes 253 to 65535 # encodes 253 to 65535
if len(data) < 3: if len(data) < 3:
raise varintDecodeError('The first byte of this varint as an integer is %s but the total length is only %s. It needs to be at least 3.' % (firstByte, len(data))) raise varintDecodeError(
encodedValue, = unpack('>H',data[1:3]) 'The first byte of this varint as an integer is {} but the\
total length is only {}.It needs to be at least 3.'.format(
firstByte, len(data)))
encodedValue, = unpack('>H', data[1:3])
if encodedValue < 253: if encodedValue < 253:
raise varintDecodeError('This varint does not encode the value with the lowest possible number of bytes.') raise varintDecodeError(
return (encodedValue,3) 'This varint does not encode the value with the\
lowest possible number of bytes.')
return (encodedValue, 3)
if firstByte == 254: if firstByte == 254:
# encodes 65536 to 4294967295 # encodes 65536 to 4294967295
if len(data) < 5: if len(data) < 5:
raise varintDecodeError('The first byte of this varint as an integer is %s but the total length is only %s. It needs to be at least 5.' % (firstByte, len(data))) raise varintDecodeError(
encodedValue, = unpack('>I',data[1:5]) 'The first byte of this varint as an integer is {} but the\
total length is only {}. It needs to be at least 5.'.format(
firstByte, len(data)))
encodedValue, = unpack('>I', data[1:5])
if encodedValue < 65536: if encodedValue < 65536:
raise varintDecodeError('This varint does not encode the value with the lowest possible number of bytes.') raise varintDecodeError(
return (encodedValue,5) 'This varint does not encode the value with\
the lowest possible number of bytes.')
return (encodedValue, 5)
if firstByte == 255: if firstByte == 255:
# encodes 4294967296 to 18446744073709551615 # encodes 4294967296 to 18446744073709551615
if len(data) < 9: if len(data) < 9:
raise varintDecodeError('The first byte of this varint as an integer is %s but the total length is only %s. It needs to be at least 9.' % (firstByte, len(data))) raise varintDecodeError(
encodedValue, = unpack('>Q',data[1:9]) 'The first byte of this varint as an integer is {} but the\
total length is only {}. It needs to be at least 9.'.format(
firstByte, len(data)))
encodedValue, = unpack('>Q', data[1:9])
if encodedValue < 4294967296: if encodedValue < 4294967296:
raise varintDecodeError('This varint does not encode the value with the lowest possible number of bytes.') raise varintDecodeError(
return (encodedValue,9) 'This varint does not encode the value with \
the lowest possible number of bytes.')
return (encodedValue, 9)
def calculateInventoryHash(data): def calculateInventoryHash(data):
@ -120,21 +145,26 @@ def calculateInventoryHash(data):
sha2.update(sha.digest()) sha2.update(sha.digest())
return sha2.digest()[0:32] return sha2.digest()[0:32]
def encodeAddress(version,stream,ripe):
def encodeAddress(version, stream, ripe):
if version >= 2 and version < 4: if version >= 2 and version < 4:
if len(ripe) != 20: if len(ripe) != 20:
raise Exception("Programming error in encodeAddress: The length of a given ripe hash was not 20.") raise Exception(
"Programming error in encodeAddress: The length\
of a given ripe hash was not 20.")
if ripe[:2] == '\x00\x00': if ripe[:2] == '\x00\x00':
ripe = ripe[2:] ripe = ripe[2:]
elif ripe[:1] == '\x00': elif ripe[:1] == '\x00':
ripe = ripe[1:] ripe = ripe[1:]
elif version == 4: elif version == 4:
if len(ripe) != 20: if len(ripe) != 20:
raise Exception("Programming error in encodeAddress: The length of a given ripe hash was not 20.") raise Exception(
"Programming error in encodeAddress: The length\
of a given ripe hash was not 20.")
ripe = ripe.lstrip('\x00') ripe = ripe.lstrip('\x00')
storedBinaryData = encodeVarint(version) + encodeVarint(stream) + ripe storedBinaryData = encodeVarint(version) + encodeVarint(stream) + ripe
# Generate the checksum # Generate the checksum
sha = hashlib.new('sha512') sha = hashlib.new('sha512')
sha.update(storedBinaryData) sha.update(storedBinaryData)
@ -143,12 +173,16 @@ def encodeAddress(version,stream,ripe):
sha.update(currentHash) sha.update(currentHash)
checksum = sha.digest()[0:4] checksum = sha.digest()[0:4]
asInt = int(hexlify(storedBinaryData) + hexlify(checksum),16) asInt = int(hexlify(storedBinaryData) + hexlify(checksum), 16)
return 'BM-'+ encodeBase58(asInt) return 'BM-' + encodeBase58(asInt)
def decodeAddress(address): def decodeAddress(address):
#returns (status, address version number, stream number, data (almost certainly a ripe hash)) """Decode Addressmethod.
returns (status, address version number, stream number, data
(almost certainly a ripe hash)).
"""
address = str(address).strip() address = str(address).strip()
if address[:3] == 'BM-': if address[:3] == 'BM-':
@ -157,14 +191,15 @@ def decodeAddress(address):
integer = decodeBase58(address) integer = decodeBase58(address)
if integer == 0: if integer == 0:
status = 'invalidcharacters' status = 'invalidcharacters'
return status,0,0,"" return status, 0, 0, ""
#after converting to hex, the string will be prepended with a 0x and appended with a L # after converting to hex, the string will be prepended with a 0x and
# appended with a L
hexdata = hex(integer)[2:-1] hexdata = hex(integer)[2:-1]
if len(hexdata) % 2 != 0: if len(hexdata) % 2 != 0:
hexdata = '0' + hexdata hexdata = '0' + hexdata
#print 'hexdata', hexdata # print 'hexdata', hexdata
data = unhexlify(hexdata) data = unhexlify(hexdata)
checksum = data[-4:] checksum = data[-4:]
@ -172,15 +207,15 @@ def decodeAddress(address):
sha = hashlib.new('sha512') sha = hashlib.new('sha512')
sha.update(data[:-4]) sha.update(data[:-4])
currentHash = sha.digest() currentHash = sha.digest()
#print 'sha after first hashing: ', sha.hexdigest() # print 'sha after first hashing: ', sha.hexdigest()
sha = hashlib.new('sha512') sha = hashlib.new('sha512')
sha.update(currentHash) sha.update(currentHash)
#print 'sha after second hashing: ', sha.hexdigest() # print 'sha after second hashing: ', sha.hexdigest()
if checksum != sha.digest()[0:4]: if checksum != sha.digest()[0:4]:
status = 'checksumfailed' status = 'checksumfailed'
return status,0,0,"" return status, 0, 0, ""
#else: # else:
# print 'checksum PASSED' # print 'checksum PASSED'
try: try:
@ -188,95 +223,118 @@ def decodeAddress(address):
except varintDecodeError as e: except varintDecodeError as e:
logger.error(str(e)) logger.error(str(e))
status = 'varintmalformed' status = 'varintmalformed'
return status,0,0,"" return status, 0, 0, ""
#print 'addressVersionNumber', addressVersionNumber # print 'addressVersionNumber', addressVersionNumber
#print 'bytesUsedByVersionNumber', bytesUsedByVersionNumber # print 'bytesUsedByVersionNumber', bytesUsedByVersionNumber
if addressVersionNumber > 4: if addressVersionNumber > 4:
logger.error('cannot decode address version numbers this high') logger.error('cannot decode address version numbers\
this high')
status = 'versiontoohigh' status = 'versiontoohigh'
return status,0,0,"" return status, 0, 0, ""
elif addressVersionNumber == 0: elif addressVersionNumber == 0:
logger.error('cannot decode address version numbers of zero.') logger.error('cannot decode address version numbers\
of zero.')
status = 'versiontoohigh' status = 'versiontoohigh'
return status,0,0,"" return status, 0, 0, ""
try: try:
streamNumber, bytesUsedByStreamNumber = decodeVarint(data[bytesUsedByVersionNumber:]) streamNumber, bytesUsedByStreamNumber = decodeVarint(
data[bytesUsedByVersionNumber:])
except varintDecodeError as e: except varintDecodeError as e:
logger.error(str(e)) logger.error(str(e))
status = 'varintmalformed' status = 'varintmalformed'
return status,0,0,"" return status, 0, 0, ""
#print streamNumber # print streamNumber
status = 'success' status = 'success'
if addressVersionNumber == 1: if addressVersionNumber == 1:
return status,addressVersionNumber,streamNumber,data[-24:-4] return status, addressVersionNumber, streamNumber, data[-24:-4]
elif addressVersionNumber == 2 or addressVersionNumber == 3: elif addressVersionNumber == 2 or addressVersionNumber == 3:
embeddedRipeData = data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4] embeddedRipeData = data[bytesUsedByVersionNumber +
bytesUsedByStreamNumber:-4]
if len(embeddedRipeData) == 19: if len(embeddedRipeData) == 19:
return status,addressVersionNumber,streamNumber,'\x00'+embeddedRipeData return status, addressVersionNumber, streamNumber, '\x00' + embeddedRipeData
elif len(embeddedRipeData) == 20: elif len(embeddedRipeData) == 20:
return status,addressVersionNumber,streamNumber,embeddedRipeData return status, addressVersionNumber, streamNumber, embeddedRipeData
elif len(embeddedRipeData) == 18: elif len(embeddedRipeData) == 18:
return status,addressVersionNumber,streamNumber,'\x00\x00'+embeddedRipeData return status, addressVersionNumber, streamNumber, '\x00\x00'
+ embeddedRipeData
elif len(embeddedRipeData) < 18: elif len(embeddedRipeData) < 18:
return 'ripetooshort',0,0,"" return 'ripetooshort', 0, 0, ""
elif len(embeddedRipeData) > 20: elif len(embeddedRipeData) > 20:
return 'ripetoolong',0,0,"" return 'ripetoolong', 0, 0, ""
else: else:
return 'otherproblem',0,0,"" return 'otherproblem', 0, 0, ""
elif addressVersionNumber == 4: elif addressVersionNumber == 4:
embeddedRipeData = data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4] embeddedRipeData = data[bytesUsedByVersionNumber +
bytesUsedByStreamNumber:-4]
if embeddedRipeData[0:1] == '\x00': if embeddedRipeData[0:1] == '\x00':
# In order to enforce address non-malleability, encoded RIPE data must have NULL bytes removed from the front # In order to enforce address non-malleability,\
return 'encodingproblem',0,0,"" # encoded RIPE data
# must have NULL bytes removed from the front
return 'encodingproblem', 0, 0, ""
elif len(embeddedRipeData) > 20: elif len(embeddedRipeData) > 20:
return 'ripetoolong',0,0,"" return 'ripetoolong', 0, 0, ""
elif len(embeddedRipeData) < 4: elif len(embeddedRipeData) < 4:
return 'ripetooshort',0,0,"" return 'ripetooshort', 0, 0, ""
else: else:
x00string = '\x00' * (20 - len(embeddedRipeData)) x00string = '\x00' * (20 - len(embeddedRipeData))
return status,addressVersionNumber,streamNumber,x00string+embeddedRipeData return status, addressVersionNumber, streamNumber,\
x00string, embeddedRipeData
def addBMIfNotPresent(address): def addBMIfNotPresent(address):
address = str(address).strip() address = str(address).strip()
if address[:3] != 'BM-': if address[:3] != 'BM-':
return 'BM-'+address return 'BM-' + address
else: else:
return address return address
if __name__ == "__main__": if __name__ == "__main__":
print 'Let us make an address from scratch. Suppose we generate two random 32 byte values and call the first one the signing key and the second one the encryption key:' print 'Let us make an address from scratch. Suppose we generate\
privateSigningKey = '93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665' two random 32 byte values and call the first one the signing\
privateEncryptionKey = '4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a' key and the second one the encryption key:'
privateSigningKey = '93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186\
bfd8f876665'
privateEncryptionKey = '4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf\
40f4d7400a3a'
print 'privateSigningKey =', privateSigningKey print 'privateSigningKey =', privateSigningKey
print 'privateEncryptionKey =', privateEncryptionKey print 'privateEncryptionKey =', privateEncryptionKey
print 'Now let us convert them to public keys by doing an elliptic curve point multiplication.' print 'Now let us convert them to public keys by doing an\
elliptic curve point multiplication.'
publicSigningKey = arithmetic.privtopub(privateSigningKey) publicSigningKey = arithmetic.privtopub(privateSigningKey)
publicEncryptionKey = arithmetic.privtopub(privateEncryptionKey) publicEncryptionKey = arithmetic.privtopub(privateEncryptionKey)
print 'publicSigningKey =', publicSigningKey print 'publicSigningKey =', publicSigningKey
print 'publicEncryptionKey =', publicEncryptionKey print 'publicEncryptionKey =', publicEncryptionKey
print 'Notice that they both begin with the \\x04 which specifies the encoding type. This prefix is not send over the wire. You must strip if off before you send your public key across the wire, and you must add it back when you receive a public key.' print 'Notice that they both begin with the \\x04 which specifies\
the encoding type. This prefix is not send over the wire.\
You must strip.if off before you send your public key across\
the wire, and you must add it back when you receive a public key.'
publicSigningKeyBinary = arithmetic.changebase(publicSigningKey,16,256,minlen=64) publicSigningKeyBinary = arithmetic.changebase(
publicEncryptionKeyBinary = arithmetic.changebase(publicEncryptionKey,16,256,minlen=64) publicSigningKey, 16, 256, minlen=64)
publicEncryptionKeyBinary = arithmetic.changebase(
publicEncryptionKey, 16, 256, minlen=64)
ripe = hashlib.new('ripemd160') ripe = hashlib.new('ripemd160')
sha = hashlib.new('sha512') sha = hashlib.new('sha512')
sha.update(publicSigningKeyBinary+publicEncryptionKeyBinary) sha.update(publicSigningKeyBinary + publicEncryptionKeyBinary)
ripe.update(sha.digest()) ripe.update(sha.digest())
addressVersionNumber = 2 addressVersionNumber = 2
streamNumber = 1 streamNumber = 1
print 'Ripe digest that we will encode in the address:', hexlify(ripe.digest()) print 'Ripe digest that we will encode in the address:',
returnedAddress = encodeAddress(addressVersionNumber,streamNumber,ripe.digest()) hexlify(ripe.digest())
returnedAddress = encodeAddress(
addressVersionNumber, streamNumber, ripe.digest())
print 'Encoded address:', returnedAddress print 'Encoded address:', returnedAddress
status,addressVersionNumber,streamNumber,data = decodeAddress(returnedAddress) status, addressVersionNumber, streamNumber, data = decodeAddress(
returnedAddress)
print '\nAfter decoding address:' print '\nAfter decoding address:'
print 'Status:', status print 'Status:', status
print 'addressVersionNumber', addressVersionNumber print 'addressVersionNumber', addressVersionNumber
print 'streamNumber', streamNumber print 'streamNumber', streamNumber
print 'length of data(the ripe hash):', len(data) print 'length of data(the ripe hash):', len(data)
print 'ripe data:', hexlify(data) print 'ripe data:', hexlify(data)

View File

@ -22,7 +22,7 @@ class AccountMixin(object):
SUBSCRIPTION = 4 SUBSCRIPTION = 4
BROADCAST = 5 BROADCAST = 5
def accountColor (self): def accountColor(self):
if not self.isEnabled: if not self.isEnabled:
return QtGui.QColor(128, 128, 128) return QtGui.QColor(128, 128, 128)
elif self.type == self.CHAN: elif self.type == self.CHAN:
@ -31,18 +31,18 @@ class AccountMixin(object):
return QtGui.QColor(137, 04, 177) return QtGui.QColor(137, 04, 177)
else: else:
return QtGui.QApplication.palette().text().color() return QtGui.QApplication.palette().text().color()
def folderColor (self): def folderColor(self):
if not self.parent().isEnabled: if not self.parent().isEnabled:
return QtGui.QColor(128, 128, 128) return QtGui.QColor(128, 128, 128)
else: else:
return QtGui.QApplication.palette().text().color() return QtGui.QApplication.palette().text().color()
def accountBrush(self): def accountBrush(self):
brush = QtGui.QBrush(self.accountColor()) brush = QtGui.QBrush(self.accountColor())
brush.setStyle(QtCore.Qt.NoBrush) brush.setStyle(QtCore.Qt.NoBrush)
return brush return brush
def folderBrush(self): def folderBrush(self):
brush = QtGui.QBrush(self.folderColor()) brush = QtGui.QBrush(self.folderColor())
brush.setStyle(QtCore.Qt.NoBrush) brush.setStyle(QtCore.Qt.NoBrush)
@ -53,7 +53,7 @@ class AccountMixin(object):
self.address = None self.address = None
else: else:
self.address = str(address) self.address = str(address)
def setUnreadCount(self, cnt): def setUnreadCount(self, cnt):
if hasattr(self, "unreadCount") and self.unreadCount == int(cnt): if hasattr(self, "unreadCount") and self.unreadCount == int(cnt):
return return
@ -82,23 +82,31 @@ class AccountMixin(object):
elif BMConfigParser().safeGetBoolean(self.address, 'mailinglist'): elif BMConfigParser().safeGetBoolean(self.address, 'mailinglist'):
self.type = self.MAILINGLIST self.type = self.MAILINGLIST
elif sqlQuery( elif sqlQuery(
'''select label from subscriptions where address=?''', self.address): '''select label from subscriptions\
where address=?''', self.address):
self.type = AccountMixin.SUBSCRIPTION self.type = AccountMixin.SUBSCRIPTION
else: else:
self.type = self.NORMAL self.type = self.NORMAL
def defaultLabel(self): def defaultLabel(self):
queryreturn = None queryreturn = None
retval = None retval = None
if self.type in (AccountMixin.NORMAL, AccountMixin.CHAN, AccountMixin.MAILINGLIST): if self.type in (
AccountMixin.NORMAL,
AccountMixin.CHAN,
AccountMixin.MAILINGLIST):
try: try:
retval = unicode(BMConfigParser().get(self.address, 'label'), 'utf-8') retval = unicode(
except Exception as e: BMConfigParser().get(
self.address, 'label'), 'utf-8')
except Exception:
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select label from addressbook where address=?''', self.address) '''select label from addressbook\
where address=?''', self.address)
elif self.type == AccountMixin.SUBSCRIPTION: elif self.type == AccountMixin.SUBSCRIPTION:
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select label from subscriptions where address=?''', self.address) '''select label from subscriptions where address=?''',
self.address)
if queryreturn is not None: if queryreturn is not None:
if queryreturn != []: if queryreturn != []:
for row in queryreturn: for row in queryreturn:
@ -115,7 +123,14 @@ class AccountMixin(object):
class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin): class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
folderWeight = {"inbox": 1, "new": 2, "sent": 3, "trash": 4} folderWeight = {"inbox": 1, "new": 2, "sent": 3, "trash": 4}
def __init__(self, parent, pos = 0, address = "", folderName = "", unreadCount = 0):
def __init__(
self,
parent,
pos=0,
address="",
folderName="",
unreadCount=0):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
self.setAddress(address) self.setAddress(address)
self.setFolderName(folderName) self.setFolderName(folderName)
@ -154,7 +169,9 @@ class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
else: else:
y = 99 y = 99
reverse = False reverse = False
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: if self.treeWidget() \
.header() \
.sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
reverse = True reverse = True
if x == y: if x == y:
return self.folderName < other.folderName return self.folderName < other.folderName
@ -162,14 +179,21 @@ class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
return (x >= y if reverse else x < y) return (x >= y if reverse else x < y)
return super(QtGui.QTreeWidgetItem, self).__lt__(other) return super(QtGui.QTreeWidgetItem, self).__lt__(other)
class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin): class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
def __init__(self, parent, pos = 0, address = None, unreadCount = 0, enabled = True): def __init__(
self,
parent,
pos=0,
address=None,
unreadCount=0,
enabled=True):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
parent.insertTopLevelItem(pos, self) parent.insertTopLevelItem(pos, self)
# only set default when creating # only set default when creating
#super(QtGui.QTreeWidgetItem, self).setExpanded(BMConfigParser().getboolean(self.address, 'enabled')) # super(QtGui.QTreeWidgetItem, self).setExpanded(BMConfigParser().
# getboolean(self.address, 'enabled'))
self.setAddress(address) self.setAddress(address)
self.setEnabled(enabled) self.setEnabled(enabled)
self.setUnreadCount(unreadCount) self.setUnreadCount(unreadCount)
@ -184,17 +208,17 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
return unicode( return unicode(
BMConfigParser().get(self.address, 'label'), BMConfigParser().get(self.address, 'label'),
'utf-8', 'ignore') 'utf-8', 'ignore')
except: except BaseException:
return unicode(self.address, 'utf-8') return unicode(self.address, 'utf-8')
def _getAddressBracket(self, unreadCount = False): def _getAddressBracket(self, unreadCount=False):
ret = "" ret = ""
if unreadCount: if unreadCount:
ret += " (" + str(self.unreadCount) + ")" ret += " (" + str(self.unreadCount) + ")"
if self.address is not None: if self.address is not None:
ret += " (" + self.address + ")" ret += " (" + self.address + ")"
return ret return ret
def data(self, column, role): def data(self, column, role):
if column == 0: if column == 0:
if role == QtCore.Qt.DisplayRole: if role == QtCore.Qt.DisplayRole:
@ -204,7 +228,7 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
return self._getLabel() + self._getAddressBracket(False) return self._getLabel() + self._getAddressBracket(False)
elif role == QtCore.Qt.EditRole: elif role == QtCore.Qt.EditRole:
return self._getLabel() return self._getLabel()
elif role == QtCore.Qt.ToolTipRole: elif role == QtCore.Qt.ToolTipRole:
return self._getLabel() + self._getAddressBracket(False) return self._getLabel() + self._getAddressBracket(False)
elif role == QtCore.Qt.DecorationRole: elif role == QtCore.Qt.DecorationRole:
if self.address is None: if self.address is None:
@ -218,23 +242,27 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
elif role == QtCore.Qt.ForegroundRole: elif role == QtCore.Qt.ForegroundRole:
return self.accountBrush() return self.accountBrush()
return super(Ui_AddressWidget, self).data(column, role) return super(Ui_AddressWidget, self).data(column, role)
def setData(self, column, role, value): def setData(self, column, role, value):
if role == QtCore.Qt.EditRole and self.type != AccountMixin.SUBSCRIPTION: if role == QtCore.Qt \
.EditRole and self.type != AccountMixin.SUBSCRIPTION:
if isinstance(value, QtCore.QVariant): if isinstance(value, QtCore.QVariant):
BMConfigParser().set(str(self.address), 'label', str(value.toString().toUtf8())) BMConfigParser() \
.set(str(
self.address), 'label',
str(value.toString().toUtf8()))
else: else:
BMConfigParser().set(str(self.address), 'label', str(value)) BMConfigParser().set(str(self.address), 'label', str(value))
BMConfigParser().save() BMConfigParser().save()
return super(Ui_AddressWidget, self).setData(column, role, value) return super(Ui_AddressWidget, self).setData(column, role, value)
def setAddress(self, address): def setAddress(self, address):
super(Ui_AddressWidget, self).setAddress(address) super(Ui_AddressWidget, self).setAddress(address)
self.setData(0, QtCore.Qt.UserRole, self.address) self.setData(0, QtCore.Qt.UserRole, self.address)
def setExpanded(self, expand): def setExpanded(self, expand):
super(Ui_AddressWidget, self).setExpanded(expand) super(Ui_AddressWidget, self).setExpanded(expand)
def _getSortRank(self): def _getSortRank(self):
ret = self.type ret = self.type
if not self.isEnabled: if not self.isEnabled:
@ -245,46 +273,61 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
def __lt__(self, other): def __lt__(self, other):
if (isinstance(other, Ui_AddressWidget)): if (isinstance(other, Ui_AddressWidget)):
reverse = False reverse = False
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: if self.treeWidget() \
.header() \
.sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
reverse = True reverse = True
if self._getSortRank() == other._getSortRank(): if self._getSortRank() == other._getSortRank():
x = self._getLabel().lower() x = self._getLabel().lower()
y = other._getLabel().lower() y = other._getLabel().lower()
return x < y return x < y
return (not reverse if self._getSortRank() < other._getSortRank() else reverse) return (not reverse if self._getSortRank() <
other._getSortRank() else reverse)
return super(QtGui.QTreeWidgetItem, self).__lt__(other) return super(QtGui.QTreeWidgetItem, self).__lt__(other)
class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin): class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
def __init__(self, parent, pos = 0, address = "", unreadCount = 0, label = "", enabled = True): def __init__(
self,
parent,
pos=0,
address="",
unreadCount=0,
label="",
enabled=True):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
parent.insertTopLevelItem(pos, self) parent.insertTopLevelItem(pos, self)
# only set default when creating # only set default when creating
#super(QtGui.QTreeWidgetItem, self).setExpanded(BMConfigParser().getboolean(self.address, 'enabled')) # super(QtGui.QTreeWidgetItem, self).
# setExpanded(BMConfigParser().
# getboolean(self.address, 'enabled'))
self.setAddress(address) self.setAddress(address)
self.setEnabled(enabled) self.setEnabled(enabled)
self.setType() self.setType()
self.setUnreadCount(unreadCount) self.setUnreadCount(unreadCount)
def _getLabel(self): def _getLabel(self):
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select label from subscriptions where address=?''', self.address) '''select label from subscriptions where address=?''',
self.address)
if queryreturn != []: if queryreturn != []:
for row in queryreturn: for row in queryreturn:
retval, = row retval, = row
return unicode(retval, 'utf-8', 'ignore') return unicode(retval, 'utf-8', 'ignore')
return unicode(self.address, 'utf-8') return unicode(self.address, 'utf-8')
def setType(self): def setType(self):
super(Ui_SubscriptionWidget, self).setType() # sets it editable super(Ui_SubscriptionWidget, self).setType() # sets it editable
self.type = AccountMixin.SUBSCRIPTION # overrides type self.type = AccountMixin.SUBSCRIPTION # overrides type
def setData(self, column, role, value): def setData(self, column, role, value):
if role == QtCore.Qt.EditRole: if role == QtCore.Qt.EditRole:
from debug import logger from debug import logger
if isinstance(value, QtCore.QVariant): if isinstance(value, QtCore.QVariant):
label = str(value.toString().toUtf8()).decode('utf-8', 'ignore') label = str(
value.toString().toUtf8()).decode(
'utf-8', 'ignore')
else: else:
label = unicode(value, 'utf-8', 'ignore') label = unicode(value, 'utf-8', 'ignore')
sqlExecute( sqlExecute(
@ -293,12 +336,16 @@ class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
return super(Ui_SubscriptionWidget, self).setData(column, role, value) return super(Ui_SubscriptionWidget, self).setData(column, role, value)
class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMixin): class MessageList_AddressWidget(
def __init__(self, parent, address = None, label = None, unread = False): QtGui.QTableWidgetItem,
AccountMixin,
SettingsMixin):
def __init__(self, parent, address=None, label=None, unread=False):
super(QtGui.QTableWidgetItem, self).__init__() super(QtGui.QTableWidgetItem, self).__init__()
#parent.insertTopLevelItem(pos, self) # parent.insertTopLevelItem(pos, self)
# only set default when creating # only set default when creating
#super(QtGui.QTreeWidgetItem, self).setExpanded(BMConfigParser().getboolean(self.address, 'enabled')) # super(QtGui.QTreeWidgetItem, self).setExpanded(
# BMConfigParser().getboolean(self.address, 'enabled'))
self.isEnabled = True self.isEnabled = True
self.setAddress(address) self.setAddress(address)
self.setLabel(label) self.setLabel(label)
@ -307,19 +354,29 @@ class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMi
self.setType() self.setType()
parent.append(self) parent.append(self)
def setLabel(self, label = None): def setLabel(self, label=None):
newLabel = self.address newLabel = self.address
if label is None: if label is None:
queryreturn = None queryreturn = None
if self.type in (AccountMixin.NORMAL, AccountMixin.CHAN, AccountMixin.MAILINGLIST): if self.type in (
AccountMixin.NORMAL,
AccountMixin.CHAN,
AccountMixin.MAILINGLIST):
try: try:
newLabel = unicode(BMConfigParser().get(self.address, 'label'), 'utf-8', 'ignore') newLabel = unicode(
except: BMConfigParser().get(
self.address,
'label'),
'utf-8',
'ignore')
except BaseException:
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select label from addressbook where address=?''', self.address) '''select label from addressbook\
where address=?''', self.address)
elif self.type == AccountMixin.SUBSCRIPTION: elif self.type == AccountMixin.SUBSCRIPTION:
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select label from subscriptions where address=?''', self.address) '''select label from subscriptions where address=?''',
self.address)
if queryreturn is not None: if queryreturn is not None:
if queryreturn != []: if queryreturn != []:
for row in queryreturn: for row in queryreturn:
@ -341,7 +398,8 @@ class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMi
elif role == QtCore.Qt.ToolTipRole: elif role == QtCore.Qt.ToolTipRole:
return self.label + " (" + self.address + ")" return self.label + " (" + self.address + ")"
elif role == QtCore.Qt.DecorationRole: elif role == QtCore.Qt.DecorationRole:
if BMConfigParser().safeGetBoolean('bitmessagesettings', 'useidenticons'): if BMConfigParser() \
.safeGetBoolean('bitmessagesettings', 'useidenticons'):
if self.address is None: if self.address is None:
return avatarize(self.label) return avatarize(self.label)
else: else:
@ -355,7 +413,7 @@ class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMi
elif role == QtCore.Qt.UserRole: elif role == QtCore.Qt.UserRole:
return self.address return self.address
return super(MessageList_AddressWidget, self).data(role) return super(MessageList_AddressWidget, self).data(role)
def setData(self, role, value): def setData(self, role, value):
if role == QtCore.Qt.EditRole: if role == QtCore.Qt.EditRole:
self.setLabel() self.setLabel()
@ -369,11 +427,13 @@ class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMi
class MessageList_SubjectWidget(QtGui.QTableWidgetItem, SettingsMixin): class MessageList_SubjectWidget(QtGui.QTableWidgetItem, SettingsMixin):
def __init__(self, parent, subject = None, label = None, unread = False): def __init__(self, parent, subject=None, label=None, unread=False):
super(QtGui.QTableWidgetItem, self).__init__() super(QtGui.QTableWidgetItem, self).__init__()
#parent.insertTopLevelItem(pos, self) # parent.insertTopLevelItem(pos, self)
# only set default when creating # only set default when creating
#super(QtGui.QTreeWidgetItem, self).setExpanded(BMConfigParser().getboolean(self.address, 'enabled')) # super(QtGui.QTreeWidgetItem, self)
# .setExpanded(BMConfigParser().
# getboolean(self.address, 'enabled'))
self.setSubject(subject) self.setSubject(subject)
self.setLabel(label) self.setLabel(label)
self.setUnread(unread) self.setUnread(unread)
@ -382,7 +442,7 @@ class MessageList_SubjectWidget(QtGui.QTableWidgetItem, SettingsMixin):
def setLabel(self, label): def setLabel(self, label):
self.label = label self.label = label
def setSubject(self, subject): def setSubject(self, subject):
self.subject = subject self.subject = subject
@ -403,7 +463,7 @@ class MessageList_SubjectWidget(QtGui.QTableWidgetItem, SettingsMixin):
elif role == QtCore.Qt.UserRole: elif role == QtCore.Qt.UserRole:
return self.subject return self.subject
return super(MessageList_SubjectWidget, self).data(role) return super(MessageList_SubjectWidget, self).data(role)
def setData(self, role, value): def setData(self, role, value):
return super(MessageList_SubjectWidget, self).setData(role, value) return super(MessageList_SubjectWidget, self).setData(role, value)
@ -415,7 +475,7 @@ class MessageList_SubjectWidget(QtGui.QTableWidgetItem, SettingsMixin):
class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin): class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
def __init__ (self, text, type = AccountMixin.NORMAL): def __init__(self, text, type=AccountMixin.NORMAL):
super(QtGui.QTableWidgetItem, self).__init__(text) super(QtGui.QTableWidgetItem, self).__init__(text)
self.label = text self.label = text
self.type = type self.type = type
@ -429,7 +489,8 @@ class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
elif role == QtCore.Qt.ToolTipRole: elif role == QtCore.Qt.ToolTipRole:
return self.label + " (" + self.address + ")" return self.label + " (" + self.address + ")"
elif role == QtCore.Qt.DecorationRole: elif role == QtCore.Qt.DecorationRole:
if BMConfigParser().safeGetBoolean('bitmessagesettings', 'useidenticons'): if BMConfigParser() \
.safeGetBoolean('bitmessagesettings', 'useidenticons'):
if self.address is None: if self.address is None:
return avatarize(self.label) return avatarize(self.label)
else: else:
@ -449,24 +510,34 @@ class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
self.label = str(value.toString().toUtf8()) self.label = str(value.toString().toUtf8())
else: else:
self.label = str(value) self.label = str(value)
if self.type in (AccountMixin.NORMAL, AccountMixin.MAILINGLIST, AccountMixin.CHAN): if self.type in (
AccountMixin.NORMAL,
AccountMixin.MAILINGLIST,
AccountMixin.CHAN):
try: try:
a = BMConfigParser().get(self.address, 'label') # a = BMConfigParser().get(self.address, 'label')
BMConfigParser().set(self.address, 'label', self.label) BMConfigParser().set(self.address, 'label', self.label)
BMConfigParser().save() BMConfigParser().save()
except: except BaseException:
sqlExecute('''UPDATE addressbook set label=? WHERE address=?''', self.label, self.address) sqlExecute(
'''UPDATE addressbook set label=? WHERE address=?''',
self.label,
self.address)
elif self.type == AccountMixin.SUBSCRIPTION: elif self.type == AccountMixin.SUBSCRIPTION:
from debug import logger from debug import logger
sqlExecute('''UPDATE subscriptions set label=? WHERE address=?''', self.label, self.address) sqlExecute(
'''UPDATE subscriptions set label=? WHERE address=?''',
self.label,
self.address)
else: else:
pass pass
return super(Ui_AddressBookWidgetItem, self).setData(role, value) return super(Ui_AddressBookWidgetItem, self).setData(role, value)
def __lt__ (self, other): def __lt__(self, other):
if (isinstance(other, Ui_AddressBookWidgetItem)): if (isinstance(other, Ui_AddressBookWidgetItem)):
reverse = False reverse = False
if self.tableWidget().horizontalHeader().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: if self.tableWidget().horizontalHeader(
).sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
reverse = True reverse = True
if self.type == other.type: if self.type == other.type:
return self.label.lower() < other.label.lower() return self.label.lower() < other.label.lower()
@ -476,7 +547,7 @@ class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem): class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem):
def __init__ (self, address, label, type): def __init__(self, address, label, type):
Ui_AddressBookWidgetItem.__init__(self, label, type) Ui_AddressBookWidgetItem.__init__(self, label, type)
self.address = address self.address = address
self.label = label self.label = label
@ -487,7 +558,7 @@ class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem):
class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem): class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem):
def __init__ (self, address, label, type): def __init__(self, address, label, type):
Ui_AddressBookWidgetItem.__init__(self, address, type) Ui_AddressBookWidgetItem.__init__(self, address, type)
self.address = address self.address = address
self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
@ -504,11 +575,11 @@ class AddressBookCompleter(QtGui.QCompleter):
def __init__(self): def __init__(self):
super(QtGui.QCompleter, self).__init__() super(QtGui.QCompleter, self).__init__()
self.cursorPos = -1 self.cursorPos = -1
def onCursorPositionChanged(self, oldPos, newPos): def onCursorPositionChanged(self, oldPos, newPos):
if oldPos != self.cursorPos: if oldPos != self.cursorPos:
self.cursorPos = -1 self.cursorPos = -1
def splitPath(self, path): def splitPath(self, path):
stringList = [] stringList = []
text = unicode(path.toUtf8(), encoding="UTF-8") text = unicode(path.toUtf8(), encoding="UTF-8")
@ -517,11 +588,14 @@ class AddressBookCompleter(QtGui.QCompleter):
str = rstrip(lstrip(str)) str = rstrip(lstrip(str))
stringList.append(str) stringList.append(str)
return stringList return stringList
def pathFromIndex(self, index): def pathFromIndex(self, index):
autoString = unicode(index.data(QtCore.Qt.EditRole).toString().toUtf8(), encoding="UTF-8") autoString = unicode(
index.data(
QtCore.Qt.EditRole).toString().toUtf8(),
encoding="UTF-8")
text = unicode(self.widget().text().toUtf8(), encoding="UTF-8") text = unicode(self.widget().text().toUtf8(), encoding="UTF-8")
# If cursor position was saved, restore it, else save it # If cursor position was saved, restore it, else save it
if self.cursorPos != -1: if self.cursorPos != -1:
self.widget().setCursorPosition(self.cursorPos) self.widget().setCursorPosition(self.cursorPos)
@ -530,14 +604,16 @@ class AddressBookCompleter(QtGui.QCompleter):
# Get current prosition # Get current prosition
curIndex = self.widget().cursorPosition() curIndex = self.widget().cursorPosition()
# prev_delimiter_index should actually point at final white space AFTER the delimiter # prev_delimiter_index should actually point at
# final white space AFTER the delimiter
# Get index of last delimiter before current position # Get index of last delimiter before current position
prevDelimiterIndex = rfind(text[0:curIndex], ";") prevDelimiterIndex = rfind(text[0:curIndex], ";")
while text[prevDelimiterIndex + 1] == " ": while text[prevDelimiterIndex + 1] == " ":
prevDelimiterIndex += 1 prevDelimiterIndex += 1
# Get index of first delimiter after current position (or EOL if no delimiter after cursor) # Get index of first delimiter after current position (or EOL if no
# delimiter after cursor)
nextDelimiterIndex = find(text, ";", curIndex) nextDelimiterIndex = find(text, ";", curIndex)
if nextDelimiterIndex == -1: if nextDelimiterIndex == -1:
nextDelimiterIndex = len(text) nextDelimiterIndex = len(text)
@ -546,9 +622,9 @@ class AddressBookCompleter(QtGui.QCompleter):
part1 = text[0:prevDelimiterIndex + 1] part1 = text[0:prevDelimiterIndex + 1]
# Get string value from before auto finished string is selected # Get string value from before auto finished string is selected
pre = text[prevDelimiterIndex + 1:curIndex - 1]; # pre = text[prevDelimiterIndex + 1:curIndex - 1]
# Get part of string that occurs AFTER cursor # Get part of string that occurs AFTER cursor
part2 = text[nextDelimiterIndex:] part2 = text[nextDelimiterIndex:]
return part1 + autoString + part2; return part1 + autoString + part2

View File

@ -5,7 +5,8 @@ import shared
import time import time
from time import strftime, localtime, gmtime from time import strftime, localtime, gmtime
import random import random
from subprocess import call # used when the API must execute an outside program from subprocess import call
# used when the API must execute an outside program
from addresses import * from addresses import *
import highlevelcrypto import highlevelcrypto
import proofofwork import proofofwork
@ -29,13 +30,15 @@ from binascii import hexlify, unhexlify
# This thread, of which there is only one, does the heavy lifting: # This thread, of which there is only one, does the heavy lifting:
# calculating POWs. # calculating POWs.
def sizeof_fmt(num, suffix='h/s'): def sizeof_fmt(num, suffix='h/s'):
for unit in ['','k','M','G','T','P','E','Z']: for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1000.0: if abs(num) < 1000.0:
return "%3.1f%s%s" % (num, unit, suffix) return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0 num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix) return "%.1f%s%s" % (num, 'Yi', suffix)
class singleWorker(threading.Thread, StoppableThread): class singleWorker(threading.Thread, StoppableThread):
def __init__(self): def __init__(self):
@ -47,7 +50,7 @@ class singleWorker(threading.Thread, StoppableThread):
def stopThread(self): def stopThread(self):
try: try:
queues.workerQueue.put(("stopThread", "data")) queues.workerQueue.put(("stopThread", "data"))
except: except BaseException:
pass pass
super(singleWorker, self).stopThread() super(singleWorker, self).stopThread()
@ -57,21 +60,33 @@ class singleWorker(threading.Thread, StoppableThread):
self.stop.wait(2) self.stop.wait(2)
if state.shutdown > 0: if state.shutdown > 0:
return return
# Initialize the neededPubkeys dictionary. # Initialize the neededPubkeys dictionary.
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''SELECT DISTINCT toaddress FROM sent WHERE (status='awaitingpubkey' AND folder='sent')''') '''SELECT DISTINCT toaddress
FROM sent WHERE (status='awaitingpubkey' AND folder='sent')''')
for row in queryreturn: for row in queryreturn:
toAddress, = row toAddress, = row
toStatus, toAddressVersionNumber, toStreamNumber, toRipe = decodeAddress(toAddress) toStatus, toAddressVersionNumber, toStreamNumber, \
if toAddressVersionNumber <= 3 : toRipe = decodeAddress(toAddress)
if toAddressVersionNumber <= 3:
state.neededPubkeys[toAddress] = 0 state.neededPubkeys[toAddress] = 0
elif toAddressVersionNumber >= 4: elif toAddressVersionNumber >= 4:
doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(encodeVarint( doubleHashOfAddressData = hashlib.sha512(
toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest() hashlib.sha512(
privEncryptionKey = doubleHashOfAddressData[:32] # Note that this is the first half of the sha512 hash. encodeVarint(
toAddressVersionNumber) +
encodeVarint(
toStreamNumber) +
toRipe).digest()).digest()
# Note that this is the first half of the sha512 hash.
privEncryptionKey = doubleHashOfAddressData[:32]
tag = doubleHashOfAddressData[32:] tag = doubleHashOfAddressData[32:]
state.neededPubkeys[tag] = (toAddress, highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))) # We'll need this for when we receive a pubkey reply: it will be encrypted and we'll need to decrypt it. # We'll need this for when we receive a pubkey reply: it will
# be encrypted and we'll need to decrypt it.
state.neededPubkeys[tag] = (
toAddress, highlevelcrypto.makeCryptor(
hexlify(privEncryptionKey)))
# Initialize the shared.ackdataForWhichImWatching data structure # Initialize the shared.ackdataForWhichImWatching data structure
queryreturn = sqlQuery( queryreturn = sqlQuery(
@ -83,16 +98,18 @@ class singleWorker(threading.Thread, StoppableThread):
# Fix legacy (headerless) watched ackdata to include header # Fix legacy (headerless) watched ackdata to include header
for oldack in shared.ackdataForWhichImWatching.keys(): for oldack in shared.ackdataForWhichImWatching.keys():
if (len(oldack)==32): if (len(oldack) == 32):
# attach legacy header, always constant (msg/1/1) # attach legacy header, always constant (msg/1/1)
newack = '\x00\x00\x00\x02\x01\x01' + oldack newack = '\x00\x00\x00\x02\x01\x01' + oldack
shared.ackdataForWhichImWatching[newack] = 0 shared.ackdataForWhichImWatching[newack] = 0
sqlExecute('UPDATE sent SET ackdata=? WHERE ackdata=?', sqlExecute('UPDATE sent SET ackdata=? WHERE ackdata=?',
newack, oldack ) newack, oldack)
del shared.ackdataForWhichImWatching[oldack] del shared.ackdataForWhichImWatching[oldack]
# give some time for the GUI to start before
# we start on existing POW tasks.
self.stop.wait( self.stop.wait(
10) # give some time for the GUI to start before we start on existing POW tasks. 10)
if state.shutdown == 0: if state.shutdown == 0:
# just in case there are any pending tasks for msg # just in case there are any pending tasks for msg
@ -109,62 +126,71 @@ class singleWorker(threading.Thread, StoppableThread):
if command == 'sendmessage': if command == 'sendmessage':
try: try:
self.sendMsg() self.sendMsg()
except: except BaseException:
pass pass
elif command == 'sendbroadcast': elif command == 'sendbroadcast':
try: try:
self.sendBroadcast() self.sendBroadcast()
except: except BaseException:
pass pass
elif command == 'doPOWForMyV2Pubkey': elif command == 'doPOWForMyV2Pubkey':
try: try:
self.doPOWForMyV2Pubkey(data) self.doPOWForMyV2Pubkey(data)
except: except BaseException:
pass pass
elif command == 'sendOutOrStoreMyV3Pubkey': elif command == 'sendOutOrStoreMyV3Pubkey':
try: try:
self.sendOutOrStoreMyV3Pubkey(data) self.sendOutOrStoreMyV3Pubkey(data)
except: except BaseException:
pass pass
elif command == 'sendOutOrStoreMyV4Pubkey': elif command == 'sendOutOrStoreMyV4Pubkey':
try: try:
self.sendOutOrStoreMyV4Pubkey(data) self.sendOutOrStoreMyV4Pubkey(data)
except: except BaseException:
pass pass
elif command == 'resetPoW': elif command == 'resetPoW':
try: try:
proofofwork.resetPoW() proofofwork.resetPoW()
except: except BaseException:
pass pass
elif command == 'stopThread': elif command == 'stopThread':
self.busy = 0 self.busy = 0
return return
else: else:
logger.error('Probable programming error: The command sent to the workerThread is weird. It is: %s\n' % command) logger.error(
'Probable programming error: The command sent\
to the workerThread is weird\
. It is: %s\n' % command)
queues.workerQueue.task_done() queues.workerQueue.task_done()
logger.info("Quitting...") logger.info("Quitting...")
def doPOWForMyV2Pubkey(self, hash): # This function also broadcasts out the pubkey message once it is done with the POW # This function also broadcasts out the pubkey message once
# it is done with the POW
def doPOWForMyV2Pubkey(self, hash):
# Look up my stream number based on my address hash # Look up my stream number based on my address hash
"""configSections = shared.config.addresses() # configSections = shared.config.addresses()
for addressInKeysFile in configSections: # for addressInKeysFile in configSections:
if addressInKeysFile <> 'bitmessagesettings': # if addressInKeysFile <> 'bitmessagesettings':
status,addressVersionNumber,streamNumber,hashFromThisParticularAddress = decodeAddress(addressInKeysFile) # status,addressVersionNumber,streamNumber,
if hash == hashFromThisParticularAddress: # hashFromThisParticularAddress =
myAddress = addressInKeysFile # decodeAddress(addressInKeysFile)
break""" # if hash == hashFromThisParticularAddress:
# myAddress = addressInKeysFile
# break
myAddress = shared.myAddressesByHash[hash] myAddress = shared.myAddressesByHash[hash]
status, addressVersionNumber, streamNumber, hash = decodeAddress( status, addressVersionNumber, streamNumber, hash = decodeAddress(
myAddress) myAddress)
TTL = int(28 * 24 * 60 * 60 + random.randrange(-300, 300))# 28 days from now plus or minus five minutes # 28 days from now plus or minus five minutes
TTL = int(28 * 24 * 60 * 60 + random.randrange(-300, 300))
embeddedTime = int(time.time() + TTL) embeddedTime = int(time.time() + TTL)
payload = pack('>Q', (embeddedTime)) payload = pack('>Q', (embeddedTime))
payload += '\x00\x00\x00\x01' # object type: pubkey payload += '\x00\x00\x00\x01' # object type: pubkey
payload += encodeVarint(addressVersionNumber) # Address version number payload += encodeVarint(addressVersionNumber) # Address version number
payload += encodeVarint(streamNumber) payload += encodeVarint(streamNumber)
payload += protocol.getBitfield(myAddress) # bitfield of features supported by me (see the wiki). # bitfield of features supported by me (see the wiki).
payload += protocol.getBitfield(myAddress)
try: try:
privSigningKeyBase58 = BMConfigParser().get( privSigningKeyBase58 = BMConfigParser().get(
@ -172,7 +198,10 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyBase58 = BMConfigParser().get( privEncryptionKeyBase58 = BMConfigParser().get(
myAddress, 'privencryptionkey') myAddress, 'privencryptionkey')
except Exception as err: except Exception as err:
logger.error('Error within doPOWForMyV2Pubkey. Could not read the keys from the keys.dat file for a requested address. %s\n' % err) logger.error(
'Error within doPOWForMyV2Pubkey. Could\
not read the keys from the keys.dat file\
for a requested address. %s\n' % err)
return return
privSigningKeyHex = hexlify(shared.decodeWalletImportFormat( privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
@ -188,17 +217,26 @@ class singleWorker(threading.Thread, StoppableThread):
payload += pubEncryptionKey[1:] payload += pubEncryptionKey[1:]
# Do the POW for this pubkey message # Do the POW for this pubkey message
target = 2 ** 64 / (defaults.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + defaults.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+defaults.networkDefaultPayloadLengthExtraBytes))/(2 ** 16))))
target = 2 ** 64 / (
defaults.networkDefaultProofOfWorkNonceTrialsPerByte * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes + (
(TTL * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes
)) / (2 ** 16))))
logger.info('(For pubkey message) Doing proof of work...') logger.info('(For pubkey message) Doing proof of work...')
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
trialValue, nonce = proofofwork.run(target, initialHash) trialValue, nonce = proofofwork.run(target, initialHash)
logger.info('(For pubkey message) Found proof of work ' + str(trialValue), ' Nonce: ', str(nonce)) logger.info('(For pubkey message) Found proof of work ' +
str(trialValue), ' Nonce: ', str(nonce))
payload = pack('>Q', nonce) + payload payload = pack('>Q', nonce) + payload
inventoryHash = calculateInventoryHash(payload) inventoryHash = calculateInventoryHash(payload)
objectType = 1 objectType = 1
Inventory()[inventoryHash] = ( Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime,'') objectType, streamNumber, payload, embeddedTime, '')
logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash)) logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash))
@ -208,7 +246,7 @@ class singleWorker(threading.Thread, StoppableThread):
BMConfigParser().set( BMConfigParser().set(
myAddress, 'lastpubkeysendtime', str(int(time.time()))) myAddress, 'lastpubkeysendtime', str(int(time.time())))
BMConfigParser().save() BMConfigParser().save()
except: except BaseException:
# The user deleted the address out of the keys.dat file before this # The user deleted the address out of the keys.dat file before this
# finished. # finished.
pass pass
@ -217,33 +255,40 @@ class singleWorker(threading.Thread, StoppableThread):
# does the necessary POW and sends it out. If it *is* a chan then it # does the necessary POW and sends it out. If it *is* a chan then it
# assembles the pubkey and stores is in the pubkey table so that we can # assembles the pubkey and stores is in the pubkey table so that we can
# send messages to "ourselves". # send messages to "ourselves".
def sendOutOrStoreMyV3Pubkey(self, hash): def sendOutOrStoreMyV3Pubkey(self, hash):
"""Send Out Or Store My V3 Pubkey.
This method send out or store the V3 pub key
"""
try: try:
myAddress = shared.myAddressesByHash[hash] myAddress = shared.myAddressesByHash[hash]
except: except BaseException:
#The address has been deleted. # The address has been deleted.
return return
if BMConfigParser().safeGetBoolean(myAddress, 'chan'): if BMConfigParser().safeGetBoolean(myAddress, 'chan'):
logger.info('This is a chan address. Not sending pubkey.') logger.info('This is a chan address. Not sending pubkey.')
return return
status, addressVersionNumber, streamNumber, hash = decodeAddress( status, addressVersionNumber, streamNumber, hash = decodeAddress(
myAddress) myAddress)
TTL = int(28 * 24 * 60 * 60 + random.randrange(-300, 300))# 28 days from now plus or minus five minutes # 28 days from now plus or minus five minutes
TTL = int(28 * 24 * 60 * 60 + random.randrange(-300, 300))
embeddedTime = int(time.time() + TTL) embeddedTime = int(time.time() + TTL)
signedTimeForProtocolV2 = embeddedTime - TTL signedTimeForProtocolV2 = embeddedTime - TTL
"""
According to the protocol specification, the expiresTime along with the pubkey information is # According to the protocol specification, the expiresTime along with
signed. But to be backwards compatible during the upgrade period, we shall sign not the # the pubkey information is signed. But to be backwards compatible
expiresTime but rather the current time. There must be precisely a 28 day difference # during the upgrade period, we shall sign not the expiresTime but
between the two. After the upgrade period we'll switch to signing the whole payload with the # rather the current time. There must be precisely a 28 day difference
expiresTime time. # between the two. After the upgrade period we'll switch to signing
""" # the whole payload with the expiresTime time.
payload = pack('>Q', (embeddedTime)) payload = pack('>Q', (embeddedTime))
payload += '\x00\x00\x00\x01' # object type: pubkey payload += '\x00\x00\x00\x01' # object type: pubkey
payload += encodeVarint(addressVersionNumber) # Address version number payload += encodeVarint(addressVersionNumber) # Address version number
payload += encodeVarint(streamNumber) payload += encodeVarint(streamNumber)
payload += protocol.getBitfield(myAddress) # bitfield of features supported by me (see the wiki). # bitfield of features supported by me (see the wiki).
payload += protocol.getBitfield(myAddress)
try: try:
privSigningKeyBase58 = BMConfigParser().get( privSigningKeyBase58 = BMConfigParser().get(
@ -251,7 +296,10 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyBase58 = BMConfigParser().get( privEncryptionKeyBase58 = BMConfigParser().get(
myAddress, 'privencryptionkey') myAddress, 'privencryptionkey')
except Exception as err: except Exception as err:
logger.error('Error within sendOutOrStoreMyV3Pubkey. Could not read the keys from the keys.dat file for a requested address. %s\n' % err) logger.error(
'Error within sendOutOrStoreMyV3Pubkey. Could not\
read the keys from the keys.dat file for a\
requested address. %s\n' % err)
return return
@ -271,23 +319,31 @@ class singleWorker(threading.Thread, StoppableThread):
myAddress, 'noncetrialsperbyte')) myAddress, 'noncetrialsperbyte'))
payload += encodeVarint(BMConfigParser().getint( payload += encodeVarint(BMConfigParser().getint(
myAddress, 'payloadlengthextrabytes')) myAddress, 'payloadlengthextrabytes'))
signature = highlevelcrypto.sign(payload, privSigningKeyHex) signature = highlevelcrypto.sign(payload, privSigningKeyHex)
payload += encodeVarint(len(signature)) payload += encodeVarint(len(signature))
payload += signature payload += signature
# Do the POW for this pubkey message # Do the POW for this pubkey message
target = 2 ** 64 / (defaults.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + defaults.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+defaults.networkDefaultPayloadLengthExtraBytes))/(2 ** 16)))) target = 2 ** 64 / (
defaults.networkDefaultProofOfWorkNonceTrialsPerByte * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes + (
(TTL * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes
)) / (2 ** 16))))
logger.info('(For pubkey message) Doing proof of work...') logger.info('(For pubkey message) Doing proof of work...')
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
trialValue, nonce = proofofwork.run(target, initialHash) trialValue, nonce = proofofwork.run(target, initialHash)
logger.info('(For pubkey message) Found proof of work. Nonce: ' + str(nonce)) logger.info(
'(For pubkey message) Found proof of work. Nonce: ' + str(nonce))
payload = pack('>Q', nonce) + payload payload = pack('>Q', nonce) + payload
inventoryHash = calculateInventoryHash(payload) inventoryHash = calculateInventoryHash(payload)
objectType = 1 objectType = 1
Inventory()[inventoryHash] = ( Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime,'') objectType, streamNumber, payload, embeddedTime, '')
logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash)) logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash))
@ -297,27 +353,28 @@ class singleWorker(threading.Thread, StoppableThread):
BMConfigParser().set( BMConfigParser().set(
myAddress, 'lastpubkeysendtime', str(int(time.time()))) myAddress, 'lastpubkeysendtime', str(int(time.time())))
BMConfigParser().save() BMConfigParser().save()
except: except BaseException:
# The user deleted the address out of the keys.dat file before this # The user deleted the address out of the keys.dat file before this
# finished. # finished.
pass pass
# If this isn't a chan address, this function assembles the pubkey data, # If this isn't a chan address, this function assembles the pubkey data,
# does the necessary POW and sends it out. # does the necessary POW and sends it out.
def sendOutOrStoreMyV4Pubkey(self, myAddress): def sendOutOrStoreMyV4Pubkey(self, myAddress):
if not BMConfigParser().has_section(myAddress): if not BMConfigParser().has_section(myAddress):
#The address has been deleted. # The address has been deleted.
return return
if shared.BMConfigParser().safeGetBoolean(myAddress, 'chan'): if shared.BMConfigParser().safeGetBoolean(myAddress, 'chan'):
logger.info('This is a chan address. Not sending pubkey.') logger.info('This is a chan address. Not sending pubkey.')
return return
status, addressVersionNumber, streamNumber, hash = decodeAddress( status, addressVersionNumber, streamNumber, hash = decodeAddress(
myAddress) myAddress)
TTL = int(28 * 24 * 60 * 60 + random.randrange(-300, 300))# 28 days from now plus or minus five minutes # 28 days from now plus or minus five minutes
TTL = int(28 * 24 * 60 * 60 + random.randrange(-300, 300))
embeddedTime = int(time.time() + TTL) embeddedTime = int(time.time() + TTL)
payload = pack('>Q', (embeddedTime)) payload = pack('>Q', (embeddedTime))
payload += '\x00\x00\x00\x01' # object type: pubkey payload += '\x00\x00\x00\x01' # object type: pubkey
payload += encodeVarint(addressVersionNumber) # Address version number payload += encodeVarint(addressVersionNumber) # Address version number
payload += encodeVarint(streamNumber) payload += encodeVarint(streamNumber)
dataToEncrypt = protocol.getBitfield(myAddress) dataToEncrypt = protocol.getBitfield(myAddress)
@ -328,7 +385,10 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyBase58 = BMConfigParser().get( privEncryptionKeyBase58 = BMConfigParser().get(
myAddress, 'privencryptionkey') myAddress, 'privencryptionkey')
except Exception as err: except Exception as err:
logger.error('Error within sendOutOrStoreMyV4Pubkey. Could not read the keys from the keys.dat file for a requested address. %s\n' % err) logger.error(
'Error within sendOutOrStoreMyV4Pubkey. Could not\
read the keys from the keys.dat file for a\
requested address. %s\n' % err)
return return
privSigningKeyHex = hexlify(shared.decodeWalletImportFormat( privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
@ -346,37 +406,51 @@ class singleWorker(threading.Thread, StoppableThread):
myAddress, 'noncetrialsperbyte')) myAddress, 'noncetrialsperbyte'))
dataToEncrypt += encodeVarint(BMConfigParser().getint( dataToEncrypt += encodeVarint(BMConfigParser().getint(
myAddress, 'payloadlengthextrabytes')) myAddress, 'payloadlengthextrabytes'))
# When we encrypt, we'll use a hash of the data # When we encrypt, we'll use a hash of the data contained in an
# contained in an address as a decryption key. This way in order to # address as a decryption key. This way in order to read the public
# read the public keys in a pubkey message, a node must know the address # keys in a pubkey message, a node must know the address first.
# first. We'll also tag, unencrypted, the pubkey with part of the hash # We'll also tag, unencrypted, the pubkey with part of the hash
# so that nodes know which pubkey object to try to decrypt when they # so that nodes know which pubkey object to try to decrypt when
# want to send a message. # they want to send a message.
doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(encodeVarint( doubleHashOfAddressData = hashlib.sha512(
addressVersionNumber) + encodeVarint(streamNumber) + hash).digest()).digest() hashlib.sha512(
payload += doubleHashOfAddressData[32:] # the tag encodeVarint(addressVersionNumber) +
signature = highlevelcrypto.sign(payload + dataToEncrypt, privSigningKeyHex) encodeVarint(streamNumber) +
hash).digest()).digest()
payload += doubleHashOfAddressData[32:] # the tag
signature = highlevelcrypto.sign(
payload + dataToEncrypt, privSigningKeyHex)
dataToEncrypt += encodeVarint(len(signature)) dataToEncrypt += encodeVarint(len(signature))
dataToEncrypt += signature dataToEncrypt += signature
privEncryptionKey = doubleHashOfAddressData[:32] privEncryptionKey = doubleHashOfAddressData[:32]
pubEncryptionKey = highlevelcrypto.pointMult(privEncryptionKey) pubEncryptionKey = highlevelcrypto.pointMult(privEncryptionKey)
payload += highlevelcrypto.encrypt( payload += highlevelcrypto.encrypt(
dataToEncrypt, hexlify(pubEncryptionKey)) dataToEncrypt, hexlify(pubEncryptionKey))
# Do the POW for this pubkey message # Do the POW for this pubkey message
target = 2 ** 64 / (defaults.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + defaults.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+defaults.networkDefaultPayloadLengthExtraBytes))/(2 ** 16)))) target = 2 ** 64 / (
defaults.networkDefaultProofOfWorkNonceTrialsPerByte * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes + (
(TTL * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes
)) / (2 ** 16))))
logger.info('(For pubkey message) Doing proof of work...') logger.info('(For pubkey message) Doing proof of work...')
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
trialValue, nonce = proofofwork.run(target, initialHash) trialValue, nonce = proofofwork.run(target, initialHash)
logger.info('(For pubkey message) Found proof of work ' + str(trialValue) + 'Nonce: ' + str(nonce)) logger.info('(For pubkey message) Found proof of work ' +
str(trialValue) + 'Nonce: ' + str(nonce))
payload = pack('>Q', nonce) + payload payload = pack('>Q', nonce) + payload
inventoryHash = calculateInventoryHash(payload) inventoryHash = calculateInventoryHash(payload)
objectType = 1 objectType = 1
Inventory()[inventoryHash] = ( Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime, doubleHashOfAddressData[32:]) objectType, streamNumber,
payload, embeddedTime,
doubleHashOfAddressData[32:])
logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash)) logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash))
@ -387,21 +461,30 @@ class singleWorker(threading.Thread, StoppableThread):
myAddress, 'lastpubkeysendtime', str(int(time.time()))) myAddress, 'lastpubkeysendtime', str(int(time.time())))
BMConfigParser().save() BMConfigParser().save()
except Exception as err: except Exception as err:
logger.error('Error: Couldn\'t add the lastpubkeysendtime to the keys.dat file. Error message: %s' % err) logger.error(
'Error: Couldn\'t add the lastpubkeysendtime to the\
keys.dat file. Error message: %s' % err)
def sendBroadcast(self): def sendBroadcast(self):
# Reset just in case # Reset just in case
sqlExecute( sqlExecute(
'''UPDATE sent SET status='broadcastqueued' WHERE status = 'doingbroadcastpow' ''') '''UPDATE sent SET status='broadcastqueued'
WHERE status = 'doingbroadcastpow' ''')
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''SELECT fromaddress, subject, message, ackdata, ttl, encodingtype FROM sent WHERE status=? and folder='sent' ''', 'broadcastqueued') '''SELECT fromaddress, subject, message,
ackdata, ttl, encodingtype FROM sent
WHERE status=? and folder='sent' ''', 'broadcastqueued')
for row in queryreturn: for row in queryreturn:
fromaddress, subject, body, ackdata, TTL, encoding = row fromaddress, subject, body, ackdata, TTL, encoding = row
status, addressVersionNumber, streamNumber, ripe = decodeAddress( status, addressVersionNumber, streamNumber, ripe = decodeAddress(
fromaddress) fromaddress)
if addressVersionNumber <= 1: if addressVersionNumber <= 1:
logger.error('Error: In the singleWorker thread, the sendBroadcast function doesn\'t understand the address version.\n') logger.error(
'Error: In the singleWorker thread,\
the sendBroadcast function \
doesn\'t understand the address\
version.\n')
return return
# We need to convert our private keys to public keys in order # We need to convert our private keys to public keys in order
# to include them. # to include them.
@ -410,13 +493,15 @@ class singleWorker(threading.Thread, StoppableThread):
fromaddress, 'privsigningkey') fromaddress, 'privsigningkey')
privEncryptionKeyBase58 = BMConfigParser().get( privEncryptionKeyBase58 = BMConfigParser().get(
fromaddress, 'privencryptionkey') fromaddress, 'privencryptionkey')
except: except BaseException:
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', ( queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (
ackdata, tr._translate("MainWindow", "Error! Could not find sender address (your address) in the keys.dat file.")))) ackdata, tr._translate("MainWindow", "Error! Could not find sender address (your address)\
in the keys.dat file."))))
continue continue
sqlExecute( sqlExecute(
'''UPDATE sent SET status='doingbroadcastpow' WHERE ackdata=? AND status='broadcastqueued' ''', '''UPDATE sent SET status='doingbroadcastpow'
WHERE ackdata=? AND status='broadcastqueued' ''',
ackdata) ackdata)
privSigningKeyHex = hexlify(shared.decodeWalletImportFormat( privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
@ -424,29 +509,36 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat( privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58)) privEncryptionKeyBase58))
pubSigningKey = highlevelcrypto.privToPub(privSigningKeyHex).decode( # At this time these pubkeys are 65 bytes long because they
'hex') # At this time these pubkeys are 65 bytes long because they include the encoding byte which we won't be sending in the broadcast message. # include the encoding byte which we won't be sending in the
# broadcast message.
pubSigningKey = highlevelcrypto.privToPub(
privSigningKeyHex).decode('hex')
pubEncryptionKey = unhexlify(highlevelcrypto.privToPub( pubEncryptionKey = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex)) privEncryptionKeyHex))
if TTL > 28 * 24 * 60 * 60: if TTL > 28 * 24 * 60 * 60:
TTL = 28 * 24 * 60 * 60 TTL = 28 * 24 * 60 * 60
if TTL < 60*60: if TTL < 60 * 60:
TTL = 60*60 TTL = 60 * 60
TTL = int(TTL + random.randrange(-300, 300))# add some randomness to the TTL # add some randomness to the TTL
TTL = int(TTL + random.randrange(-300, 300))
embeddedTime = int(time.time() + TTL) embeddedTime = int(time.time() + TTL)
payload = pack('>Q', embeddedTime) payload = pack('>Q', embeddedTime)
payload += '\x00\x00\x00\x03' # object type: broadcast payload += '\x00\x00\x00\x03' # object type: broadcast
if addressVersionNumber <= 3: if addressVersionNumber <= 3:
payload += encodeVarint(4) # broadcast version payload += encodeVarint(4) # broadcast version
else: else:
payload += encodeVarint(5) # broadcast version payload += encodeVarint(5) # broadcast version
payload += encodeVarint(streamNumber) payload += encodeVarint(streamNumber)
if addressVersionNumber >= 4: if addressVersionNumber >= 4:
doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(encodeVarint( doubleHashOfAddressData = hashlib.sha512(
addressVersionNumber) + encodeVarint(streamNumber) + ripe).digest()).digest() hashlib.sha512(
encodeVarint(addressVersionNumber) +
encodeVarint(streamNumber) +
ripe).digest()).digest()
tag = doubleHashOfAddressData[32:] tag = doubleHashOfAddressData[32:]
payload += tag payload += tag
else: else:
@ -454,31 +546,39 @@ class singleWorker(threading.Thread, StoppableThread):
dataToEncrypt = encodeVarint(addressVersionNumber) dataToEncrypt = encodeVarint(addressVersionNumber)
dataToEncrypt += encodeVarint(streamNumber) dataToEncrypt += encodeVarint(streamNumber)
dataToEncrypt += protocol.getBitfield(fromaddress) # behavior bitfield # behavior bitfield
dataToEncrypt += protocol.getBitfield(fromaddress)
dataToEncrypt += pubSigningKey[1:] dataToEncrypt += pubSigningKey[1:]
dataToEncrypt += pubEncryptionKey[1:] dataToEncrypt += pubEncryptionKey[1:]
if addressVersionNumber >= 3: if addressVersionNumber >= 3:
dataToEncrypt += encodeVarint(BMConfigParser().getint(fromaddress,'noncetrialsperbyte')) dataToEncrypt += encodeVarint(
dataToEncrypt += encodeVarint(BMConfigParser().getint(fromaddress,'payloadlengthextrabytes')) BMConfigParser().getint(fromaddress, 'noncetrialsperbyte'))
dataToEncrypt += encodeVarint(encoding) # message encoding type dataToEncrypt += encodeVarint(BMConfigParser().getint(
encodedMessage = helper_msgcoding.MsgEncode({"subject": subject, "body": body}, encoding) fromaddress, 'payloadlengthextrabytes'))
dataToEncrypt += encodeVarint(encoding) # message encoding type
encodedMessage = helper_msgcoding.MsgEncode(
{"subject": subject, "body": body}, encoding)
dataToEncrypt += encodeVarint(encodedMessage.length) dataToEncrypt += encodeVarint(encodedMessage.length)
dataToEncrypt += encodedMessage.data dataToEncrypt += encodedMessage.data
dataToSign = payload + dataToEncrypt dataToSign = payload + dataToEncrypt
signature = highlevelcrypto.sign( signature = highlevelcrypto.sign(
dataToSign, privSigningKeyHex) dataToSign, privSigningKeyHex)
dataToEncrypt += encodeVarint(len(signature)) dataToEncrypt += encodeVarint(len(signature))
dataToEncrypt += signature dataToEncrypt += signature
# Encrypt the broadcast with the information contained in the broadcaster's address. # Encrypt the broadcast with the information contained in the
# Anyone who knows the address can generate the private encryption key to decrypt # broadcaster's address.Anyone who knows the address can generate
# the broadcast. This provides virtually no privacy; its purpose is to keep # the private encryption key to decrypt the broadcast. This
# questionable and illegal content from flowing through the Internet connections # provides virtually no privacy; its purpose is to keep
# and being stored on the disk of 3rd parties. # questionable and illegal content from flowing through the
# Internet connections and being stored on the disk of 3rd parties.
if addressVersionNumber <= 3: if addressVersionNumber <= 3:
privEncryptionKey = hashlib.sha512(encodeVarint( privEncryptionKey = hashlib.sha512(
addressVersionNumber) + encodeVarint(streamNumber) + ripe).digest()[:32] encodeVarint(addressVersionNumber) +
encodeVarint(streamNumber) +
ripe).digest()[
:32]
else: else:
privEncryptionKey = doubleHashOfAddressData[:32] privEncryptionKey = doubleHashOfAddressData[:32]
@ -486,31 +586,51 @@ class singleWorker(threading.Thread, StoppableThread):
payload += highlevelcrypto.encrypt( payload += highlevelcrypto.encrypt(
dataToEncrypt, hexlify(pubEncryptionKey)) dataToEncrypt, hexlify(pubEncryptionKey))
target = 2 ** 64 / (defaults.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + defaults.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+defaults.networkDefaultPayloadLengthExtraBytes))/(2 ** 16)))) target = 2 ** 64 / (
logger.info('(For broadcast message) Doing proof of work...') defaults.networkDefaultProofOfWorkNonceTrialsPerByte * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes + (
(TTL * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes
)) / (2 ** 16))))
logger.info('(For broadcast message) Doing\
proof of work...')
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', ( queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (
ackdata, tr._translate("MainWindow", "Doing work necessary to send broadcast...")))) ackdata, tr._translate("MainWindow", "Doing work\
necessary to send broadcast..."))))
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
trialValue, nonce = proofofwork.run(target, initialHash) trialValue, nonce = proofofwork.run(target, initialHash)
logger.info('(For broadcast message) Found proof of work ' + str(trialValue) + ' Nonce: ' + str(nonce)) logger.info('(For broadcast message) Found proof of work ' +
str(trialValue) + ' Nonce: ' + str(nonce))
payload = pack('>Q', nonce) + payload payload = pack('>Q', nonce) + payload
# Sanity check. The payload size should never be larger than 256 KiB. There should # Sanity check. The payload size should never be larger
# be checks elsewhere in the code to not let the user try to send a message this large # than 256 KiB. There should
# until we implement message continuation. # be checks elsewhere in the code to not let the user try to send
if len(payload) > 2 ** 18: # 256 KiB # a message this large
logger.critical('This broadcast object is too large to send. This should never happen. Object size: %s' % len(payload)) # until we implement message continuation.
if len(payload) > 2 ** 18: # 256 KiB
logger.critical(
'This broadcast object is too large to\
send. This should never happen.\
Object size: %s' % len(payload))
continue continue
inventoryHash = calculateInventoryHash(payload) inventoryHash = calculateInventoryHash(payload)
objectType = 3 objectType = 3
Inventory()[inventoryHash] = ( Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime, tag) objectType, streamNumber, payload, embeddedTime, tag)
logger.info('sending inv (within sendBroadcast function) for object: ' + hexlify(inventoryHash)) logger.info(
'sending inv (within sendBroadcast function)\
for object: ' + hexlify(inventoryHash))
queues.invQueue.put((streamNumber, inventoryHash)) queues.invQueue.put((streamNumber, inventoryHash))
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (ackdata, tr._translate("MainWindow", "Broadcast sent on %1").arg(l10n.formatTimestamp())))) queues.UISignalQueue.put(
('updateSentItemStatusByAckdata', (ackdata, tr._translate(
"MainWindow", "Broadcast sent on %1").arg(
l10n.formatTimestamp()))))
# Update the status of the message in the 'sent' table to have # Update the status of the message in the 'sent' table to have
# a 'broadcastsent' status # a 'broadcastsent' status
@ -520,155 +640,249 @@ class singleWorker(threading.Thread, StoppableThread):
'broadcastsent', 'broadcastsent',
int(time.time()), int(time.time()),
ackdata) ackdata)
def sendMsg(self): def sendMsg(self):
# Reset just in case # Reset just in case
sqlExecute( sqlExecute(
'''UPDATE sent SET status='msgqueued' WHERE status IN ('doingpubkeypow', 'doingmsgpow')''') '''UPDATE sent SET status='msgqueued'\
WHERE status IN \
('doingpubkeypow', 'doingmsgpow')''')
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''SELECT toaddress, fromaddress, subject, message, ackdata, status, ttl, retrynumber, encodingtype FROM sent WHERE (status='msgqueued' or status='forcepow') and folder='sent' ''') '''SELECT toaddress, fromaddress, subject,\
for row in queryreturn: # while we have a msg that needs some work message, ackdata, status, ttl, retrynumber\
toaddress, fromaddress, subject, message, ackdata, status, TTL, retryNumber, encoding = row , encodingtype FROM sent WHERE \
toStatus, toAddressVersionNumber, toStreamNumber, toRipe = decodeAddress( (status='msgqueued' or status='forcepow')\
toaddress) and folder='sent' ''')
fromStatus, fromAddressVersionNumber, fromStreamNumber, fromRipe = decodeAddress( for row in queryreturn:
fromaddress) # while we have a msg that needs some work
toaddress, fromaddress, subject, message, ackdata,\
# We may or may not already have the pubkey for this toAddress. Let's check. status, TTL, retryNumber, encoding = row
toStatus, toAddressVersionNumber, toStreamNumber,\
toRipe = decodeAddress(toaddress)
fromStatus, fromAddressVersionNumber, fromStreamNumber,\
fromRipe = decodeAddress(fromaddress)
# We may or may not already have the pubkey for this toAddress.
# Let's check.
if status == 'forcepow': if status == 'forcepow':
# if the status of this msg is 'forcepow' then clearly we have the pubkey already # if the status of this msg is 'forcepow' then clearly we have
# because the user could not have overridden the message about the POW being # the pubkey already because the user could not have
# too difficult without knowing the required difficulty. # overridden the message about the POW being too difficult
# without knowing the required difficulty.
pass pass
elif status == 'doingmsgpow': elif status == 'doingmsgpow':
# We wouldn't have set the status to doingmsgpow if we didn't already have the pubkey # We wouldn't have set the status to doingmsgpow if we didn't
# so let's assume that we have it. # already have the pubkey so let's assume that we have it.
pass pass
# If we are sending a message to ourselves or a chan then we won't need an entry in the pubkeys table; we can calculate the needed pubkey using the private keys in our keys.dat file. # If we are sending a message to ourselves or a chan then we won't
# need an entry in the pubkeys table; we can calculate the needed
# pubkey using the private keys in our keys.dat file.
elif BMConfigParser().has_section(toaddress): elif BMConfigParser().has_section(toaddress):
sqlExecute( sqlExecute(
'''UPDATE sent SET status='doingmsgpow' WHERE toaddress=? AND status='msgqueued' ''', '''UPDATE sent SET status='doingmsgpow'\
WHERE toaddress=? AND \
status='msgqueued' ''',
toaddress) toaddress)
status='doingmsgpow' status = 'doingmsgpow'
elif status == 'msgqueued': elif status == 'msgqueued':
# Let's see if we already have the pubkey in our pubkeys table # Let's see if we already have the pubkey in our pubkeys table
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''SELECT address FROM pubkeys WHERE address=?''', toaddress) '''SELECT address FROM pubkeys
if queryreturn != []: # If we have the needed pubkey in the pubkey table already, WHERE address=?''', toaddress)
if queryreturn != []:
# If we have the needed pubkey in the pubkey table already,
# set the status of this msg to doingmsgpow # set the status of this msg to doingmsgpow
sqlExecute( sqlExecute(
'''UPDATE sent SET status='doingmsgpow' WHERE toaddress=? AND status='msgqueued' ''', '''UPDATE sent SET status='doingmsgpow'\
WHERE toaddress=? AND\
status='msgqueued' ''',
toaddress) toaddress)
status = 'doingmsgpow' status = 'doingmsgpow'
# mark the pubkey as 'usedpersonally' so that we don't delete it later. If the pubkey version # mark the pubkey as 'usedpersonally' so that we don't
# is >= 4 then usedpersonally will already be set to yes because we'll only ever have # delete it later.If the pubkey version is >= 4 then
# usedpersonally v4 pubkeys in the pubkeys table. # usedpersonally will already be set to yes because we'll
# only ever have usedpersonally v4 pubkeys in the pubkeys
# table.
sqlExecute( sqlExecute(
'''UPDATE pubkeys SET usedpersonally='yes' WHERE address=?''', '''UPDATE pubkeys SET \
usedpersonally='yes' \
WHERE address=?''',
toaddress) toaddress)
else: # We don't have the needed pubkey in the pubkeys table already. else:
# We don't have the needed pubkey in
# the pubkeys table already.
if toAddressVersionNumber <= 3: if toAddressVersionNumber <= 3:
toTag = '' toTag = ''
else: else:
toTag = hashlib.sha512(hashlib.sha512(encodeVarint(toAddressVersionNumber)+encodeVarint(toStreamNumber)+toRipe).digest()).digest()[32:] toTag = hashlib.sha512(
if toaddress in state.neededPubkeys or toTag in state.neededPubkeys: hashlib.sha512(
encodeVarint(toAddressVersionNumber) +
encodeVarint(toStreamNumber) +
toRipe).digest()).digest()[
32:]
if toaddress in state.neededPubkeys \
or toTag in state.neededPubkeys:
# We already sent a request for the pubkey # We already sent a request for the pubkey
sqlExecute( sqlExecute(
'''UPDATE sent SET status='awaitingpubkey', sleeptill=? WHERE toaddress=? AND status='msgqueued' ''', '''UPDATE sent SET
int(time.time()) + 2.5*24*60*60, status='awaitingpubkey',
sleeptill=? WHERE toaddress=?
AND status='msgqueued' ''',
int(time.time()) + 2.5 * 24 * 60 * 60,
toaddress) toaddress)
queues.UISignalQueue.put(('updateSentItemStatusByToAddress', ( queues.UISignalQueue.put(
toaddress, tr._translate("MainWindow",'Encryption key was requested earlier.')))) ('updateSentItemStatusByToAddress', (toaddress, tr._translate(
continue #on with the next msg on which we can do some work "MainWindow", 'Encryption key\
was requested earlier.'))))
continue
# on with the next msg on which we can do some work
else: else:
# We have not yet sent a request for the pubkey # We have not yet sent a request for the pubkey
needToRequestPubkey = True needToRequestPubkey = True
if toAddressVersionNumber >= 4: # If we are trying to send to address version >= 4 then if toAddressVersionNumber >= 4:
# the needed pubkey might be encrypted in the inventory. # If we are trying to send to address
# If we have it we'll need to decrypt it and put it in # version >= 4 then the needed pubkey might be
# the pubkeys table. # encrypted in the inventory.If we have it we'll
# need to decrypt it and put it in the pubkeys
# The decryptAndCheckPubkeyPayload function expects that the shared.neededPubkeys # table.The decryptAndCheckPubkeyPayload function
# dictionary already contains the toAddress and cryptor object associated with # expects that the shared.neededPubkeys dictionary
# the tag for this toAddress. # already contains the toAddress and cryptor object
doubleHashOfToAddressData = hashlib.sha512(hashlib.sha512(encodeVarint( # associated with the tag for this toAddress.
toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest() doubleHashOfToAddressData = hashlib.sha512(
privEncryptionKey = doubleHashOfToAddressData[:32] # The first half of the sha512 hash. hashlib.sha512(
tag = doubleHashOfToAddressData[32:] # The second half of the sha512 hash. encodeVarint(toAddressVersionNumber) +
state.neededPubkeys[tag] = (toaddress, highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))) encodeVarint(toStreamNumber) +
toRipe).digest()).digest()
# The first half of the sha512 hash.
privEncryptionKey = doubleHashOfToAddressData[:32]
# The second half of the sha512 hash.
tag = doubleHashOfToAddressData[32:]
state.neededPubkeys[tag] = (
toaddress, highlevelcrypto.makeCryptor(
hexlify(privEncryptionKey)))
for value in Inventory().by_type_and_tag(1, toTag): for value in Inventory().by_type_and_tag(1, toTag):
if shared.decryptAndCheckPubkeyPayload(value.payload, toaddress) == 'successful': #if valid, this function also puts it in the pubkeys table. # if valid, this function also puts it in the
# pubkeys table.
if shared.decryptAndCheckPubkeyPayload(
value.payload, toaddress) == 'successful':
needToRequestPubkey = False needToRequestPubkey = False
sqlExecute( sqlExecute(
'''UPDATE sent SET status='doingmsgpow', retrynumber=0 WHERE toaddress=? AND (status='msgqueued' or status='awaitingpubkey' or status='doingpubkeypow')''', '''UPDATE sent SET \
status='doingmsgpow', \
retrynumber=0 WHERE \
toaddress=? AND \
(status='msgqueued' or\
status='awaitingpubkey' or\
status='doingpubkeypow')''',
toaddress) toaddress)
del state.neededPubkeys[tag] del state.neededPubkeys[tag]
break break
#else: # There was something wrong with this pubkey object even # else:
# though it had the correct tag- almost certainly because # There was something wrong with this
# of malicious behavior or a badly programmed client. If # pubkey object even though it had the
# there are any other pubkeys in our inventory with the correct # correct tag- almost certainly because
# tag then we'll try to decrypt those. # of malicious behavior or a badly
# programmed client. If there are any
# other pubkeys in our inventory with
# the correct tag then we'll try to decrypt
# those.
if needToRequestPubkey: if needToRequestPubkey:
sqlExecute( sqlExecute(
'''UPDATE sent SET status='doingpubkeypow' WHERE toaddress=? AND status='msgqueued' ''', '''UPDATE sent SET status='doingpubkeypow' WHERE toaddress=? AND
status='msgqueued' ''',
toaddress) toaddress)
queues.UISignalQueue.put(('updateSentItemStatusByToAddress', ( queues.UISignalQueue.put(
toaddress, tr._translate("MainWindow",'Sending a request for the recipient\'s encryption key.')))) ('updateSentItemStatusByToAddress', (toaddress, tr._translate(
"MainWindow", 'Sending a request\
for the recipient\'s encryption key.'))))
self.requestPubKey(toaddress) self.requestPubKey(toaddress)
continue #on with the next msg on which we can do some work continue
# on with the next msg on which we can do some work
# At this point we know that we have the necessary pubkey in the pubkeys table. # At this point we know that we have the necessary
# pubkey in the pubkeys table.
TTL *= 2**retryNumber TTL *= 2**retryNumber
if TTL > 28 * 24 * 60 * 60: if TTL > 28 * 24 * 60 * 60:
TTL = 28 * 24 * 60 * 60 TTL = 28 * 24 * 60 * 60
TTL = int(TTL + random.randrange(-300, 300))# add some randomness to the TTL # add some randomness to the TTL
TTL = int(TTL + random.randrange(-300, 300))
embeddedTime = int(time.time() + TTL) embeddedTime = int(time.time() + TTL)
if not BMConfigParser().has_section(toaddress): # if we aren't sending this to ourselves or a chan
shared.ackdataForWhichImWatching[ackdata] = 0
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (
ackdata, tr._translate("MainWindow", "Looking up the receiver\'s public key"))))
logger.info('Sending a message.')
logger.debug('First 150 characters of message: ' + repr(message[:150]))
# Let us fetch the recipient's public key out of our database. If if not BMConfigParser().has_section(
# the required proof of work difficulty is too hard then we'll toaddress):
# abort. # if we aren't sending this to ourselves or a chan
shared.ackdataForWhichImWatching[ackdata] = 0
queues.UISignalQueue.put(
('updateSentItemStatusByAckdata', (ackdata, tr._translate(
"MainWindow", "Looking up\
the receiver\'s public key"))))
logger.info('Sending a message.')
logger.debug('First 150 characters of message: ' +
repr(message[:150]))
# Let us fetch the recipient's public key out of our
# database. # If the required proof of work difficulty
# is too hard then we'll abort.
queryreturn = sqlQuery( queryreturn = sqlQuery(
'SELECT transmitdata FROM pubkeys WHERE address=?', 'SELECT transmitdata FROM pubkeys WHERE address=?',
toaddress) toaddress)
for row in queryreturn: for row in queryreturn:
pubkeyPayload, = row pubkeyPayload, = row
# The pubkey message is stored with the following items all appended: # The pubkey message is stored with the following items all
# appended:
# -address version # -address version
# -stream number # -stream number
# -behavior bitfield # -behavior bitfield
# -pub signing key # -pub signing key
# -pub encryption key # -pub encryption key
# -nonce trials per byte (if address version is >= 3) # -nonce trials per byte (if address version is >= 3)
# -length extra bytes (if address version is >= 3) # -length extra bytes (if address version is >= 3)
readPosition = 1 # to bypass the address version whose length is definitely 1 readPosition = 1
# to bypass the address version whose length is definitely 1
streamNumber, streamNumberLength = decodeVarint( streamNumber, streamNumberLength = decodeVarint(
pubkeyPayload[readPosition:readPosition + 10]) pubkeyPayload[readPosition:readPosition + 10])
readPosition += streamNumberLength readPosition += streamNumberLength
behaviorBitfield = pubkeyPayload[readPosition:readPosition + 4] behaviorBitfield = pubkeyPayload[readPosition:readPosition + 4]
# Mobile users may ask us to include their address's RIPE hash on a message # Mobile users may ask us to include their address's
# unencrypted. Before we actually do it the sending human must check a box # RIPE hash on a message unencrypted. Before we
# in the settings menu to allow it. # actually do it the sending human must check a box
if shared.isBitSetWithinBitfield(behaviorBitfield,30): # if receiver is a mobile device who expects that their address RIPE is included unencrypted on the front of the message.. # in the settings menu to allow it.if receiver is a mobile
if not shared.BMConfigParser().safeGetBoolean('bitmessagesettings','willinglysendtomobile'): # if we are Not willing to include the receiver's RIPE hash on the message.. # device who expects that their address RIPE is included
logger.info('The receiver is a mobile user but the sender (you) has not selected that you are willing to send to mobiles. Aborting send.') # unencrypted on the front of the message..
queues.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,tr._translate("MainWindow",'Problem: Destination is a mobile device who requests that the destination be included in the message but this is disallowed in your settings. %1').arg(l10n.formatTimestamp())))) if shared.isBitSetWithinBitfield(behaviorBitfield, 30):
# if the human changes their setting and then sends another message or restarts their client, this one will send at that time. # if we are Not willing to include the receiver's RIPE hash
# on the message..
if not shared.BMConfigParser().safeGetBoolean(
'bitmessagesettings', 'willinglysendtomobile'):
logger.info(
'The receiver is a mobile user\
but the sender (you) has not \
selected that you are willing\
to send to mobiles. Aborting send.')
queues.UISignalQueue.put(
('updateSentItemStatusByAckdata',
(ackdata,
tr._translate(
"MainWindow",
'Problem: Destination is a \
mobile device who requests\
that the destination be\
included in the message\
but this is disallowed \
in your settings. %1').arg(
l10n.formatTimestamp()))))
# if the human changes their setting and then sends
# another message or restarts their client, this one
# will send at that time.
continue continue
readPosition += 4 # to bypass the bitfield of behaviors readPosition += 4
# pubSigningKeyBase256 = pubkeyPayload[readPosition:readPosition+64] # We don't use this key for anything here. # to bypass the bitfield of behaviors
# pubSigningKeyBase256 =
# pubkeyPayload[readPosition:readPosition+64] # We don't use
# this key for anything here.
readPosition += 64 readPosition += 64
pubEncryptionKeyBase256 = pubkeyPayload[ pubEncryptionKeyBase256 = pubkeyPayload[
readPosition:readPosition + 64] readPosition:readPosition + 64]
@ -676,45 +890,126 @@ class singleWorker(threading.Thread, StoppableThread):
# Let us fetch the amount of work required by the recipient. # Let us fetch the amount of work required by the recipient.
if toAddressVersionNumber == 2: if toAddressVersionNumber == 2:
requiredAverageProofOfWorkNonceTrialsPerByte = defaults.networkDefaultProofOfWorkNonceTrialsPerByte requiredAverageProofOfWorkNonceTrialsPerByte = \
defaults.networkDefaultProofOfWorkNonceTrialsPerByte
requiredPayloadLengthExtraBytes = defaults.networkDefaultPayloadLengthExtraBytes requiredPayloadLengthExtraBytes = defaults.networkDefaultPayloadLengthExtraBytes
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', ( queues.UISignalQueue.put(
ackdata, tr._translate("MainWindow", "Doing work necessary to send message.\nThere is no required difficulty for version 2 addresses like this.")))) ('updateSentItemStatusByAckdata',
(ackdata,
tr._translate(
"MainWindow",
"Doing work necessary to send \
message.\nThere is no required \
difficulty for version 2 addresses\
like this."))))
elif toAddressVersionNumber >= 3: elif toAddressVersionNumber >= 3:
requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint( requiredAverageProofOfWorkNonceTrialsPerByte, \
pubkeyPayload[readPosition:readPosition + 10]) varintLength = decodeVarint(
pubkeyPayload[readPosition:readPosition + 10])
readPosition += varintLength readPosition += varintLength
requiredPayloadLengthExtraBytes, varintLength = decodeVarint( requiredPayloadLengthExtraBytes, \
pubkeyPayload[readPosition:readPosition + 10]) varintLength = decodeVarint(
pubkeyPayload[readPosition:readPosition + 10])
readPosition += varintLength readPosition += varintLength
if requiredAverageProofOfWorkNonceTrialsPerByte < defaults.networkDefaultProofOfWorkNonceTrialsPerByte: # We still have to meet a minimum POW difficulty regardless of what they say is allowed in order to get our message to propagate through the network. # We still have to meet a minimum POW difficulty regardless
requiredAverageProofOfWorkNonceTrialsPerByte = defaults.networkDefaultProofOfWorkNonceTrialsPerByte # of what they say is allowed in order to get our message
if requiredPayloadLengthExtraBytes < defaults.networkDefaultPayloadLengthExtraBytes: # to propagate through the network.
requiredPayloadLengthExtraBytes = defaults.networkDefaultPayloadLengthExtraBytes if requiredAverageProofOfWorkNonceTrialsPerByte < \
logger.debug('Using averageProofOfWorkNonceTrialsPerByte: %s and payloadLengthExtraBytes: %s.' % (requiredAverageProofOfWorkNonceTrialsPerByte, requiredPayloadLengthExtraBytes)) defaults.networkDefaultProofOfWorkNonceTrialsPerByte:
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (ackdata, tr._translate("MainWindow", "Doing work necessary to send message.\nReceiver\'s required difficulty: %1 and %2").arg(str(float( requiredAverageProofOfWorkNonceTrialsPerByte = \
requiredAverageProofOfWorkNonceTrialsPerByte) / defaults.networkDefaultProofOfWorkNonceTrialsPerByte)).arg(str(float(requiredPayloadLengthExtraBytes) / defaults.networkDefaultPayloadLengthExtraBytes))))) defaults.networkDefaultProofOfWorkNonceTrialsPerByte
if requiredPayloadLengthExtraBytes < \
defaults.networkDefaultPayloadLengthExtraBytes:
requiredPayloadLengthExtraBytes = \
defaults.networkDefaultPayloadLengthExtraBytes
logger.debug(
'Using \
averageProofOfWorkNonceTrialsPerByte\
: %s and \
payloadLengthExtraBytes: %s.' %
(requiredAverageProofOfWorkNonceTrialsPerByte, requiredPayloadLengthExtraBytes))
queues.UISignalQueue.put(
('updateSentItemStatusByAckdata',
(ackdata,
tr._translate(
"MainWindow",
"Doing work necessary to \
send message.\nReceiver\'s \
required difficulty: %1 and %2").arg(
str(
float(requiredAverageProofOfWorkNonceTrialsPerByte) /
defaults.networkDefaultProofOfWorkNonceTrialsPerByte)).arg(
str(
float(requiredPayloadLengthExtraBytes) /
defaults.networkDefaultPayloadLengthExtraBytes)))))
if status != 'forcepow': if status != 'forcepow':
if (requiredAverageProofOfWorkNonceTrialsPerByte > BMConfigParser().getint('bitmessagesettings', 'maxacceptablenoncetrialsperbyte') and BMConfigParser().getint('bitmessagesettings', 'maxacceptablenoncetrialsperbyte') != 0) or (requiredPayloadLengthExtraBytes > BMConfigParser().getint('bitmessagesettings', 'maxacceptablepayloadlengthextrabytes') and BMConfigParser().getint('bitmessagesettings', 'maxacceptablepayloadlengthextrabytes') != 0): if (
# The demanded difficulty is more than we are willing requiredAverageProofOfWorkNonceTrialsPerByte >
# to do. BMConfigParser().getint(
'bitmessagesettings',
'maxacceptablenoncetrialsperbyte') and
BMConfigParser().getint(
'bitmessagesettings',
'maxacceptablenoncetrialsperbyte') != 0) or (
requiredPayloadLengthExtraBytes >
BMConfigParser().getint(
'bitmessagesettings',
'maxacceptablepayload\
lengthextrabytes') and
BMConfigParser().getint(
'bitmessagesettings',
'maxacceptablepayloadlengthextrabytes'
) != 0):
# The demanded difficulty is more than we are
# willing to do.
sqlExecute( sqlExecute(
'''UPDATE sent SET status='toodifficult' WHERE ackdata=? ''', '''UPDATE sent SET status='toodifficult' WHERE ackdata=? ''', ackdata)
ackdata) queues.UISignalQueue.put(
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (ackdata, tr._translate("MainWindow", "Problem: The work demanded by the recipient (%1 and %2) is more difficult than you are willing to do. %3").arg(str(float(requiredAverageProofOfWorkNonceTrialsPerByte) / defaults.networkDefaultProofOfWorkNonceTrialsPerByte)).arg(str(float( ('updateSentItemStatusByAckdata',
requiredPayloadLengthExtraBytes) / defaults.networkDefaultPayloadLengthExtraBytes)).arg(l10n.formatTimestamp())))) (ackdata,
tr._translate(
"MainWindow",
"Problem: The work demanded\
by the recipient\
(%1 and %2) is more\
difficult than you \
are willing to do. %3").arg(
str(
float(requiredAverageProofOfWorkNonceTrialsPerByte) /
defaults.networkDefaultProofOfWorkNonceTrialsPerByte)).arg(
str(
float(requiredPayloadLengthExtraBytes) /
defaults.networkDefaultPayloadLengthExtraBytes)).arg(
l10n.formatTimestamp()))))
continue continue
else: # if we are sending a message to ourselves or a chan.. else: # if we are sending a message to ourselves or a chan..
logger.info('Sending a message.') logger.info('Sending a message.')
logger.debug('First 150 characters of message: ' + repr(message[:150])) logger.debug('First 150 characters of message: ' +
repr(message[:150]))
behaviorBitfield = protocol.getBitfield(fromaddress) behaviorBitfield = protocol.getBitfield(fromaddress)
try: try:
privEncryptionKeyBase58 = BMConfigParser().get( privEncryptionKeyBase58 = BMConfigParser().get(
toaddress, 'privencryptionkey') toaddress, 'privencryptionkey')
except Exception as err: except Exception as err:
queues.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,tr._translate("MainWindow",'Problem: You are trying to send a message to yourself or a chan but your encryption key could not be found in the keys.dat file. Could not encrypt message. %1').arg(l10n.formatTimestamp())))) queues.UISignalQueue.put(
logger.error('Error within sendMsg. Could not read the keys from the keys.dat file for our own address. %s\n' % err) ('updateSentItemStatusByAckdata',
(ackdata,
tr._translate(
"MainWindow",
'Problem: You are trying to\
send a message to yourself\
or a chan but your encryption\
key could not be found in\
the keys.dat file. Could\
not encrypt message. %1').arg(
l10n.formatTimestamp()))))
logger.error(
'Error within sendMsg. \
Could not read the keys \
from the keys.dat file \
for our own address. %s\n' %
err)
continue continue
privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat( privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58)) privEncryptionKeyBase58))
@ -722,13 +1017,18 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyHex))[1:] privEncryptionKeyHex))[1:]
requiredAverageProofOfWorkNonceTrialsPerByte = defaults.networkDefaultProofOfWorkNonceTrialsPerByte requiredAverageProofOfWorkNonceTrialsPerByte = defaults.networkDefaultProofOfWorkNonceTrialsPerByte
requiredPayloadLengthExtraBytes = defaults.networkDefaultPayloadLengthExtraBytes requiredPayloadLengthExtraBytes = defaults.networkDefaultPayloadLengthExtraBytes
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', ( queues.UISignalQueue.put(
ackdata, tr._translate("MainWindow", "Doing work necessary to send message.")))) ('updateSentItemStatusByAckdata', (ackdata, tr._translate(
"MainWindow", "Doing work \
necessary to send message."))))
# Now we can start to assemble our message. # Now we can start to assemble our message.
payload = encodeVarint(fromAddressVersionNumber) payload = encodeVarint(fromAddressVersionNumber)
payload += encodeVarint(fromStreamNumber) payload += encodeVarint(fromStreamNumber)
payload += protocol.getBitfield(fromaddress) # Bitfield of features and behaviors that can be expected from me. (See https://bitmessage.org/wiki/Protocol_specification#Pubkey_bitfield_features ) # Bitfield of features and behaviors that can be expected from me.
# (See https://bitmessage.org/wiki/Protocol_specification
# Pubkey_bitfield_features)
payload += protocol.getBitfield(fromaddress)
# We need to convert our private keys to public keys in order # We need to convert our private keys to public keys in order
# to include them. # to include them.
@ -737,9 +1037,15 @@ class singleWorker(threading.Thread, StoppableThread):
fromaddress, 'privsigningkey') fromaddress, 'privsigningkey')
privEncryptionKeyBase58 = BMConfigParser().get( privEncryptionKeyBase58 = BMConfigParser().get(
fromaddress, 'privencryptionkey') fromaddress, 'privencryptionkey')
except: except BaseException:
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', ( queues.UISignalQueue.put(
ackdata, tr._translate("MainWindow", "Error! Could not find sender address (your address) in the keys.dat file.")))) ('updateSentItemStatusByAckdata',
(ackdata,
tr._translate(
"MainWindow",
"Error! Could not find sender\
address (your address) \
in the keys.dat file."))))
continue continue
privSigningKeyHex = hexlify(shared.decodeWalletImportFormat( privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
@ -752,8 +1058,10 @@ class singleWorker(threading.Thread, StoppableThread):
pubEncryptionKey = unhexlify(highlevelcrypto.privToPub( pubEncryptionKey = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex)) privEncryptionKeyHex))
payload += pubSigningKey[ # The \x04 on the beginning of the public keys are not sent. This
1:] # The \x04 on the beginning of the public keys are not sent. This way there is only one acceptable way to encode and send a public key. # way there is only one acceptable way to encode and send a public
# key.
payload += pubSigningKey[1:]
payload += pubEncryptionKey[1:] payload += pubEncryptionKey[1:]
if fromAddressVersionNumber >= 3: if fromAddressVersionNumber >= 3:
@ -761,7 +1069,8 @@ class singleWorker(threading.Thread, StoppableThread):
# subscriptions list, or whitelist then we will allow them to # subscriptions list, or whitelist then we will allow them to
# do the network-minimum proof of work. Let us check to see if # do the network-minimum proof of work. Let us check to see if
# the receiver is in any of those lists. # the receiver is in any of those lists.
if shared.isAddressInMyAddressBookSubscriptionsListOrWhitelist(toaddress): if shared.isAddressInMyAddressBookSubscriptionsListOrWhitelist(
toaddress):
payload += encodeVarint( payload += encodeVarint(
defaults.networkDefaultProofOfWorkNonceTrialsPerByte) defaults.networkDefaultProofOfWorkNonceTrialsPerByte)
payload += encodeVarint( payload += encodeVarint(
@ -772,91 +1081,164 @@ class singleWorker(threading.Thread, StoppableThread):
payload += encodeVarint(BMConfigParser().getint( payload += encodeVarint(BMConfigParser().getint(
fromaddress, 'payloadlengthextrabytes')) fromaddress, 'payloadlengthextrabytes'))
payload += toRipe # This hash will be checked by the receiver of the message to verify that toRipe belongs to them. This prevents a Surreptitious Forwarding Attack. # This hash will be checked by the receiver of the message to
payload += encodeVarint(encoding) # message encoding type # verify that toRipe belongs to them. This prevents a Surreptitious
encodedMessage = helper_msgcoding.MsgEncode({"subject": subject, "body": message}, encoding) # Forwarding Attack.
payload += toRipe
payload += encodeVarint(encoding) # message encoding type
encodedMessage = helper_msgcoding.MsgEncode(
{"subject": subject, "body": message}, encoding)
payload += encodeVarint(encodedMessage.length) payload += encodeVarint(encodedMessage.length)
payload += encodedMessage.data payload += encodedMessage.data
if BMConfigParser().has_section(toaddress): if BMConfigParser().has_section(toaddress):
logger.info('Not bothering to include ackdata because we are sending to ourselves or a chan.') logger.info(
'Not bothering to include \
ackdata because we are \
sending to ourselves or a chan.')
fullAckPayload = ''
elif not protocol.checkBitfield(
behaviorBitfield, protocol.BITFIELD_DOESACK):
logger.info(
'Not bothering to include \
ackdata because the receiver\
said that they won\'t relay it anyway.')
fullAckPayload = '' fullAckPayload = ''
elif not protocol.checkBitfield(behaviorBitfield, protocol.BITFIELD_DOESACK):
logger.info('Not bothering to include ackdata because the receiver said that they won\'t relay it anyway.')
fullAckPayload = ''
else: else:
# The fullAckPayload is a normal msg protocol message with the
# proof of work already completed that the receiver of this
# message can easily send out.
fullAckPayload = self.generateFullAckMessage( fullAckPayload = self.generateFullAckMessage(
ackdata, toStreamNumber, TTL) # The fullAckPayload is a normal msg protocol message with the proof of work already completed that the receiver of this message can easily send out. ackdata, toStreamNumber, TTL)
payload += encodeVarint(len(fullAckPayload)) payload += encodeVarint(len(fullAckPayload))
payload += fullAckPayload payload += fullAckPayload
dataToSign = pack('>Q', embeddedTime) + '\x00\x00\x00\x02' + encodeVarint(1) + encodeVarint(toStreamNumber) + payload dataToSign = pack('>Q', embeddedTime) + '\x00\x00\x00\x02' + \
encodeVarint(1) + encodeVarint(toStreamNumber) + payload
signature = highlevelcrypto.sign(dataToSign, privSigningKeyHex) signature = highlevelcrypto.sign(dataToSign, privSigningKeyHex)
payload += encodeVarint(len(signature)) payload += encodeVarint(len(signature))
payload += signature payload += signature
# We have assembled the data that will be encrypted. # We have assembled the data that will be encrypted.
try: try:
encrypted = highlevelcrypto.encrypt(payload,"04"+hexlify(pubEncryptionKeyBase256)) encrypted = highlevelcrypto.encrypt(
except: payload, "04" + hexlify(pubEncryptionKeyBase256))
sqlExecute('''UPDATE sent SET status='badkey' WHERE ackdata=?''', ackdata) except BaseException:
queues.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,tr._translate("MainWindow",'Problem: The recipient\'s encryption key is no good. Could not encrypt message. %1').arg(l10n.formatTimestamp())))) sqlExecute(
'''UPDATE sent SET status='badkey' WHERE ackdata=?''',
ackdata)
queues.UISignalQueue.put(
('updateSentItemStatusByAckdata',
(ackdata,
tr._translate(
"MainWindow",
'Problem: The recipient\'s\
encryption key is no good.\
Could not encrypt message. %1').arg(
l10n.formatTimestamp()))))
continue continue
encryptedPayload = pack('>Q', embeddedTime) encryptedPayload = pack('>Q', embeddedTime)
encryptedPayload += '\x00\x00\x00\x02' # object type: msg encryptedPayload += '\x00\x00\x00\x02' # object type: msg
encryptedPayload += encodeVarint(1) # msg version encryptedPayload += encodeVarint(1) # msg version
encryptedPayload += encodeVarint(toStreamNumber) + encrypted encryptedPayload += encodeVarint(toStreamNumber) + encrypted
target = 2 ** 64 / (requiredAverageProofOfWorkNonceTrialsPerByte*(len(encryptedPayload) + 8 + requiredPayloadLengthExtraBytes + ((TTL*(len(encryptedPayload)+8+requiredPayloadLengthExtraBytes))/(2 ** 16)))) target = 2 ** 64 / (
logger.info('(For msg message) Doing proof of work. Total required difficulty: %f. Required small message difficulty: %f.', float(requiredAverageProofOfWorkNonceTrialsPerByte) / defaults.networkDefaultProofOfWorkNonceTrialsPerByte, float(requiredPayloadLengthExtraBytes) / defaults.networkDefaultPayloadLengthExtraBytes) requiredAverageProofOfWorkNonceTrialsPerByte * (
len(encryptedPayload) + 8 +
requiredPayloadLengthExtraBytes + (
(TTL * (
len(encryptedPayload) + 8 +
requiredPayloadLengthExtraBytes
)) / (2 ** 16))))
logger.info(
'(For msg message) Doing proof\
of work. Total required difficulty\
: %f. Required small message\
difficulty: %f.',
float(requiredAverageProofOfWorkNonceTrialsPerByte) /
defaults.networkDefaultProofOfWorkNonceTrialsPerByte,
float(requiredPayloadLengthExtraBytes) /
defaults.networkDefaultPayloadLengthExtraBytes)
powStartTime = time.time() powStartTime = time.time()
initialHash = hashlib.sha512(encryptedPayload).digest() initialHash = hashlib.sha512(encryptedPayload).digest()
trialValue, nonce = proofofwork.run(target, initialHash) trialValue, nonce = proofofwork.run(target, initialHash)
logger.info('(For msg message) Found proof of work ' + str(trialValue) + ' Nonce: ' + str(nonce)) logger.info('(For msg message) Found proof of work ' +
str(trialValue) + ' Nonce: ' + str(nonce))
try: try:
logger.info('PoW took %.1f seconds, speed %s.', time.time() - powStartTime, sizeof_fmt(nonce / (time.time() - powStartTime))) logger.info('PoW took %.1f seconds, speed %s.',
except: time.time() - powStartTime,
sizeof_fmt(nonce / (time.time() - powStartTime)))
except BaseException:
pass pass
encryptedPayload = pack('>Q', nonce) + encryptedPayload encryptedPayload = pack('>Q', nonce) + encryptedPayload
# Sanity check. The encryptedPayload size should never be larger than 256 KiB. There should # Sanity check. The encryptedPayload size should never be
# be checks elsewhere in the code to not let the user try to send a message this large # larger than 256 KiB. There should be checks elsewhere
# until we implement message continuation. # in the code to not let the user try to send a message
if len(encryptedPayload) > 2 ** 18: # 256 KiB # this large until we implement message continuation.
logger.critical('This msg object is too large to send. This should never happen. Object size: %s' % len(encryptedPayload)) if len(encryptedPayload) > 2 ** 18: # 256 KiB
logger.critical(
'This msg object is too large\
to send. This should never \
happen. Object size: {}'.format(len(encryptedPayload)))
continue continue
inventoryHash = calculateInventoryHash(encryptedPayload) inventoryHash = calculateInventoryHash(encryptedPayload)
objectType = 2 objectType = 2
Inventory()[inventoryHash] = ( Inventory()[inventoryHash] = (
objectType, toStreamNumber, encryptedPayload, embeddedTime, '') objectType, toStreamNumber, encryptedPayload, embeddedTime, '')
if BMConfigParser().has_section(toaddress) or not protocol.checkBitfield(behaviorBitfield, protocol.BITFIELD_DOESACK): if BMConfigParser().has_section(toaddress) \
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (ackdata, tr._translate("MainWindow", "Message sent. Sent at %1").arg(l10n.formatTimestamp())))) or not protocol.checkBitfield(
behaviorBitfield, protocol.BITFIELD_DOESACK):
queues.UISignalQueue.put(
('updateSentItemStatusByAckdata', (ackdata, tr._translate(
"MainWindow", "Message sent\
. Sent at %1").arg(
l10n.formatTimestamp()))))
else: else:
# not sending to a chan or one of my addresses # not sending to a chan or one of my addresses
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (ackdata, tr._translate("MainWindow", "Message sent. Waiting for acknowledgement. Sent on %1").arg(l10n.formatTimestamp())))) queues.UISignalQueue.put(
logger.info('Broadcasting inv for my msg(within sendmsg function):' + hexlify(inventoryHash)) ('updateSentItemStatusByAckdata', (ackdata, tr._translate(
"MainWindow", "Message sent\
. Waiting for acknowledgement\
. Sent on %1").arg(
l10n.formatTimestamp()))))
logger.info(
'Broadcasting inv for my msg\
(within sendmsg function):' +
hexlify(inventoryHash))
queues.invQueue.put((toStreamNumber, inventoryHash)) queues.invQueue.put((toStreamNumber, inventoryHash))
# Update the sent message in the sent table with the necessary information. # Update the sent message in the sent table with the necessary
if BMConfigParser().has_section(toaddress) or not protocol.checkBitfield(behaviorBitfield, protocol.BITFIELD_DOESACK): # information.
if BMConfigParser()\
.has_section(toaddress) or not protocol.checkBitfield(
behaviorBitfield, protocol.BITFIELD_DOESACK):
newStatus = 'msgsentnoackexpected' newStatus = 'msgsentnoackexpected'
else: else:
newStatus = 'msgsent' newStatus = 'msgsent'
# wait 10% past expiration # wait 10% past expiration
sleepTill = int(time.time() + TTL * 1.1) sleepTill = int(time.time() + TTL * 1.1)
sqlExecute('''UPDATE sent SET msgid=?, status=?, retrynumber=?, sleeptill=?, lastactiontime=? WHERE ackdata=?''', sqlExecute(
inventoryHash, '''UPDATE sent SET msgid=?,
newStatus, status=?, retrynumber=?,
retryNumber+1, sleeptill=?,
sleepTill, lastactiontime=? WHERE ackdata=?''',
int(time.time()), inventoryHash,
ackdata) newStatus,
retryNumber +
1,
sleepTill,
int(
time.time()),
ackdata)
# If we are sending to ourselves or a chan, let's put the message in # If we are sending to ourselves or a chan, let's put the message
# our own inbox. # in our own inbox.
if BMConfigParser().has_section(toaddress): if BMConfigParser().has_section(toaddress):
sigHash = hashlib.sha512(hashlib.sha512(signature).digest()).digest()[32:] # Used to detect and ignore duplicate messages in our inbox # Used to detect and ignore duplicate messages in our inbox
sigHash = hashlib.sha512(hashlib.sha512(
signature).digest()).digest()[32:]
t = (inventoryHash, toaddress, fromaddress, subject, int( t = (inventoryHash, toaddress, fromaddress, subject, int(
time.time()), message, 'inbox', encoding, 0, sigHash) time.time()), message, 'inbox', encoding, 0, sigHash)
helper_inbox.insert(t) helper_inbox.insert(t)
@ -871,7 +1253,7 @@ class singleWorker(threading.Thread, StoppableThread):
try: try:
apiNotifyPath = BMConfigParser().get( apiNotifyPath = BMConfigParser().get(
'bitmessagesettings', 'apinotifypath') 'bitmessagesettings', 'apinotifypath')
except: except BaseException:
apiNotifyPath = '' apiNotifyPath = ''
if apiNotifyPath != '': if apiNotifyPath != '':
call([apiNotifyPath, "newMessage"]) call([apiNotifyPath, "newMessage"])
@ -880,111 +1262,187 @@ class singleWorker(threading.Thread, StoppableThread):
toStatus, addressVersionNumber, streamNumber, ripe = decodeAddress( toStatus, addressVersionNumber, streamNumber, ripe = decodeAddress(
toAddress) toAddress)
if toStatus != 'success': if toStatus != 'success':
logger.error('Very abnormal error occurred in requestPubKey. toAddress is: ' + repr( logger.error(
toAddress) + '. Please report this error to Atheros.') 'Very abnormal error occurred in \
requestPubKey. toAddress is: ' +
repr(toAddress) +
'. Please report this error to Atheros.')
return return
queryReturn = sqlQuery( queryReturn = sqlQuery(
'''SELECT retrynumber FROM sent WHERE toaddress=? AND (status='doingpubkeypow' OR status='awaitingpubkey') LIMIT 1''', '''SELECT retrynumber FROM sent \
WHERE toaddress=? AND \
(status='doingpubkeypow' OR \
status='awaitingpubkey') LIMIT 1''',
toAddress) toAddress)
if len(queryReturn) == 0: if len(queryReturn) == 0:
logger.critical("BUG: Why are we requesting the pubkey for %s if there are no messages in the sent folder to that address?" % toAddress) logger.critical(
"BUG: Why are we requesting the \
pubkey for %s if there are no \
messages in the sent folder \
to that address?" %
toAddress)
return return
retryNumber = queryReturn[0][0] retryNumber = queryReturn[0][0]
if addressVersionNumber <= 3: if addressVersionNumber <= 3:
state.neededPubkeys[toAddress] = 0 state.neededPubkeys[toAddress] = 0
elif addressVersionNumber >= 4: elif addressVersionNumber >= 4:
# If the user just clicked 'send' then the tag (and other information) will already # If the user just clicked 'send' then the tag (and other
# be in the neededPubkeys dictionary. But if we are recovering from a restart # information) will already be in the neededPubkeys dictionary.
# of the client then we have to put it in now. # But if we are recovering from a restart of the client then we
privEncryptionKey = hashlib.sha512(hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()).digest()[:32] # Note that this is the first half of the sha512 hash. # have to put it in now.Note that this is the first half of the
tag = hashlib.sha512(hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()).digest()[32:] # Note that this is the second half of the sha512 hash. # sha512 hash.
privEncryptionKey = hashlib.sha512(
hashlib.sha512(
encodeVarint(addressVersionNumber) +
encodeVarint(streamNumber) +
ripe).digest()).digest()[
:32]
# Note that this is the second half of the sha512 hash.
tag = hashlib.sha512(
hashlib.sha512(
encodeVarint(addressVersionNumber) +
encodeVarint(streamNumber) +
ripe).digest()).digest()[
32:]
if tag not in state.neededPubkeys: if tag not in state.neededPubkeys:
state.neededPubkeys[tag] = (toAddress, highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))) # We'll need this for when we receive a pubkey reply: it will be encrypted and we'll need to decrypt it. # We'll need this for when we receive a pubkey reply: it will
# be encrypted and we'll need to decrypt it.
TTL = 2.5*24*60*60 # 2.5 days. This was chosen fairly arbitrarily. state.neededPubkeys[tag] = (
TTL *= 2**retryNumber toAddress, highlevelcrypto.makeCryptor(
if TTL > 28*24*60*60: hexlify(privEncryptionKey)))
TTL = 28*24*60*60
TTL = TTL + random.randrange(-300, 300) # add some randomness to the TTL TTL = 2.5 * 24 * 60 * 60 # 2.5 days. This was chosen fairly
# arbitrarily.
TTL *= 2 ** retryNumber
if TTL > 28 * 24 * 60 * 60:
TTL = 28 * 24 * 60 * 60
# add some randomness to the TTL
TTL = TTL + random.randrange(-300, 300)
embeddedTime = int(time.time() + TTL) embeddedTime = int(time.time() + TTL)
payload = pack('>Q', embeddedTime) payload = pack('>Q', embeddedTime)
payload += '\x00\x00\x00\x00' # object type: getpubkey payload += '\x00\x00\x00\x00' # object type: getpubkey
payload += encodeVarint(addressVersionNumber) payload += encodeVarint(addressVersionNumber)
payload += encodeVarint(streamNumber) payload += encodeVarint(streamNumber)
if addressVersionNumber <= 3: if addressVersionNumber <= 3:
payload += ripe payload += ripe
logger.info('making request for pubkey with ripe: %s', hexlify(ripe)) logger.info('making request for pubkey\
with ripe: %s',
hexlify(ripe))
else: else:
payload += tag payload += tag
logger.info('making request for v4 pubkey with tag: %s', hexlify(tag)) logger.info(
'making request for v4 pubkey\
with tag: %s', hexlify(tag))
# print 'trial value', trialValue # print 'trial value', trialValue
statusbar = 'Doing the computations necessary to request the recipient\'s public key.' statusbar = 'Doing the computations\
necessary to request the \
recipient\'s public key.'
queues.UISignalQueue.put(('updateStatusBar', statusbar)) queues.UISignalQueue.put(('updateStatusBar', statusbar))
queues.UISignalQueue.put(('updateSentItemStatusByToAddress', ( queues.UISignalQueue.put(
toAddress, tr._translate("MainWindow",'Doing work necessary to request encryption key.')))) ('updateSentItemStatusByToAddress',
(toAddress,
target = 2 ** 64 / (defaults.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + defaults.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+defaults.networkDefaultPayloadLengthExtraBytes))/(2 ** 16)))) tr._translate(
"MainWindow",
'Doing work necessary to \
request encryption key.'))))
target = 2 ** 64 / (
defaults.networkDefaultProofOfWorkNonceTrialsPerByte * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes + (
(TTL * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes
)) / (2 ** 16))))
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
trialValue, nonce = proofofwork.run(target, initialHash) trialValue, nonce = proofofwork.run(target, initialHash)
logger.info('Found proof of work ' + str(trialValue) + ' Nonce: ' + str(nonce)) logger.info('Found proof of work ' +
str(trialValue) + ' Nonce: ' + str(nonce))
payload = pack('>Q', nonce) + payload payload = pack('>Q', nonce) + payload
inventoryHash = calculateInventoryHash(payload) inventoryHash = calculateInventoryHash(payload)
objectType = 1 objectType = 1
Inventory()[inventoryHash] = ( Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime, '') objectType, streamNumber, payload, embeddedTime, '')
logger.info('sending inv (for the getpubkey message)') logger.info('sending inv \
(for the getpubkey message)')
queues.invQueue.put((streamNumber, inventoryHash)) queues.invQueue.put((streamNumber, inventoryHash))
# wait 10% past expiration # wait 10% past expiration
sleeptill = int(time.time() + TTL * 1.1) sleeptill = int(time.time() + TTL * 1.1)
sqlExecute( sqlExecute(
'''UPDATE sent SET lastactiontime=?, status='awaitingpubkey', retrynumber=?, sleeptill=? WHERE toaddress=? AND (status='doingpubkeypow' OR status='awaitingpubkey') ''', '''UPDATE sent SET lastactiontime=?,
status='awaitingpubkey',
retrynumber=?,
sleeptill=? WHERE toaddress=? AND
(status='doingpubkeypow' OR status='awaitingpubkey') ''',
int(time.time()), int(time.time()),
retryNumber+1, retryNumber + 1,
sleeptill, sleeptill,
toAddress) toAddress)
queues.UISignalQueue.put(( queues.UISignalQueue.put((
'updateStatusBar', tr._translate("MainWindow",'Broadcasting the public key request. This program will auto-retry if they are offline.'))) 'updateStatusBar', tr._translate("MainWindow", 'Broadcasting the public key request.\
queues.UISignalQueue.put(('updateSentItemStatusByToAddress', (toAddress, tr._translate("MainWindow",'Sending public key request. Waiting for reply. Requested at %1').arg(l10n.formatTimestamp())))) This program will\
auto-retry if they\
are offline.')))
queues.UISignalQueue.put(
('updateSentItemStatusByToAddress',
(toAddress,
tr._translate(
"MainWindow",
'Sending public key request\
. Waiting for reply. Requested at\
%1').arg(
l10n.formatTimestamp()))))
def generateFullAckMessage(self, ackdata, toStreamNumber, TTL): def generateFullAckMessage(self, ackdata, toStreamNumber, TTL):
# It might be perfectly fine to just use the same TTL for # It might be perfectly fine to just use the same TTL for
# the ackdata that we use for the message. But I would rather # the ackdata that we use for the message. But I would rather
# it be more difficult for attackers to associate ackData with # it be more difficult for attackers to associate ackData with
# the associated msg object. However, users would want the TTL # the associated msg object. However, users would want the TTL
# of the acknowledgement to be about the same as they set # of the acknowledgement to be about the same as they set
# for the message itself. So let's set the TTL of the # for the message itself. So let's set the TTL of the
# acknowledgement to be in one of three 'buckets': 1 hour, 7 # acknowledgement to be in one of three 'buckets': 1 hour, 7
# days, or 28 days, whichever is relatively close to what the # days, or 28 days, whichever is relatively close to what the
# user specified. # user specified.
if TTL < 24*60*60: # 1 day if TTL < 24 * 60 * 60: # 1 day
TTL = 24*60*60 # 1 day TTL = 24 * 60 * 60 # 1 day
elif TTL < 7*24*60*60: # 1 week elif TTL < 7 * 24 * 60 * 60: # 1 week
TTL = 7*24*60*60 # 1 week TTL = 7 * 24 * 60 * 60 # 1 week
else: else:
TTL = 28*24*60*60 # 4 weeks TTL = 28 * 24 * 60 * 60 # 4 weeks
TTL = int(TTL + random.randrange(-300, 300)) # Add some randomness to the TTL # Add some randomness to the TTL
TTL = int(TTL + random.randrange(-300, 300))
embeddedTime = int(time.time() + TTL) embeddedTime = int(time.time() + TTL)
# type/version/stream already included # type/version/stream already included
payload = pack('>Q', (embeddedTime)) + ackdata payload = pack('>Q', (embeddedTime)) + ackdata
target = 2 ** 64 / (defaults.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + defaults.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+defaults.networkDefaultPayloadLengthExtraBytes))/(2 ** 16)))) target = 2 ** 64 / (
logger.info('(For ack message) Doing proof of work. TTL set to ' + str(TTL)) defaults.networkDefaultProofOfWorkNonceTrialsPerByte * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes + (
(TTL * (
len(payload) + 8 +
defaults.networkDefaultPayloadLengthExtraBytes
)) / (2 ** 16))))
logger.info(
'(For ack message) Doing proof of work. TTL set to ' + str(TTL))
powStartTime = time.time() powStartTime = time.time()
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
trialValue, nonce = proofofwork.run(target, initialHash) trialValue, nonce = proofofwork.run(target, initialHash)
logger.info('(For ack message) Found proof of work ' + str(trialValue) + ' Nonce: ' + str(nonce)) logger.info('(For ack message) Found proof of work ' +
str(trialValue) + ' Nonce: ' + str(nonce))
try: try:
logger.info('PoW took %.1f seconds, speed %s.', time.time() - powStartTime, sizeof_fmt(nonce / (time.time() - powStartTime))) logger.info('PoW took %.1f seconds, speed %s.', time.time(
except: ) - powStartTime, sizeof_fmt(nonce / (time.time() - powStartTime)))
except BaseException:
pass pass
payload = pack('>Q', nonce) + payload payload = pack('>Q', nonce) + payload

View File

@ -4,14 +4,16 @@ import sys
import os import os
import pyelliptic.openssl import pyelliptic.openssl
#Only really old versions of Python don't have sys.hexversion. We don't support # Only really old versions of Python don't have sys.hexversion. We don't
#them. The logging module was introduced in Python 2.3 # support them. The logging module was introduced in Python 2.3
if not hasattr(sys, 'hexversion') or sys.hexversion < 0x20300F0: if not hasattr(sys, 'hexversion') or sys.hexversion < 0x20300F0:
sys.stdout.write('Python version: ' + sys.version) sys.stdout.write('Python version: ' + sys.version)
sys.stdout.write('PyBitmessage requires Python 2.7.3 or greater (but not Python 3)') sys.stdout.write(
'PyBitmessage requires Python 2.7.3\
or greater (but not Python 3)')
sys.exit() sys.exit()
#We can now use logging so set up a simple configuration # We can now use logging so set up a simple configuration
import logging import logging
formatter = logging.Formatter( formatter = logging.Formatter(
'%(levelname)s: %(message)s' '%(levelname)s: %(message)s'
@ -22,28 +24,43 @@ logger = logging.getLogger(__name__)
logger.addHandler(handler) logger.addHandler(handler)
logger.setLevel(logging.ERROR) logger.setLevel(logging.ERROR)
#We need to check hashlib for RIPEMD-160, as it won't be available if OpenSSL is # We need to check hashlib for RIPEMD-160, as it won't be
#not linked against or the linked OpenSSL has RIPEMD disabled. # available if OpenSSL # is not linked against or the
# linked OpenSSL has RIPEMD disabled.
def check_hashlib(): def check_hashlib():
if sys.hexversion < 0x020500F0: if sys.hexversion < 0x020500F0:
logger.error('The hashlib module is not included in this version of Python.') logger.error(
'The hashlib module is not included\
in this version of Python.')
return False return False
import hashlib import hashlib
if '_hashlib' not in hashlib.__dict__: if '_hashlib' not in hashlib.__dict__:
logger.error('The RIPEMD-160 hash algorithm is not available. The hashlib module is not linked against OpenSSL.') logger.error(
'The RIPEMD-160 hash algorithm is not available\
. The hashlib module is not linked against OpenSSL.')
return False return False
try: try:
hashlib.new('ripemd160') hashlib.new('ripemd160')
except ValueError: except ValueError:
logger.error('The RIPEMD-160 hash algorithm is not available. The hashlib module utilizes an OpenSSL library with RIPEMD disabled.') logger.error(
'The RIPEMD-160 hash algorithm is not available\
. The hashlib module utilizes an OpenSSL\
library with RIPEMD disabled.')
return False return False
return True return True
def check_sqlite(): def check_sqlite():
if sys.hexversion < 0x020500F0: if sys.hexversion < 0x020500F0:
logger.error('The sqlite3 module is not included in this version of Python.') logger.error(
'The sqlite3 module is not included in this\
version of Python.')
if sys.platform.startswith('freebsd'): if sys.platform.startswith('freebsd'):
logger.error('On FreeBSD, try running "pkg install py27-sqlite3" as root.') logger.error(
'On FreeBSD, try running\
"pkg install py27-sqlite3" as root.')
return False return False
try: try:
import sqlite3 import sqlite3
@ -53,40 +70,52 @@ def check_sqlite():
logger.info('sqlite3 Module Version: ' + sqlite3.version) logger.info('sqlite3 Module Version: ' + sqlite3.version)
logger.info('SQLite Library Version: ' + sqlite3.sqlite_version) logger.info('SQLite Library Version: ' + sqlite3.sqlite_version)
#sqlite_version_number formula: https://sqlite.org/c3ref/c_source_id.html # sqlite_version_number formula: https://sqlite.org/c3ref/c_source_id.html
sqlite_version_number = sqlite3.sqlite_version_info[0] * 1000000 + sqlite3.sqlite_version_info[1] * 1000 + sqlite3.sqlite_version_info[2] sqlite_version_number = sqlite3.sqlite_version_info[0] * 1000000 + \
sqlite3.sqlite_version_info[1] * 1000 + sqlite3.sqlite_version_info[2]
conn = None conn = None
try: try:
try: try:
conn = sqlite3.connect(':memory:') conn = sqlite3.connect(':memory:')
if sqlite_version_number >= 3006018: if sqlite_version_number >= 3006018:
sqlite_source_id = conn.execute('SELECT sqlite_source_id();').fetchone()[0] sqlite_source_id = conn.execute(
'SELECT sqlite_source_id();').fetchone()[0]
logger.info('SQLite Library Source ID: ' + sqlite_source_id) logger.info('SQLite Library Source ID: ' + sqlite_source_id)
if sqlite_version_number >= 3006023: if sqlite_version_number >= 3006023:
compile_options = ', '.join(map(lambda row: row[0], conn.execute('PRAGMA compile_options;'))) compile_options = ', '.join(
logger.info('SQLite Library Compile Options: ' + compile_options) map(lambda row: row[0],
#There is no specific version requirement as yet, so we just use the conn.execute('PRAGMA compile_options;')))
#first version that was included with Python. logger.info(
'SQLite Library Compile Options: ' +
compile_options)
# There is no specific version requirement as yet, so we just use
# the first version that was included with Python.
if sqlite_version_number < 3000008: if sqlite_version_number < 3000008:
logger.error('This version of SQLite is too old. PyBitmessage requires SQLite 3.0.8 or later') logger.error(
'This version of SQLite is too old\
. PyBitmessage requires SQLite 3.0.8 or later')
return False return False
return True return True
except sqlite3.Error: except sqlite3.Error:
logger.exception('An exception occured while checking sqlite.') logger.exception('An exception occured while \
checking sqlite.')
return False return False
finally: finally:
if conn: if conn:
conn.close() conn.close()
def check_openssl(): def check_openssl():
try: try:
import ctypes import ctypes
except ImportError: except ImportError:
logger.error('Unable to check OpenSSL. The ctypes module is not available.') logger.error(
'Unable to check OpenSSL. The ctypes module\
is not available.')
return False return False
#We need to emulate the way PyElliptic searches for OpenSSL. # We need to emulate the way PyElliptic searches for OpenSSL.
if sys.platform == 'win32': if sys.platform == 'win32':
paths = ['libeay32.dll'] paths = ['libeay32.dll']
if getattr(sys, 'frozen', False): if getattr(sys, 'frozen', False):
@ -107,7 +136,7 @@ def check_openssl():
path = ctypes.util.find_library('ssl') path = ctypes.util.find_library('ssl')
if path not in paths: if path not in paths:
paths.append(path) paths.append(path)
except: except BaseException:
pass pass
openssl_version = None openssl_version = None
@ -123,83 +152,125 @@ def check_openssl():
except OSError: except OSError:
continue continue
logger.info('OpenSSL Name: ' + library._name) logger.info('OpenSSL Name: ' + library._name)
openssl_version, openssl_hexversion, openssl_cflags = pyelliptic.openssl.get_version(library) openssl_version, openssl_hexversion, openssl_cflags = pyelliptic.openssl.get_version(
library)
if not openssl_version: if not openssl_version:
logger.error('Cannot determine version of this OpenSSL library.') logger.error('Cannot determine version\
of this OpenSSL library.')
return False return False
logger.info('OpenSSL Version: ' + openssl_version) logger.info('OpenSSL Version: ' + openssl_version)
logger.info('OpenSSL Compile Options: ' + openssl_cflags) logger.info('OpenSSL Compile Options: ' + openssl_cflags)
#PyElliptic uses EVP_CIPHER_CTX_new and EVP_CIPHER_CTX_free which were # PyElliptic uses EVP_CIPHER_CTX_new and EVP_CIPHER_CTX_free which were
#introduced in 0.9.8b. # introduced in 0.9.8b.
if openssl_hexversion < 0x90802F: if openssl_hexversion < 0x90802F:
logger.error('This OpenSSL library is too old. PyBitmessage requires OpenSSL 0.9.8b or later with AES, Elliptic Curves (EC), ECDH, and ECDSA enabled.') logger.error(
'This OpenSSL library is too old. \
PyBitmessage requires OpenSSL 0.9.8b\
or later with AES, Elliptic Curves (EC)\
, ECDH, and ECDSA enabled.')
return False return False
matches = cflags_regex.findall(openssl_cflags) matches = cflags_regex.findall(openssl_cflags)
if len(matches) > 0: if len(matches) > 0:
logger.error('This OpenSSL library is missing the following required features: ' + ', '.join(matches) + '. PyBitmessage requires OpenSSL 0.9.8b or later with AES, Elliptic Curves (EC), ECDH, and ECDSA enabled.') logger.error(
'This OpenSSL library is missing the\
following required features: ' +
', '.join(matches) +
'. PyBitmessage requires OpenSSL 0.9.8b\
or later with AES, Elliptic Curves (EC)\
, ECDH, and ECDSA enabled.')
return False return False
return True return True
return False return False
#TODO: The minimum versions of pythondialog and dialog need to be determined # TODO: The minimum versions of pythondialog and dialog need to be determined
def check_curses(): def check_curses():
if sys.hexversion < 0x20600F0: if sys.hexversion < 0x20600F0:
logger.error('The curses interface requires the pythondialog package and the dialog utility.') logger.error(
'The curses interface requires the pythondialog\
package and the dialog utility.')
return False return False
try: try:
import curses import curses
except ImportError: except ImportError:
logger.error('The curses interface can not be used. The curses module is not available.') logger.error(
'The curses interface can not be used.\
The curses module is not available.')
return False return False
logger.info('curses Module Version: ' + curses.version) logger.info('curses Module Version: ' + curses.version)
try: try:
import dialog import dialog
except ImportError: except ImportError:
logger.error('The curses interface can not be used. The pythondialog package is not available.') logger.error(
'The curses interface can not be used\
. The pythondialog package is not available.')
return False return False
logger.info('pythondialog Package Version: ' + dialog.__version__) logger.info('pythondialog Package Version: ' + dialog.__version__)
dialog_util_version = dialog.Dialog().cached_backend_version dialog_util_version = dialog.Dialog().cached_backend_version
#The pythondialog author does not like Python2 str, so we have to use # The pythondialog author does not like Python2 str, so we have to use
#unicode for just the version otherwise we get the repr form which includes # unicode for just the version otherwise we get the repr form which
#the module and class names along with the actual version. # includes the module and class names along with the actual version.
logger.info('dialog Utility Version' + unicode(dialog_util_version)) logger.info('dialog Utility Version' + unicode(dialog_util_version))
return True return True
def check_pyqt(): def check_pyqt():
try: try:
import PyQt4.QtCore import PyQt4.QtCore
except ImportError: except ImportError:
logger.error('The PyQt4 package is not available. PyBitmessage requires PyQt 4.8 or later and Qt 4.7 or later.') logger.error(
'The PyQt4 package is not available. PyBitmessage\
requires PyQt 4.8 or later and Qt 4.7 or later.')
if sys.platform.startswith('openbsd'): if sys.platform.startswith('openbsd'):
logger.error('On OpenBSD, try running "pkg_add py-qt4" as root.') logger.error('On OpenBSD, try running\
"pkg_add py-qt4" as root.')
elif sys.platform.startswith('freebsd'): elif sys.platform.startswith('freebsd'):
logger.error('On FreeBSD, try running "pkg install py27-qt4" as root.') logger.error(
'On FreeBSD, try running\
"pkg install py27-qt4" as root.')
elif os.path.isfile("/etc/os-release"): elif os.path.isfile("/etc/os-release"):
with open("/etc/os-release", 'rt') as osRelease: with open("/etc/os-release", 'rt') as osRelease:
for line in osRelease: for line in osRelease:
if line.startswith("NAME="): if line.startswith("NAME="):
if "fedora" in line.lower(): if "fedora" in line.lower():
logger.error('On Fedora, try running "dnf install PyQt4" as root.') logger.error(
'On Fedora, try running\
"dnf install PyQt4" as root.')
elif "opensuse" in line.lower(): elif "opensuse" in line.lower():
logger.error('On openSUSE, try running "zypper install python-qt" as root.') logger.error(
'On openSUSE, try running\
"zypper install python-qt" as root.')
elif "ubuntu" in line.lower(): elif "ubuntu" in line.lower():
logger.error('On Ubuntu, try running "apt-get install python-qt4" as root.') logger.error(
'On Ubuntu, try running\
"apt-get install python-qt4" as root.')
elif "debian" in line.lower(): elif "debian" in line.lower():
logger.error('On Debian, try running "apt-get install python-qt4" as root.') logger.error(
'On Debian, try running\
"apt-get install python-qt4" as root.')
else: else:
logger.error('If your package manager does not have this package, try running "pip install PyQt4".') logger.error(
'If your package manager does not\
have this package, try running\
"pip install PyQt4".')
return False return False
logger.info('PyQt Version: ' + PyQt4.QtCore.PYQT_VERSION_STR) logger.info('PyQt Version: ' + PyQt4.QtCore.PYQT_VERSION_STR)
logger.info('Qt Version: ' + PyQt4.QtCore.QT_VERSION_STR) logger.info('Qt Version: ' + PyQt4.QtCore.QT_VERSION_STR)
passed = True passed = True
if PyQt4.QtCore.PYQT_VERSION < 0x40800: if PyQt4.QtCore.PYQT_VERSION < 0x40800:
logger.error('This version of PyQt is too old. PyBitmessage requries PyQt 4.8 or later.') logger.error(
'This version of PyQt is too old. PyBitmessage\
requries PyQt 4.8 or later.')
passed = False passed = False
if PyQt4.QtCore.QT_VERSION < 0x40700: if PyQt4.QtCore.QT_VERSION < 0x40700:
logger.error('This version of Qt is too old. PyBitmessage requries Qt 4.7 or later.') logger.error(
'This version of Qt is too old. PyBitmessage\
requries Qt 4.7 or later.')
passed = False passed = False
return passed return passed
def check_msgpack(): def check_msgpack():
try: try:
import msgpack import msgpack
@ -208,58 +279,85 @@ def check_msgpack():
'The msgpack package is not available.' 'The msgpack package is not available.'
'It is highly recommended for messages coding.') 'It is highly recommended for messages coding.')
if sys.platform.startswith('openbsd'): if sys.platform.startswith('openbsd'):
logger.error('On OpenBSD, try running "pkg_add py-msgpack" as root.') logger.error(
'On OpenBSD, try running\
"pkg_add py-msgpack" as root.')
elif sys.platform.startswith('freebsd'): elif sys.platform.startswith('freebsd'):
logger.error('On FreeBSD, try running "pkg install py27-msgpack-python" as root.') logger.error(
'On FreeBSD, try running\
"pkg install py27-msgpack-python" as root.')
elif os.path.isfile("/etc/os-release"): elif os.path.isfile("/etc/os-release"):
with open("/etc/os-release", 'rt') as osRelease: with open("/etc/os-release", 'rt') as osRelease:
for line in osRelease: for line in osRelease:
if line.startswith("NAME="): if line.startswith("NAME="):
if "fedora" in line.lower(): if "fedora" in line.lower():
logger.error('On Fedora, try running "dnf install python2-msgpack" as root.') logger.error(
'On Fedora, try running\
"dnf install python2-msgpack" as root.')
elif "opensuse" in line.lower(): elif "opensuse" in line.lower():
logger.error('On openSUSE, try running "zypper install python-msgpack-python" as root.') logger.error(
'On openSUSE, try running\
"zypper install \
python-msgpack-python" as root.')
elif "ubuntu" in line.lower(): elif "ubuntu" in line.lower():
logger.error('On Ubuntu, try running "apt-get install python-msgpack" as root.') logger.error(
'On Ubuntu, try running \
"apt-get install python-msgpack" as root.')
elif "debian" in line.lower(): elif "debian" in line.lower():
logger.error('On Debian, try running "apt-get install python-msgpack" as root.') logger.error(
'On Debian, try running \
"apt-get install python-msgpack" as root.')
else: else:
logger.error('If your package manager does not have this package, try running "pip install msgpack-python".') logger.error(
'If your package manager does \
not have this package, \
try running "pip install msgpack-python".')
return True return True
def check_dependencies(verbose = False, optional = False):
def check_dependencies(verbose=False, optional=False):
if verbose: if verbose:
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
has_all_dependencies = True has_all_dependencies = True
#Python 2.7.3 is the required minimum. Python 3+ is not supported, but it is # Python 2.7.3 is the required minimum. Python 3+ is not supported, but it
#still useful to provide information about our other requirements. # is still useful to provide information about our other requirements.
logger.info('Python version: %s', sys.version) logger.info('Python version: %s', sys.version)
if sys.hexversion < 0x20703F0: if sys.hexversion < 0x20703F0:
logger.error('PyBitmessage requires Python 2.7.3 or greater (but not Python 3+)') logger.error(
'PyBitmessage requires Python 2.7.3 or \
greater (but not Python 3+)')
has_all_dependencies = False has_all_dependencies = False
if sys.hexversion >= 0x3000000: if sys.hexversion >= 0x3000000:
logger.error('PyBitmessage does not support Python 3+. Python 2.7.3 or greater is required.') logger.error(
'PyBitmessage does not support Python 3+.\
Python 2.7.3 or greater is required.')
has_all_dependencies = False has_all_dependencies = False
check_functions = [check_hashlib, check_sqlite, check_openssl, check_msgpack] check_functions = [
check_hashlib,
check_sqlite,
check_openssl,
check_msgpack]
if optional: if optional:
check_functions.extend([check_pyqt, check_curses]) check_functions.extend([check_pyqt, check_curses])
#Unexpected exceptions are handled here # Unexpected exceptions are handled here
for check in check_functions: for check in check_functions:
try: try:
has_all_dependencies &= check() has_all_dependencies &= check()
except: except BaseException:
logger.exception(check.__name__ + ' failed unexpectedly.') logger.exception(check.__name__ + ' failed unexpectedly.')
has_all_dependencies = False has_all_dependencies = False
if not has_all_dependencies: if not has_all_dependencies:
logger.critical('PyBitmessage cannot start. One or more dependencies are unavailable.') logger.critical(
'PyBitmessage cannot start. One or \
more dependencies are unavailable.')
sys.exit() sys.exit()
if __name__ == '__main__': if __name__ == '__main__':
check_dependencies(True, True) check_dependencies(True, True)

View File

@ -1,9 +1,43 @@
import os import os
import random
from pyelliptic.openssl import OpenSSL from pyelliptic.openssl import OpenSSL
def randomBytes(n): def randomBytes(n):
"""Method randomBytes."""
try: try:
return os.urandom(n) return os.urandom(n)
except NotImplementedError: except NotImplementedError:
return OpenSSL.rand(n) return OpenSSL.rand(n)
def randomshuffle(population):
"""Method randomShuffle.
shuffle the sequence x in place.
shuffles the elements in list in place,
so they are in a random order.
"""
return random.shuffle(population)
def randomsample(population, k):
"""Method randomSample.
return a k length list of unique elements
chosen from the population sequence.
Used for random sampling
without replacement
"""
return random.sample(population, k)
def randomrandrange(x, y):
"""Method randomRandrange.
return a randomly selected element from
range(start, stop). This is equivalent to
choice(range(start, stop)),
but doesnt actually build a range object.
"""
return random.randrange(x, y)