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
from struct import *
from pyelliptic import arithmetic
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):
a = __builtins__.hex(n)
if a[-1:] == 'L':
@ -13,12 +17,14 @@ def convertIntToString(n):
if (len(a) % 2) == 0:
return unhexlify(a[2:])
else:
return unhexlify('0'+a[2:])
return unhexlify('0' + a[2:])
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def encodeBase58(num, alphabet=ALPHABET):
"""Encode a number in Base X
"""Encode a number in Base X.
`num`: The number to encode
`alphabet`: The alphabet to use for encoding
@ -29,14 +35,15 @@ def encodeBase58(num, alphabet=ALPHABET):
base = len(alphabet)
while num:
rem = num % base
#print 'num is:', num
# print 'num is:', num
num = num // base
arr.append(alphabet[rem])
arr.reverse()
return ''.join(arr)
def decodeBase58(string, alphabet=ALPHABET):
"""Decode a Base X encoded string into the number
"""Decode a Base X encoded string into the number.
Arguments:
- `string`: The encoded string
@ -44,73 +51,91 @@ def decodeBase58(string, alphabet=ALPHABET):
"""
base = len(alphabet)
num = 0
try:
for char in string:
num *= base
num += alphabet.index(char)
except:
#character not found (like a space character or a 0)
except BaseException:
# character not found (like a space character or a 0)
return 0
return num
def encodeVarint(integer):
if integer < 0:
logger.error('varint cannot be < 0')
raise SystemExit
if integer < 253:
return pack('>B',integer)
return pack('>B', integer)
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:
return pack('>B',254) + pack('>I',integer)
return pack('>B', 254) + pack('>I', integer)
if integer >= 4294967296 and integer < 18446744073709551616:
return pack('>B',255) + pack('>Q',integer)
return pack('>B', 255) + pack('>Q', integer)
if integer >= 18446744073709551616:
logger.error('varint cannot be >= 18446744073709551616')
raise SystemExit
class varintDecodeError(Exception):
pass
def decodeVarint(data):
"""
Decodes an encoded varint to an integer and returns it.
Per protocol v3, the encoded value must be encoded with
the minimum amount of data possible or else it is malformed.
"""Decode an encoded varint.
Decode an encoded varint to an integer and returns it.
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)
"""
if len(data) == 0:
return (0,0)
firstByte, = unpack('>B',data[0:1])
return (0, 0)
firstByte, = unpack('>B', data[0:1])
if firstByte < 253:
# 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:
# encodes 253 to 65535
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)))
encodedValue, = unpack('>H',data[1:3])
raise varintDecodeError(
'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:
raise varintDecodeError('This varint does not encode the value with the lowest possible number of bytes.')
return (encodedValue,3)
raise varintDecodeError(
'This varint does not encode the value with the\
lowest possible number of bytes.')
return (encodedValue, 3)
if firstByte == 254:
# encodes 65536 to 4294967295
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)))
encodedValue, = unpack('>I',data[1:5])
raise varintDecodeError(
'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:
raise varintDecodeError('This varint does not encode the value with the lowest possible number of bytes.')
return (encodedValue,5)
raise varintDecodeError(
'This varint does not encode the value with\
the lowest possible number of bytes.')
return (encodedValue, 5)
if firstByte == 255:
# encodes 4294967296 to 18446744073709551615
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)))
encodedValue, = unpack('>Q',data[1:9])
raise varintDecodeError(
'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:
raise varintDecodeError('This varint does not encode the value with the lowest possible number of bytes.')
return (encodedValue,9)
raise varintDecodeError(
'This varint does not encode the value with \
the lowest possible number of bytes.')
return (encodedValue, 9)
def calculateInventoryHash(data):
@ -120,21 +145,26 @@ def calculateInventoryHash(data):
sha2.update(sha.digest())
return sha2.digest()[0:32]
def encodeAddress(version,stream,ripe):
def encodeAddress(version, stream, ripe):
if version >= 2 and version < 4:
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':
ripe = ripe[2:]
elif ripe[:1] == '\x00':
ripe = ripe[1:]
elif version == 4:
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')
storedBinaryData = encodeVarint(version) + encodeVarint(stream) + ripe
# Generate the checksum
sha = hashlib.new('sha512')
sha.update(storedBinaryData)
@ -143,12 +173,16 @@ def encodeAddress(version,stream,ripe):
sha.update(currentHash)
checksum = sha.digest()[0:4]
asInt = int(hexlify(storedBinaryData) + hexlify(checksum),16)
return 'BM-'+ encodeBase58(asInt)
asInt = int(hexlify(storedBinaryData) + hexlify(checksum), 16)
return 'BM-' + encodeBase58(asInt)
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()
if address[:3] == 'BM-':
@ -157,14 +191,15 @@ def decodeAddress(address):
integer = decodeBase58(address)
if integer == 0:
status = 'invalidcharacters'
return status,0,0,""
#after converting to hex, the string will be prepended with a 0x and appended with a L
return status, 0, 0, ""
# after converting to hex, the string will be prepended with a 0x and
# appended with a L
hexdata = hex(integer)[2:-1]
if len(hexdata) % 2 != 0:
hexdata = '0' + hexdata
#print 'hexdata', hexdata
# print 'hexdata', hexdata
data = unhexlify(hexdata)
checksum = data[-4:]
@ -172,15 +207,15 @@ def decodeAddress(address):
sha = hashlib.new('sha512')
sha.update(data[:-4])
currentHash = sha.digest()
#print 'sha after first hashing: ', sha.hexdigest()
# print 'sha after first hashing: ', sha.hexdigest()
sha = hashlib.new('sha512')
sha.update(currentHash)
#print 'sha after second hashing: ', sha.hexdigest()
# print 'sha after second hashing: ', sha.hexdigest()
if checksum != sha.digest()[0:4]:
status = 'checksumfailed'
return status,0,0,""
#else:
return status, 0, 0, ""
# else:
# print 'checksum PASSED'
try:
@ -188,95 +223,118 @@ def decodeAddress(address):
except varintDecodeError as e:
logger.error(str(e))
status = 'varintmalformed'
return status,0,0,""
#print 'addressVersionNumber', addressVersionNumber
#print 'bytesUsedByVersionNumber', bytesUsedByVersionNumber
return status, 0, 0, ""
# print 'addressVersionNumber', addressVersionNumber
# print 'bytesUsedByVersionNumber', bytesUsedByVersionNumber
if addressVersionNumber > 4:
logger.error('cannot decode address version numbers this high')
logger.error('cannot decode address version numbers\
this high')
status = 'versiontoohigh'
return status,0,0,""
return status, 0, 0, ""
elif addressVersionNumber == 0:
logger.error('cannot decode address version numbers of zero.')
logger.error('cannot decode address version numbers\
of zero.')
status = 'versiontoohigh'
return status,0,0,""
return status, 0, 0, ""
try:
streamNumber, bytesUsedByStreamNumber = decodeVarint(data[bytesUsedByVersionNumber:])
streamNumber, bytesUsedByStreamNumber = decodeVarint(
data[bytesUsedByVersionNumber:])
except varintDecodeError as e:
logger.error(str(e))
status = 'varintmalformed'
return status,0,0,""
#print streamNumber
return status, 0, 0, ""
# print streamNumber
status = 'success'
if addressVersionNumber == 1:
return status,addressVersionNumber,streamNumber,data[-24:-4]
return status, addressVersionNumber, streamNumber, data[-24:-4]
elif addressVersionNumber == 2 or addressVersionNumber == 3:
embeddedRipeData = data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]
embeddedRipeData = data[bytesUsedByVersionNumber +
bytesUsedByStreamNumber:-4]
if len(embeddedRipeData) == 19:
return status,addressVersionNumber,streamNumber,'\x00'+embeddedRipeData
return status, addressVersionNumber, streamNumber, '\x00' + embeddedRipeData
elif len(embeddedRipeData) == 20:
return status,addressVersionNumber,streamNumber,embeddedRipeData
return status, addressVersionNumber, streamNumber, embeddedRipeData
elif len(embeddedRipeData) == 18:
return status,addressVersionNumber,streamNumber,'\x00\x00'+embeddedRipeData
return status, addressVersionNumber, streamNumber, '\x00\x00'
+ embeddedRipeData
elif len(embeddedRipeData) < 18:
return 'ripetooshort',0,0,""
return 'ripetooshort', 0, 0, ""
elif len(embeddedRipeData) > 20:
return 'ripetoolong',0,0,""
return 'ripetoolong', 0, 0, ""
else:
return 'otherproblem',0,0,""
return 'otherproblem', 0, 0, ""
elif addressVersionNumber == 4:
embeddedRipeData = data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]
embeddedRipeData = data[bytesUsedByVersionNumber +
bytesUsedByStreamNumber:-4]
if embeddedRipeData[0:1] == '\x00':
# In order to enforce address non-malleability, encoded RIPE data must have NULL bytes removed from the front
return 'encodingproblem',0,0,""
# In order to enforce address non-malleability,\
# encoded RIPE data
# must have NULL bytes removed from the front
return 'encodingproblem', 0, 0, ""
elif len(embeddedRipeData) > 20:
return 'ripetoolong',0,0,""
return 'ripetoolong', 0, 0, ""
elif len(embeddedRipeData) < 4:
return 'ripetooshort',0,0,""
return 'ripetooshort', 0, 0, ""
else:
x00string = '\x00' * (20 - len(embeddedRipeData))
return status,addressVersionNumber,streamNumber,x00string+embeddedRipeData
return status, addressVersionNumber, streamNumber,\
x00string, embeddedRipeData
def addBMIfNotPresent(address):
address = str(address).strip()
if address[:3] != 'BM-':
return 'BM-'+address
return 'BM-' + address
else:
return address
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:'
privateSigningKey = '93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665'
privateEncryptionKey = '4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a'
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:'
privateSigningKey = '93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186\
bfd8f876665'
privateEncryptionKey = '4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf\
40f4d7400a3a'
print 'privateSigningKey =', privateSigningKey
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)
publicEncryptionKey = arithmetic.privtopub(privateEncryptionKey)
print 'publicSigningKey =', publicSigningKey
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)
publicEncryptionKeyBinary = arithmetic.changebase(publicEncryptionKey,16,256,minlen=64)
publicSigningKeyBinary = arithmetic.changebase(
publicSigningKey, 16, 256, minlen=64)
publicEncryptionKeyBinary = arithmetic.changebase(
publicEncryptionKey, 16, 256, minlen=64)
ripe = hashlib.new('ripemd160')
sha = hashlib.new('sha512')
sha.update(publicSigningKeyBinary+publicEncryptionKeyBinary)
sha.update(publicSigningKeyBinary + publicEncryptionKeyBinary)
ripe.update(sha.digest())
addressVersionNumber = 2
streamNumber = 1
print 'Ripe digest that we will encode in the address:', hexlify(ripe.digest())
returnedAddress = encodeAddress(addressVersionNumber,streamNumber,ripe.digest())
print 'Ripe digest that we will encode in the address:',
hexlify(ripe.digest())
returnedAddress = encodeAddress(
addressVersionNumber, streamNumber, ripe.digest())
print 'Encoded address:', returnedAddress
status,addressVersionNumber,streamNumber,data = decodeAddress(returnedAddress)
status, addressVersionNumber, streamNumber, data = decodeAddress(
returnedAddress)
print '\nAfter decoding address:'
print 'Status:', status
print 'addressVersionNumber', addressVersionNumber
print 'streamNumber', streamNumber
print 'length of data(the ripe hash):', len(data)
print 'ripe data:', hexlify(data)

