refactor address.py

This commit is contained in:
Muzahid 2021-10-29 18:58:41 +05:30
parent 2ef6463439
commit c0f5e1b1a2
Signed by untrusted user: cis-muzahid
GPG Key ID: 1DC85E7D3AB613EA

View File

@ -13,7 +13,7 @@ logger = logging.getLogger('default')
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def encodeBase58(num, alphabet=ALPHABET): def encodeBase58(num):
"""Encode a number in Base X """Encode a number in Base X
Args: Args:
@ -23,30 +23,30 @@ def encodeBase58(num, alphabet=ALPHABET):
if num < 0: if num < 0:
return None return None
if num == 0: if num == 0:
return alphabet[0] return ALPHABET[0]
arr = [] arr = []
base = len(alphabet) base = len(ALPHABET)
while num: while num:
num, rem = divmod(num, base) num, rem = divmod(num, base)
arr.append(alphabet[rem]) arr.append(ALPHABET[rem])
arr.reverse() arr.reverse()
return ''.join(arr) return ''.join(arr)
def decodeBase58(string, alphabet=ALPHABET): def decodeBase58(string):
"""Decode a Base X encoded string into the number """Decode a Base X encoded string into the number
Args: Args:
string: The encoded string string: The encoded string
alphabet: The alphabet to use for encoding alphabet: The alphabet to use for encoding
""" """
base = len(alphabet) base = len(ALPHABET)
num = 0 num = 0
try: try:
for char in string: for char in string:
num *= base num *= base
num += alphabet.index(char) num += ALPHABET.index(char)
except ValueError: except ValueError:
# character not found (like a space character or a 0) # character not found (like a space character or a 0)
return 0 return 0