commit e31c2d491d4301c6ec44e977ee22f2bc81381410
parent d71d22d279b57dbe1f1509b7ba37fc421b2f173a
Author: SomberNight <somber.night@protonmail.com>
Date: Sun, 11 Mar 2018 07:18:07 +0100
fix #4093
Diffstat:
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/lib/bitcoin.py b/lib/bitcoin.py
@@ -408,7 +408,10 @@ def base_decode(v, length, base):
chars = __b43chars
long_value = 0
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()
while long_value >= 256:
div, mod = divmod(long_value, 256)
@@ -428,6 +431,10 @@ def base_decode(v, length, base):
return bytes(result)
+class InvalidChecksum(Exception):
+ pass
+
+
def EncodeBase58Check(vchIn):
hash = Hash(vchIn)
return base_encode(vchIn + hash[0:4], base=58)
@@ -440,7 +447,7 @@ def DecodeBase58Check(psz):
hash = Hash(key)
cs32 = hash[0:4]
if cs32 != csum:
- return None
+ raise InvalidChecksum('expected {}, actual {}'.format(bh2u(cs32), bh2u(csum)))
else:
return key
@@ -479,8 +486,9 @@ def deserialize_privkey(key):
if ':' in key:
txin_type, key = key.split(sep=':', maxsplit=1)
assert txin_type in SCRIPT_TYPES
- vch = DecodeBase58Check(key)
- if not vch:
+ try:
+ vch = DecodeBase58Check(key)
+ except BaseException:
neutered_privkey = str(key)[:3] + '..' + str(key)[-2:]
raise BaseException("cannot deserialize", neutered_privkey)