Skip to content

Commit 8ecc820

Browse files
committed
cleaned code as instructed in TODO blocks
1 parent 329caf4 commit 8ecc820

1 file changed

Lines changed: 19 additions & 29 deletions

File tree

bitcoinutils/transactions.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -652,13 +652,12 @@ def get_transaction_digest(
652652
for txin in tmp_tx.inputs:
653653
txin.script_sig = Script([])
654654

655-
#
656-
# TODO Deal with (delete?) script's OP_CODESEPARATORs, if any
657-
# Very early versions of Bitcoin were using a different design for
658-
# scripts that were flawed. OP_CODESEPARATOR has no purpose currently
659-
# but we could not delete it for compatibility purposes. If it exists
660-
# in a script it needs to be removed.
661-
#
655+
# If there are OP_CODESEPARATORs in the script, we need to remove them
656+
# for the signature hash calculation
657+
if "OP_CODESEPARATOR" in script.script:
658+
# Create a new script without OP_CODESEPARATORs
659+
filtered_script_elements = [element for element in script.script if element != "OP_CODESEPARATOR"]
660+
script = Script(filtered_script_elements)
662661

663662
# the temporary transaction's scriptSig needs to be set to the
664663
# scriptPubKey of the UTXO we are trying to spend - this is required to
@@ -844,7 +843,6 @@ def get_transaction_segwit_digest(
844843
return hashlib.sha256(hashlib.sha256(tx_for_signing).digest()).digest()
845844

846845
# TODO Update doc with TAPROOT_SIGHASH_ALL
847-
# clean prints after finishing other sighashes
848846
def get_transaction_taproot_digest(
849847
self,
850848
txin_index: int,
@@ -861,11 +859,11 @@ def get_transaction_taproot_digest(
861859
And: https://github.com/bitcoin/bitcoin/blob/b5f33ac1f82aea290b4653af36ac2ad1bf1cce7b/test/functional/test_framework/script.py
862860
863861
| SIGHASH types (see constants.py):
864-
| TAPROOT_SIGHASH_ALL - signs all inputs and outputs (default)
865-
| SIGHASH_ALL - signs all inputs and outputs
866-
| SIGHASH_NONE - signs all of the inputs
867-
| SIGHASH_SINGLE - signs all inputs but only txin_index output
868-
| SIGHASH_ANYONECANPAY (only combined with one of the above)
862+
| TAPROOT_SIGHASH_ALL (0x00) - signs all inputs and outputs (default)
863+
| SIGHASH_ALL (0x01) - signs all inputs and outputs
864+
| SIGHASH_NONE (0x02) - signs all of the inputs
865+
| SIGHASH_SINGLE (0x03) - signs all inputs but only txin_index output
866+
| SIGHASH_ANYONECANPAY (0x80) (only combined with one of the above)
869867
| - with ALL - signs all outputs but only txin_index input
870868
| - with NONE - signs only the txin_index input
871869
| - with SINGLE - signs txin_index input and output
@@ -888,10 +886,6 @@ def get_transaction_taproot_digest(
888886
The type of the signature hash to be created
889887
"""
890888

891-
# clone transaction to modify without messing up the real transaction
892-
# tmp_tx is not really used for its to_bytes() here
893-
# TODO we could use self directly to access fields
894-
tmp_tx = Transaction.copy(self)
895889

896890
# acquiring the signature type
897891
# sign_all = sig_hash & 0x03 == SIGHASH_ALL
@@ -920,9 +914,8 @@ def get_transaction_taproot_digest(
920914

921915
# Data about the transaction
922916
if not anyone_can_pay:
923-
# print('1')
924917
# the SHA256 of the serialization of all input outpoints
925-
for txin in tmp_tx.inputs:
918+
for txin in self.inputs:
926919
hash_prevouts += h_to_b(txin.txid)[::-1] + struct.pack(
927920
"<I",
928921
txin.txout_index,
@@ -945,14 +938,13 @@ def get_transaction_taproot_digest(
945938
tx_for_signing += hash_script_pubkeys
946939

947940
# the SHA256 of the serialization of all input nSequence
948-
for txin in tmp_tx.inputs:
941+
for txin in self.inputs:
949942
hash_sequences += txin.sequence
950943
hash_sequences = hashlib.sha256(hash_sequences).digest()
951944
tx_for_signing += hash_sequences
952945

953946
if not (sighash_none or sighash_single):
954-
# print('2')
955-
for txout in tmp_tx.outputs:
947+
for txout in self.outputs:
956948
amount_bytes = struct.pack("<Q", txout.amount)
957949
script_bytes = txout.script_pubkey.to_bytes()
958950
hash_outputs += (
@@ -967,8 +959,7 @@ def get_transaction_taproot_digest(
967959
tx_for_signing += bytes([spend_type])
968960

969961
if anyone_can_pay:
970-
# print('3')
971-
txin = tmp_tx.inputs[txin_index]
962+
txin = self.inputs[txin_index]
972963
# convert txid to big-endian first
973964
tx_for_signing += h_to_b(txin.txid)[::-1] + struct.pack(
974965
"<I",
@@ -983,16 +974,15 @@ def get_transaction_taproot_digest(
983974

984975
tx_for_signing += txin.sequence
985976
else:
986-
# print('4')
987977
tx_for_signing += txin_index.to_bytes(4, "little")
988978

989-
# TODO if annex is present it should be added here
990-
# length of annex should use compact_size
979+
if hasattr(self, 'annex') and self.annex is not None:
980+
annex_data = self.annex[txin_index] if isinstance(self.annex, list) else self.annex
981+
tx_for_signing += prepend_compact_size(h_to_b(annex_data))
991982

992983
# Data about this output
993984
if sighash_single:
994-
# print('5')
995-
txout = tmp_tx.outputs[txin_index]
985+
txout = self.outputs[txin_index]
996986
amount_bytes = struct.pack("<Q", txout.amount)
997987
script_bytes = txout.script_pubkey.to_bytes()
998988
hash_output = (

0 commit comments

Comments
 (0)