removed unused feature
Implementation of Base58 and Base58Check, originally from
https://github.com/keis/base58, with the following modifications:
- type annotated python3
- - removal of string support (binary and hex-string only)
+ - removal of string support (bytes only)
- minor improvements
- added check functionalities
"""
@@ -22,32 +22,17 @@ def double_sha256(s: bytes) -> bytes:
return sha256(sha256(s).digest()).digest()
-def to_bytes(v: Union[str, bytes]) -> bytes:
- '''Return bytes from bytes or hex-string'''
- if isinstance(v, str):
- v = bytes.fromhex(v)
- if not isinstance(v, bytes):
- raise TypeError(
- "a bytes-like object is required (also hex-string), not '%s'" %
- type(v).__name__)
- return v
-
-
-def b58encode_check(v: Union[str, bytes]) -> bytes:
+def b58encode_check(v: bytes) -> bytes:
'''Encode bytes using Base58 with a 4 character checksum'''
- v = to_bytes(v)
-
digest = double_sha256(v)
result = b58encode(v + digest[:4])
return result
-def b58encode(v: Union[str, bytes]) -> bytes:
+def b58encode(v: bytes) -> bytes:
'''Encode bytes using Base58'''
- v = to_bytes(v)
-
# preserve leading-0s
# leading-0s become base58 leading-1s
nPad = len(v)
@@ -74,10 +59,8 @@ def b58encode_int(i: int) -> bytes:
return result
-def b58decode_check(v: Union[str, bytes], output_size: Optional[int] = None) -> bytes:
- '''Decode Base58 encoded bytes (or hex-string); also verify checksum and required output length'''
-
- v = to_bytes(v)
+def b58decode_check(v: bytes, output_size: Optional[int] = None) -> bytes:
+ '''Decode Base58 encoded bytes; also verify checksum and required output length'''
if output_size is not None:
output_size += 4
@@ -91,10 +74,8 @@ def b58decode_check(v: Union[str, bytes], output_size: Optional[int] = None) ->
return result
-def b58decode(v: Union[str, bytes], output_size: Optional[int] = None) -> bytes:
- '''Decode Base58 encoded bytes (or hex-string) and verify required output length'''
-
- v = to_bytes(v)
+def b58decode(v: bytes, output_size: Optional[int] = None) -> bytes:
+ '''Decode Base58 encoded bytes and verify required output length'''
# preserve leading-0s
# base58 leading-1s become leading-0s
