Skip to content

Commit

Permalink
more cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 6, 2025
1 parent 4aab7af commit 76940ba
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 105 deletions.
4 changes: 3 additions & 1 deletion counterparty-core/counterpartycore/lib/api/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,10 +982,12 @@ def get_tx_info(tx_hex, block_index=None):
# block_index mandatory for transactions before block 335000
use_txid = util.enabled("correct_segwit_txids", block_index=block_index)
with LedgerDBConnectionPool().connection() as db:
decoded_tx = deserialize.deserialize_tx(tx_hex, use_txid=use_txid, parse_vouts=True)
print("DEcoded tx", decoded_tx)
source, destination, btc_amount, fee, data, _dispensers_outs, _utxos_info = (
gettxinfo.get_tx_info(
db,
deserialize.deserialize_tx(tx_hex, use_txid=use_txid),
decoded_tx,
block_index=block_index,
composing=True,
)
Expand Down
5 changes: 4 additions & 1 deletion counterparty-core/counterpartycore/lib/api/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,10 @@ def info(db, rawtransaction: str, block_index: int = None):
"""
try:
decoded_tx = deserialize.deserialize_tx(
rawtransaction, use_txid=util.enabled("correct_segwit_txids", block_index)
rawtransaction,
use_txid=util.enabled("correct_segwit_txids", block_index),
parse_vouts=True,
block_index=block_index,
)
except Exception as e:
raise exceptions.ComposeError("Invalid rawtransaction") from e
Expand Down
4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/lib/backend/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def get_decoded_transaction(tx_hash, block_index=None):

raw_tx = getrawtransaction(tx_hash)
use_txid = util.enabled("correct_segwit_txids", block_index=block_index)
tx = deserialize.deserialize_tx(raw_tx, use_txid=use_txid)
tx = deserialize.deserialize_tx(raw_tx, use_txid=use_txid, block_index=block_index)

add_transaction_in_cache(tx_hash, tx)

Expand Down Expand Up @@ -387,7 +387,7 @@ def decoderawtransaction(rawtx: str):
Proxy to `decoderawtransaction` RPC call.
:param rawtx: The raw transaction hex. (e.g. 0200000000010199c94580cbea44aead18f429be20552e640804dc3b4808e39115197f1312954d000000001600147c6b1112ed7bc76fd03af8b91d02fd6942c5a8d0ffffffff0280f0fa02000000001976a914a11b66a67b3ff69671c8f82254099faf374b800e88ac70da0a27010000001600147c6b1112ed7bc76fd03af8b91d02fd6942c5a8d002000000000000)
"""
return rpc("decoderawtransaction", [rawtx])
return deserialize.deserialize_tx(rawtx, False)


def search_pubkey_in_transactions(pubkeyhash, tx_hashes):
Expand Down
10 changes: 7 additions & 3 deletions counterparty-core/counterpartycore/lib/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
config,
deserialize,
exceptions,
gettxinfo,
ledger,
script,
util,
Expand Down Expand Up @@ -128,6 +127,7 @@ def prepare_opreturn_output(data, arc4_key):
if len(data) + len(config.PREFIX) > config.OP_RETURN_MAX_SIZE:
raise exceptions.ComposeError("One `OP_RETURN` output per transaction")
opreturn_data = config.PREFIX + data
print(f"opreturn_data: {opreturn_data}", config.OP_RETURN_MAX_SIZE, len(data))
opreturn_data = encrypt_data(opreturn_data, arc4_key)
return [TxOutput(0, Script(["OP_RETURN", b_to_h(opreturn_data)]))]

Expand Down Expand Up @@ -833,7 +833,7 @@ def construct(db, tx_info, construct_params):
def check_transaction_sanity(tx_info, composed_tx, construct_params):
tx_hex = composed_tx["rawtransaction"]
source, destinations, data = tx_info
decoded_tx = deserialize.deserialize_tx(tx_hex, use_txid=True)
decoded_tx = deserialize.deserialize_tx(tx_hex, use_txid=True, parse_vouts=True)

# check if source address matches the first input address
first_utxo_txid = decoded_tx["vin"][0]["hash"]
Expand Down Expand Up @@ -877,7 +877,11 @@ def check_transaction_sanity(tx_info, composed_tx, construct_params):

# check if data matches the output data
if data:
_, _, _, tx_data, _ = gettxinfo.parse_transaction_vouts(decoded_tx)
if isinstance(decoded_tx["parsed_vouts"], Exception):
raise exceptions.ComposeError(
f"Sanity check error: cannot parse the output data from the transaction ({decoded_tx['parsed_vouts']})"
)
_, _, _, tx_data, _ = decoded_tx["parsed_vouts"]
if tx_data != data:
raise exceptions.ComposeError("Sanity check error: data does not match the output data")

Expand Down
2 changes: 2 additions & 0 deletions counterparty-core/counterpartycore/lib/deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def deserialize_tx(tx_hex, use_txid, parse_vouts=False, block_index=None):
"network": config.NETWORK_NAME,
"db_dir": "",
"log_file": "",
"prefix": config.PREFIX,
}
)
return deserializer.parse_transaction(
Expand All @@ -29,6 +30,7 @@ def deserialize_block(block_hex, use_txid, parse_vouts=False, block_index=None):
"network": config.NETWORK_NAME,
"db_dir": "",
"log_file": "",
"prefix": config.PREFIX,
}
)
return deserializer.parse_block(
Expand Down
89 changes: 8 additions & 81 deletions counterparty-core/counterpartycore/lib/gettxinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,77 +411,6 @@ def get_dispensers_tx_info(sources, dispensers_outputs):
return source, destination, btc_amount, fee, data, outs


def parse_transaction_vouts(decoded_tx):
# Get destinations and data outputs.
destinations, btc_amount, fee, data, potential_dispensers = [], 0, 0, b"", []

for vout in decoded_tx["vout"]:
potential_dispensers.append((None, None))
# Fee is the input values minus output values.
output_value = vout["value"]
fee -= output_value

script_pub_key = vout["script_pub_key"]

# Ignore transactions with invalid script.
asm = script.script_to_asm(script_pub_key)
if asm[0] == OP_RETURN: # noqa: F405
new_destination, new_data = decode_opreturn(asm, decoded_tx)
elif asm[-1] == OP_CHECKSIG: # noqa: F405
new_destination, new_data = decode_checksig(asm, decoded_tx)
potential_dispensers[-1] = (new_destination, output_value)
elif asm[-1] == OP_CHECKMULTISIG: # noqa: F405
try:
new_destination, new_data = decode_checkmultisig(asm, decoded_tx)
potential_dispensers[-1] = (new_destination, output_value)
except script.MultiSigAddressError:
raise DecodeError("invalid OP_CHECKMULTISIG") # noqa: B904
elif (
util.enabled("p2sh_addresses")
and asm[0] == OP_HASH160 # noqa: F405
and asm[-1] == OP_EQUAL # noqa: F405
and len(asm) == 3
):
new_destination, new_data = decode_scripthash(asm)
if util.enabled("p2sh_dispensers_support"):
potential_dispensers[-1] = (new_destination, output_value)
elif util.enabled("segwit_support") and asm[0] == b"":
# Segwit Vout, second param is redeemScript
# redeemScript = asm[1]
new_destination = script.script_to_address(script_pub_key)
new_data = None
if util.enabled("correct_segwit_txids"):
potential_dispensers[-1] = (new_destination, output_value)
else:
raise DecodeError("unrecognised output type")
assert not (new_destination and new_data)
assert (
new_destination != None or new_data != None # noqa: E711
) # `decode_*()` should never return `None, None`.

if util.enabled("null_data_check"):
if new_data == []:
raise DecodeError("new destination is `None`")

# All destinations come before all data.
if (
not data
and not new_data
and destinations
!= [
config.UNSPENDABLE,
]
):
destinations.append(new_destination)
btc_amount += output_value
else:
if new_destination: # Change.
break
data += new_data # Data.

return destinations, btc_amount, fee, data, potential_dispensers


def get_tx_info_new(db, decoded_tx, block_index, p2sh_is_segwit=False, composing=False):
"""Get multisig transaction info.
The destinations, if they exists, always comes before the data output; the
Expand All @@ -492,16 +421,14 @@ def get_tx_info_new(db, decoded_tx, block_index, p2sh_is_segwit=False, composing
raise DecodeError("coinbase transaction")

# Get destinations and data outputs.
if "parsed_vouts" in decoded_tx and str(decoded_tx["parsed_vouts"]) != "Not Parsed":
if isinstance(decoded_tx["parsed_vouts"], Exception):
raise DecodeError(str(decoded_tx["parsed_vouts"]))
elif decoded_tx["parsed_vouts"] == "DecodeError":
raise DecodeError("unrecognised output type")
destinations, btc_amount, fee, data, potential_dispensers = decoded_tx["parsed_vouts"]
else:
destinations, btc_amount, fee, data, potential_dispensers = parse_transaction_vouts(
decoded_tx
)
if "parsed_vouts" not in decoded_tx:
raise DecodeError("no parsed_vouts in decoded_tx")

if isinstance(decoded_tx["parsed_vouts"], Exception):

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 DecodeError(str(decoded_tx["parsed_vouts"]))
elif decoded_tx["parsed_vouts"] == "DecodeError":
raise DecodeError("unrecognised output type")
destinations, btc_amount, fee, data, potential_dispensers = decoded_tx["parsed_vouts"]

# source can be determined by parsing the p2sh_data transaction
# or from the first spent output
Expand Down
Loading

0 comments on commit 76940ba

Please sign in to comment.