Repository: keis/base58

Issue:
output as a byte array
currently your encode function spits out a string, which is annoying because I have to wrap the output in the str.encode() method. 


Discussion:

Patch:
diff --git a/base58.py b/base58.py
index 734e5d1..f03e3a4 100644
--- a/base58.py
+++ b/base58.py
@@ -14,7 +14,7 @@ from hashlib import sha256
 __version__ = '0.2.5'
 
 # 58 character alphabet used
-alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
+alphabet = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
 
 
 if bytes == str:  # python2
@@ -31,22 +31,33 @@ else:  # python3
     )
 
 
+def scrub_input(v):
+    if isinstance(v, str) and not isinstance(v, bytes):
+        v = v.encode('ascii')
+
+    if not isinstance(v, bytes):
+        raise TypeError(
+            "a bytes-like object is required (also str), not '%s'" %
+            type(v).__name__)
+
+    return v
+
+
 def b58encode_int(i, default_one=True):
     '''Encode an integer using Base58'''
     if not i and default_one:
-        return alphabet[0]
-    string = ""
+        return alphabet[0:1]
+    string = b""
     while i:
         i, idx = divmod(i, 58)
-        string = alphabet[idx] + string
+        string = alphabet[idx:idx+1] + string
     return string
 
 
 def b58encode(v):
     '''Encode a string using Base58'''
-    if not isinstance(v, bytes):
-        raise TypeError("a bytes-like object is required, not '%s'" %
-                        type(v).__name__)
+
+    v = scrub_input(v)
 
     nPad = len(v)
     v = v.lstrip(b'\0')
@@ -59,14 +70,13 @@ def b58encode(v):
 
     result = b58encode_int(acc, default_one=False)
 
-    return (alphabet[0] * nPad + result)
+    return (alphabet[0:1] * nPad + result)
 
 
 def b58decode_int(v):
     '''Decode a Base58 encoded string as an integer'''
 
-    if not isinstance(v, str):
-        v = v.decode('ascii')
+    v = scrub_input(v)
 
     decimal = 0
     for char in v:
@@ -77,16 +87,10 @@ def b58decode_int(v):
 def b58decode(v):
     '''Decode a Base58 encoded string'''
 
-    if not isinstance(v, str):
-        v = v.decode('ascii')
-
-    if not isinstance(v, str):
-        raise TypeError(
-            "a string-like object is required (also bytes), not '%s'" %
-            type(v).__name__)
+    v = scrub_input(v)
 
     origlen = len(v)
-    v = v.lstrip(alphabet[0])
+    v = v.lstrip(alphabet[0:1])
     newlen = len(v)
 
     acc = b58decode_int(v)
diff --git a/tox.ini b/tox.ini
index 33e915e..45d5087 100644
--- a/tox.ini
+++ b/tox.ini
@@ -3,4 +3,4 @@ envlist = py27,py35
 
 [testenv]
 deps = -rtest-requirements.txt
-commands = pytest --pep8 --flakes --cov=base58 .
+commands = pytest --pep8 --flakes --cov=base58 . []


Test patch:
diff --git a/test_base58.py b/test_base58.py
index d76c2c1..feb020a 100644
--- a/test_base58.py
+++ b/test_base58.py
@@ -6,6 +6,16 @@ from base58 import (
     b58decode_int, alphabet)
 
 
+if bytes == str:
+    bytes_from_char = (
+        lambda c: c
+    )
+else:
+    bytes_from_char = (
+        lambda c: bytes([c])
+    )
+
+
 class RaisesContext(object):
     pass
 
@@ -27,17 +37,17 @@ def assert_raises(matcher=None, message=''):
 
 def test_simple_encode():
     data = b58encode(b'hello world')
-    assert_that(data, equal_to('StV1DL6CwTryKyV'))
+    assert_that(data, equal_to(b'StV1DL6CwTryKyV'))
 
 
 def test_leadingz_encode():
     data = b58encode(b'\0\0hello world')
-    assert_that(data, equal_to('11StV1DL6CwTryKyV'))
+    assert_that(data, equal_to(b'11StV1DL6CwTryKyV'))
 
 
 def test_encode_empty():
     data = b58encode(b'')
-    assert_that(data, equal_to(''))
+    assert_that(data, equal_to(b''))
 
 
 def test_simple_decode():
@@ -91,14 +101,9 @@ def test_round_trips():
             assert_that(bytes_in, equal_to(bytes_out))
 
 
-def test_input_should_be_bytes():
-    data = u'3vQB7B6MrGQZaxCuFg4oH'
-    with assert_raises(TypeError):
-        b58encode(data)
-
-
 def test_simple_integers():
     for idx, char in enumerate(alphabet):
+        char = bytes_from_char(char)
         assert_that(b58decode_int(char), equal_to(idx))
         assert_that(b58encode_int(idx), equal_to(char))