Skip to content

Commit

Permalink
[FIX] Some docstests and unittest issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed Nov 1, 2021
1 parent a650cb4 commit edde65b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
14 changes: 7 additions & 7 deletions bitcoinlib/keys.py
Expand Up @@ -108,13 +108,13 @@ def get_key_format(key, is_private=None):
This method does not validate if a key is valid.
>>> get_key_format('L4dTuJf2ceEdWDvCPsLhYf8GiiuYqXtqfbcKdC21BPDvEM1ykJRC')
{'format': 'wif_compressed', 'networks': ['bitcoin'], 'is_private': True, 'script_types': [], 'witness_types': ['legacy'], 'multisig': [False]}
{'format': 'wif_compressed', 'networks': ['bitcoin', 'regtest'], 'is_private': True, 'script_types': [], 'witness_types': ['legacy'], 'multisig': [False]}
>>> get_key_format('becc7ac3b383cd609bd644aa5f102a811bac49b6a34bbd8afe706e32a9ac5c5e')
{'format': 'hex', 'networks': None, 'is_private': True, 'script_types': [], 'witness_types': ['legacy'], 'multisig': [False]}
>>> get_key_format('Zpub6vZyhw1ShkEwNxtqfjk7jiwoEbZYMJdbWLHvEwo6Ns2fFc9rdQn3SerYFQXYxtZYbA8a1d83shW3g4WbsnVsymy2L8m7wpeApiuPxug3ARu')
{'format': 'hdkey_public', 'networks': ['bitcoin'], 'is_private': False, 'script_types': ['p2wsh'], 'witness_types': ['segwit'], 'multisig': [True]}
{'format': 'hdkey_public', 'networks': ['bitcoin', 'regtest'], 'is_private': False, 'script_types': ['p2wsh'], 'witness_types': ['segwit'], 'multisig': [True]}
:param key: Any private or public key
:type key: str, int, bytes
Expand Down Expand Up @@ -187,14 +187,14 @@ def get_key_format(key, is_private=None):
else:
prefix_data = wif_prefix_search(key_hex[:8])
if prefix_data:
networks = list(set([n['network'] for n in prefix_data]))
networks = list(dict.fromkeys([n['network'] for n in prefix_data]))
if is_private is None and len(set([n['is_private'] for n in prefix_data])) > 1:
raise BKeyError("Cannot determine if key is private or public, please specify is_private "
"attribute")
is_private = prefix_data[0]['is_private']
script_types = list(set([n['script_type'] for n in prefix_data]))
witness_types = list(set([n['witness_type'] for n in prefix_data]))
multisig = list(set([n['multisig'] for n in prefix_data]))
script_types = list(dict.fromkeys([n['script_type'] for n in prefix_data]))
witness_types = list(dict.fromkeys([n['witness_type'] for n in prefix_data]))
multisig = list(dict.fromkeys([n['multisig'] for n in prefix_data]))
key_format = 'hdkey_public'
if is_private:
key_format = 'hdkey_private'
Expand Down Expand Up @@ -241,7 +241,7 @@ def deserialize_address(address, encoding=None, network=None):
If more networks and or script types are found you can find these in the 'networks' field.
>>> deserialize_address('1Khyc5eUddbhYZ8bEZi9wiN8TrmQ8uND4j')
{'address': '1Khyc5eUddbhYZ8bEZi9wiN8TrmQ8uND4j', 'encoding': 'base58', 'public_key_hash': 'cd322766c02e7c37c3e3f9b825cd41ffbdcd17d7', 'public_key_hash_bytes': b"\\xcd2'f\\xc0.|7\\xc3\\xe3\\xf9\\xb8%\\xcdA\\xff\\xbd\\xcd\\x17\\xd7", 'prefix': b'\\x00', 'network': 'bitcoin', 'script_type': 'p2pkh', 'witness_type': 'legacy', 'networks': ['bitcoin']}
{'address': '1Khyc5eUddbhYZ8bEZi9wiN8TrmQ8uND4j', 'encoding': 'base58', 'public_key_hash': 'cd322766c02e7c37c3e3f9b825cd41ffbdcd17d7', 'public_key_hash_bytes': b"\\xcd2'f\\xc0.|7\\xc3\\xe3\\xf9\\xb8%\\xcdA\\xff\\xbd\\xcd\\x17\\xd7", 'prefix': b'\\x00', 'network': 'bitcoin', 'script_type': 'p2pkh', 'witness_type': 'legacy', 'networks': ['bitcoin', 'regtest']}
:param address: A base58 or bech32 encoded address
:type address: str
Expand Down
8 changes: 4 additions & 4 deletions bitcoinlib/networks.py
Expand Up @@ -72,16 +72,16 @@ def network_values_for(field):
Return all prefixes for field, i.e.: prefix_wif, prefix_address_p2sh, etc
>>> network_values_for('prefix_wif')
[b'\\x99', b'\\x80', b'\\xef', b'\\xb0', b'\\xb0', b'\\xef', b'\\xcc', b'\\xef', b'\\x9e', b'\\xf1']
[b'\\x99', b'\\x80', b'\\xef', b'\\xb0', b'\\xcc', b'\\x9e', b'\\xf1']
>>> network_values_for('prefix_address_p2sh')
[b'\\x95', b'\\x05', b'\\xc4', b'2', b'\\x05', b':', b'\\x10', b'\\x13', b'\\x16', b'\\xc4']
[b'\\x95', b'\\x05', b'\\xc4', b'2', b':', b'\\x10', b'\\x13', b'\\x16']
:param field: Prefix name from networks definitions (networks.json)
:type field: str
:return str:
"""
return [_format_value(field, nv[field]) for nv in NETWORK_DEFINITIONS.values()]
return list(dict.fromkeys([_format_value(field, nv[field]) for nv in NETWORK_DEFINITIONS.values()]))


def network_by_value(field, value):
Expand Down Expand Up @@ -157,7 +157,7 @@ def wif_prefix_search(wif, witness_type=None, multisig=None, network=None):
Can return multiple items if no network is specified:
>>> [nw['network'] for nw in wif_prefix_search('0488ADE4', multisig=True)]
['bitcoin', 'dash', 'dogecoin']
['bitcoin', 'regtest', 'dash', 'dogecoin']
:param wif: WIF string or prefix as hexadecimal string
:type wif: str
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/transactions.py
Expand Up @@ -1559,7 +1559,7 @@ def __init__(self, inputs=None, outputs=None, locktime=0, version=None,
if not input_total:
input_total = sum([i.value for i in inputs])
id_list = [i.index_n for i in self.inputs]
if list(set(id_list)) != id_list:
if list(dict.fromkeys(id_list)) != id_list:
_logger.info("Identical transaction indexes (tid) found in inputs, please specify unique index. "
"Indexes will be automatically recreated")
index_n = 0
Expand Down
8 changes: 4 additions & 4 deletions bitcoinlib/wallets.py
Expand Up @@ -728,7 +728,7 @@ def sign(self, keys=None, index_n=0, multisig_key_n=None, hash_type=SIGHASH_ALL,
"""
priv_key_list_arg = []
if keys:
key_paths = list(set([ti.key_path for ti in self.inputs if ti.key_path[0] == 'm']))
key_paths = list(dict.fromkeys([ti.key_path for ti in self.inputs if ti.key_path[0] == 'm']))
if not isinstance(keys, list):
keys = [keys]
for priv_key in keys:
Expand Down Expand Up @@ -2482,7 +2482,7 @@ def accounts(self, network=DEFAULT_NETWORK):
accounts = [wk.account_id for wk in self.keys_accounts(network=network)]
if not accounts:
accounts = [self.default_account_id]
return list(set(accounts))
return list(dict.fromkeys(accounts))

