diff --git a/trezorctl b/trezorctl index d59a6c91f..7c9091a45 100755 --- a/trezorctl +++ b/trezorctl @@ -30,9 +30,9 @@ import sys from trezorlib.client import TrezorClient, TrezorClientVerbose, CallException, format_protobuf from trezorlib.transport import get_transport, enumerate_devices, TransportException +from trezorlib import coins from trezorlib import messages as proto from trezorlib import protobuf -from trezorlib.coins import coins_txapi from trezorlib.ckd_public import PRIME_DERIVATION_FLAG @@ -470,11 +470,11 @@ def get_public_node(connect, coin, address, curve, show_display): @click.pass_obj def sign_tx(connect, coin): client = connect() - if coin in coins_txapi: - txapi = coins_txapi[coin] + if coin in coins.tx_api: + txapi = coins.tx_api[coin] else: click.echo('Coin "%s" is not recognized.' % coin, err=True) - click.echo('Supported coin types: %s' % ', '.join(coins_txapi.keys()), err=True) + click.echo('Supported coin types: %s' % ', '.join(coins.tx_api.keys()), err=True) sys.exit(1) client.set_tx_api(txapi) diff --git a/trezorlib/client.py b/trezorlib/client.py index ffded2439..ae9ae0c84 100644 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -34,7 +34,7 @@ from . import messages as proto from . import tools from . import mapping from . import nem -from .coins import coins_slip44 +from .coins import slip44 from .debuglink import DebugLink from .protobuf import MessageType @@ -530,8 +530,8 @@ class ProtocolMixin(object): n = n[1:] # coin_name/a/b/c => 44'/SLIP44_constant'/a/b/c - if n[0] in coins_slip44: - n = ["44'", "%d'" % coins_slip44[n[0]]] + n[1:] + if n[0] in slip44: + n = ["44'", "%d'" % slip44[n[0]]] + n[1:] path = [] for x in n: diff --git a/trezorlib/coins.py b/trezorlib/coins.py index 4298e0c61..8a2dbe1f6 100644 --- a/trezorlib/coins.py +++ b/trezorlib/coins.py @@ -1,31 +1,41 @@ -from .tx_api import TxApiBitcoin, TxApiTestnet, TxApiLitecoin, TxApiZcash, TxApiDash, TxApiBcash, TxApiDecredTestnet, TxApiDogecoin, TxApiMonacoin, TxApiBitcoinGold +import os.path +import json -coins_slip44 = { - 'Bitcoin': 0, - 'Testnet': 1, - 'Decred Testnet': 1, - 'Litecoin': 2, - 'Dogecoin': 3, - 'Dash': 5, - 'Namecoin': 7, - 'Monacoin': 22, - 'Decred': 42, - 'Ether': 60, - 'EtherClassic': 61, - 'Zcash': 133, - 'Bcash': 145, - 'Bitcoin Gold': 156, -} +from .tx_api import TxApiInsight, TxApiBlockCypher -coins_txapi = { - 'Bitcoin': TxApiBitcoin, - 'Testnet': TxApiTestnet, - 'Litecoin': TxApiLitecoin, - 'Dash': TxApiDash, - 'Zcash': TxApiZcash, - 'Bcash': TxApiBcash, - 'Decred Testnet': TxApiDecredTestnet, - 'Dogecoin': TxApiDogecoin, - 'Monacoin': TxApiMonacoin, - 'Bitcoin Gold': TxApiBitcoinGold, -} +COINS_JSON = os.path.join(os.path.dirname(__file__), 'coins.json') + + +def _load_coins_json(): + # Load coins.json to local variables + # NOTE: coins.json comes from 'vendor/trezor-common/coins.json', + # which is a git submodule. If you're trying to run trezorlib directly + # from the checkout (or tarball), initialize the submodule with: + # $ git submodule update --init + # and install coins.json with: + # $ python setup.py prebuild + with open(COINS_JSON) as coins_json: + coins_list = json.load(coins_json) + return {coin['coin_name']: coin for coin in coins_list} + + +def _insight_for_coin(coin): + if not coin['bitcore']: + return None + zcash = coin['coin_name'].lower().startswith('zcash') + network = 'insight_{}'.format(coin['coin_name'].lower().replace(' ', '_')) + url = coin['bitcore'][0] + 'api/' + return TxApiInsight(network=network, url=url, zcash=zcash) + + +# exported variables +__all__ = ['by_name', 'slip44', 'tx_api'] + +by_name = _load_coins_json() +slip44 = {name: coin['bip44'] for name, coin in by_name.items()} +tx_api = {name: _insight_for_coin(coin) + for name, coin in by_name.items() + if coin["bitcore"]} + +# fixup for Dogecoin +tx_api['Dogecoin'] = TxApiBlockCypher(network='blockcypher_dogecoin', url='https://api.blockcypher.com/v1/doge/main/') diff --git a/trezorlib/tests/device_tests/common.py b/trezorlib/tests/device_tests/common.py index 0fffb7ae9..8681d947f 100644 --- a/trezorlib/tests/device_tests/common.py +++ b/trezorlib/tests/device_tests/common.py @@ -22,9 +22,10 @@ from binascii import hexlify, unhexlify import pytest import os +from trezorlib import coins +from trezorlib import tx_api from trezorlib.client import TrezorClient, TrezorClientDebugLink from trezorlib.transport import get_transport -from trezorlib import tx_api tests_dir = os.path.dirname(os.path.abspath(__file__)) tx_api.cache_dir = os.path.join(tests_dir, '../txcache') @@ -42,7 +43,7 @@ class TrezorTest: debuglink = wirelink.find_debug() self.client = TrezorClientDebugLink(wirelink) self.client.set_debuglink(debuglink) - self.client.set_tx_api(tx_api.TxApiBitcoin) + self.client.set_tx_api(coins.tx_api['Bitcoin']) # self.client.set_buttonwait(3) # 1 2 3 4 5 6 7 8 9 10 11 12 diff --git a/trezorlib/tests/device_tests/test_msg_signtx.py b/trezorlib/tests/device_tests/test_msg_signtx.py index 1237bb3d5..7b92325aa 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx.py +++ b/trezorlib/tests/device_tests/test_msg_signtx.py @@ -18,9 +18,11 @@ from .common import * +from trezorlib import coins from trezorlib import messages as proto from trezorlib.client import CallException -from trezorlib.tx_api import TxApiTestnet + +TxApiTestnet = coins.tx_api['Testnet'] TXHASH_157041 = unhexlify('1570416eb4302cf52979afd5e6909e37d8fdd874301f7cc87e547e509cb1caa6') diff --git a/trezorlib/tests/device_tests/test_msg_signtx_bcash.py b/trezorlib/tests/device_tests/test_msg_signtx_bcash.py index 1e29ecf47..f397d5b6a 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx_bcash.py +++ b/trezorlib/tests/device_tests/test_msg_signtx_bcash.py @@ -16,11 +16,13 @@ # along with this library. If not, see . from .common import * +from trezorlib import coins from trezorlib import messages as proto -from trezorlib.tx_api import TxApiBcash from trezorlib.ckd_public import deserialize from trezorlib.client import CallException +TxApiBcash = coins.tx_api['Bcash'] + @pytest.mark.skip_t2 class TestMsgSigntxBch(TrezorTest): diff --git a/trezorlib/tests/device_tests/test_msg_signtx_bitcoin_gold.py b/trezorlib/tests/device_tests/test_msg_signtx_bitcoin_gold.py index 6f033b8cc..99b5082c0 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx_bitcoin_gold.py +++ b/trezorlib/tests/device_tests/test_msg_signtx_bitcoin_gold.py @@ -17,11 +17,13 @@ # along with this library. If not, see . from .common import * +from trezorlib import coins from trezorlib import messages as proto -from trezorlib.tx_api import TxApiBitcoinGold from trezorlib.ckd_public import deserialize from trezorlib.client import CallException +TxApiBitcoinGold = coins.tx_api["Bitcoin Gold"] + # All data taken from T1 class TestMsgSigntxBitcoinGold(TrezorTest): diff --git a/trezorlib/tests/device_tests/test_msg_signtx_decred.py b/trezorlib/tests/device_tests/test_msg_signtx_decred.py index 80a183e8b..77b0040a5 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx_decred.py +++ b/trezorlib/tests/device_tests/test_msg_signtx_decred.py @@ -17,8 +17,10 @@ from .common import * +from trezorlib import coins from trezorlib import messages as proto -from trezorlib.tx_api import TxApiDecredTestnet + +TxApiDecredTestnet = coins.tx_api['Decred Testnet'] TXHASH_e16248 = unhexlify("e16248f0b39a0a0c0e53d6f2f84c2a944f0d50e017a82701e8e02e46e979d5ed") diff --git a/trezorlib/tests/device_tests/test_msg_signtx_segwit.py b/trezorlib/tests/device_tests/test_msg_signtx_segwit.py index 5d35a07cd..65136c3a3 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx_segwit.py +++ b/trezorlib/tests/device_tests/test_msg_signtx_segwit.py @@ -17,11 +17,13 @@ from .common import * +from trezorlib import coins from trezorlib import messages as proto -from trezorlib.tx_api import TxApiTestnet from trezorlib.ckd_public import deserialize from trezorlib.client import CallException +TxApiTestnet = coins.tx_api["Testnet"] + class TestMsgSigntxSegwit(TrezorTest): diff --git a/trezorlib/tests/device_tests/test_msg_signtx_segwit_native.py b/trezorlib/tests/device_tests/test_msg_signtx_segwit_native.py index 4c4300aa9..f82f93ff8 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx_segwit_native.py +++ b/trezorlib/tests/device_tests/test_msg_signtx_segwit_native.py @@ -17,10 +17,12 @@ from .common import * +from trezorlib import coins from trezorlib import messages as proto -from trezorlib.tx_api import TxApiTestnet from trezorlib.ckd_public import deserialize +TxApiTestnet = coins.tx_api['Testnet'] + class TestMsgSigntxSegwitNative(TrezorTest): diff --git a/trezorlib/tests/device_tests/test_msg_signtx_zcash.py b/trezorlib/tests/device_tests/test_msg_signtx_zcash.py index e45a01a97..e4dac926e 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx_zcash.py +++ b/trezorlib/tests/device_tests/test_msg_signtx_zcash.py @@ -18,8 +18,11 @@ from .common import * + +from trezorlib import coins from trezorlib import messages as proto -from trezorlib.tx_api import TxApiZcash + +TxApiZcash = coins.tx_api["Zcash"] TXHASH_93373e = unhexlify('93373e63cc626c4a7d049ad775d6511bb5eba985f142db660c9b9f955c722f5c') diff --git a/trezorlib/tests/device_tests/test_multisig_change.py b/trezorlib/tests/device_tests/test_multisig_change.py index ed9607622..2d2d1c4dd 100644 --- a/trezorlib/tests/device_tests/test_multisig_change.py +++ b/trezorlib/tests/device_tests/test_multisig_change.py @@ -19,14 +19,14 @@ from .common import * from trezorlib import messages as proto import trezorlib.ckd_public as bip32 -from trezorlib import tx_api +from trezorlib.coins import tx_api class TestMultisigChange(TrezorTest): def setup_method(self, method): super(TestMultisigChange, self).setup_method(method) - self.client.set_tx_api(tx_api.TxApiTestnet) + self.client.set_tx_api(tx_api['Testnet']) node_ext1 = bip32.deserialize('tpubDADHV9u9Y6gkggintTdMjJE3be58zKNLhpxBQyuEM6Pwx3sN9JVLmMCMN4DNVwL9AKec27z5TaWcWuHzMXiGAtcra5DjwWbvppGX4gaEGVN') # m/1 => 02c0d0c5fee952620757c6128dbf327c996cd72ed3358d15d6518a1186099bc15e diff --git a/trezorlib/tests/unit_tests/test_tx_api.py b/trezorlib/tests/unit_tests/test_tx_api.py index 888874dd0..6111b15d8 100644 --- a/trezorlib/tests/unit_tests/test_tx_api.py +++ b/trezorlib/tests/unit_tests/test_tx_api.py @@ -18,8 +18,11 @@ import os +from trezorlib import coins from trezorlib import tx_api -from trezorlib.tx_api import TxApiBitcoin, TxApiTestnet + +TxApiBitcoin = coins.tx_api['Bitcoin'] +TxApiTestnet = coins.tx_api['Testnet'] tests_dir = os.path.dirname(os.path.abspath(__file__)) diff --git a/trezorlib/tx_api.py b/trezorlib/tx_api.py index cefe505a7..e7e8a02d0 100644 --- a/trezorlib/tx_api.py +++ b/trezorlib/tx_api.py @@ -148,15 +148,3 @@ class TxApiBlockCypher(TxApi): o.script_pubkey = binascii.unhexlify(vout['script']) return t - - -TxApiBitcoin = TxApiInsight(network='insight_bitcoin', url='https://btc-bitcore1.trezor.io/api/') -TxApiTestnet = TxApiInsight(network='insight_testnet', url='https://testnet-bitcore3.trezor.io/api/') -TxApiLitecoin = TxApiInsight(network='insight_litecoin', url='https://ltc-bitcore1.trezor.io/api/') -TxApiDash = TxApiInsight(network='insight_dash', url='https://dash-bitcore1.trezor.io/api/') -TxApiZcash = TxApiInsight(network='insight_zcash', url='https://zec-bitcore1.trezor.io/api/', zcash=True) -TxApiBcash = TxApiInsight(network='insight_bcash', url='https://bch-bitcore2.trezor.io/api/') -TxApiBitcoinGold = TxApiInsight(network='insight_bitcoin_gold', url='https://btg-bitcore2.trezor.io/api/') -TxApiDecredTestnet = TxApiInsight(network='insight_decred_testnet', url='https://testnet.decred.org/api/') -TxApiDogecoin = TxApiBlockCypher(network='blockcypher_dogecoin', url='https://api.blockcypher.com/v1/doge/main/') -TxApiMonacoin = TxApiInsight(network='insight_monacoin', url='https://mona.insight.monaco-ex.org/insight-api-monacoin/')