Skip to content

Commit

Permalink
Fix regtest scenarios: always provide inputs_set and pubkey; tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Dec 18, 2024
1 parent 46f04ad commit c22ad8d
Show file tree
Hide file tree
Showing 14 changed files with 4,314 additions and 5,307 deletions.
5,103 changes: 2,288 additions & 2,815 deletions apiary.apib

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions counterparty-core/counterpartycore/lib/api/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,16 @@ def handle_route(**kwargs):
script.AddressError,
exceptions.ElectrError,
) as e:
import traceback

print(traceback.format_exc())
return return_result(400, error=str(e), start_time=start_time, query_args=query_args)
except Exception as e:
capture_exception(e)
logger.error("Error in API: %s", e)
# import traceback
# print(traceback.format_exc())
import traceback

print(traceback.format_exc())
return return_result(
503, error="Unknown error", start_time=start_time, query_args=query_args
)
Expand Down
17 changes: 12 additions & 5 deletions counterparty-core/counterpartycore/lib/backend/electr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import requests

from counterpartycore.lib import config
from counterpartycore.lib import config, exceptions


def electr_query(url):
if config.ELECTR_URL is None:
raise exceptions.ElectrError("Electr server not configured")
try:
return requests.get(f"{config.ELECTR_URL}/{url}", timeout=10).json()
except requests.exceptions.RequestException as e:
raise exceptions.ElectrError(f"Electr error: {e}") from e


def get_utxos(address, unconfirmed: bool = False, unspent_tx_hash: str = None):
Expand All @@ -10,8 +19,7 @@ def get_utxos(address, unconfirmed: bool = False, unspent_tx_hash: str = None):
:param unconfirmed: Include unconfirmed transactions
:param unspent_tx_hash: Filter by unspent_tx_hash
"""
url = f"{config.ELECTR_URL}/address/{address}/utxo"
utxo_list = requests.get(url, timeout=10).json()
utxo_list = electr_query(f"address/{address}/utxo")
result = []
for utxo in utxo_list:
if not utxo["status"]["confirmed"] and not unconfirmed:
Expand All @@ -27,8 +35,7 @@ def get_history(address: str, unconfirmed: bool = False):
Returns all transactions involving a given address
:param address: The address to search for (e.g. $ADDRESS_3)
"""
url = f"{config.ELECTR_URL}/address/{address}/history"
tx_list = requests.get(url, timeout=10).json()
tx_list = electr_query(f"address/{address}/history")
result = []
for tx in tx_list:
if tx["status"]["confirmed"] or unconfirmed:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ def chunks(l, n): # noqa: E741
yield l[i : i + n]


def pubkey_from_tx(tx, pubkeyhash):
for vin in tx["vin"]:
if "witness" in vin:
if len(vin["witness"]) >= 2:
# catch unhexlify errs for when txinwitness[1] isn't a witness program (eg; for P2W)
try:
pubkey = vin["witness"][1]
if pubkeyhash == script.pubkey_to_p2whash(util.unhexlify(pubkey)):
return pubkey
except binascii.Error:
pass
elif "is_coinbase" not in vin or not vin["is_coinbase"]:
asm = vin["scriptsig_asm"].split(" ")
if len(asm) >= 2:
# catch unhexlify errs for when asm[1] isn't a pubkey (eg; for P2SH)
try:
pubkey = asm[1]
if pubkeyhash == script.pubkey_to_pubkeyhash(util.unhexlify(pubkey)):
return pubkey
except binascii.Error:
pass
return None


def pubkeyhash_to_pubkey(pubkeyhash, provided_pubkeys=None):
# Search provided pubkeys.
if provided_pubkeys:
Expand All @@ -34,26 +58,9 @@ def pubkeyhash_to_pubkey(pubkeyhash, provided_pubkeys=None):
raw_transactions = backend.electr.get_history(pubkeyhash)
for tx_id in raw_transactions:
tx = raw_transactions[tx_id]
for vin in tx["vin"]:
if "witness" in vin:
if len(vin["witness"]) >= 2:
# catch unhexlify errs for when txinwitness[1] isn't a witness program (eg; for P2W)
try:
pubkey = vin["witness"][1]
if pubkeyhash == script.pubkey_to_p2whash(util.unhexlify(pubkey)):
return pubkey
except binascii.Error:
pass
elif "is_coinbase" not in vin or not vin["is_coinbase"]:
asm = vin["scriptsig_asm"].split(" ")
if len(asm) >= 2:
# catch unhexlify errs for when asm[1] isn't a pubkey (eg; for P2SH)
try:
pubkey = asm[1]
if pubkeyhash == script.pubkey_to_pubkeyhash(util.unhexlify(pubkey)):
return pubkey
except binascii.Error:
pass
pubkey = pubkey_from_tx(tx, pubkeyhash)
if pubkey:
return pubkey

raise exceptions.UnknownPubKeyError(
"Public key was neither provided nor published in blockchain."
Expand Down
2 changes: 0 additions & 2 deletions counterparty-core/counterpartycore/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ def api_server_v2(request, cp_server):
"backend_port": None,
"backend_user": None,
"backend_password": None,
"indexd_connect": None,
"indexd_port": None,
"backend_ssl": False,
"backend_ssl_no_verify": False,
"backend_poll_interval": None,
Expand Down
Loading

0 comments on commit c22ad8d

Please sign in to comment.