Open
Description
In the Base58 decode
function here, the integer is parsed into 25 bytes
i.e.
def decode_base58(s):
num = 0
for c in s: # <1>
num *= 58
num += BASE58_ALPHABET.index(c)
combined = num.to_bytes(25, byteorder='big') # <2>
checksum = combined[-4:]
if hash256(combined[:-4])[:4] != checksum:
raise ValueError('bad address: {} {}'.format(checksum,
hash256(combined[:-4])[:4]))
return combined[1:-4] # <3>
Why is the value 25 bytes chosen as a container size for the decoded value?
when I define a Base58
class, like
class Base58:
SYMBOLS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
ORDER = SYMBOLS.__len__() # aka cardinality
def encode(s: bytes):
# Counting leading zeros
#
count = 0
for c in s:
if c == 0:
count += 1
else:
break
num = int.from_bytes(s, "big")
# adding leading zeros in base58 representation (1s), back
prefix = Base58.SYMBOLS[0] * count
result = ""
while num > 0:
num, mod = divmod(num, Base58.ORDER)
result = Base58.SYMBOLS[mod] + result
return prefix + result
def decode(s):
num = 0
for c in s:
num *= Base58.ORDER # increment the value each digit placeholder can hold
num += Base58.SYMBOLS.index(c)
return f"{num:x}"
and then test the encoding and decoding scheme with
b16 = '7c076ff316692a3d7eb3c3bb0f8b1488cf72e1afcd929e29307032997a838a3d'
def test_base58_encoding(x):
a = bytes.fromhex(x)
return Base58.encode(a)
def test_base58_decoding(x):
return Base58.decode(x)
b58 = test_base58_encoding(b16)
b16 == test_base58_decoding(b58)
it evaluates to be true, so what is the purpose of parsing the in the integer into a 25 byte container?
Metadata
Metadata
Assignees
Labels
No labels