Skip to content

Commit a82b006

Browse files
vubvub
authored andcommitted
Added support for zcash-style multi-byte versions
1 parent cfab762 commit a82b006

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

bitcoin/py2specials.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ def changebase(string, frm, to, minlen=0):
4040
return encode(decode(string, frm), to, minlen)
4141

4242
def bin_to_b58check(inp, magicbyte=0):
43-
inp_fmtd = chr(int(magicbyte)) + inp
44-
leadingzbytes = len(re.match('^\x00*', inp_fmtd).group(0))
45-
checksum = bin_dbl_sha256(inp_fmtd)[:4]
46-
return '1' * leadingzbytes + changebase(inp_fmtd+checksum, 256, 58)
43+
if magicbyte == 0:
44+
inp = '\x00' + inp
45+
while magicbyte > 0:
46+
inp = chr(int(magicbyte % 256)) + inp
47+
magicbyte //= 256
48+
leadingzbytes = len(re.match('^\x00*', inp).group(0))
49+
checksum = bin_dbl_sha256(inp)[:4]
50+
return '1' * leadingzbytes + changebase(inp+checksum, 256, 58)
4751

4852
def bytes_to_hex_string(b):
4953
return b.encode('hex')

bitcoin/py3specials.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,20 @@ def changebase(string, frm, to, minlen=0):
3838
return encode(decode(string, frm), to, minlen)
3939

4040
def bin_to_b58check(inp, magicbyte=0):
41-
inp_fmtd = from_int_to_byte(int(magicbyte))+inp
41+
if magicbyte == 0:
42+
inp = from_int_to_byte(0) + inp
43+
while magicbyte > 0:
44+
inp = from_int_to_byte(magicbyte % 256) + inp
45+
magicbyte //= 256
4246

4347
leadingzbytes = 0
44-
for x in inp_fmtd:
48+
for x in inp:
4549
if x != 0:
4650
break
4751
leadingzbytes += 1
4852

49-
checksum = bin_dbl_sha256(inp_fmtd)[:4]
50-
return '1' * leadingzbytes + changebase(inp_fmtd+checksum, 256, 58)
53+
checksum = bin_dbl_sha256(inp)[:4]
54+
return '1' * leadingzbytes + changebase(inp+checksum, 256, 58)
5155

5256
def bytes_to_hex_string(b):
5357
if isinstance(b, str):

0 commit comments

Comments
 (0)