1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

coins: refactor, use data from coins.json instead of hardcoded lists

This commit is contained in:
matejcik 2018-04-10 11:42:21 +02:00
parent c4f38fd740
commit bd43363b1c
14 changed files with 77 additions and 60 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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/')

View File

@ -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

View File

@ -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')

View File

@ -16,11 +16,13 @@
# along with this library. If not, see <http://www.gnu.org/licenses/>.
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):

View File

@ -17,11 +17,13 @@
# along with this library. If not, see <http://www.gnu.org/licenses/>.
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):

View File

@ -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")

View File

@ -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):

View File

@ -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):

View File

@ -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')

View File

@ -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

View File

@ -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__))

View File

@ -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/')