aboutsummaryrefslogtreecommitdiff
path: root/contrib/testgen/base58.py
diff options
context:
space:
mode:
authorEvan Klitzke <evan@eklitzke.org>2018-03-27 19:06:03 -0700
committerJohn Newbery <john@johnnewbery.com>2018-03-28 09:20:42 -0400
commit18740586baee546064cba9286e2d681a849ae443 (patch)
treefe118527de7016416bbeaadee8054e3fea41851a /contrib/testgen/base58.py
parentbc6fdf2d15648a5fc68df8021d9186737de6fe7b (diff)
Make base58 python contrib code work with python3
Diffstat (limited to 'contrib/testgen/base58.py')
-rw-r--r--contrib/testgen/base58.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/contrib/testgen/base58.py b/contrib/testgen/base58.py
index 816d40b49c..0dbb79a707 100644
--- a/contrib/testgen/base58.py
+++ b/contrib/testgen/base58.py
@@ -28,7 +28,9 @@ def b58encode(v):
"""
long_value = 0
for (i, c) in enumerate(v[::-1]):
- long_value += (256**i) * ord(c)
+ if isinstance(c, str):
+ c = ord(c)
+ long_value += (256**i) * c
result = ''
while long_value >= __b58base:
@@ -41,7 +43,7 @@ def b58encode(v):
# leading 0-bytes in the input become leading-1s
nPad = 0
for c in v:
- if c == '\0': nPad += 1
+ if c == 0: nPad += 1
else: break
return (__b58chars[0]*nPad) + result
@@ -50,8 +52,10 @@ def b58decode(v, length = None):
""" decode v into a string of len bytes
"""
long_value = 0
- for (i, c) in enumerate(v[::-1]):
- long_value += __b58chars.find(c) * (__b58base**i)
+ for i, c in enumerate(v[::-1]):
+ pos = __b58chars.find(c)
+ assert pos != -1
+ long_value += pos * (__b58base**i)
result = bytes()
while long_value >= 256:
@@ -62,10 +66,12 @@ def b58decode(v, length = None):
nPad = 0
for c in v:
- if c == __b58chars[0]: nPad += 1
- else: break
+ if c == __b58chars[0]:
+ nPad += 1
+ continue
+ break
- result = chr(0)*nPad + result
+ result = bytes(nPad) + result
if length is not None and len(result) != length:
return None