View File

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

View File

@ -5,7 +5,8 @@ import shared
import time
from time import strftime, localtime, gmtime
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 *
import highlevelcrypto
import proofofwork
@ -29,13 +30,15 @@ from binascii import hexlify, unhexlify
# This thread, of which there is only one, does the heavy lifting:
# calculating POWs.
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:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
class singleWorker(threading.Thread, StoppableThread):
def __init__(self):
@ -47,7 +50,7 @@ class singleWorker(threading.Thread, StoppableThread):
def stopThread(self):
try:
queues.workerQueue.put(("stopThread", "data"))
except:
except BaseException:
pass
super(singleWorker, self).stopThread()
@ -57,21 +60,33 @@ class singleWorker(threading.Thread, StoppableThread):
self.stop.wait(2)
if state.shutdown > 0:
return
# Initialize the neededPubkeys dictionary.
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:
toAddress, = row
toStatus, toAddressVersionNumber, toStreamNumber, toRipe = decodeAddress(toAddress)
if toAddressVersionNumber <= 3 :
toStatus, toAddressVersionNumber, toStreamNumber, \
toRipe = decodeAddress(toAddress)
if toAddressVersionNumber <= 3:
state.neededPubkeys[toAddress] = 0
elif toAddressVersionNumber >= 4:
doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(encodeVarint(
toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest()
privEncryptionKey = doubleHashOfAddressData[:32] # Note that this is the first half of the sha512 hash.
doubleHashOfAddressData = hashlib.sha512(
hashlib.sha512(
encodeVarint(
toAddressVersionNumber) +
encodeVarint(
toStreamNumber) +
toRipe).digest()).digest()
# Note that this is the first half of the sha512 hash.
privEncryptionKey = 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
queryreturn = sqlQuery(
@ -83,16 +98,18 @@ class singleWorker(threading.Thread, StoppableThread):
# Fix legacy (headerless) watched ackdata to include header
for oldack in shared.ackdataForWhichImWatching.keys():
if (len(oldack)==32):
if (len(oldack) == 32):
# attach legacy header, always constant (msg/1/1)
newack = '\x00\x00\x00\x02\x01\x01' + oldack
shared.ackdataForWhichImWatching[newack] = 0
sqlExecute('UPDATE sent SET ackdata=? WHERE ackdata=?',
newack, oldack )
newack, oldack)
del shared.ackdataForWhichImWatching[oldack]
# give some time for the GUI to start before
# we start on existing POW tasks.
self.stop.wait(
10) # give some time for the GUI to start before we start on existing POW tasks.
10)
if state.shutdown == 0:
# just in case there are any pending tasks for msg
@ -109,62 +126,71 @@ class singleWorker(threading.Thread, StoppableThread):
if command == 'sendmessage':
try:
self.sendMsg()
except:
except BaseException:
pass
elif command == 'sendbroadcast':
try:
self.sendBroadcast()
except:
except BaseException:
pass
elif command == 'doPOWForMyV2Pubkey':
try:
self.doPOWForMyV2Pubkey(data)
except:
except BaseException:
pass
elif command == 'sendOutOrStoreMyV3Pubkey':
try:
self.sendOutOrStoreMyV3Pubkey(data)
except:
except BaseException:
pass
elif command == 'sendOutOrStoreMyV4Pubkey':
try:
self.sendOutOrStoreMyV4Pubkey(data)
except:
except BaseException:
pass
elif command == 'resetPoW':
try:
proofofwork.resetPoW()
except:
except BaseException:
pass
elif command == 'stopThread':
self.busy = 0
return
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()
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
"""configSections = shared.config.addresses()
for addressInKeysFile in configSections:
if addressInKeysFile <> 'bitmessagesettings':
status,addressVersionNumber,streamNumber,hashFromThisParticularAddress = decodeAddress(addressInKeysFile)
if hash == hashFromThisParticularAddress:
myAddress = addressInKeysFile
break"""
# configSections = shared.config.addresses()
# for addressInKeysFile in configSections:
# if addressInKeysFile <> 'bitmessagesettings':
# status,addressVersionNumber,streamNumber,
# hashFromThisParticularAddress =
# decodeAddress(addressInKeysFile)
# if hash == hashFromThisParticularAddress:
# myAddress = addressInKeysFile
# break
myAddress = shared.myAddressesByHash[hash]
status, addressVersionNumber, streamNumber, hash = decodeAddress(
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)
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(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:
privSigningKeyBase58 = BMConfigParser().get(
@ -172,7 +198,10 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyBase58 = BMConfigParser().get(
myAddress, 'privencryptionkey')
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
privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
@ -188,17 +217,26 @@ class singleWorker(threading.Thread, StoppableThread):
payload += pubEncryptionKey[1:]
# 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...')
initialHash = hashlib.sha512(payload).digest()
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
inventoryHash = calculateInventoryHash(payload)
objectType = 1
Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime,'')
objectType, streamNumber, payload, embeddedTime, '')
logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash))
@ -208,7 +246,7 @@ class singleWorker(threading.Thread, StoppableThread):
BMConfigParser().set(
myAddress, 'lastpubkeysendtime', str(int(time.time())))
BMConfigParser().save()
except:
except BaseException:
# The user deleted the address out of the keys.dat file before this
# finished.
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
# assembles the pubkey and stores is in the pubkey table so that we can
# 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:
myAddress = shared.myAddressesByHash[hash]
except:
#The address has been deleted.
except BaseException:
# The address has been deleted.
return
if BMConfigParser().safeGetBoolean(myAddress, 'chan'):
logger.info('This is a chan address. Not sending pubkey.')
return
status, addressVersionNumber, streamNumber, hash = decodeAddress(
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)
signedTimeForProtocolV2 = embeddedTime - TTL
"""
According to the protocol specification, the expiresTime along with the pubkey information is
signed. But to be backwards compatible during the upgrade period, we shall sign not the
expiresTime but rather the current time. There must be precisely a 28 day difference
between the two. After the upgrade period we'll switch to signing the whole payload with the
expiresTime time.
"""
# According to the protocol specification, the expiresTime along with
# the pubkey information is signed. But to be backwards compatible
# during the upgrade period, we shall sign not the expiresTime but
# rather the current time. There must be precisely a 28 day difference
# between the two. After the upgrade period we'll switch to signing
# the whole payload with the expiresTime time.
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(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:
privSigningKeyBase58 = BMConfigParser().get(
@ -251,7 +296,10 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyBase58 = BMConfigParser().get(
myAddress, 'privencryptionkey')
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
@ -271,23 +319,31 @@ class singleWorker(threading.Thread, StoppableThread):
myAddress, 'noncetrialsperbyte'))
payload += encodeVarint(BMConfigParser().getint(
myAddress, 'payloadlengthextrabytes'))
signature = highlevelcrypto.sign(payload, privSigningKeyHex)
payload += encodeVarint(len(signature))
payload += signature
# 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...')
initialHash = hashlib.sha512(payload).digest()
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
inventoryHash = calculateInventoryHash(payload)
objectType = 1
Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime,'')
objectType, streamNumber, payload, embeddedTime, '')
logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash))
@ -297,27 +353,28 @@ class singleWorker(threading.Thread, StoppableThread):
BMConfigParser().set(
myAddress, 'lastpubkeysendtime', str(int(time.time())))
BMConfigParser().save()
except:
except BaseException:
# The user deleted the address out of the keys.dat file before this
# finished.
pass
# 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):
if not BMConfigParser().has_section(myAddress):
#The address has been deleted.
# The address has been deleted.
return
if shared.BMConfigParser().safeGetBoolean(myAddress, 'chan'):
logger.info('This is a chan address. Not sending pubkey.')
return
status, addressVersionNumber, streamNumber, hash = decodeAddress(
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)
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(streamNumber)
dataToEncrypt = protocol.getBitfield(myAddress)
@ -328,7 +385,10 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyBase58 = BMConfigParser().get(
myAddress, 'privencryptionkey')
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
privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
@ -346,37 +406,51 @@ class singleWorker(threading.Thread, StoppableThread):
myAddress, 'noncetrialsperbyte'))
dataToEncrypt += encodeVarint(BMConfigParser().getint(
myAddress, 'payloadlengthextrabytes'))
# When we encrypt, we'll use a hash of the data
# contained in an address as a decryption key. This way in order to
# read the public keys in a pubkey message, a node must know the address
# first. 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
# want to send a message.
doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(encodeVarint(
addressVersionNumber) + encodeVarint(streamNumber) + hash).digest()).digest()
payload += doubleHashOfAddressData[32:] # the tag
signature = highlevelcrypto.sign(payload + dataToEncrypt, privSigningKeyHex)
# When we encrypt, we'll use a hash of the data contained in an
# address as a decryption key. This way in order to read the public
# keys in a pubkey message, a node must know the address first.
# 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 want to send a message.
doubleHashOfAddressData = hashlib.sha512(
hashlib.sha512(
encodeVarint(addressVersionNumber) +
encodeVarint(streamNumber) +
hash).digest()).digest()
payload += doubleHashOfAddressData[32:] # the tag
signature = highlevelcrypto.sign(
payload + dataToEncrypt, privSigningKeyHex)
dataToEncrypt += encodeVarint(len(signature))
dataToEncrypt += signature
privEncryptionKey = doubleHashOfAddressData[:32]
pubEncryptionKey = highlevelcrypto.pointMult(privEncryptionKey)
payload += highlevelcrypto.encrypt(
dataToEncrypt, hexlify(pubEncryptionKey))
# 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...')
initialHash = hashlib.sha512(payload).digest()
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
inventoryHash = calculateInventoryHash(payload)
objectType = 1
Inventory()[inventoryHash] = (
objectType, streamNumber, payload, embeddedTime, doubleHashOfAddressData[32:])
objectType, streamNumber,
payload, embeddedTime,
doubleHashOfAddressData[32:])
logger.info('broadcasting inv with hash: ' + hexlify(inventoryHash))
@ -387,21 +461,30 @@ class singleWorker(threading.Thread, StoppableThread):
myAddress, 'lastpubkeysendtime', str(int(time.time())))
BMConfigParser().save()
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):
# Reset just in case
sqlExecute(
'''UPDATE sent SET status='broadcastqueued' WHERE status = 'doingbroadcastpow' ''')
'''UPDATE sent SET status='broadcastqueued'
WHERE status = 'doingbroadcastpow' ''')
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:
fromaddress, subject, body, ackdata, TTL, encoding = row
status, addressVersionNumber, streamNumber, ripe = decodeAddress(
fromaddress)
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
# We need to convert our private keys to public keys in order
# to include them.
@ -410,13 +493,15 @@ class singleWorker(threading.Thread, StoppableThread):
fromaddress, 'privsigningkey')
privEncryptionKeyBase58 = BMConfigParser().get(
fromaddress, 'privencryptionkey')
except:
except BaseException:
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
sqlExecute(
'''UPDATE sent SET status='doingbroadcastpow' WHERE ackdata=? AND status='broadcastqueued' ''',
'''UPDATE sent SET status='doingbroadcastpow'
WHERE ackdata=? AND status='broadcastqueued' ''',
ackdata)
privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
@ -424,29 +509,36 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58))
pubSigningKey = highlevelcrypto.privToPub(privSigningKeyHex).decode(
'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.
# 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.
pubSigningKey = highlevelcrypto.privToPub(
privSigningKeyHex).decode('hex')
pubEncryptionKey = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex))
if TTL > 28 * 24 * 60 * 60:
TTL = 28 * 24 * 60 * 60
if TTL < 60*60:
TTL = 60*60
TTL = int(TTL + random.randrange(-300, 300))# add some randomness to the TTL
if TTL < 60 * 60:
TTL = 60 * 60
# add some randomness to the TTL
TTL = int(TTL + random.randrange(-300, 300))
embeddedTime = int(time.time() + TTL)
payload = pack('>Q', embeddedTime)
payload += '\x00\x00\x00\x03' # object type: broadcast
payload += '\x00\x00\x00\x03' # object type: broadcast
if addressVersionNumber <= 3:
payload += encodeVarint(4) # broadcast version
else:
payload += encodeVarint(5) # broadcast version
payload += encodeVarint(streamNumber)
if addressVersionNumber >= 4:
doubleHashOfAddressData = hashlib.sha512(hashlib.sha512(encodeVarint(
addressVersionNumber) + encodeVarint(streamNumber) + ripe).digest()).digest()
doubleHashOfAddressData = hashlib.sha512(
hashlib.sha512(
encodeVarint(addressVersionNumber) +
encodeVarint(streamNumber) +
ripe).digest()).digest()
tag = doubleHashOfAddressData[32:]
payload += tag
else:
@ -454,31 +546,39 @@ class singleWorker(threading.Thread, StoppableThread):
dataToEncrypt = encodeVarint(addressVersionNumber)
dataToEncrypt += encodeVarint(streamNumber)
dataToEncrypt += protocol.getBitfield(fromaddress) # behavior bitfield
# behavior bitfield
dataToEncrypt += protocol.getBitfield(fromaddress)
dataToEncrypt += pubSigningKey[1:]
dataToEncrypt += pubEncryptionKey[1:]
if addressVersionNumber >= 3:
dataToEncrypt += encodeVarint(BMConfigParser().getint(fromaddress,'noncetrialsperbyte'))
dataToEncrypt += encodeVarint(BMConfigParser().getint(fromaddress,'payloadlengthextrabytes'))
dataToEncrypt += encodeVarint(encoding) # message encoding type
encodedMessage = helper_msgcoding.MsgEncode({"subject": subject, "body": body}, encoding)
dataToEncrypt += encodeVarint(
BMConfigParser().getint(fromaddress, 'noncetrialsperbyte'))
dataToEncrypt += encodeVarint(BMConfigParser().getint(
fromaddress, 'payloadlengthextrabytes'))
dataToEncrypt += encodeVarint(encoding) # message encoding type
encodedMessage = helper_msgcoding.MsgEncode(
{"subject": subject, "body": body}, encoding)
dataToEncrypt += encodeVarint(encodedMessage.length)
dataToEncrypt += encodedMessage.data
dataToSign = payload + dataToEncrypt
signature = highlevelcrypto.sign(
dataToSign, privSigningKeyHex)
dataToEncrypt += encodeVarint(len(signature))
dataToEncrypt += signature
# Encrypt the broadcast with the information contained in the broadcaster's address.
# Anyone who knows the address can generate the private encryption key to decrypt
# the broadcast. This provides virtually no privacy; its purpose is to keep
# questionable and illegal content from flowing through the Internet connections
# and being stored on the disk of 3rd parties.
# Encrypt the broadcast with the information contained in the
# broadcaster's address.Anyone who knows the address can generate
# the private encryption key to decrypt the broadcast. This
# provides virtually no privacy; its purpose is to keep
# questionable and illegal content from flowing through the
# Internet connections and being stored on the disk of 3rd parties.
if addressVersionNumber <= 3:
privEncryptionKey = hashlib.sha512(encodeVarint(
addressVersionNumber) + encodeVarint(streamNumber) + ripe).digest()[:32]
privEncryptionKey = hashlib.sha512(
encodeVarint(addressVersionNumber) +
encodeVarint(streamNumber) +
ripe).digest()[
:32]
else:
privEncryptionKey = doubleHashOfAddressData[:32]
@ -486,31 +586,51 @@ class singleWorker(threading.Thread, StoppableThread):
payload += highlevelcrypto.encrypt(
dataToEncrypt, hexlify(pubEncryptionKey))
target = 2 ** 64 / (defaults.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + defaults.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+defaults.networkDefaultPayloadLengthExtraBytes))/(2 ** 16))))
logger.info('(For broadcast message) Doing proof of work...')
target = 2 ** 64 / (
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', (
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()
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
# Sanity check. The payload size should never be larger than 256 KiB. There should
# be checks elsewhere in the code to not let the user try to send a message this large
# 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))
# Sanity check. The payload size should never be larger
# than 256 KiB. There should
# be checks elsewhere in the code to not let the user try to send
# a message this large
# 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
inventoryHash = calculateInventoryHash(payload)
objectType = 3
Inventory()[inventoryHash] = (
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.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
# a 'broadcastsent' status
@ -520,155 +640,249 @@ class singleWorker(threading.Thread, StoppableThread):
'broadcastsent',
int(time.time()),
ackdata)
def sendMsg(self):
# Reset just in case
sqlExecute(
'''UPDATE sent SET status='msgqueued' WHERE status IN ('doingpubkeypow', 'doingmsgpow')''')
'''UPDATE sent SET status='msgqueued'\
WHERE status IN \
('doingpubkeypow', 'doingmsgpow')''')
queryreturn = sqlQuery(
'''SELECT toaddress, fromaddress, subject, message, ackdata, status, ttl, retrynumber, encodingtype FROM sent WHERE (status='msgqueued' or status='forcepow') and folder='sent' ''')
for row in queryreturn: # while we have a msg that needs some work
toaddress, fromaddress, subject, message, ackdata, 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.
'''SELECT toaddress, fromaddress, subject,\
message, ackdata, status, ttl, retrynumber\
, encodingtype FROM sent WHERE \
(status='msgqueued' or status='forcepow')\
and folder='sent' ''')
for row in queryreturn:
# while we have a msg that needs some work
toaddress, fromaddress, subject, message, ackdata,\
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 the status of this msg is 'forcepow' then clearly we have the pubkey already
# because the user could not have overridden the message about the POW being
# too difficult without knowing the required difficulty.
# if the status of this msg is 'forcepow' then clearly we have
# the pubkey already because the user could not have
# overridden the message about the POW being too difficult
# without knowing the required difficulty.
pass
elif status == 'doingmsgpow':
# We wouldn't have set the status to doingmsgpow if we didn't already have the pubkey
# so let's assume that we have it.
# We wouldn't have set the status to doingmsgpow if we didn't
# already have the pubkey so let's assume that we have it.
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):
sqlExecute(
'''UPDATE sent SET status='doingmsgpow' WHERE toaddress=? AND status='msgqueued' ''',
'''UPDATE sent SET status='doingmsgpow'\
WHERE toaddress=? AND \
status='msgqueued' ''',
toaddress)
status='doingmsgpow'
status = 'doingmsgpow'
elif status == 'msgqueued':
# Let's see if we already have the pubkey in our pubkeys table
queryreturn = sqlQuery(
'''SELECT address FROM pubkeys WHERE address=?''', toaddress)
if queryreturn != []: # If we have the needed pubkey in the pubkey table already,
'''SELECT address FROM pubkeys
WHERE address=?''', toaddress)
if queryreturn != []:
# If we have the needed pubkey in the pubkey table already,
# set the status of this msg to doingmsgpow
sqlExecute(
'''UPDATE sent SET status='doingmsgpow' WHERE toaddress=? AND status='msgqueued' ''',
'''UPDATE sent SET status='doingmsgpow'\
WHERE toaddress=? AND\
status='msgqueued' ''',
toaddress)
status = 'doingmsgpow'
# mark the pubkey as 'usedpersonally' so that we don't delete it later. If the pubkey version
# is >= 4 then usedpersonally will already be set to yes because we'll only ever have
# usedpersonally v4 pubkeys in the pubkeys table.
# mark the pubkey as 'usedpersonally' so that we don't
# delete it later.If the pubkey version is >= 4 then
# usedpersonally will already be set to yes because we'll
# only ever have usedpersonally v4 pubkeys in the pubkeys
# table.
sqlExecute(
'''UPDATE pubkeys SET usedpersonally='yes' WHERE address=?''',
'''UPDATE pubkeys SET \
usedpersonally='yes' \
WHERE address=?''',
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:
toTag = ''
else:
toTag = hashlib.sha512(hashlib.sha512(encodeVarint(toAddressVersionNumber)+encodeVarint(toStreamNumber)+toRipe).digest()).digest()[32:]
if toaddress in state.neededPubkeys or toTag in state.neededPubkeys:
toTag = hashlib.sha512(
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
sqlExecute(
'''UPDATE sent SET status='awaitingpubkey', sleeptill=? WHERE toaddress=? AND status='msgqueued' ''',
int(time.time()) + 2.5*24*60*60,
'''UPDATE sent SET
status='awaitingpubkey',
sleeptill=? WHERE toaddress=?
AND status='msgqueued' ''',
int(time.time()) + 2.5 * 24 * 60 * 60,
toaddress)
queues.UISignalQueue.put(('updateSentItemStatusByToAddress', (
toaddress, tr._translate("MainWindow",'Encryption key was requested earlier.'))))
continue #on with the next msg on which we can do some work
queues.UISignalQueue.put(
('updateSentItemStatusByToAddress', (toaddress, tr._translate(
"MainWindow", 'Encryption key\
was requested earlier.'))))
continue
# on with the next msg on which we can do some work
else:
# We have not yet sent a request for the pubkey
needToRequestPubkey = True
if toAddressVersionNumber >= 4: # If we are trying to send to address version >= 4 then
# the needed pubkey might be encrypted in the inventory.
# If we have it we'll need to decrypt it and put it in
# the pubkeys table.
# The decryptAndCheckPubkeyPayload function expects that the shared.neededPubkeys
# dictionary already contains the toAddress and cryptor object associated with
# the tag for this toAddress.
doubleHashOfToAddressData = hashlib.sha512(hashlib.sha512(encodeVarint(
toAddressVersionNumber) + encodeVarint(toStreamNumber) + toRipe).digest()).digest()
privEncryptionKey = doubleHashOfToAddressData[:32] # The first half of the sha512 hash.
tag = doubleHashOfToAddressData[32:] # The second half of the sha512 hash.
state.neededPubkeys[tag] = (toaddress, highlevelcrypto.makeCryptor(hexlify(privEncryptionKey)))
if toAddressVersionNumber >= 4:
# If we are trying to send to address
# version >= 4 then the needed pubkey might be
# encrypted in the inventory.If we have it we'll
# need to decrypt it and put it in the pubkeys
# table.The decryptAndCheckPubkeyPayload function
# expects that the shared.neededPubkeys dictionary
# already contains the toAddress and cryptor object
# associated with the tag for this toAddress.
doubleHashOfToAddressData = hashlib.sha512(
hashlib.sha512(
encodeVarint(toAddressVersionNumber) +
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):
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
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)
del state.neededPubkeys[tag]
break
#else: # There was something wrong with this pubkey object even
# though it had the correct tag- almost certainly because
# 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.
# else:
# There was something wrong with this
# pubkey object even though it had the
# correct tag- almost certainly because
# 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:
sqlExecute(
'''UPDATE sent SET status='doingpubkeypow' WHERE toaddress=? AND status='msgqueued' ''',
'''UPDATE sent SET status='doingpubkeypow' WHERE toaddress=? AND
status='msgqueued' ''',
toaddress)
queues.UISignalQueue.put(('updateSentItemStatusByToAddress', (
toaddress, tr._translate("MainWindow",'Sending a request for the recipient\'s encryption key.'))))
queues.UISignalQueue.put(
('updateSentItemStatusByToAddress', (toaddress, tr._translate(
"MainWindow", 'Sending a request\
for the recipient\'s encryption key.'))))
self.requestPubKey(toaddress)
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.
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.
TTL *= 2**retryNumber
if 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)
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
# the required proof of work difficulty is too hard then we'll
# abort.
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 the required proof of work difficulty
# is too hard then we'll abort.
queryreturn = sqlQuery(
'SELECT transmitdata FROM pubkeys WHERE address=?',
toaddress)
for row in queryreturn:
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
# -stream number
# -behavior bitfield
# -pub signing 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)
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(
pubkeyPayload[readPosition:readPosition + 10])
readPosition += streamNumberLength
behaviorBitfield = pubkeyPayload[readPosition:readPosition + 4]
# Mobile users may ask us to include their address's RIPE hash on a message
# unencrypted. Before we actually do it the sending human must check a box
# in the settings menu to allow it.
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..
if not shared.BMConfigParser().safeGetBoolean('bitmessagesettings','willinglysendtomobile'): # if we are Not willing to include the receiver's RIPE hash on the message..
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.
# Mobile users may ask us to include their address's
# RIPE hash on a message unencrypted. Before we
# actually do it the sending human must check a box
# in the settings menu to allow it.if receiver is a mobile
# device who expects that their address RIPE is included
# unencrypted on the front of the message..
if shared.isBitSetWithinBitfield(behaviorBitfield, 30):
# 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
readPosition += 4 # to bypass the bitfield of behaviors
# pubSigningKeyBase256 = pubkeyPayload[readPosition:readPosition+64] # We don't use this key for anything here.
readPosition += 4
# to bypass the bitfield of behaviors
# pubSigningKeyBase256 =
# pubkeyPayload[readPosition:readPosition+64] # We don't use
# this key for anything here.
readPosition += 64
pubEncryptionKeyBase256 = pubkeyPayload[
readPosition:readPosition + 64]
@ -676,45 +890,126 @@ class singleWorker(threading.Thread, StoppableThread):
# Let us fetch the amount of work required by the recipient.
if toAddressVersionNumber == 2:
requiredAverageProofOfWorkNonceTrialsPerByte = defaults.networkDefaultProofOfWorkNonceTrialsPerByte
requiredAverageProofOfWorkNonceTrialsPerByte = \
defaults.networkDefaultProofOfWorkNonceTrialsPerByte
requiredPayloadLengthExtraBytes = defaults.networkDefaultPayloadLengthExtraBytes
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (
ackdata, tr._translate("MainWindow", "Doing work necessary to send message.\nThere is no required difficulty for version 2 addresses like this."))))
queues.UISignalQueue.put(
('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:
requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(
pubkeyPayload[readPosition:readPosition + 10])
requiredAverageProofOfWorkNonceTrialsPerByte, \
varintLength = decodeVarint(
pubkeyPayload[readPosition:readPosition + 10])
readPosition += varintLength
requiredPayloadLengthExtraBytes, varintLength = decodeVarint(
pubkeyPayload[readPosition:readPosition + 10])
requiredPayloadLengthExtraBytes, \
varintLength = decodeVarint(
pubkeyPayload[readPosition:readPosition + 10])
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.
requiredAverageProofOfWorkNonceTrialsPerByte = 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)))))
# 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.
if requiredAverageProofOfWorkNonceTrialsPerByte < \
defaults.networkDefaultProofOfWorkNonceTrialsPerByte:
requiredAverageProofOfWorkNonceTrialsPerByte = \
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 (requiredAverageProofOfWorkNonceTrialsPerByte > BMConfigParser().getint('bitmessagesettings', 'maxacceptablenoncetrialsperbyte') and BMConfigParser().getint('bitmessagesettings', 'maxacceptablenoncetrialsperbyte') != 0) or (requiredPayloadLengthExtraBytes > BMConfigParser().getint('bitmessagesettings', 'maxacceptablepayloadlengthextrabytes') and BMConfigParser().getint('bitmessagesettings', 'maxacceptablepayloadlengthextrabytes') != 0):
# The demanded difficulty is more than we are willing
# to do.
if (
requiredAverageProofOfWorkNonceTrialsPerByte >
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(
'''UPDATE sent SET status='toodifficult' WHERE ackdata=? ''',
ackdata)
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(
requiredPayloadLengthExtraBytes) / defaults.networkDefaultPayloadLengthExtraBytes)).arg(l10n.formatTimestamp()))))
'''UPDATE sent SET status='toodifficult' WHERE ackdata=? ''', ackdata)
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(requiredPayloadLengthExtraBytes) /
defaults.networkDefaultPayloadLengthExtraBytes)).arg(
l10n.formatTimestamp()))))
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.debug('First 150 characters of message: ' + repr(message[:150]))
logger.debug('First 150 characters of message: ' +
repr(message[:150]))
behaviorBitfield = protocol.getBitfield(fromaddress)
try:
privEncryptionKeyBase58 = BMConfigParser().get(
toaddress, 'privencryptionkey')
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()))))
logger.error('Error within sendMsg. Could not read the keys from the keys.dat file for our own address. %s\n' % 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()))))
logger.error(
'Error within sendMsg. \
Could not read the keys \
from the keys.dat file \
for our own address. %s\n' %
err)
continue
privEncryptionKeyHex = hexlify(shared.decodeWalletImportFormat(
privEncryptionKeyBase58))
@ -722,13 +1017,18 @@ class singleWorker(threading.Thread, StoppableThread):
privEncryptionKeyHex))[1:]
requiredAverageProofOfWorkNonceTrialsPerByte = defaults.networkDefaultProofOfWorkNonceTrialsPerByte
requiredPayloadLengthExtraBytes = defaults.networkDefaultPayloadLengthExtraBytes
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (
ackdata, tr._translate("MainWindow", "Doing work necessary to send message."))))
queues.UISignalQueue.put(
('updateSentItemStatusByAckdata', (ackdata, tr._translate(
"MainWindow", "Doing work \
necessary to send message."))))
# Now we can start to assemble our message.
payload = encodeVarint(fromAddressVersionNumber)
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
# to include them.
@ -737,9 +1037,15 @@ class singleWorker(threading.Thread, StoppableThread):
fromaddress, 'privsigningkey')
privEncryptionKeyBase58 = BMConfigParser().get(
fromaddress, 'privencryptionkey')
except:
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (
ackdata, tr._translate("MainWindow", "Error! Could not find sender address (your address) in the keys.dat file."))))
except BaseException:
queues.UISignalQueue.put(
('updateSentItemStatusByAckdata',
(ackdata,
tr._translate(
"MainWindow",
"Error! Could not find sender\
address (your address) \
in the keys.dat file."))))
continue
privSigningKeyHex = hexlify(shared.decodeWalletImportFormat(
@ -752,8 +1058,10 @@ class singleWorker(threading.Thread, StoppableThread):
pubEncryptionKey = unhexlify(highlevelcrypto.privToPub(
privEncryptionKeyHex))
payload += pubSigningKey[
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.
# 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.
payload += pubSigningKey[1:]
payload += pubEncryptionKey[1:]
if fromAddressVersionNumber >= 3:
@ -761,7 +1069,8 @@ class singleWorker(threading.Thread, StoppableThread):
# subscriptions list, or whitelist then we will allow them to
# do the network-minimum proof of work. Let us check to see if
# the receiver is in any of those lists.
if shared.isAddressInMyAddressBookSubscriptionsListOrWhitelist(toaddress):
if shared.isAddressInMyAddressBookSubscriptionsListOrWhitelist(
toaddress):
payload += encodeVarint(
defaults.networkDefaultProofOfWorkNonceTrialsPerByte)
payload += encodeVarint(
@ -772,91 +1081,164 @@ class singleWorker(threading.Thread, StoppableThread):
payload += encodeVarint(BMConfigParser().getint(
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.
payload += encodeVarint(encoding) # message encoding type
encodedMessage = helper_msgcoding.MsgEncode({"subject": subject, "body": message}, encoding)
# This hash will be checked by the receiver of the message to
# verify that toRipe belongs to them. This prevents a Surreptitious
# Forwarding Attack.
payload += toRipe
payload += encodeVarint(encoding) # message encoding type
encodedMessage = helper_msgcoding.MsgEncode(
{"subject": subject, "body": message}, encoding)
payload += encodeVarint(encodedMessage.length)
payload += encodedMessage.data
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 = ''
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:
# 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(
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 += 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)
payload += encodeVarint(len(signature))
payload += signature
# We have assembled the data that will be encrypted.
try:
encrypted = highlevelcrypto.encrypt(payload,"04"+hexlify(pubEncryptionKeyBase256))
except:
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()))))
encrypted = highlevelcrypto.encrypt(
payload, "04" + hexlify(pubEncryptionKeyBase256))
except BaseException:
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
encryptedPayload = pack('>Q', embeddedTime)
encryptedPayload += '\x00\x00\x00\x02' # object type: msg
encryptedPayload += encodeVarint(1) # msg version
encryptedPayload += '\x00\x00\x00\x02' # object type: msg
encryptedPayload += encodeVarint(1) # msg version
encryptedPayload += encodeVarint(toStreamNumber) + encrypted
target = 2 ** 64 / (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)
target = 2 ** 64 / (
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()
initialHash = hashlib.sha512(encryptedPayload).digest()
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:
logger.info('PoW took %.1f seconds, speed %s.', time.time() - powStartTime, sizeof_fmt(nonce / (time.time() - powStartTime)))
except:
logger.info('PoW took %.1f seconds, speed %s.',
time.time() - powStartTime,
sizeof_fmt(nonce / (time.time() - powStartTime)))
except BaseException:
pass
encryptedPayload = pack('>Q', nonce) + encryptedPayload
# Sanity check. The encryptedPayload size should never be larger than 256 KiB. There should
# be checks elsewhere in the code to not let the user try to send a message this large
# until we implement message continuation.
if len(encryptedPayload) > 2 ** 18: # 256 KiB
logger.critical('This msg object is too large to send. This should never happen. Object size: %s' % len(encryptedPayload))
# Sanity check. The encryptedPayload size should never be
# larger than 256 KiB. There should be checks elsewhere
# in the code to not let the user try to send a message
# this large until we implement message continuation.
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
inventoryHash = calculateInventoryHash(encryptedPayload)
objectType = 2
Inventory()[inventoryHash] = (
objectType, toStreamNumber, encryptedPayload, embeddedTime, '')
if BMConfigParser().has_section(toaddress) or not protocol.checkBitfield(behaviorBitfield, protocol.BITFIELD_DOESACK):
queues.UISignalQueue.put(('updateSentItemStatusByAckdata', (ackdata, tr._translate("MainWindow", "Message sent. Sent at %1").arg(l10n.formatTimestamp()))))
if BMConfigParser().has_section(toaddress) \
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:
# 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()))))
logger.info('Broadcasting inv for my msg(within sendmsg function):' + hexlify(inventoryHash))
queues.UISignalQueue.put(
('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))
# Update the sent message in the sent table with the necessary information.
if BMConfigParser().has_section(toaddress) or not protocol.checkBitfield(behaviorBitfield, protocol.BITFIELD_DOESACK):
# Update the sent message in the sent table with the necessary
# information.
if BMConfigParser()\
.has_section(toaddress) or not protocol.checkBitfield(
behaviorBitfield, protocol.BITFIELD_DOESACK):
newStatus = 'msgsentnoackexpected'
else:
newStatus = 'msgsent'
# wait 10% past expiration
sleepTill = int(time.time() + TTL * 1.1)
sqlExecute('''UPDATE sent SET msgid=?, status=?, retrynumber=?, sleeptill=?, lastactiontime=? WHERE ackdata=?''',
inventoryHash,
newStatus,
retryNumber+1,
sleepTill,
int(time.time()),
ackdata)
sqlExecute(
'''UPDATE sent SET msgid=?,
status=?, retrynumber=?,
sleeptill=?,
lastactiontime=? WHERE ackdata=?''',
inventoryHash,
newStatus,
retryNumber +
1,
sleepTill,
int(
time.time()),
ackdata)
# If we are sending to ourselves or a chan, let's put the message in
# our own inbox.
# If we are sending to ourselves or a chan, let's put the message
# in our own inbox.
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(
time.time()), message, 'inbox', encoding, 0, sigHash)
helper_inbox.insert(t)
@ -871,7 +1253,7 @@ class singleWorker(threading.Thread, StoppableThread):
try:
apiNotifyPath = BMConfigParser().get(
'bitmessagesettings', 'apinotifypath')
except:
except BaseException:
apiNotifyPath = ''
if apiNotifyPath != '':
call([apiNotifyPath, "newMessage"])
@ -880,111 +1262,187 @@ class singleWorker(threading.Thread, StoppableThread):
toStatus, addressVersionNumber, streamNumber, ripe = decodeAddress(
toAddress)
if toStatus != 'success':
logger.error('Very abnormal error occurred in requestPubKey. toAddress is: ' + repr(
toAddress) + '. Please report this error to Atheros.')
logger.error(
'Very abnormal error occurred in \
requestPubKey. toAddress is: ' +
repr(toAddress) +
'. Please report this error to Atheros.')
return
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)
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
retryNumber = queryReturn[0][0]
if addressVersionNumber <= 3:
state.neededPubkeys[toAddress] = 0
elif addressVersionNumber >= 4:
# If the user just clicked 'send' then the tag (and other information) will already
# be in the neededPubkeys dictionary. But if we are recovering from a restart
# of the client then we have to put it in now.
privEncryptionKey = hashlib.sha512(hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()).digest()[:32] # Note that this is the first half of the sha512 hash.
tag = hashlib.sha512(hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()).digest()[32:] # Note that this is the second half of the sha512 hash.
# If the user just clicked 'send' then the tag (and other
# information) will already be in the neededPubkeys dictionary.
# But if we are recovering from a restart of the client then we
# have to put it in now.Note that this is the first half of the
# 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:
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.
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
TTL = TTL + random.randrange(-300, 300) # add some randomness to the TTL
# 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)))
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)
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(streamNumber)
if addressVersionNumber <= 3:
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:
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
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(('updateSentItemStatusByToAddress', (
toAddress, 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))))
queues.UISignalQueue.put(
('updateSentItemStatusByToAddress',
(toAddress,
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()
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
inventoryHash = calculateInventoryHash(payload)
objectType = 1
Inventory()[inventoryHash] = (
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))
# wait 10% past expiration
sleeptill = int(time.time() + TTL * 1.1)
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()),
retryNumber+1,
retryNumber + 1,
sleeptill,
toAddress)
queues.UISignalQueue.put((
'updateStatusBar', tr._translate("MainWindow",'Broadcasting the public key request. 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()))))
'updateStatusBar', tr._translate("MainWindow", 'Broadcasting the public key request.\
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):
# It might be perfectly fine to just use the same TTL for
# 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
# of the acknowledgement to be about the same as they set
# for the message itself. So let's set the TTL of the
# acknowledgement to be in one of three 'buckets': 1 hour, 7
# days, or 28 days, whichever is relatively close to what the
# for the message itself. So let's set the TTL of the
# acknowledgement to be in one of three 'buckets': 1 hour, 7
# days, or 28 days, whichever is relatively close to what the
# user specified.
if TTL < 24*60*60: # 1 day
TTL = 24*60*60 # 1 day
elif TTL < 7*24*60*60: # 1 week
TTL = 7*24*60*60 # 1 week
if TTL < 24 * 60 * 60: # 1 day
TTL = 24 * 60 * 60 # 1 day
elif TTL < 7 * 24 * 60 * 60: # 1 week
TTL = 7 * 24 * 60 * 60 # 1 week
else:
TTL = 28*24*60*60 # 4 weeks
TTL = int(TTL + random.randrange(-300, 300)) # Add some randomness to the TTL
TTL = 28 * 24 * 60 * 60 # 4 weeks
# Add some randomness to the TTL
TTL = int(TTL + random.randrange(-300, 300))
embeddedTime = int(time.time() + TTL)
# type/version/stream already included
# type/version/stream already included
payload = pack('>Q', (embeddedTime)) + ackdata
target = 2 ** 64 / (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))
target = 2 ** 64 / (
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()
initialHash = hashlib.sha512(payload).digest()
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:
logger.info('PoW took %.1f seconds, speed %s.', time.time() - powStartTime, sizeof_fmt(nonce / (time.time() - powStartTime)))
except:
logger.info('PoW took %.1f seconds, speed %s.', time.time(
) - powStartTime, sizeof_fmt(nonce / (time.time() - powStartTime)))
except BaseException:
pass
payload = pack('>Q', nonce) + payload

View File

@ -4,14 +4,16 @@ import sys
import os
import pyelliptic.openssl
#Only really old versions of Python don't have sys.hexversion. We don't support
#them. The logging module was introduced in Python 2.3
# Only really old versions of Python don't have sys.hexversion. We don't
# support them. The logging module was introduced in Python 2.3
if not hasattr(sys, 'hexversion') or sys.hexversion < 0x20300F0:
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()
#We can now use logging so set up a simple configuration
# We can now use logging so set up a simple configuration
import logging
formatter = logging.Formatter(
'%(levelname)s: %(message)s'
@ -22,28 +24,43 @@ logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.ERROR)
#We need to check hashlib for RIPEMD-160, as it won't be available if OpenSSL is
#not linked against or the linked OpenSSL has RIPEMD disabled.
# We need to check hashlib for RIPEMD-160, as it won't be
# available if OpenSSL # is not linked against or the
# linked OpenSSL has RIPEMD disabled.
def check_hashlib():
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
import hashlib
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
try:
hashlib.new('ripemd160')
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 True
def check_sqlite():
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'):
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
try:
import sqlite3
@ -53,40 +70,52 @@ def check_sqlite():
logger.info('sqlite3 Module Version: ' + sqlite3.version)
logger.info('SQLite Library Version: ' + sqlite3.sqlite_version)
#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 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]
conn = None
try:
try:
conn = sqlite3.connect(':memory:')
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)
if sqlite_version_number >= 3006023:
compile_options = ', '.join(map(lambda row: row[0], conn.execute('PRAGMA compile_options;')))
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.
compile_options = ', '.join(
map(lambda row: row[0],
conn.execute('PRAGMA compile_options;')))
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:
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 True
except sqlite3.Error:
logger.exception('An exception occured while checking sqlite.')
logger.exception('An exception occured while \
checking sqlite.')
return False
finally:
if conn:
conn.close()
def check_openssl():
try:
import ctypes
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
#We need to emulate the way PyElliptic searches for OpenSSL.
# We need to emulate the way PyElliptic searches for OpenSSL.
if sys.platform == 'win32':
paths = ['libeay32.dll']
if getattr(sys, 'frozen', False):
@ -107,7 +136,7 @@ def check_openssl():
path = ctypes.util.find_library('ssl')
if path not in paths:
paths.append(path)
except:
except BaseException:
pass
openssl_version = None
@ -123,83 +152,125 @@ def check_openssl():
except OSError:
continue
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:
logger.error('Cannot determine version of this OpenSSL library.')
logger.error('Cannot determine version\
of this OpenSSL library.')
return False
logger.info('OpenSSL Version: ' + openssl_version)
logger.info('OpenSSL Compile Options: ' + openssl_cflags)
#PyElliptic uses EVP_CIPHER_CTX_new and EVP_CIPHER_CTX_free which were
#introduced in 0.9.8b.
# PyElliptic uses EVP_CIPHER_CTX_new and EVP_CIPHER_CTX_free which were
# introduced in 0.9.8b.
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
matches = cflags_regex.findall(openssl_cflags)
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 True
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():
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
try:
import curses
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
logger.info('curses Module Version: ' + curses.version)
try:
import dialog
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
logger.info('pythondialog Package Version: ' + dialog.__version__)
dialog_util_version = dialog.Dialog().cached_backend_version
#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
#the module and class names along with the actual version.
# 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 the module and class names along with the actual version.
logger.info('dialog Utility Version' + unicode(dialog_util_version))
return True
def check_pyqt():
try:
import PyQt4.QtCore
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'):
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'):
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"):
with open("/etc/os-release", 'rt') as osRelease:
for line in osRelease:
if line.startswith("NAME="):
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():
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():
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():
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:
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
logger.info('PyQt Version: ' + PyQt4.QtCore.PYQT_VERSION_STR)
logger.info('Qt Version: ' + PyQt4.QtCore.QT_VERSION_STR)
passed = True
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
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
return passed
def check_msgpack():
try:
import msgpack
@ -208,58 +279,85 @@ def check_msgpack():
'The msgpack package is not available.'
'It is highly recommended for messages coding.')
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'):
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"):
with open("/etc/os-release", 'rt') as osRelease:
for line in osRelease:
if line.startswith("NAME="):
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():
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():
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():
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:
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
def check_dependencies(verbose = False, optional = False):
def check_dependencies(verbose=False, optional=False):
if verbose:
logger.setLevel(logging.INFO)
has_all_dependencies = True
#Python 2.7.3 is the required minimum. Python 3+ is not supported, but it is
#still useful to provide information about our other requirements.
# Python 2.7.3 is the required minimum. Python 3+ is not supported, but it
# is still useful to provide information about our other requirements.
logger.info('Python version: %s', sys.version)
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
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
check_functions = [check_hashlib, check_sqlite, check_openssl, check_msgpack]
check_functions = [
check_hashlib,
check_sqlite,
check_openssl,
check_msgpack]
if optional:
check_functions.extend([check_pyqt, check_curses])
#Unexpected exceptions are handled here
# Unexpected exceptions are handled here
for check in check_functions:
try:
has_all_dependencies &= check()
except:
except BaseException:
logger.exception(check.__name__ + ' failed unexpectedly.')
has_all_dependencies = False
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()
if __name__ == '__main__':
check_dependencies(True, True)

View File

@ -1,9 +1,43 @@
import os
import random
from pyelliptic.openssl import OpenSSL
def randomBytes(n):
"""Method randomBytes."""
try:
return os.urandom(n)
except NotImplementedError:
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)