From 325dae890e9d58ef156d9726be9a32da8f8acaa6 Mon Sep 17 00:00:00 2001 From: matejcik Date: Wed, 4 Mar 2020 13:38:33 +0100 Subject: [PATCH] python: completely drop trezorlib.coins and trezorlib.tx_api --- python/CHANGELOG.md | 6 +- python/docs/OPTIONS.rst | 4 - python/src/trezorlib/cli/trezorctl.py | 8 +- python/src/trezorlib/coins.py | 39 -------- python/src/trezorlib/tools.py | 10 +- python/src/trezorlib/tx_api.py | 53 ---------- python/tests/test_tx_api.py | 103 -------------------- tests/device_tests/test_msg_signtx_zcash.py | 10 +- 8 files changed, 12 insertions(+), 221 deletions(-) delete mode 100644 python/src/trezorlib/coins.py delete mode 100644 python/src/trezorlib/tx_api.py delete mode 100644 python/tests/test_tx_api.py diff --git a/python/CHANGELOG.md b/python/CHANGELOG.md index 82ce983de..018a823cc 100644 --- a/python/CHANGELOG.md +++ b/python/CHANGELOG.md @@ -21,6 +21,8 @@ _At the moment, the project does **not** adhere to [Semantic Versioning](https:/ ### Changed +- `trezorlib.tx_api.json_to_tx` was reduced to only support Bitcoin fields, and moved + to `trezorlib.btc.from_json`. - API for `cosi` module was streamlined: `verify_m_of_n` is now `verify`, the old `verify` is `verify_combined` - internals of firmware parsing were reworked to support signing firmware headers @@ -34,8 +36,10 @@ _At the moment, the project does **not** adhere to [Semantic Versioning](https:/ ### Removed +- `trezorlib.tx_api` was removed +- `trezorlib.coins` and coin data was removed - `btc.sign_tx` will not preload transaction data from `prev_txes`, as usage with TxApi - is being phased out + is being removed - PIN protection and passphrase protection for `ping()` command was removed ## [0.11.6] - 2019-12-30 diff --git a/python/docs/OPTIONS.rst b/python/docs/OPTIONS.rst index 85f3a7dd8..145708c0b 100644 --- a/python/docs/OPTIONS.rst +++ b/python/docs/OPTIONS.rst @@ -14,10 +14,6 @@ The full list of subcommands can be seen like so: Each subcommand has its own help, shown with ``trezorctl --help``. -In addition, aliases are available for currency ticker symbols. For example, instead -of ``trezorctl btc sign-message``, it is possible to use ``trezorctl ltc sign-message``. -In that case, the ``-c`` option of ``sign-message`` can be omitted. - List of subcommands ------------------- diff --git a/python/src/trezorlib/cli/trezorctl.py b/python/src/trezorlib/cli/trezorctl.py index 1f0049a41..caa13ded5 100755 --- a/python/src/trezorlib/cli/trezorctl.py +++ b/python/src/trezorlib/cli/trezorctl.py @@ -23,7 +23,7 @@ import time import click -from .. import coins, log, messages, protobuf, ui +from .. import log, messages, protobuf, ui from ..client import TrezorClient from ..transport import enumerate_devices, get_transport from ..transport.udp import UdpTransport @@ -116,12 +116,6 @@ class TrezorctlGroup(click.Group): except Exception: pass - # try to find a bitcoin-like coin whose shortcut matches the command - for coin in coins.coins_list: - if cmd_name.lower() == coin["shortcut"].lower(): - btc.DEFAULT_COIN = coin["coin_name"] - return btc.cli - return None diff --git a/python/src/trezorlib/coins.py b/python/src/trezorlib/coins.py deleted file mode 100644 index ea84086e9..000000000 --- a/python/src/trezorlib/coins.py +++ /dev/null @@ -1,39 +0,0 @@ -# This file is part of the Trezor project. -# -# Copyright (C) 2012-2019 SatoshiLabs and contributors -# -# This library is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License version 3 -# as published by the Free Software Foundation. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the License along with this library. -# If not, see . - -import json -import os.path - -COINS_JSON = os.path.join(os.path.dirname(__file__), "coins.json") - - -def _load_coins_json(): - # Load coins.json to local variables - # NOTE: coins.json is generated via `make coins_json` command. - with open(COINS_JSON) as coins_json: - return json.load(coins_json) - - -# exported variables -__all__ = ["by_name", "slip44"] - -try: - coins_list = _load_coins_json() - by_name = {coin["coin_name"]: coin for coin in coins_list} -except Exception as e: - raise ImportError("Failed to load coins.json. Check your installation.") from e - -slip44 = {name: coin["slip44"] for name, coin in by_name.items()} diff --git a/python/src/trezorlib/tools.py b/python/src/trezorlib/tools.py index e36fe8667..96540b733 100644 --- a/python/src/trezorlib/tools.py +++ b/python/src/trezorlib/tools.py @@ -21,7 +21,6 @@ import struct import unicodedata from typing import List, NewType -from .coins import slip44 from .exceptions import TrezorFailure CallException = TrezorFailure @@ -168,11 +167,6 @@ def parse_path(nstr: str) -> Address: if n[0] == "m": n = n[1:] - # coin_name/a/b/c => 44'/SLIP44_constant'/a/b/c - if n[0] in slip44: - coin_id = slip44[n[0]] - n[0:1] = ["44h", "{}h".format(coin_id)] - def str_to_harden(x: str) -> int: if x.startswith("-"): return H_(abs(int(x))) @@ -183,8 +177,8 @@ def parse_path(nstr: str) -> Address: try: return [str_to_harden(x) for x in n] - except Exception: - raise ValueError("Invalid BIP32 path", nstr) + except Exception as e: + raise ValueError("Invalid BIP32 path", nstr) from e def normalize_nfc(txt): diff --git a/python/src/trezorlib/tx_api.py b/python/src/trezorlib/tx_api.py deleted file mode 100644 index b77c9334e..000000000 --- a/python/src/trezorlib/tx_api.py +++ /dev/null @@ -1,53 +0,0 @@ -# This file is part of the Trezor project. -# -# Copyright (C) 2012-2019 SatoshiLabs and contributors -# -# This library is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License version 3 -# as published by the Free Software Foundation. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the License along with this library. -# If not, see . - -from decimal import Decimal - - -from . import messages - - -def _json_to_input(vin): - i = messages.TxInputType() - if "coinbase" in vin: - i.prev_hash = b"\0" * 32 - i.prev_index = 0xFFFFFFFF # signed int -1 - i.script_sig = bytes.fromhex(vin["coinbase"]) - i.sequence = vin["sequence"] - - else: - i.prev_hash = bytes.fromhex(vin["txid"]) - i.prev_index = vin["vout"] - i.script_sig = bytes.fromhex(vin["scriptSig"]["hex"]) - i.sequence = vin["sequence"] - - return i - - -def _json_to_bin_output(vout): - o = messages.TxOutputBinType() - o.amount = int(Decimal(vout["value"]) * (10 ** 8)) - o.script_pubkey = bytes.fromhex(vout["scriptPubKey"]["hex"]) - return o - - -def json_to_tx(data): - t = messages.TransactionType() - t.version = data["version"] - t.lock_time = data.get("locktime") - t.inputs = [_json_to_input(vin) for vin in data["vin"]] - t.bin_outputs = [_json_to_bin_output(vout) for vout in data["vout"]] - return t diff --git a/python/tests/test_tx_api.py b/python/tests/test_tx_api.py deleted file mode 100644 index 4d9125688..000000000 --- a/python/tests/test_tx_api.py +++ /dev/null @@ -1,103 +0,0 @@ -# This file is part of the Trezor project. -# -# Copyright (C) 2012-2019 SatoshiLabs and contributors -# -# This library is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License version 3 -# as published by the Free Software Foundation. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the License along with this library. -# If not, see . - -import decimal -import json -import os - -from trezorlib import coins, tx_api - -CACHE_PATH = os.path.join(os.path.dirname(__file__), "../../tests/txcache") - -TxApiBitcoin = coins.tx_api["Bitcoin"] -TxApiZencash = coins.tx_api["Horizen"] - - -def load_tx_json(coin_name, txhash): - filename = "insight_{}_tx_{}.json".format(coin_name, txhash) - with open(os.path.join(CACHE_PATH, filename)) as f: - return json.load(f, parse_float=decimal.Decimal) - - -def test_tx_api_gettx(): - TxApiBitcoin.get_tx( - "39a29e954977662ab3879c66fb251ef753e0912223a83d1dcb009111d28265e5" - ) - TxApiBitcoin.get_tx( - "54aa5680dea781f45ebb536e53dffc526d68c0eb5c00547e323b2c32382dfba3" - ) - TxApiBitcoin.get_tx( - "58497a7757224d1ff1941488d23087071103e5bf855f4c1c44e5c8d9d82ca46e" - ) - TxApiBitcoin.get_tx( - "6189e3febb5a21cee8b725aa1ef04ffce7e609448446d3a8d6f483c634ef5315" - ) - TxApiBitcoin.get_tx( - "a6e2829d089cee47e481b1a753a53081b40738cc87e38f1d9b23ab57d9ad4396" - ) - TxApiBitcoin.get_tx( - "c6091adf4c0c23982a35899a6e58ae11e703eacd7954f588ed4b9cdefc4dba52" - ) - TxApiBitcoin.get_tx( - "c63e24ed820c5851b60c54613fbc4bcb37df6cd49b4c96143e99580a472f79fb" - ) - TxApiBitcoin.get_tx( - "c6be22d34946593bcad1d2b013e12f74159e69574ffea21581dad115572e031c" - ) - TxApiBitcoin.get_tx( - "d1d08ea63255af4ad16b098e9885a252632086fa6be53301521d05253ce8a73d" - ) - TxApiBitcoin.get_tx( - "d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882" - ) - TxApiBitcoin.get_tx( - "e4bc1ae5e5007a08f2b3926fe11c66612e8f73c6b00c69c7027213b84d259be3" - ) - - -def test_tx_api_current_block(): - height = TxApiZencash.current_height() - assert height > 347041 - - -def test_tx_api_get_block_hash(): - hash = TxApiZencash.get_block_hash(110000) - assert ( - hash.hex() == "000000003f5d6ba1385c6cd2d4f836dfc5adf7f98834309ad67e26faef462454" - ) - - -def test_tx_api_dash_dip2(): - dash_data = coins.by_name["Dash"] - - def get_tx(txhash): - data = load_tx_json("dash", txhash) - return tx_api.json_to_tx(dash_data, data) - - # Test if pre-DIP2 TXs are still working as expected - tx = get_tx("acb3b7f259429989fc9c51ae4a5e3e3eab0723dceb21577533ac7c4b4ba4db5d") - assert tx.version == 2 # pre-DIP2 - assert tx.extra_data is None and tx.extra_data_len is None - - # Test if version 3 TX with type=0 is treated as normal TX - tx = get_tx("5579eaa64b2a0233e7d8d037f5a5afc957cedf48f1c4067e9e33ca6df22ab04f") - assert tx.version == 3 - assert tx.extra_data is None and tx.extra_data_len is None - - # Test if DIP2 payloads are initialized correctly - tx = get_tx("15575a1c874bd60a819884e116c42e6791c8283ce1fc3b79f0d18531a61bbb8a") - assert tx.version == (3 | (5 << 16)) # DIP2 type 1 (ProRegTx) - assert len(tx.extra_data) == (38 + 1) # real length + varint size diff --git a/tests/device_tests/test_msg_signtx_zcash.py b/tests/device_tests/test_msg_signtx_zcash.py index 6c5e374df..216cd4522 100644 --- a/tests/device_tests/test_msg_signtx_zcash.py +++ b/tests/device_tests/test_msg_signtx_zcash.py @@ -39,9 +39,8 @@ class TestMsgSigntxZcash: # input 1: 3.0 TAZ inp1 = proto.TxInputType( - address_n=parse_path( - "m/Zcash Testnet/0h/0/0" - ), # tmQoJ3PTXgQLaRRZZYT6xk8XtjRbr2kCqwu + # tmQoJ3PTXgQLaRRZZYT6xk8XtjRbr2kCqwu + address_n=parse_path("m/44h/1h/0h/0/0"), amount=300000000, prev_hash=TXHASH_aaf51e, prev_index=1, @@ -104,9 +103,8 @@ class TestMsgSigntxZcash: # input 1: 3.0 TAZ inp1 = proto.TxInputType( - address_n=parse_path( - "m/Zcash Testnet/0h/0/0" - ), # tmQoJ3PTXgQLaRRZZYT6xk8XtjRbr2kCqwu + # tmQoJ3PTXgQLaRRZZYT6xk8XtjRbr2kCqwu + address_n=parse_path("m/44h/1h/0h/0/0"), amount=300000000, prev_hash=TXHASH_e38206, prev_index=0,