def networks(self, as_dict=False):
"""
Expand All @@ -2506,7 +2506,7 @@ def networks(self, as_dict=False):
nw_list.append(Network(wk.network_name))

networks = []
nw_list = list(set(nw_list))
nw_list = list(dict.fromkeys(nw_list))
for nw in nw_list:
if as_dict:
nw = nw.__dict__
Expand Down Expand Up @@ -2994,7 +2994,7 @@ def transactions_update_by_txids(self, txids):
"""
if not isinstance(txids, list):
txids = [txids]
txids = list(set(txids))
txids = list(dict.fromkeys(txids))

txs = []
srv = Service(network=self.network.name, providers=self.providers, cache_uri=self.db_cache_uri)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_services.py
Expand Up @@ -986,10 +986,9 @@ def test_service_cache_transaction_coinbase(self):
srv.gettransaction('68104dbd6819375e7bdf96562f89290b41598df7b002089ecdd3c8d999025b13')
self.assertGreaterEqual(srv.results_cache_n, 1)

# FIXME: Fails with some providers, needs testing
def test_service_cache_transaction_segwit_database(self):
for db_uri in DATABASES_CACHE:
srv = ServiceTest(cache_uri=db_uri, network='bitcoin', exclude_providers=['blocksmurfer'])
srv = ServiceTest(cache_uri=db_uri, network='bitcoin')
rawtx \
= "020000000001012d83146744d37b0aa13b609a206206860d550e911882c89afabc3abaeebc5f7d0100000000ffffffff01" \
"cb2500000000000017a914da4540e3b144eb0c6d3be573fba0ffab3887c6138702473044022067be0a16037511c15b310b" \
Expand Down

0 comments on commit edde65b

Please sign in to comment.