Skip to content

Commit

Permalink
Cleanup code, fix warnings and update todo's
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Jun 10, 2024
1 parent 7c01000 commit a9e5450
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 74 deletions.
12 changes: 6 additions & 6 deletions bitcoinlib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# BLOCK parsing and construction
# © 2022 October - 1200 Web Development <http://1200wd.com/>
# © 2022 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -145,7 +145,7 @@ def parse(cls, raw, block_hash=None, height=None, parse_transactions=False, limi
:param raw: Raw serialize block
:type raw: BytesIO, bytes
:param block_hash: Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified.
:param block_hash: Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different from specified hash.
:type block_hash: bytes
:param height: Specify height if known. Will be derived from coinbase transaction if not provided.
:type height: int
Expand Down Expand Up @@ -182,7 +182,7 @@ def parse_bytes(cls, raw_bytes, block_hash=None, height=None, parse_transactions
:param raw_bytes: Raw serialize block
:type raw_bytes: bytes
:param block_hash: Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified.
:param block_hash: Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different from specified hash.
:type block_hash: bytes
:param height: Specify height if known. Will be derived from coinbase transaction if not provided.
:type height: int
Expand Down Expand Up @@ -217,7 +217,7 @@ def parse_bytesio(cls, raw, block_hash=None, height=None, parse_transactions=Fal
:param raw: Raw serialize block
:type raw: BytesIO
:param block_hash: Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified.
:param block_hash: Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different from specified hash.
:type block_hash: bytes
:param height: Specify height if known. Will be derived from coinbase transaction if not provided.
:type height: int
Expand Down Expand Up @@ -464,7 +464,7 @@ def target_hex(self):
@property
def difficulty(self):
"""
Block difficulty calculated from bits / target. Human readable representation of block's target.
Block difficulty calculated from bits / target. Human-readable representation of block's target.
Genesis block has difficulty of 1.0
Expand All @@ -484,7 +484,7 @@ def serialize(self):
"""
Serialize raw block in bytes.
A block consists of a 80 bytes header:
A block consists of an 80 bytes header:
* version - 4 bytes
* previous block - 32 bytes
* merkle root - 32 bytes
Expand Down
5 changes: 2 additions & 3 deletions bitcoinlib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# DataBase - SqlAlchemy database definitions
# © 2016 - 2023 May - 1200 Web Development <http://1200wd.com/>
# © 2016 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -265,7 +265,7 @@ def __repr__(self):
class DbKeyMultisigChildren(Base):
"""
Use many-to-many relationship for multisig keys. A multisig keys contains 2 or more child keys
and a child key can be used in more then one multisig key.
and a child key can be used in more than one multisig key.
"""
__tablename__ = 'key_multisig_children'
Expand Down Expand Up @@ -520,7 +520,6 @@ def db_update(db, version_db, code_version=BITCOINLIB_VERSION): # pragma: no co
if version_db < '0.7.0' and code_version >= '0.7.0':
raise ValueError("Old database version %s is not supported in version 0.7+. "
"Please copy private keys and recreate wallets" % version_db)
# TODO: write update script to copy private keys from db
# column = Column('witness_type', String(20), doc="Wallet witness type. Can be 'legacy', 'segwit' or "
# "'p2sh-segwit'. Default is segwit.")
# add_column(db.engine, 'keys', column)
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/db_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# Cache DataBase - SqlAlchemy database definitions for caching
# © 2020 - 2023 May - 1200 Web Development <http://1200wd.com/>
# © 2020 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down
4 changes: 2 additions & 2 deletions bitcoinlib/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# ENCODING - Methods for encoding and conversion
# © 2016 - 2023 May - 1200 Web Development <http://1200wd.com/>
# © 2016 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -493,7 +493,7 @@ def addr_to_pubkeyhash(address, as_hex=False, encoding=None):
Wrapper for the :func:`addr_base58_to_pubkeyhash` and :func:`addr_bech32_to_pubkeyhash` method
:param address: Crypto currency address in base-58 format
:param address: Cryptocurrency address in base-58 format
:type address: str
:param as_hex: Output as hexstring
:type as_hex: bool
Expand Down
12 changes: 6 additions & 6 deletions bitcoinlib/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# Public key cryptography and Hierarchical Deterministic Key Management
# © 2016 - 2022 October - 1200 Web Development <http://1200wd.com/>
# © 2016 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -615,7 +615,7 @@ def bip38_intermediate_password(passphrase, lot=None, sequence=None, owner_salt=
Intermediate passphrase generator for EC multiplied BIP38 encrypted private keys.
Source: https://github.com/meherett/python-bip38/blob/master/bip38/bip38.py
Use intermediate password to create a encrypted WIF key with the :func:`bip38_create_new_encrypted_wif` method.
Use intermediate password to create an encrypted WIF key with the :func:`bip38_create_new_encrypted_wif` method.
:param passphrase: Passphrase or password text
:type passphrase: str
Expand Down Expand Up @@ -668,7 +668,7 @@ def bip38_intermediate_password(passphrase, lot=None, sequence=None, owner_salt=
def bip38_create_new_encrypted_wif(intermediate_passphrase, compressed=True, seed=os.urandom(24),
network=DEFAULT_NETWORK):
"""
Create new encrypted WIF BIP38 EC multiplied key. Use :func:`bip38_intermediate_password` to create a
Create new encrypted WIF BIP38 EC multiplied key. Use :func:`bip38_intermediate_password` to create an
intermediate passphrase first.
:param intermediate_passphrase: Intermediate passphrase text
Expand Down Expand Up @@ -1155,7 +1155,7 @@ def __init__(self, import_key=None, network=None, compressed=True, password='',
# raise BKeyError("More then one network found with this versionbyte, please specify network. "
# "Networks found: %s" % found_networks)
# else:
# _logger.warning("Current network %s is different then the one found in key: %s" %
# _logger.warning("Current network %s is different from the one found in key: %s" %
# (network, found_networks[0]))
# self.network = Network(found_networks[0])
if key[-1:] == b'\x01':
Expand Down Expand Up @@ -1601,7 +1601,7 @@ class HDKey(Key):
"""
Class for Hierarchical Deterministic keys as defined in BIP0032
Besides a private or public key a HD Key has a chain code, allowing to create
Besides a private or public key a HDKey has a chain code, allowing to create
a structure of related keys.
The structure and key-path are defined in BIP0043 and BIP0044.
Expand Down Expand Up @@ -1774,7 +1774,7 @@ def __init__(self, import_key=None, key=None, chain=None, depth=0, parent_finger
# Generate new Master Key
seed = os.urandom(64)
key, chain = self._key_derivation(seed)
# If key is 64 bytes long assume a HD Key with key and chain part
# If key is 64 bytes long assume a HDKey with key and chain part
elif isinstance(import_key, bytes) and len(import_key) == 64:
key = import_key[:32]
chain = import_key[32:]
Expand Down
4 changes: 2 additions & 2 deletions bitcoinlib/mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# MNEMONIC class for BIP0039 Mnemonic Key management
# © 2016 - 2022 October - 1200 Web Development <http://1200wd.com/>
# © 2016 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -35,7 +35,7 @@ def __init__(self, language=DEFAULT_LANGUAGE):
"""
Init Mnemonic class and read wordlist of specified language
:param language: use specific wordlist, i.e. chinese, dutch (in development), english, french, italian, japanese or spanish. Leave empty for default 'english'
:param language: use specific wordlist, i.e. Chinese, Dutch (in development), English, French, Italian, Japanese or Spanish. Leave empty for default 'English'
:type language: str
"""
Expand Down
6 changes: 4 additions & 2 deletions bitcoinlib/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# Scripts class - Parse, Serialize and Evaluate scripts
# © 2022 - 1200 Web Development <http://1200wd.com/>
# © 2022 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -688,6 +688,8 @@ def evaluate(self, message=None, env_data=None, trace=False):
:type message: bytes
:param env_data: Dictionary with extra information needed to verify script. Such as 'redeemscript' for
:type env_data: dict()
:param trace: Write trace information to stdout
:type trace: bool
multisignature scripts and 'blockcount' for time locked scripts. Leave emtpy to use Script.data. If supplied Script.data will be ignored
Expand Down Expand Up @@ -793,7 +795,7 @@ def as_ints(self):
:return list of int:
"""
# TODO: What to do with data/hashes?
# This converts data to integers as well, which might be no always useful
return Stack([decode_num(x) for x in self])

def pop_as_number(self):
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/services/baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# Base Client
# © 2016-2022 - 1200 Web Development <http://1200wd.com/>
# © 2016 - 2024 May - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/services/bcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# Client for Bcoin Node
# © 2019 - 2023 March - 1200 Web Development <http://1200wd.com/>
# © 2019 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/services/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# Client for bitcoind deamon
# © 2017 - 2020 Oct - 1200 Web Development <http://1200wd.com/>
# © 2017 - 2024 May - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/services/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# SERVICES - Main Service connector
# © 2017-2022 - 1200 Web Development <http://1200wd.com/>
# © 2017 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/tools/import_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# IMPORT DATABASE - Extract wallet keys and information from old Bitcoinlib database and move to actual database
# © 2024 Februari - 1200 Web Development <http://1200wd.com/>
# © 2024 February - 1200 Web Development <http://1200wd.com/>
#
# TODO: Currently skips multisig wallets

Expand Down
46 changes: 23 additions & 23 deletions bitcoinlib/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# TRANSACTION class to create, verify and sign Transactions
# © 2017 - 2022 - 1200 Web Development <http://1200wd.com/>
# © 2017 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand All @@ -24,7 +24,7 @@
import random
from io import BytesIO
from bitcoinlib.encoding import *
from bitcoinlib.config.opcodes import *
# from bitcoinlib.config.opcodes import *
from bitcoinlib.keys import HDKey, Key, deserialize_address, Address, sign, verify, Signature
from bitcoinlib.networks import Network
from bitcoinlib.values import Value, value_to_satoshi
Expand All @@ -46,22 +46,22 @@ def __str__(self):
return self.msg


@deprecated # Replaced by Script class in version 0.6
def script_add_locktime_cltv(locktime_cltv, script): # pragma: no cover
lockbytes = bytes([op.op_checklocktimeverify, op.op_drop])
if script and len(script) > 6:
if script[4:6] == lockbytes:
return script
return varstr(locktime_cltv.to_bytes(4, 'little')) + lockbytes + script


@deprecated # Replaced by Script class in version 0.6
def script_add_locktime_csv(locktime_csv, script): # pragma: no cover
lockbytes = bytes([op.op_checklocktimeverify, op.op_drop])
if script and len(script) > 6:
if script[4:6] == lockbytes:
return script
return varstr(locktime_csv.to_bytes(4, 'little')) + lockbytes + script
# @deprecated # Replaced by Script class in version 0.6
# def script_add_locktime_cltv(locktime_cltv, script): # pragma: no cover
# lockbytes = bytes([op.op_checklocktimeverify, op.op_drop])
# if script and len(script) > 6:
# if script[4:6] == lockbytes:
# return script
# return varstr(locktime_cltv.to_bytes(4, 'little')) + lockbytes + script
#
#
# @deprecated # Replaced by Script class in version 0.6
# def script_add_locktime_csv(locktime_csv, script): # pragma: no cover
# lockbytes = bytes([op.op_checklocktimeverify, op.op_drop])
# if script and len(script) > 6:
# if script[4:6] == lockbytes:
# return script
# return varstr(locktime_csv.to_bytes(4, 'little')) + lockbytes + script


def get_unlocking_script_type(locking_script_type, witness_type='legacy', multisig=False):
Expand Down Expand Up @@ -341,10 +341,8 @@ def __init__(self, prev_txid, output_n, keys=None, signatures=None, public_hash=
if sig.hash_type:
self.hash_type = sig.hash_type

# fixme: p2wpkh == p2sh_p2wpkh
if self.script_type in ['sig_pubkey', 'p2sh_p2wpkh', 'p2wpkh'] and self.witnesses and not self.signatures and \
self.script_type in ['sig_pubkey', 'p2sh_p2wpkh'] and len(self.witnesses) == 2 and \
b'\0' not in self.witnesses:
if self.script_type in ['sig_pubkey', 'p2sh_p2wpkh'] and self.witnesses and not self.signatures and \
len(self.witnesses) == 2 and b'\0' not in self.witnesses:
self.signatures = [Signature.parse_bytes(self.witnesses[0])]
self.hash_type = self.signatures[0].hash_type
self.keys = [Key(self.witnesses[1], network=self.network, strict=self.strict)]
Expand Down Expand Up @@ -1851,6 +1849,8 @@ def add_input(self, prev_txid, output_n, keys=None, signatures=None, public_hash
:type value: int
:param double_spend: True if double spend is detected, depends on which service provider is selected
:type double_spend: bool
:param locktime_cltv: Check Locktime Verify value.
:type locktime_cltv: int
:param locktime_csv: Check Sequency Verify value.
:type locktime_csv: int
:param key_path: Key path of input key as BIP32 string or list
Expand Down Expand Up @@ -1975,7 +1975,7 @@ def estimate_size(self, number_of_change_outputs=0):
witness_size = 2
if self.witness_type != 'legacy':
est_size += 2
# TODO: if no inputs assume 1 input
# If no inputs assume 1 input
if not self.inputs:
est_size += 125
witness_size += 72
Expand Down
6 changes: 3 additions & 3 deletions bitcoinlib/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# BitcoinLib - Python Cryptocurrency Library
# VALUE class - representing cryptocurrency values
# © 2022 October - 1200 Web Development <http://1200wd.com/>
# © 2022 - 2024 June - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -56,7 +56,7 @@ def from_satoshi(cls, value, denominator=None, network=DEFAULT_NETWORK):
Initialize Value class with the smallest denominator as input. Such as represented in script and transactions
cryptocurrency values.
:param value: Amount of Satoshi's / smallest denominator for this network
:param value: Amount of Satoshi's / the smallest denominator for this network
:type value: int
:param denominator: Denominator as integer or string. Such as 0.001 or m for milli, 1000 or k for kilo, etc. See NETWORK_DENOMINATORS for list of available denominator symbols.
:type denominator: int, float, str
Expand Down Expand Up @@ -133,7 +133,7 @@ def __init__(self, value, denominator=None, network=DEFAULT_NETWORK):
>>> hex(Value("10.1 BTC"))
'0x3c336080'
:param value: Value as integer, float or string. Numeric values must be supllied in smallest denominator such as Satoshi's. String values must be in the format: <value> [<denominator>][<currency_symbol>]
:param value: Value as integer, float or string. Numeric values must be supllied in the smallest denominator such as Satoshi's. String values must be in the format: <value> [<denominator>][<currency_symbol>]
:type value: int, float, str
:param denominator: Denominator as integer or string. Such as 0.001 or m for milli, 1000 or k for kilo, etc. See NETWORK_DENOMINATORS for list of available denominator symbols.
:type denominator: int, float, str
Expand Down
Loading

0 comments on commit a9e5450

Please sign in to comment.