From 692aaba6895cddc89cae8d0fd5dc38c233ff04f8 Mon Sep 17 00:00:00 2001 From: 4tochka Date: Tue, 19 Jun 2018 03:13:55 +0400 Subject: [PATCH] Pure functions docs --- docs/source/conf.py | 4 +++- docs/source/functional.rst | 3 +++ pybtc/address.py | 40 +++++++++++++++++++-------------- pybtc/tools.py | 8 +++---- setup.py | 32 ++++++++++++++++++++------ tests/test/__init__.py | 12 +++++----- tests/test/address_functions.py | 2 +- 7 files changed, 65 insertions(+), 36 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 0d02df5..5a89d26 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -20,9 +20,11 @@ sys.path.insert(0, os.path.abspath('..')) sys.path.insert(0, os.path.abspath('../_static/')) sys.path.insert(0, os.path.abspath('../../_static/')) sys.path.insert(0, os.path.abspath('../../pybtc/')) +sys.path.insert(0, os.path.abspath('../../pybtc')) sys.path.insert(0, os.path.abspath('./_static/')) - +sys.path.insert(0, os.path.abspath('..')) +sys.path.insert(0, os.path.abspath('../..')) # -- Project information ----------------------------------------------------- project = 'pybtc' diff --git a/docs/source/functional.rst b/docs/source/functional.rst index 4843270..2446767 100644 --- a/docs/source/functional.rst +++ b/docs/source/functional.rst @@ -47,3 +47,6 @@ Addresses .. autofunction:: pybtc.is_address_valid +Signatures +========== + diff --git a/pybtc/address.py b/pybtc/address.py index b1c5c28..132a82b 100644 --- a/pybtc/address.py +++ b/pybtc/address.py @@ -6,21 +6,22 @@ class PrivateKey(): if key is None: self.compressed = compressed self.testnet = testnet - self.raw_key = create_private_key() + self.raw_key = create_private_key(wif=False) else: - if type(key) == str: + if isinstance(key, str): try: key = unhexlify(key) except: pass - if type(key) == bytes: - assert len(key) == 32 + if isinstance(key, bytes): + if len(key) != 32: + raise TypeError("private key invalid") self.raw_key = key self.compressed = compressed self.testnet = testnet return - assert type(key) == str - self.raw_key = wif_to_private_key(key) + assert isinstance(key, str) + self.raw_key = wif_to_private_key(key, hex=False) if key[0] in (MAINNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX, TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX): self.compressed = False @@ -48,16 +49,19 @@ class PrivateKey(): class PublicKey(): def __init__(self, key=None): - if type(key) == str: + if isinstance(key, str): try: key = unhexlify(key) except: pass - if type(key) == PrivateKey: + if isinstance(key, PrivateKey): key = private_to_public_key(key.raw_key, - compressed=key.compressed) - assert type(key) == bytes - assert len(key) == 33 or len(key) == 65 + compressed=key.compressed, + hex=False) + if not isinstance(key, bytes): + raise TypeError("public key invalid") + if len(key) != 33 and len(key) != 65: + raise TypeError("public key invalid") if len(key) == 33: self.compressed = True else: @@ -68,7 +72,7 @@ class PublicKey(): return hexlify(self.raw_key).decode() def __str__(self): - return hex() + return self.hex() class Address(): @@ -79,18 +83,20 @@ class Address(): compressed=compressed) self.public_key = PublicKey(self.private_key) self.testnet = testnet - elif type(key) == PrivateKey: + elif isinstance(key, PrivateKey): self.private_key = key self.testnet = key.testnet compressed = key.compressed self.public_key = PublicKey(self.private_key) - elif type(key) == PublicKey: + elif isinstance(key, PublicKey): self.public_key = key self.testnet = testnet compressed = key.compressed - assert address_type in ("P2PKH", "PUBKEY", "P2WPKH", "P2SH_P2WPKH") + if address_type not in ("P2PKH", "PUBKEY", "P2WPKH", "P2SH_P2WPKH"): + raise TypeError("address type invalid") if not compressed: - assert address_type in ("P2PKH", "PUBKEY") + if address_type not in ("P2PKH", "PUBKEY", "P2SH"): + raise TypeError("compressed public key invalid") self.type = address_type if address_type in ("P2WPKH"): @@ -120,7 +126,7 @@ class ScriptAddress(): testnet=False, witness_version=None): self.witness_version = witness_version self.testnet = testnet - if type(script) == str: + if isinstance(script, str): script = unhexlify(script) self.script_raw = script self.script = hexlify(self.script_raw).decode() diff --git a/pybtc/tools.py b/pybtc/tools.py index 2b08a97..918d209 100644 --- a/pybtc/tools.py +++ b/pybtc/tools.py @@ -129,7 +129,7 @@ def private_to_public_key(private_key, compressed=True, hex=True): if private_key[0] in (MAINNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX, TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX): compressed = False - private_key = wif_to_private_key(private_key) + private_key = wif_to_private_key(private_key, hex=0) else: raise TypeError("private key must be a bytes or WIF or hex encoded string") pubkey_ptr = ffi.new('secp256k1_pubkey *') @@ -334,7 +334,7 @@ def address_to_script(address, hex=False): MAINNET_SCRIPT_ADDRESS_PREFIX): s = [BYTE_OPCODE["OP_HASH160"], b'\x14', - address_to_hash(address), + address_to_hash(address, hex=False), BYTE_OPCODE["OP_EQUAL"]] elif address[0] in (MAINNET_ADDRESS_PREFIX, TESTNET_ADDRESS_PREFIX, @@ -342,12 +342,12 @@ def address_to_script(address, hex=False): s = [BYTE_OPCODE["OP_DUP"], BYTE_OPCODE["OP_HASH160"], b'\x14', - address_to_hash(address), + address_to_hash(address, hex=False), BYTE_OPCODE["OP_EQUALVERIFY"], BYTE_OPCODE["OP_CHECKSIG"]] elif address[:2] in (TESTNET_SEGWIT_ADDRESS_PREFIX, MAINNET_SEGWIT_ADDRESS_PREFIX): - h = address_to_hash(address) + h = address_to_hash(address, hex=False) s = [BYTE_OPCODE["OP_0"], bytes([len(h)]), h] diff --git a/setup.py b/setup.py index 1fcd128..5450bec 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,31 @@ #!/usr/bin/python3 # coding: utf-8 -# todo install libsec256 part -from distutils.core import setup + +from setuptools import setup, find_packages + setup(name='pybtc', - version='1.0.1', - description='Bitcoin library', + version='0.1', + description='Python Bitcoin library', + keywords='bitcoin', + url='https://github.com/bitaps-com/pybtc', author='Alexsei Karpov', author_email='admin@bitaps.com', - url='https://github.com/bitaps-com/pybtc', - packages=['pybtc', ], - ) \ No newline at end of file + license='GPL-3.0', + packages=find_packages(), + install_requires=[ 'secp256k1', ], + include_package_data=True, + zip_safe=False) + +# +# from distutils.core import setup +# +# setup(name='pybtc', +# version='1.0.1', +# description='Bitcoin library', +# author='Alexsei Karpov', +# author_email='admin@bitaps.com', +# url='https://github.com/bitaps-com/pybtc', +# packages=['pybtc'], +# +# ) diff --git a/tests/test/__init__.py b/tests/test/__init__.py index df242f5..9fc40a2 100644 --- a/tests/test/__init__.py +++ b/tests/test/__init__.py @@ -1,10 +1,10 @@ -# from .hash_functions import * -# from .integer import * -# from .address_functions import * +from .hash_functions import * +from .integer import * +from .address_functions import * from .address_class import * -# from .ecdsa import * -# from .transaction_deserialize import * -# from .transaction_constructor import * +from .ecdsa import * +from .transaction_deserialize import * +from .transaction_constructor import * # from .script_deserialize import * diff --git a/tests/test/address_functions.py b/tests/test/address_functions.py index fbbe0f7..254a2a9 100644 --- a/tests/test/address_functions.py +++ b/tests/test/address_functions.py @@ -47,7 +47,7 @@ class AddressFunctionsTests(unittest.TestCase): hex=1),p) def test_create_private_key(self): - p = tools.create_private_key() + p = tools.create_private_key(wif=0) pw = tools.private_key_to_wif(p) self.assertEqual(tools.is_wif_valid(pw), True)