support for encoding and decoding variable length addresses #450

Merged
Atheros1 merged 7 commits from master into master 2013-08-26 21:29:30 +02:00
Showing only changes of commit f6a07a374a - Show all commits

View File

@ -97,13 +97,23 @@ def calculateInventoryHash(data):
return sha2.digest()[0:32]
def encodeAddress(version,stream,ripe):
if version >= 2:
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.")
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.")
emptybitcounter = 0
while True:
if ripe[emptybitcounter] != '\x00':
break
emptybitcounter += 1
ripe = ripe[emptybitcounter:]
a = encodeVarint(version) + encodeVarint(stream) + ripe
sha = hashlib.new('sha512')
sha.update(a)
@ -164,7 +174,7 @@ def decodeAddress(address):
#print 'addressVersionNumber', addressVersionNumber
#print 'bytesUsedByVersionNumber', bytesUsedByVersionNumber
if addressVersionNumber > 3:
if addressVersionNumber > 4:
print 'cannot decode address version numbers this high'
status = 'versiontoohigh'
return status,0,0,0
@ -191,6 +201,17 @@ def decodeAddress(address):
return 'ripetoolong',0,0,0
else:
return 'otherproblem',0,0,0
elif addressVersionNumber == 4:
if len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) > 20:
return 'ripetoolong',0,0,0
elif len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) < 4:
return 'ripetooshort',0,0,0
else:
x00string = ''
for i in range(20 - len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4])):
x00string += '\x00'
return status,addressVersionNumber,streamNumber,x00string+data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]
def addBMIfNotPresent(address):
address = str(address).strip()