1- ''' Base58 encoding
1+ """ Base58 encoding
22
33Implementations of Base58 and Base58Check encodings that are compatible
44with the bitcoin network.
2525 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2626 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2727 THE SOFTWARE.
28- '''
28+ """
2929
3030# This module is based upon base58 snippets found scattered over many bitcoin
3131# tools written in python. From what I gather the original source is from a
3737from typing import Mapping , Union
3838
3939# 58 character alphabet used
40- BITCOIN_ALPHABET = \
41- b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
42- RIPPLE_ALPHABET = b'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz'
40+ BITCOIN_ALPHABET = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
41+ RIPPLE_ALPHABET = b"rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"
4342XRP_ALPHABET = RIPPLE_ALPHABET
4443
4544# Retro compatibility
4847
4948def scrub_input (v : Union [str , bytes ]) -> bytes :
5049 if isinstance (v , str ):
51- v = v .encode (' ascii' )
50+ v = v .encode (" ascii" )
5251
5352 return v
5453
@@ -65,35 +64,32 @@ def b58encode_int(
6564 base = len (alphabet )
6665 while i :
6766 i , idx = divmod (i , base )
68- string = alphabet [idx : idx + 1 ] + string
67+ string = alphabet [idx : idx + 1 ] + string
6968 return string
7069
7170
72- def b58encode (
73- v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET
74- ) -> bytes :
71+ def b58encode (v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET ) -> bytes :
7572 """
7673 Encode a string using Base58
7774 """
7875 v = scrub_input (v )
7976
8077 origlen = len (v )
81- v = v .lstrip (b' \0 ' )
78+ v = v .lstrip (b" \0 " )
8279 newlen = len (v )
8380
84- acc = int .from_bytes (v , byteorder = ' big' ) # first byte is most significant
81+ acc = int .from_bytes (v , byteorder = " big" ) # first byte is most significant
8582
8683 result = b58encode_int (acc , default_one = False , alphabet = alphabet )
8784 return alphabet [0 :1 ] * (origlen - newlen ) + result
8885
8986
9087@lru_cache ()
91- def _get_base58_decode_map (alphabet : bytes ,
92- autofix : bool ) -> Mapping [int , int ]:
88+ def _get_base58_decode_map (alphabet : bytes , autofix : bool ) -> Mapping [int , int ]:
9389 invmap = {char : index for index , char in enumerate (alphabet )}
9490
9591 if autofix :
96- groups = [b' 0Oo' , b' Il1' ]
92+ groups = [b" 0Oo" , b" Il1" ]
9793 for group in groups :
9894 pivots = [c for c in group if c in invmap ]
9995 if len (pivots ) == 1 :
@@ -104,13 +100,12 @@ def _get_base58_decode_map(alphabet: bytes,
104100
105101
106102def b58decode_int (
107- v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET , * ,
108- autofix : bool = False
103+ v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET , * , autofix : bool = False
109104) -> int :
110105 """
111106 Decode a Base58 encoded string as an integer
112107 """
113- if b' ' not in alphabet :
108+ if b" " not in alphabet :
114109 v = v .rstrip ()
115110 v = scrub_input (v )
116111
@@ -122,15 +117,12 @@ def b58decode_int(
122117 for char in v :
123118 decimal = decimal * base + map [char ]
124119 except KeyError as e :
125- raise ValueError (
126- "Invalid character {!r}" .format (chr (e .args [0 ]))
127- ) from None
120+ raise ValueError ("Invalid character {!r}" .format (chr (e .args [0 ]))) from None
128121 return decimal
129122
130123
131124def b58decode (
132- v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET , * ,
133- autofix : bool = False
125+ v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET , * , autofix : bool = False
134126) -> bytes :
135127 """
136128 Decode a Base58 encoded string
@@ -144,12 +136,10 @@ def b58decode(
144136
145137 acc = b58decode_int (v , alphabet = alphabet , autofix = autofix )
146138
147- return acc .to_bytes (origlen - newlen + (acc .bit_length () + 7 ) // 8 , ' big' )
139+ return acc .to_bytes (origlen - newlen + (acc .bit_length () + 7 ) // 8 , " big" )
148140
149141
150- def b58encode_check (
151- v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET
152- ) -> bytes :
142+ def b58encode_check (v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET ) -> bytes :
153143 """
154144 Encode a string using Base58 with a 4 character checksum
155145 """
@@ -160,10 +150,9 @@ def b58encode_check(
160150
161151
162152def b58decode_check (
163- v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET , * ,
164- autofix : bool = False
153+ v : Union [str , bytes ], alphabet : bytes = BITCOIN_ALPHABET , * , autofix : bool = False
165154) -> bytes :
166- ''' Decode and verify the checksum of a Base58 encoded string'''
155+ """ Decode and verify the checksum of a Base58 encoded string"""
167156
168157 result = b58decode (v , alphabet = alphabet , autofix = autofix )
169158 result , check = result [:- 4 ], result [- 4 :]
0 commit comments