Encode bytes into a Base58 string. Args: data_bytes (bytes) : Data bytes alph_idx (Base58Alphabets, optional): Alphabet index, Bitcoin by default Returns: str: Encoded string Raises: TypeError: If al
(data_bytes: bytes,
alph_idx: Base58Alphabets = Base58Alphabets.BITCOIN)
| 73 | |
| 74 | @staticmethod |
| 75 | def Encode(data_bytes: bytes, |
| 76 | alph_idx: Base58Alphabets = Base58Alphabets.BITCOIN) -> str: |
| 77 | """ |
| 78 | Encode bytes into a Base58 string. |
| 79 | |
| 80 | Args: |
| 81 | data_bytes (bytes) : Data bytes |
| 82 | alph_idx (Base58Alphabets, optional): Alphabet index, Bitcoin by default |
| 83 | |
| 84 | Returns: |
| 85 | str: Encoded string |
| 86 | |
| 87 | Raises: |
| 88 | TypeError: If alphabet index is not a Base58Alphabets enumerative |
| 89 | """ |
| 90 | if not isinstance(alph_idx, Base58Alphabets): |
| 91 | raise TypeError("Alphabet index is not an enumerative of Base58Alphabets") |
| 92 | |
| 93 | enc = "" |
| 94 | |
| 95 | # Get alphabet |
| 96 | alphabet = Base58Const.ALPHABETS[alph_idx] |
| 97 | |
| 98 | # Convert bytes to integer |
| 99 | val = BytesUtils.ToInteger(data_bytes) |
| 100 | |
| 101 | # Algorithm implementation |
| 102 | while val > 0: |
| 103 | val, mod = divmod(val, Base58Const.RADIX) |
| 104 | enc = alphabet[mod] + enc |
| 105 | |
| 106 | # Get number of leading zeros |
| 107 | n = len(data_bytes) - len(data_bytes.lstrip(b"\x00")) |
| 108 | # Add padding |
| 109 | return (alphabet[0] * n) + enc |
| 110 | |
| 111 | @staticmethod |
| 112 | def CheckEncode(data_bytes: bytes, |