From 10387e186926fb14ec4909d072000e0fe156e58c Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Mon, 27 Jul 2020 17:50:03 +0200 Subject: [PATCH] core: drop zcash v3 tx signing support --- core/src/apps/bitcoin/sign_tx/helpers.py | 7 +- core/src/apps/bitcoin/sign_tx/zcash.py | 65 +++---- core/tests/test_apps.bitcoin.zcash.zip143.py | 184 ------------------- core/tests/test_apps.bitcoin.zcash.zip243.py | 11 +- tests/device_tests/test_msg_signtx_zcash.py | 43 ++--- tests/ui_tests/fixtures.json | 1 - 6 files changed, 43 insertions(+), 268 deletions(-) delete mode 100644 core/tests/test_apps.bitcoin.zcash.zip143.py diff --git a/core/src/apps/bitcoin/sign_tx/helpers.py b/core/src/apps/bitcoin/sign_tx/helpers.py index e0bd628663..2d8e593d02 100644 --- a/core/src/apps/bitcoin/sign_tx/helpers.py +++ b/core/src/apps/bitcoin/sign_tx/helpers.py @@ -186,8 +186,11 @@ def sanitize_sign_tx(tx: SignTx, coin: CoinInfo) -> SignTx: raise wire.DataError("Timestamp must be set.") elif not coin.timestamp and tx.timestamp: raise wire.DataError("Timestamp not enabled on this coin.") - if coin.overwintered and tx.version_group_id is None: - raise wire.DataError("Version group ID must be set.") + if coin.overwintered: + if tx.version_group_id is None: + raise wire.DataError("Version group ID must be set.") + if tx.branch_id is None: + raise wire.DataError("Branch ID must be set.") elif not coin.overwintered: if tx.version_group_id is not None: raise wire.DataError("Version group ID not enabled on this coin.") diff --git a/core/src/apps/bitcoin/sign_tx/zcash.py b/core/src/apps/bitcoin/sign_tx/zcash.py index 1b95acb2a9..6e21cd8fca 100644 --- a/core/src/apps/bitcoin/sign_tx/zcash.py +++ b/core/src/apps/bitcoin/sign_tx/zcash.py @@ -39,27 +39,16 @@ class Overwintered(Bitcoinlike): ensure(coin.overwintered) super().__init__(tx, keychain, coin) - if self.tx.version == 3: - if not self.tx.branch_id: - self.tx.branch_id = 0x5BA81B19 # Overwinter - elif self.tx.version == 4: - if not self.tx.branch_id: - self.tx.branch_id = 0x76B809BB # Sapling - else: - raise wire.DataError("Unsupported version for overwintered transaction") + if self.tx.version != 4: + raise wire.DataError("Unsupported transaction version.") async def step8_finish(self) -> None: self.write_tx_footer(self.serialized_tx, self.tx) - if self.tx.version == 3: - write_bitcoin_varint(self.serialized_tx, 0) # nJoinSplit - elif self.tx.version == 4: - write_uint64(self.serialized_tx, 0) # valueBalance - write_bitcoin_varint(self.serialized_tx, 0) # nShieldedSpend - write_bitcoin_varint(self.serialized_tx, 0) # nShieldedOutput - write_bitcoin_varint(self.serialized_tx, 0) # nJoinSplit - else: - raise wire.DataError("Unsupported version for overwintered transaction") + write_uint64(self.serialized_tx, 0) # valueBalance + write_bitcoin_varint(self.serialized_tx, 0) # nShieldedSpend + write_bitcoin_varint(self.serialized_tx, 0) # nShieldedOutput + write_bitcoin_varint(self.serialized_tx, 0) # nJoinSplit await helpers.request_tx_finish(self.tx_req) @@ -121,33 +110,21 @@ class Overwintered(Bitcoinlike): # 5. hashOutputs write_bytes_fixed(h_preimage, get_tx_hash(self.h_outputs), TX_HASH_SIZE) - if self.tx.version == 3: - # 6. hashJoinSplits - write_bytes_fixed(h_preimage, b"\x00" * TX_HASH_SIZE, TX_HASH_SIZE) - # 7. nLockTime - write_uint32(h_preimage, self.tx.lock_time) - # 8. expiryHeight - write_uint32(h_preimage, self.tx.expiry) - # 9. nHashType - write_uint32(h_preimage, self.get_sighash_type(txi)) - elif self.tx.version == 4: - zero_hash = b"\x00" * TX_HASH_SIZE - # 6. hashJoinSplits - write_bytes_fixed(h_preimage, zero_hash, TX_HASH_SIZE) - # 7. hashShieldedSpends - write_bytes_fixed(h_preimage, zero_hash, TX_HASH_SIZE) - # 8. hashShieldedOutputs - write_bytes_fixed(h_preimage, zero_hash, TX_HASH_SIZE) - # 9. nLockTime - write_uint32(h_preimage, self.tx.lock_time) - # 10. expiryHeight - write_uint32(h_preimage, self.tx.expiry) - # 11. valueBalance - write_uint64(h_preimage, 0) - # 12. nHashType - write_uint32(h_preimage, self.get_sighash_type(txi)) - else: - raise wire.DataError("Unsupported version for overwintered transaction") + zero_hash = b"\x00" * TX_HASH_SIZE + # 6. hashJoinSplits + write_bytes_fixed(h_preimage, zero_hash, TX_HASH_SIZE) + # 7. hashShieldedSpends + write_bytes_fixed(h_preimage, zero_hash, TX_HASH_SIZE) + # 8. hashShieldedOutputs + write_bytes_fixed(h_preimage, zero_hash, TX_HASH_SIZE) + # 9. nLockTime + write_uint32(h_preimage, self.tx.lock_time) + # 10. expiryHeight + write_uint32(h_preimage, self.tx.expiry) + # 11. valueBalance + write_uint64(h_preimage, 0) + # 12. nHashType + write_uint32(h_preimage, self.get_sighash_type(txi)) # 10a /13a. outpoint write_bytes_reversed(h_preimage, txi.prev_hash, TX_HASH_SIZE) diff --git a/core/tests/test_apps.bitcoin.zcash.zip143.py b/core/tests/test_apps.bitcoin.zcash.zip143.py deleted file mode 100644 index 856ec27daf..0000000000 --- a/core/tests/test_apps.bitcoin.zcash.zip143.py +++ /dev/null @@ -1,184 +0,0 @@ -from common import * -from trezor.messages import InputScriptType -from trezor.messages.SignTx import SignTx -from trezor.messages.TxInputType import TxInputType -from trezor.messages.TxOutputBinType import TxOutputBinType - -from apps.common import coins -from apps.bitcoin.writers import get_tx_hash - -if not utils.BITCOIN_ONLY: - from apps.bitcoin.sign_tx.zcash import Overwintered - - -# test vectors inspired from https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/zip_0143.py -@unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin") -class TestZcashZip143(unittest.TestCase): - - VECTORS = [ - { - "expiry": 71895707, - "inputs": [ - { - "amount": 35268204, - "prevout": [ - "702c35a67cd7364d3fab552fb349e35c15c50250453fd18f7b855992632e2c76", - 4025613248, - ], - "pubkey": "03c6d9cc725bb7e19c026df03bf693ee1171371a8eaf25f04b7a58f6befabcd38c", - "script_type": InputScriptType.SPENDADDRESS, - "sequence": 1999822371, - } - ], - "lock_time": 452079490, - "outputs": [ - {"script_pubkey": "06535251635252", "amount": 1246336469307855} - ], - "version": 3, - "version_group_id": 0x3C48270, - "hash_type": 1, - "prevouts_hash": b"bd4318eecf841a0cf01c2be532cf4bc3303e881e2aface159f1882f153152688", - "sequence_hash": b"9ac6a31952ff626bf5a0a30d3d8ac63a0d4298d33d7bc38854bfa5860695e30a", - "outputs_hash": b"d0cadf116b4441f5e1e17814908dee509ec262a79f3c88f7f3389e8200658992", - "preimage_hash": b"fed855ea5fcec81928fa35d39b8582c6e026a0bf52cebeed4445a7fc7d730280", - }, - { - "expiry": 231041495, - "inputs": [ - { - "amount": 39263472, - "prevout": [ - "76647d2be4c2cd6b3d17d6870971d7a098baf72c6f6f1214cf1faae488bd7de2", - 1547817817, - ], - "pubkey": "03c6d9cc725bb7e19c026df03bf693ee1171371a8eaf25f04b7a58f6befabcd38c", - "script_type": InputScriptType.SPENDADDRESS, - "sequence": 3973122135, - }, - { - "amount": 57533728, - "prevout": [ - "cccc0df65a04943ad5cbc13f295f000fe056c40b2d88f27dc34cfeb803be3483", - 3053054889, - ], - "pubkey": "02c651a011009e2c7e7b3ed2068857ca0a47cba35b73e06c32e3c06ef3aa67621d", - "script_type": InputScriptType.SPENDADDRESS, - "sequence": 3932380530, - }, - ], - "lock_time": 3087412294, - "outputs": [ - {"script_pubkey": "03ac6552", "amount": 546412698509744}, - {"script_pubkey": "00", "amount": 166856241017532}, - ], - "version": 3, - "version_group_id": 0x3C48270, - "hash_type": 1, - "prevouts_hash": b"8e286c6c0dde3119271c9c1398ef46614b0253c502b00a3691cec2e9047da35b", - "sequence_hash": b"58477fd9ecd5faf3e08159e0ab5fdaab66cab364d081498ddcef41de0af3624e", - "outputs_hash": b"c518797fc6f2c08fc22aa3f66122047b360e1db4df5c3feb28573c00cdf45fa1", - "preimage_hash": b"1c6f563d2f16002f4c59bec5e7d56ed298315630c1d7e9a431b89e6f81026a02", - }, - { - "expiry": 186996458, - "inputs": [ - { - "amount": 14267260, - "prevout": [ - "6c6fae359f645c276891c0dcab3faf187700c082dc477740fb3f2cd7bb59fb35", - 1290359941, - ], - "script_type": InputScriptType.SPENDADDRESS, - "pubkey": "03c6d9cc725bb7e19c026df03bf693ee1171371a8eaf25f04b7a58f6befabcd38c", - "sequence": 1230917966, - } - ], - "lock_time": 1520002857, - "outputs": [], - "version": 3, - "version_group_id": 0x3C48270, - "hash_type": 1, - "prevouts_hash": b"445bc6328cd33b3c86259953dd674bded341ff1e1104dc21856919e9761036dd", - "sequence_hash": b"42e1d5c2636f165afaa954afa6d7a50779eb145e947bf668f1a40dd771c711fc", - "outputs_hash": b"869eda84eecf7257f9979a4848bbf52f4969a5736594ab7ba41452e7bb906824", - "preimage_hash": b"7159247daa16cc7e683f03ebf968314ce03324028ac138468a7b76c77e551fe8", - }, - { - "expiry": 254788522, - "inputs": [ - { - "amount": 36100600, - "prevout": [ - "e818f9057c5abaaa2e5c15b94945cd424c28a5fa385dadfe4907b274d842707d", - 1517971891, - ], - "script_type": InputScriptType.SPENDADDRESS, - "pubkey": "03c6d9cc725bb7e19c026df03bf693ee1171371a8eaf25f04b7a58f6befabcd38c", - "sequence": 3833577708, - }, - { - "amount": 71238918, - "prevout": [ - "7350d1014670212efe81fb7c73e8450df814ef6232f7490f63ccf07480f884a6", - 687648622, - ], - "script_type": InputScriptType.SPENDADDRESS, - "pubkey": "02c651a011009e2c7e7b3ed2068857ca0a47cba35b73e06c32e3c06ef3aa67621d", - "sequence": 4190617831, - }, - ], - "lock_time": 1557067344, - "outputs": [ - {"script_pubkey": "076a53655151516a", "amount": 470086065540185} - ], - "version": 3, - "version_group_id": 0x3C48270, - "hash_type": 1, - "prevouts_hash": b"509abdfafcc75265037f1ce6a4658ac9ecadd7b82378c3fbaeb48ab437ff6898", - "sequence_hash": b"2b13f671cd1a9aa04c1e250eef74a316d7d2b049360d20604514ddc2dfacfd23", - "outputs_hash": b"4f01b8785e80779290aa86c16b24952f9b7f8bc09da44e68f760ab1920ab8f2a", - "preimage_hash": b"16b24c5d599107efb41cbc6cb0127094878bab1e0d33d734cfccce58e07b3386", - }, - ] - - def test_zip143(self): - coin = coins.by_name("Zcash") - - for v in self.VECTORS: - tx = SignTx( - coin_name="Zcash", - inputs_count=len(v["inputs"]), - outputs_count=len(v["outputs"]), - version=v["version"], - lock_time=v["lock_time"], - expiry=v["expiry"], - version_group_id=v["version_group_id"], - ) - - zip143 = Overwintered(tx, None, coin) - - for i in v["inputs"]: - txi = TxInputType() - txi.amount = i["amount"] - txi.prev_hash = unhexlify(i["prevout"][0]) - txi.prev_index = i["prevout"][1] - txi.script_type = i["script_type"] - txi.sequence = i["sequence"] - zip143.hash143_add_input(txi) - for o in v["outputs"]: - txo = TxOutputBinType() - txo.amount = o["amount"] - txo.script_pubkey = unhexlify(o["script_pubkey"]) - zip143.hash143_add_output(txo, txo.script_pubkey) - - 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["pubkey"])], 1)), - v["preimage_hash"], - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/core/tests/test_apps.bitcoin.zcash.zip243.py b/core/tests/test_apps.bitcoin.zcash.zip243.py index 4e6a7d964d..83c44c6c12 100644 --- a/core/tests/test_apps.bitcoin.zcash.zip243.py +++ b/core/tests/test_apps.bitcoin.zcash.zip243.py @@ -36,7 +36,7 @@ class TestZcashZip243(unittest.TestCase): ], "version": 4, "version_group_id": 0x892F2085, - "hash_type": 1, + "branch_id": 0x76B809BB, "prevouts_hash": b"bd4318eecf841a0cf01c2be532cf4bc3303e881e2aface159f1882f153152688", "sequence_hash": b"9ac6a31952ff626bf5a0a30d3d8ac63a0d4298d33d7bc38854bfa5860695e30a", "outputs_hash": b"d0cadf116b4441f5e1e17814908dee509ec262a79f3c88f7f3389e8200658992", @@ -73,7 +73,7 @@ class TestZcashZip243(unittest.TestCase): ], "version": 4, "version_group_id": 0x892F2085, - "hash_type": 1, + "branch_id": 0x76B809BB, "prevouts_hash": b"8e286c6c0dde3119271c9c1398ef46614b0253c502b00a3691cec2e9047da35b", "sequence_hash": b"58477fd9ecd5faf3e08159e0ab5fdaab66cab364d081498ddcef41de0af3624e", "outputs_hash": b"c518797fc6f2c08fc22aa3f66122047b360e1db4df5c3feb28573c00cdf45fa1", @@ -97,7 +97,7 @@ class TestZcashZip243(unittest.TestCase): "outputs": [], "version": 4, "version_group_id": 0x892F2085, - "hash_type": 1, + "branch_id": 0x76B809BB, "prevouts_hash": b"445bc6328cd33b3c86259953dd674bded341ff1e1104dc21856919e9761036dd", "sequence_hash": b"42e1d5c2636f165afaa954afa6d7a50779eb145e947bf668f1a40dd771c711fc", "outputs_hash": b"869eda84eecf7257f9979a4848bbf52f4969a5736594ab7ba41452e7bb906824", @@ -133,7 +133,7 @@ class TestZcashZip243(unittest.TestCase): ], "version": 4, "version_group_id": 0x892F2085, - "hash_type": 1, + "branch_id": 0x76B809BB, "prevouts_hash": b"509abdfafcc75265037f1ce6a4658ac9ecadd7b82378c3fbaeb48ab437ff6898", "sequence_hash": b"2b13f671cd1a9aa04c1e250eef74a316d7d2b049360d20604514ddc2dfacfd23", "outputs_hash": b"4f01b8785e80779290aa86c16b24952f9b7f8bc09da44e68f760ab1920ab8f2a", @@ -167,7 +167,7 @@ class TestZcashZip243(unittest.TestCase): ], "version": 4, "version_group_id": 0x892f2085, - "hash_type": 1, + "branch_id": 0x76B809BB, "prevouts_hash": b"fae31b8dec7b0b77e2c8d6b6eb0e7e4e55abc6574c26dd44464d9408a8e33f11", "sequence_hash": b"6c80d37f12d89b6f17ff198723e7db1247c4811d1a695d74d930f99e98418790", "outputs_hash": b"d2b04118469b7810a0d1cc59568320aad25a84f407ecac40b4f605a4e6868454", @@ -187,6 +187,7 @@ class TestZcashZip243(unittest.TestCase): lock_time=v["lock_time"], expiry=v["expiry"], version_group_id=v["version_group_id"], + branch_id=v["branch_id"], ) zip243 = Overwintered(tx, None, coin) diff --git a/tests/device_tests/test_msg_signtx_zcash.py b/tests/device_tests/test_msg_signtx_zcash.py index 093fe7ccf2..7621f971d5 100644 --- a/tests/device_tests/test_msg_signtx_zcash.py +++ b/tests/device_tests/test_msg_signtx_zcash.py @@ -56,7 +56,8 @@ TXHASH_v4 = bytes.fromhex( @pytest.mark.altcoin @pytest.mark.zcash class TestMsgSigntxZcash: - def test_one_one_fee_overwinter(self, client): + @pytest.mark.skip_ui + def test_v3_not_supported(self, client): # prevout: aaf51e4606c264e47e5c42c958fe4cf1539c5172684721e38e69f4ef634d75dc:1 # input 1: 3.0 TAZ @@ -75,40 +76,18 @@ class TestMsgSigntxZcash: ) with client: - client.set_expected_responses( - [ - request_input(0), - request_meta(TXHASH_aaf51e), - request_input(0, TXHASH_aaf51e), - request_output(0, TXHASH_aaf51e), - request_output(1, TXHASH_aaf51e), - request_extra_data(0, 1, TXHASH_aaf51e), - request_output(0), - proto.ButtonRequest(code=B.ConfirmOutput), - proto.ButtonRequest(code=B.SignTx), - request_input(0), - request_output(0), - request_finished(), - ] - ) - details = proto.SignTx( version=3, version_group_id=0x03C48270, branch_id=0x5BA81B19, ) - _, serialized_tx = btc.sign_tx( - client, - "Zcash Testnet", - [inp1], - [out1], - details=details, - prev_txes=TX_API, - ) - - # Accepted by network: tx eda9b772c47f0c29310759960e0081c98707aa67a0a2738bcc71439fcf360675 - assert ( - serialized_tx.hex() - == "030000807082c40301dc754d63eff4698ee321476872519c53f14cfe58c9425c7ee464c206461ef5aa010000006a47304402207e45f303b4e42be824513855eb21653e1d2749cd94dcd0f0613d3f85d4efd1e20220699ffbdbcad889af7ede5ce9febf7a5ef8f5619b2464824529974c400cffaebc0121030e669acac1f280d1ddf441cd2ba5e97417bf2689e4bbec86df4f831bf9f7ffd0ffffffff016c9be111000000001976a9145b157a678a10021243307e4bb58f36375aa80e1088ac000000000000000000" - ) + with pytest.raises(TrezorFailure, match="DataError"): + _, serialized_tx = btc.sign_tx( + client, + "Zcash Testnet", + [inp1], + [out1], + details=details, + prev_txes=TX_API, + ) def test_one_one_fee_sapling(self, client): # prevout: e3820602226974b1dd87b7113cc8aea8c63e5ae29293991e7bfa80c126930368:0 diff --git a/tests/ui_tests/fixtures.json b/tests/ui_tests/fixtures.json index cf395aa857..a6adb67c74 100644 --- a/tests/ui_tests/fixtures.json +++ b/tests/ui_tests/fixtures.json @@ -335,7 +335,6 @@ "test_msg_signtx_segwit_native.py-test_send_p2sh": "0c15ccc36f6b081cab2ebb1632310cd7e02c0b4650c1d2d607cbefc252c1b9ef", "test_msg_signtx_segwit_native.py-test_send_p2sh_change": "2a35826b696d0fcf779bc2670a588279c3beb8770c0f79a7942c22eaf6594cc2", "test_msg_signtx_zcash.py-test_external_presigned": "539b10ad2f3a626a880b9823897ff827a6c30af1dfd041da147bf009fcec2497", -"test_msg_signtx_zcash.py-test_one_one_fee_overwinter": "112df2ad84f4a517623eed893b3be809e0ba43e6139fd70b7f94c12f28cc633c", "test_msg_signtx_zcash.py-test_one_one_fee_sapling": "112df2ad84f4a517623eed893b3be809e0ba43e6139fd70b7f94c12f28cc633c", "test_msg_signtx_zcash.py-test_spend_old_versions": "da64743acaead691ec617cd56886b65453f82a2e3eb78b3f9dc737dbfc4155e6", "test_msg_stellar_get_address.py-test_stellar_get_address": "5a80508a71a9ef64f94762b07636f90e464832f0f4a3102af8fa1a8c69e94586",