Merge PR #28: fix pip installation bug, improve base58

This commit is contained in:
Michał Sałaban 2018-07-03 14:40:08 +02:00
commit 241b1cb78f
7 changed files with 48 additions and 39 deletions

2
.gitignore vendored
View File

@ -1,5 +1,7 @@
.*
!.editorconfig
!.gitignore
!.travis.yml
*.py[co]
*~
*.bak

View File

@ -1,7 +1,9 @@
language: python
dist: xenial # minimal version required for python 3.7 support
python:
- "2.7"
- "3.6"
- "3.7-dev" # until 3.7 is officially supported by travis this is only (simple) way to test it
- "nightly"
matrix:
@ -10,9 +12,10 @@ matrix:
cache: pip
before_install:
- pip install -r test_requirements.txt
- rm -rf dist && python setup.py sdist # prep package distribution
install:
- pip install -e . # install dependencies as specified in setup.py
- pip install dist/*.tar.gz # install dependencies as specified in setup.py
- pip install -r test_requirements.txt
script:
- pytest
after_success:

1
MANIFEST.in Normal file
View File

@ -0,0 +1 @@
include *requirements.txt

View File

@ -61,7 +61,7 @@ Development
.. code-block:: bash
python3.6 -m venv .venv
python3 -m venv .venv
source .venv/bin/activate
3. Install dependencies

View File

@ -14,11 +14,11 @@ __fullEncodedBlockSize = 11
def _hexToBin(hex):
if len(hex) % 2 != 0:
raise ValueError("Hex string has invalid length!")
raise ValueError("Hex string has invalid length: %d" % len(hex))
return [int(hex[i*2:i*2+2], 16) for i in range(len(hex)//2)]
def _binToHex(bin):
return "".join([("0" + hex(int(bin[i])).split('x')[1])[-2:] for i in range(len(bin))])
return "".join('%02x' % int(b) for b in bin)
def _strToBin(a):
return [ord(s) for s in a]
@ -27,38 +27,18 @@ def _binToStr(bin):
return ''.join([chr(bin[i]) for i in range(len(bin))])
def _uint8be_to_64(data):
l_data = len(data)
if l_data < 1 or l_data > 8:
raise ValueError("Invalid input length: %d" % l_data)
if not (1 <= len(data) <= 8):
raise ValueError("Invalid input length: %d" % len(data))
res = 0
switch = 9 - l_data
for i in range(l_data):
if switch == 1:
res = res << 8 | data[i]
elif switch == 2:
res = res << 8 | data[i]
elif switch == 3:
res = res << 8 | data[i]
elif switch == 4:
res = res << 8 | data[i]
elif switch == 5:
res = res << 8 | data[i]
elif switch == 6:
res = res << 8 | data[i]
elif switch == 7:
res = res << 8 | data[i]
elif switch == 8:
res = res << 8 | data[i]
else:
raise ValueError("Impossible condition (9 - l_data = %d)" % switch)
for b in data:
res = res << 8 | b
return res
def _uint64_to_8be(num, size):
res = [0] * size;
if size < 1 or size > 8:
raise ValueError("Invalid input length: %d" % size)
res = [0] * size
twopow8 = 2**8
for i in range(size-1,-1,-1):
@ -79,7 +59,7 @@ def encode_block(data, buf, index):
while num > 0:
remainder = num % __b58base
num = num // __b58base
buf[index+i] = __alphabet[remainder];
buf[index+i] = __alphabet[remainder]
i -= 1
return buf
@ -96,9 +76,7 @@ def encode(hex):
last_block_size = l_data % __fullBlockSize
res_size = full_block_count * __fullEncodedBlockSize + __encodedBlockSizes[last_block_size]
res = [0] * res_size
for i in range(res_size):
res[i] = __alphabet[0]
res = [__alphabet[0]] * res_size
for i in range(full_block_count):
res = encode_block(data[(i*__fullBlockSize):(i*__fullBlockSize+__fullBlockSize)], res, i * __fullEncodedBlockSize)
@ -151,10 +129,10 @@ def decode(enc):
full_block_count = l_enc // __fullEncodedBlockSize
last_block_size = l_enc % __fullEncodedBlockSize
last_block_decoded_size = __encodedBlockSizes.index(last_block_size)
if last_block_decoded_size < 0:
raise ValueError("Invalid encoded length: %d" % last_block_decoded_size)
try:
last_block_decoded_size = __encodedBlockSizes.index(last_block_size)
except ValueError:
raise ValueError("Invalid encoded length: %d" % l_enc)
data_size = full_block_count * __fullBlockSize + last_block_decoded_size

View File

@ -32,7 +32,7 @@ setup(
url = 'https://github.com/emesik/monero-python/',
long_description = open('README.rst', 'rb').read().decode('utf-8'),
install_requires = open('requirements.txt', 'r').read().splitlines(),
tests_requires=open('test_requirements.txt', 'r').read().splitlines(),
tests_require=open('test_requirements.txt', 'r').read().splitlines(),
setup_requires=[
'pytest-runner',
],
@ -47,6 +47,8 @@ setup(
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
keywords = 'monero cryptocurrency',
test_suite='tests',

23
tests/test_base58.py Normal file
View File

@ -0,0 +1,23 @@
import unittest
from monero.base58 import decode, encode
class Base58EncodeTestCase(unittest.TestCase):
def test_encode_empty(self):
self.assertEqual(encode(''), '')
def test_encode_invalid_hex_length(self):
with self.assertRaises(ValueError) as cm:
encode('abcde')
self.assertEqual(str(cm.exception), 'Hex string has invalid length: 5')
class Base58DecodeTestCase(unittest.TestCase):
def test_decode_empty(self):
self.assertEqual(decode(''), '')
def test_decode_invalid_length_block(self):
with self.assertRaises(ValueError) as cm:
decode('f')
self.assertEqual(str(cm.exception), 'Invalid encoded length: 1')