This commit is contained in:
SomberNight 2018-03-11 07:18:07 +01:00
parent d71d22d279
commit e31c2d491d

View file

@ -408,7 +408,10 @@ def base_decode(v, length, base):
chars = __b43chars chars = __b43chars
long_value = 0 long_value = 0
for (i, c) in enumerate(v[::-1]): for (i, c) in enumerate(v[::-1]):
long_value += chars.find(bytes([c])) * (base**i) digit = chars.find(bytes([c]))
if digit == -1:
raise ValueError('Forbidden character {} for base {}'.format(c, base))
long_value += digit * (base**i)
result = bytearray() result = bytearray()
while long_value >= 256: while long_value >= 256:
div, mod = divmod(long_value, 256) div, mod = divmod(long_value, 256)
@ -428,6 +431,10 @@ def base_decode(v, length, base):
return bytes(result) return bytes(result)
class InvalidChecksum(Exception):
pass
def EncodeBase58Check(vchIn): def EncodeBase58Check(vchIn):
hash = Hash(vchIn) hash = Hash(vchIn)
return base_encode(vchIn + hash[0:4], base=58) return base_encode(vchIn + hash[0:4], base=58)
@ -440,7 +447,7 @@ def DecodeBase58Check(psz):
hash = Hash(key) hash = Hash(key)
cs32 = hash[0:4] cs32 = hash[0:4]
if cs32 != csum: if cs32 != csum:
return None raise InvalidChecksum('expected {}, actual {}'.format(bh2u(cs32), bh2u(csum)))
else: else:
return key return key
@ -479,8 +486,9 @@ def deserialize_privkey(key):
if ':' in key: if ':' in key:
txin_type, key = key.split(sep=':', maxsplit=1) txin_type, key = key.split(sep=':', maxsplit=1)
assert txin_type in SCRIPT_TYPES assert txin_type in SCRIPT_TYPES
try:
vch = DecodeBase58Check(key) vch = DecodeBase58Check(key)
if not vch: except BaseException:
neutered_privkey = str(key)[:3] + '..' + str(key)[-2:] neutered_privkey = str(key)[:3] + '..' + str(key)[-2:]
raise BaseException("cannot deserialize", neutered_privkey) raise BaseException("cannot deserialize", neutered_privkey)