Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Dec 31, 2024
1 parent e2b3745 commit 419fe65
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
15 changes: 15 additions & 0 deletions counterparty-core/counterpartycore/lib/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ def list_unspent(source, allow_unconfirmed_inputs):
"No UTXOs found with Bitcoin Core and Electr is not configured, use the `inputs_set` parameter to provide UTXOs"
)
return backend.electrs.list_unspent(source, allow_unconfirmed_inputs)


def search_pubkey(source, tx_hashes=None):
# first search with Bitcoin Core
if isinstance(tx_hashes, list) and len(tx_hashes) > 0:
pubkey = backend.bitcoind.search_pubkey_in_transactions(source, tx_hashes)
if pubkey is not None:
return pubkey
# then search with Electrs
if config.ELECTRS_URL is None:
return None
pubkey = backend.electrs.search_pubkey(source)
if pubkey is not None:
return pubkey
return None
6 changes: 0 additions & 6 deletions counterparty-core/counterpartycore/lib/backend/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,6 @@ def search_pubkey_in_transactions(pubkeyhash, tx_hashes):
return None


def pubkey_from_inputs_set(inputs_set, pubkeyhash):
tx_hashes = inputs_set.split(",")
tx_hashes = [utxo.split(":")[0] for utxo in tx_hashes]
return search_pubkey_in_transactions(pubkeyhash, tx_hashes)


def list_unspent(source, allow_unconfirmed_inputs):
min_conf = 0 if allow_unconfirmed_inputs else 1
bitcoind_unspent_list = []
Expand Down
30 changes: 13 additions & 17 deletions counterparty-core/counterpartycore/lib/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
def address_to_script_pub_key(address, unspent_list, construct_params):
if script.is_multisig(address):
signatures_required, addresses, signatures_possible = script.extract_array(address)
pubkeys = [search_pubkey(address, unspent_list, construct_params) for address in addresses]
pubkeys = [search_pubkey(addr, unspent_list, construct_params) for addr in addresses]
if None in pubkeys:
raise exceptions.ComposeError(
f"Pubkeys not found for {address}, please provide them with the `pubkeys` parameter"
)
multisig_script = Script(
[signatures_required] + pubkeys + [signatures_possible] + ["OP_CHECKMULTISIG"]
)
Expand Down Expand Up @@ -135,6 +139,7 @@ def is_valid_pubkey(pubkey):


def search_pubkey(source, unspent_list, construct_params):
# search in provided pubkeys
if construct_params is not None:
pubkeys = construct_params.get("pubkeys")
if pubkeys is not None:
Expand All @@ -145,22 +150,9 @@ def search_pubkey(source, unspent_list, construct_params):
for pubkey in pubkeys:
if PublicKey.from_hex(pubkey).get_address(compressed=True).to_string() == source:
return pubkey
# search with Bitcoin Core
# then search with Bitcoin Core or Electrs
tx_hashes = [utxo["txid"] for utxo in unspent_list]
multisig_pubkey = backend.bitcoind.search_pubkey_in_transactions(source, tx_hashes)
if multisig_pubkey is not None:
return multisig_pubkey
# else search with Electrs
if config.ELECTRS_URL is None:
raise exceptions.ComposeError(
"No multisig pubkey found with Bitcoin Core and Electrs is not configured, use the `multisig_pubkey` parameter to provide it"
)
multisig_pubkey = backend.electrs.search_pubkey(source)
if multisig_pubkey is not None:
return multisig_pubkey
raise exceptions.ComposeError(
f"No multisig pubkey found for {source}, use the `multisig_pubkey` parameter to provide it"
)
return backend.search_pubkey(source, tx_hashes)


def make_valid_pubkey(pubkey_start):
Expand Down Expand Up @@ -218,7 +210,11 @@ def prepare_multisig_output(source, data, arc4_key, unspent_list, construct_para
multisig_pubkey = construct_params.get("multisig_pubkey")
if multisig_pubkey is None:
multisig_pubkey = search_pubkey(source, unspent_list, construct_params)
if not is_valid_pubkey(multisig_pubkey):
if multisig_pubkey is None:

Check warning

Code scanning / pylint

Unnecessary "elif" after "raise", remove the leading "el" from "elif". Warning

Unnecessary "elif" after "raise", remove the leading "el" from "elif".
raise exceptions.ComposeError(
f"Pubkey not found for {source}, please provide it with the `multisig_pubkey` parameter"
)
elif not is_valid_pubkey(multisig_pubkey):
raise exceptions.ComposeError(f"Invalid multisig pubkey: {multisig_pubkey}")
# generate pubkey pairs from data
pubkey_pairs = data_to_pubkey_pairs(data, arc4_key)
Expand Down

0 comments on commit 419fe65

Please sign in to comment.