From 8b89a309559fc948179ed0b0c39caf317c97d47a Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Mon, 27 Apr 2020 15:00:22 +0200 Subject: [PATCH] core/sign_tx: Remove get_prevouts_hash(), get_sequence_hash(), get_outputs_hash() methods from signer classes, because they are only used internally. --- core/src/apps/wallet/sign_tx/bitcoin.py | 34 ++++++++----------- core/src/apps/wallet/sign_tx/zcash.py | 23 ++++--------- ...apps.wallet.segwit.bip143.native_p2wpkh.py | 11 +++--- ...pps.wallet.segwit.bip143.p2wpkh_in_p2sh.py | 11 +++--- core/tests/test_apps.wallet.zcash.zip143.py | 7 ++-- core/tests/test_apps.wallet.zcash.zip243.py | 10 +++--- 6 files changed, 45 insertions(+), 51 deletions(-) diff --git a/core/src/apps/wallet/sign_tx/bitcoin.py b/core/src/apps/wallet/sign_tx/bitcoin.py index 88703a287a..6b355044fa 100644 --- a/core/src/apps/wallet/sign_tx/bitcoin.py +++ b/core/src/apps/wallet/sign_tx/bitcoin.py @@ -114,11 +114,6 @@ class Bitcoin: # BIP-0143 transaction hashing self.init_hash143() - def init_hash143(self) -> None: - self.h_prevouts = HashWriter(sha256()) - self.h_sequence = HashWriter(sha256()) - self.h_outputs = HashWriter(sha256()) - def create_hash_writer(self) -> HashWriter: return HashWriter(sha256()) @@ -512,6 +507,11 @@ class Bitcoin: # BIP-0143 # === + def init_hash143(self) -> None: + self.h_prevouts = HashWriter(sha256()) + self.h_sequence = HashWriter(sha256()) + self.h_outputs = HashWriter(sha256()) + def hash143_add_input(self, txi: TxInputType) -> None: writers.write_bytes_reversed( self.h_prevouts, txi.prev_hash, writers.TX_HASH_SIZE @@ -522,15 +522,6 @@ class Bitcoin: def hash143_add_output(self, txo_bin: TxOutputBinType) -> None: writers.write_tx_output(self.h_outputs, txo_bin) - def get_prevouts_hash(self) -> bytes: - return writers.get_tx_hash(self.h_prevouts, double=self.coin.sign_hash_double) - - def get_sequence_hash(self) -> bytes: - return writers.get_tx_hash(self.h_sequence, double=self.coin.sign_hash_double) - - def get_outputs_hash(self) -> bytes: - return writers.get_tx_hash(self.h_outputs, double=self.coin.sign_hash_double) - def hash143_preimage_hash(self, txi: TxInputType, pubkeyhash: bytes) -> bytes: h_preimage = HashWriter(sha256()) @@ -538,14 +529,16 @@ class Bitcoin: writers.write_uint32(h_preimage, self.tx.version) # hashPrevouts - writers.write_bytes_fixed( - h_preimage, self.get_prevouts_hash(), writers.TX_HASH_SIZE + prevouts_hash = writers.get_tx_hash( + self.h_prevouts, double=self.coin.sign_hash_double ) + writers.write_bytes_fixed(h_preimage, prevouts_hash, writers.TX_HASH_SIZE) # hashSequence - writers.write_bytes_fixed( - h_preimage, self.get_sequence_hash(), writers.TX_HASH_SIZE + sequence_hash = writers.get_tx_hash( + self.h_sequence, double=self.coin.sign_hash_double ) + writers.write_bytes_fixed(h_preimage, sequence_hash, writers.TX_HASH_SIZE) # outpoint writers.write_bytes_reversed(h_preimage, txi.prev_hash, writers.TX_HASH_SIZE) @@ -562,9 +555,10 @@ class Bitcoin: writers.write_uint32(h_preimage, txi.sequence) # hashOutputs - writers.write_bytes_fixed( - h_preimage, self.get_outputs_hash(), writers.TX_HASH_SIZE + outputs_hash = writers.get_tx_hash( + self.h_outputs, double=self.coin.sign_hash_double ) + writers.write_bytes_fixed(h_preimage, outputs_hash, writers.TX_HASH_SIZE) # nLockTime writers.write_uint32(h_preimage, self.tx.lock_time) diff --git a/core/src/apps/wallet/sign_tx/zcash.py b/core/src/apps/wallet/sign_tx/zcash.py index 2355bc1514..2353d1ef0d 100644 --- a/core/src/apps/wallet/sign_tx/zcash.py +++ b/core/src/apps/wallet/sign_tx/zcash.py @@ -50,11 +50,6 @@ class Overwintered(Bitcoinlike): "Unsupported version for overwintered transaction", ) - def init_hash143(self) -> None: - self.h_prevouts = HashWriter(blake2b(outlen=32, personal=b"ZcashPrevoutHash")) - self.h_sequence = HashWriter(blake2b(outlen=32, personal=b"ZcashSequencHash")) - self.h_outputs = HashWriter(blake2b(outlen=32, personal=b"ZcashOutputsHash")) - async def step7_finish(self) -> None: self.write_tx_footer(self.serialized_tx, self.tx) @@ -91,14 +86,10 @@ class Overwintered(Bitcoinlike): # ZIP-0143 / ZIP-0243 # === - def get_prevouts_hash(self) -> bytes: - return get_tx_hash(self.h_prevouts) - - def get_sequence_hash(self) -> bytes: - return get_tx_hash(self.h_sequence) - - def get_outputs_hash(self) -> bytes: - return get_tx_hash(self.h_outputs) + def init_hash143(self) -> None: + self.h_prevouts = HashWriter(blake2b(outlen=32, personal=b"ZcashPrevoutHash")) + self.h_sequence = HashWriter(blake2b(outlen=32, personal=b"ZcashSequencHash")) + self.h_outputs = HashWriter(blake2b(outlen=32, personal=b"ZcashOutputsHash")) def hash143_preimage_hash(self, txi: TxInputType, pubkeyhash: bytes) -> bytes: h_preimage = HashWriter( @@ -113,11 +104,11 @@ class Overwintered(Bitcoinlike): # 2. nVersionGroupId write_uint32(h_preimage, self.tx.version_group_id) # 3. hashPrevouts - write_bytes_fixed(h_preimage, self.get_prevouts_hash(), TX_HASH_SIZE) + write_bytes_fixed(h_preimage, get_tx_hash(self.h_prevouts), TX_HASH_SIZE) # 4. hashSequence - write_bytes_fixed(h_preimage, self.get_sequence_hash(), TX_HASH_SIZE) + write_bytes_fixed(h_preimage, get_tx_hash(self.h_sequence), TX_HASH_SIZE) # 5. hashOutputs - write_bytes_fixed(h_preimage, self.get_outputs_hash(), TX_HASH_SIZE) + write_bytes_fixed(h_preimage, get_tx_hash(self.h_outputs), TX_HASH_SIZE) if self.tx.version == 3: # 6. hashJoinSplits diff --git a/core/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py b/core/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py index e08469ccd0..1fec84e321 100644 --- a/core/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py +++ b/core/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py @@ -2,6 +2,7 @@ from common import * from apps.wallet.sign_tx.scripts import output_derive_script from apps.wallet.sign_tx.bitcoin import Bitcoin +from apps.wallet.sign_tx.writers import get_tx_hash from apps.common import coins from trezor.messages.SignTx import SignTx from trezor.messages.TxInputType import TxInputType @@ -48,14 +49,16 @@ class TestSegwitBip143NativeP2WPKH(unittest.TestCase): bip143 = Bitcoin(self.tx, None, coin) bip143.hash143_add_input(self.inp1) bip143.hash143_add_input(self.inp2) - self.assertEqual(hexlify(bip143.get_prevouts_hash()), b'96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37') + prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double) + self.assertEqual(hexlify(prevouts_hash), b'96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37') def test_sequence(self): coin = coins.by_name(self.tx.coin_name) bip143 = Bitcoin(self.tx, None, coin) bip143.hash143_add_input(self.inp1) bip143.hash143_add_input(self.inp2) - self.assertEqual(hexlify(bip143.get_sequence_hash()), b'52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b') + sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double) + self.assertEqual(hexlify(sequence_hash), b'52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b') def test_outputs(self): @@ -69,8 +72,8 @@ class TestSegwitBip143NativeP2WPKH(unittest.TestCase): txo_bin.script_pubkey = output_derive_script(txo, coin) bip143.hash143_add_output(txo_bin) - self.assertEqual(hexlify(bip143.get_outputs_hash()), - b'863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5') + outputs_hash = get_tx_hash(bip143.h_outputs, double=coin.sign_hash_double) + self.assertEqual(hexlify(outputs_hash), b'863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5') def test_preimage_testdata(self): diff --git a/core/tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py b/core/tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py index ddb9fbaea3..cfc0f3b990 100644 --- a/core/tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py +++ b/core/tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py @@ -2,6 +2,7 @@ from common import * from apps.wallet.sign_tx.scripts import output_derive_script from apps.wallet.sign_tx.bitcoin import Bitcoin +from apps.wallet.sign_tx.writers import get_tx_hash from apps.common import coins from trezor.messages.SignTx import SignTx from trezor.messages.TxInputType import TxInputType @@ -39,13 +40,15 @@ class TestSegwitBip143(unittest.TestCase): coin = coins.by_name(self.tx.coin_name) bip143 = Bitcoin(self.tx, None, coin) bip143.hash143_add_input(self.inp1) - self.assertEqual(hexlify(bip143.get_prevouts_hash()), b'b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a') + prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double) + self.assertEqual(hexlify(prevouts_hash), b'b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a') def test_bip143_sequence(self): coin = coins.by_name(self.tx.coin_name) bip143 = Bitcoin(self.tx, None, coin) bip143.hash143_add_input(self.inp1) - self.assertEqual(hexlify(bip143.get_sequence_hash()), b'18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198') + sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double) + self.assertEqual(hexlify(sequence_hash), b'18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198') def test_bip143_outputs(self): seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') @@ -58,8 +61,8 @@ class TestSegwitBip143(unittest.TestCase): txo_bin.script_pubkey = output_derive_script(txo, coin) bip143.hash143_add_output(txo_bin) - self.assertEqual(hexlify(bip143.get_outputs_hash()), - b'de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83') + outputs_hash = get_tx_hash(bip143.h_outputs, double=coin.sign_hash_double) + self.assertEqual(hexlify(outputs_hash), b'de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83') def test_bip143_preimage_testdata(self): seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') diff --git a/core/tests/test_apps.wallet.zcash.zip143.py b/core/tests/test_apps.wallet.zcash.zip143.py index 7d39250d9b..c42b537089 100644 --- a/core/tests/test_apps.wallet.zcash.zip143.py +++ b/core/tests/test_apps.wallet.zcash.zip143.py @@ -5,6 +5,7 @@ from trezor.messages.TxInputType import TxInputType from trezor.messages.TxOutputBinType import TxOutputBinType from apps.common import coins +from apps.wallet.sign_tx.writers import get_tx_hash if not utils.BITCOIN_ONLY: from apps.wallet.sign_tx.zcash import Overwintered @@ -170,9 +171,9 @@ class TestZcashZip143(unittest.TestCase): txo.script_pubkey = unhexlify(o["script_pubkey"]) zip143.hash143_add_output(txo) - self.assertEqual(hexlify(zip143.get_prevouts_hash()), v["prevouts_hash"]) - self.assertEqual(hexlify(zip143.get_sequence_hash()), v["sequence_hash"]) - self.assertEqual(hexlify(zip143.get_outputs_hash()), v["outputs_hash"]) + self.assertEqual(hexlify(get_tx_hash(zip143.h_prevouts)), v["prevouts_hash"]) + self.assertEqual(hexlify(get_tx_hash(zip143.h_sequence)), v["sequence_hash"]) + self.assertEqual(hexlify(get_tx_hash(zip143.h_outputs)), v["outputs_hash"]) self.assertEqual( hexlify(zip143.hash143_preimage_hash(txi, unhexlify(i["pubkeyhash"]))), v["preimage_hash"], diff --git a/core/tests/test_apps.wallet.zcash.zip243.py b/core/tests/test_apps.wallet.zcash.zip243.py index ff1c68f2eb..77ddb5f0f9 100644 --- a/core/tests/test_apps.wallet.zcash.zip243.py +++ b/core/tests/test_apps.wallet.zcash.zip243.py @@ -5,8 +5,10 @@ from trezor.messages.TxInputType import TxInputType from trezor.messages.TxOutputBinType import TxOutputBinType from apps.common import coins +from apps.wallet.sign_tx.writers import get_tx_hash -from apps.wallet.sign_tx.zcash import Overwintered +if not utils.BITCOIN_ONLY: + from apps.wallet.sign_tx.zcash import Overwintered # test vectors inspired from https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/zip_0243.py @@ -203,9 +205,9 @@ class TestZcashZip243(unittest.TestCase): txo.script_pubkey = unhexlify(o["script_pubkey"]) zip243.hash143_add_output(txo) - self.assertEqual(hexlify(zip243.get_prevouts_hash()), v["prevouts_hash"]) - self.assertEqual(hexlify(zip243.get_sequence_hash()), v["sequence_hash"]) - self.assertEqual(hexlify(zip243.get_outputs_hash()), v["outputs_hash"]) + self.assertEqual(hexlify(get_tx_hash(zip243.h_prevouts)), v["prevouts_hash"]) + self.assertEqual(hexlify(get_tx_hash(zip243.h_sequence)), v["sequence_hash"]) + self.assertEqual(hexlify(get_tx_hash(zip243.h_outputs)), v["outputs_hash"]) self.assertEqual(hexlify(zip243.hash143_preimage_hash(txi, unhexlify(i["pubkeyhash"]))), v["preimage_hash"])