From ca972cd72d8998c24bcc77682c379a1fdd70c0c5 Mon Sep 17 00:00:00 2001 From: Kristaps Kaupe Date: Tue, 28 Nov 2023 18:38:56 +0200 Subject: [PATCH] Get rid of tx_total_outputs_value() --- src/jmbitcoin/secp256k1_transaction.py | 9 --------- src/jmclient/maker.py | 9 ++++----- test/jmbitcoin/test_tx_signing.py | 1 - 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/jmbitcoin/secp256k1_transaction.py b/src/jmbitcoin/secp256k1_transaction.py index 05d26ee75..4be9fe66e 100644 --- a/src/jmbitcoin/secp256k1_transaction.py +++ b/src/jmbitcoin/secp256k1_transaction.py @@ -184,15 +184,6 @@ def tx_vsize(tx): non_witness_size = raw_tx_size - witness_size return ceil(non_witness_size + .25 * witness_size) -def tx_total_outputs_value(tx: CTransaction) -> int: - """ - Returns sum of all transaction outputs value in satoshis. - """ - tx_out_sum = 0 - for txout in tx.vout: - tx_out_sum = tx_out_sum + txout.nValue - return tx_out_sum - def pubkey_to_p2pkh_script(pub, require_compressed=False): """ Given a pubkey in bytes, return a CScript diff --git a/src/jmclient/maker.py b/src/jmclient/maker.py index a6586a3a7..7e984417d 100644 --- a/src/jmclient/maker.py +++ b/src/jmclient/maker.py @@ -179,7 +179,7 @@ def on_tx_received(self, nick, tx, offerinfo): sigs.append(base64.b64encode(sigmsg).decode('ascii')) return (True, sigs) - def verify_unsigned_tx(self, tx, offerinfo): + def verify_unsigned_tx(self, tx: btc.CTransaction, offerinfo): """This code is security-critical. Before signing the transaction the Maker must ensure that all details are as expected, and most importantly @@ -191,10 +191,9 @@ def verify_unsigned_tx(self, tx, offerinfo): if self.minimum_tx_fee_rate > 1000: tx_inp_data = jm_single().bc_interface.query_utxo_set(tx_utxo_set) - total_input_value_sum = 0 - for utxo in tx_inp_data: - total_input_value_sum = total_input_value_sum + utxo["value"] - tx_fee = total_input_value_sum - btc.tx_total_outputs_value(tx) + total_input_value_sum = sum([x["value"] for x in tx_inp_data]) + total_output_value_sum = sum([x.nValue for x in tx.vout]) + tx_fee = total_input_value_sum - total_output_value_sum tx_fee_rate = tx_fee / btc.tx_vsize(tx) * 1000 if tx_fee_rate < self.minimum_tx_fee_rate: return (False, "tx feerate below configured minimum tx feerate") diff --git a/test/jmbitcoin/test_tx_signing.py b/test/jmbitcoin/test_tx_signing.py index 36c6f7bee..ebe73eff5 100644 --- a/test/jmbitcoin/test_tx_signing.py +++ b/test/jmbitcoin/test_tx_signing.py @@ -85,7 +85,6 @@ def test_sign_standard_txs(addrtype): txin = btc.CTxIn(btc.COutPoint(txid[::-1], vout)) txout = btc.CTxOut(amount_less_fee, target_scriptPubKey) tx = btc.CMutableTransaction([txin], [txout]) - assert btc.tx_total_outputs_value(tx) == amount_less_fee # Calculate the signature hash for the transaction. This is then signed by the # private key that controls the UTXO being spent here at this txin_index.