From aa2966705900b36271472114364396f2bf92cdac Mon Sep 17 00:00:00 2001 From: Tomas Susanka Date: Thu, 9 Nov 2017 15:37:22 +0100 Subject: [PATCH] common: coins updated --- src/apps/common/coins.py | 154 ++++++++++++++++-- src/apps/wallet/sign_tx/segwit_bip143.py | 3 +- ...apps.wallet.segwit.bip143.native_p2wpkh.py | 96 +++++++++++ ...ps.wallet.segwit.bip143.p2wpkh_in_p2sh.py} | 0 tools/codegen/gen_coins.py | 17 +- 5 files changed, 255 insertions(+), 15 deletions(-) create mode 100644 tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py rename tests/{test_apps.wallet.segwit.bip143.py => test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py} (100%) diff --git a/src/apps/common/coins.py b/src/apps/common/coins.py index 5954ce1565..be35029011 100644 --- a/src/apps/common/coins.py +++ b/src/apps/common/coins.py @@ -1,127 +1,257 @@ from trezor.messages.CoinType import CoinType -# the following list is generated using tools/coins-gen.py +# the following list is generated using tools/codegen/gen_coins.py # do not edit manually! COINS = [ CoinType( coin_name='Bitcoin', coin_shortcut='BTC', + coin_label='Bitcoin', address_type=0, address_type_p2sh=5, maxfee_kb=500000, + minfee_kb=1000, signed_message_header='Bitcoin Signed Message:\n', - xpub_magic=0x0488b21e, + hash_genesis_block='000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', xprv_magic=0x0488ade4, + xpub_magic=0x0488b21e, + bech32_prefix='bc', bip44=0, - segwit=False, + segwit=True, + forkid=None, + default_fee_b={'Low': 10, 'Economy': 70, 'Normal': 140, 'High': 200}, + dust_limit=546, + blocktime_minutes=10, + firmware='stable', + address_prefix='bitcoin:', + min_address_length=27, + max_address_length=34, + bitcore=['https://btc-bitcore3.trezor.io', 'https://btc-bitcore1.trezor.io'], ), CoinType( coin_name='Testnet', coin_shortcut='TEST', + coin_label='Testnet', address_type=111, address_type_p2sh=196, maxfee_kb=10000000, + minfee_kb=1000, signed_message_header='Bitcoin Signed Message:\n', - xpub_magic=0x043587cf, + hash_genesis_block='000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943', xprv_magic=0x04358394, + xpub_magic=0x043587cf, + bech32_prefix='tb', bip44=1, segwit=True, + forkid=None, + default_fee_b={'Normal': 10}, + dust_limit=546, + blocktime_minutes=10, + firmware='stable', + address_prefix='bitcoin:', + min_address_length=27, + max_address_length=34, + bitcore=['https://testnet-bitcore3.trezor.io', 'https://testnet-bitcore4.trezor.io'], ), CoinType( coin_name='Bcash', coin_shortcut='BCH', + coin_label='Bitcoin Cash', address_type=0, address_type_p2sh=5, maxfee_kb=500000, + minfee_kb=1000, signed_message_header='Bitcoin Signed Message:\n', - xpub_magic=0x0488b21e, + hash_genesis_block='000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', xprv_magic=0x0488ade4, + xpub_magic=0x0488b21e, + bech32_prefix=None, bip44=145, segwit=False, + forkid=0, + default_fee_b={'Low': 10, 'Economy': 70, 'Normal': 140, 'High': 200}, + dust_limit=546, + blocktime_minutes=10, + firmware='stable', + address_prefix='bitcoincash:', + min_address_length=27, + max_address_length=34, + bitcore=['https://bch-bitcore2.trezor.io'], ), CoinType( coin_name='Bcash Testnet', coin_shortcut='TBCH', + coin_label='Bitcoin Cash Testnet', address_type=111, address_type_p2sh=196, maxfee_kb=10000000, + minfee_kb=1000, signed_message_header='Bitcoin Signed Message:\n', - xpub_magic=0x043587cf, + hash_genesis_block='000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943', xprv_magic=0x04358394, + xpub_magic=0x043587cf, + bech32_prefix=None, bip44=1, segwit=False, + forkid=0, + default_fee_b={'Normal': 10}, + dust_limit=546, + blocktime_minutes=10, + firmware='debug', + address_prefix='bitcoincash:', + min_address_length=27, + max_address_length=34, + bitcore=[], ), CoinType( coin_name='Namecoin', coin_shortcut='NMC', + coin_label='Namecoin', address_type=52, address_type_p2sh=5, maxfee_kb=10000000, + minfee_kb=1000, signed_message_header='Namecoin Signed Message:\n', - xpub_magic=0x019da462, + hash_genesis_block='000000000062b72c5e2ceb45fbc8587e807c155b0da735e6483dfba2f0a9c770', xprv_magic=0x019d9cfe, + xpub_magic=0x019da462, + bech32_prefix=None, bip44=7, segwit=False, + forkid=None, + default_fee_b={'Normal': 10}, + dust_limit=2940, + blocktime_minutes=10, + firmware='stable', + address_prefix='namecoin:', + min_address_length=27, + max_address_length=34, + bitcore=[], ), CoinType( coin_name='Litecoin', coin_shortcut='LTC', + coin_label='Litecoin', address_type=48, address_type_p2sh=50, maxfee_kb=40000000, + minfee_kb=100000, signed_message_header='Litecoin Signed Message:\n', - xpub_magic=0x019da462, + hash_genesis_block='12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2', xprv_magic=0x019d9cfe, + xpub_magic=0x019da462, + bech32_prefix='ltc', bip44=2, segwit=True, + forkid=None, + default_fee_b={'Normal': 1000}, + dust_limit=54600, + blocktime_minutes=2.5, + firmware='stable', + address_prefix='litecoin:', + min_address_length=27, + max_address_length=34, + bitcore=['https://ltc-bitcore3.trezor.io'], ), CoinType( coin_name='Dogecoin', coin_shortcut='DOGE', + coin_label='Dogecoin', address_type=30, address_type_p2sh=22, maxfee_kb=1000000000, + minfee_kb=1000, signed_message_header='Dogecoin Signed Message:\n', - xpub_magic=0x02facafd, + hash_genesis_block='1a91e3dace36e2be3bf030a65679fe821aa1d6ef92e7c9902eb318182c355691', xprv_magic=0x02fac398, + xpub_magic=0x02facafd, + bech32_prefix=None, bip44=3, segwit=False, + forkid=None, + default_fee_b={'Normal': 10}, + dust_limit=10000000, + blocktime_minutes=1, + firmware='stable', + address_prefix='dogecoin:', + min_address_length=27, + max_address_length=34, + bitcore=[], ), CoinType( coin_name='Dash', coin_shortcut='DASH', + coin_label='Dash', address_type=76, address_type_p2sh=16, maxfee_kb=100000, + minfee_kb=10000, signed_message_header='DarkCoin Signed Message:\n', - xpub_magic=0x02fe52cc, + hash_genesis_block='00000ffd590b1485b3caadc19b22e6379c733355108f107a430458cdf3407ab6', xprv_magic=0x02fe52f8, + xpub_magic=0x02fe52cc, + bech32_prefix=None, bip44=5, segwit=False, + forkid=None, + default_fee_b={'Normal': 10}, + dust_limit=5460, + blocktime_minutes=2.5, + firmware='stable', + address_prefix='dash:', + min_address_length=27, + max_address_length=34, + bitcore=['https://dash-bitcore1.trezor.io', 'https://dash-bitcore3.trezor.io'], ), CoinType( coin_name='Zcash', coin_shortcut='ZEC', + coin_label='Zcash', address_type=7352, address_type_p2sh=7357, maxfee_kb=1000000, + minfee_kb=1000, signed_message_header='Zcash Signed Message:\n', - xpub_magic=0x0488b21e, + hash_genesis_block='00040fe8ec8471911baa1db1266ea15dd06b4a8a5c453883c000b031973dce08', xprv_magic=0x0488ade4, + xpub_magic=0x0488b21e, + bech32_prefix=None, bip44=133, segwit=False, + forkid=None, + default_fee_b={'Normal': 10}, + dust_limit=546, + blocktime_minutes=2.5, + firmware='stable', + address_prefix='zcash:', + min_address_length=35, + max_address_length=95, + bitcore=['https://zec-bitcore1.trezor.io/'], ), CoinType( coin_name='Zcash Testnet', coin_shortcut='TAZ', + coin_label='Zcash Testnet', address_type=7461, address_type_p2sh=7354, maxfee_kb=10000000, + minfee_kb=1000, signed_message_header='Zcash Signed Message:\n', - xpub_magic=0x043587cf, + hash_genesis_block='05a60a92d99d85997cce3b87616c089f6124d7342af37106edc76126334a2c38', xprv_magic=0x04358394, + xpub_magic=0x043587cf, + bech32_prefix=None, bip44=1, segwit=False, + forkid=None, + default_fee_b={'Normal': 10}, + dust_limit=546, + blocktime_minutes=2.5, + firmware='debug', + address_prefix='zcash:', + min_address_length=35, + max_address_length=95, + bitcore=[], ), ] diff --git a/src/apps/wallet/sign_tx/segwit_bip143.py b/src/apps/wallet/sign_tx/segwit_bip143.py index 4d45922522..4a97c52336 100644 --- a/src/apps/wallet/sign_tx/segwit_bip143.py +++ b/src/apps/wallet/sign_tx/segwit_bip143.py @@ -58,7 +58,8 @@ class Bip143: # this not redeemScript nor scriptPubKey # for P2WPKH this is always 0x1976a914{20-byte-pubkey-hash}88ac def derive_script_code(self, txi: TxInputType, pubkeyhash: bytes) -> bytearray: - if txi.script_type == InputScriptType.SPENDP2SHWITNESS: + # p2wpkh in p2sh or native p2wpkh + if txi.script_type in (InputScriptType.SPENDP2SHWITNESS, InputScriptType.SPENDWITNESS): s = bytearray(25) s[0] = 0x76 # OP_DUP s[1] = 0xA9 # OP_HASH_160 diff --git a/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py b/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py new file mode 100644 index 0000000000..1a390f4cfa --- /dev/null +++ b/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py @@ -0,0 +1,96 @@ +from common import * + +from apps.wallet.sign_tx.signing import * +from apps.common import coins +from trezor.messages.SignTx import SignTx +from trezor.messages.TxInputType import TxInputType +from trezor.messages.TxOutputType import TxOutputType +from trezor.messages import OutputScriptType +from trezor.crypto import bip32, bip39 + + +class TestSegwitBip143NativeP2WPKH(unittest.TestCase): + # pylint: disable=C0301 + + tx = SignTx(coin_name='Bitcoin', version=1, lock_time=0x00000011, inputs_count=2, outputs_count=2) + inp1 = TxInputType(address_n=[0], + # Trezor expects hash in reversed format + prev_hash=unhexlify('9f96ade4b41d5433f4eda31e1738ec2b36f6e7d1420d94a6af99801a88f7f7ff'), + prev_index=0, + amount=625000000, # 6.25 btc + script_type=InputScriptType.SPENDWITNESS, + sequence=0xffffffee) + inp2 = TxInputType(address_n=[1], + # Trezor expects hash in reversed format + prev_hash=unhexlify('8ac60eb9575db5b2d987e29f301b5b819ea83a5c6579d282d189cc04b8e151ef'), + prev_index=1, + amount=600000000, # 6 btc + script_type=InputScriptType.SPENDWITNESS, + sequence=0xffffffff) + out1 = TxOutputType(address='1Cu32FVupVCgHkMMRJdYJugxwo2Aprgk7H', # derived + amount=0x0000000006b22c20, + script_type=OutputScriptType.PAYTOADDRESS, + address_n=None) + out2 = TxOutputType(address='16TZ8J6Q5iZKBWizWzFAYnrsaox5Z5aBRV', # derived + amount=0x000000000d519390, + script_type=OutputScriptType.PAYTOADDRESS, + address_n=None) + + def test_prevouts(self): + + bip143 = Bip143() + bip143.add_prevouts(self.inp1) + bip143.add_prevouts(self.inp2) + self.assertEqual(hexlify(bip143.get_prevouts_hash()), b'96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37') + + def test_sequence(self): + + bip143 = Bip143() + bip143.add_sequence(self.inp1) + bip143.add_sequence(self.inp2) + self.assertEqual(hexlify(bip143.get_sequence_hash()), b'52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b') + + def test_outputs(self): + + seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') + root = bip32.from_seed(seed, 'secp256k1') + coin = coins.by_name(self.tx.coin_name) + + bip143 = Bip143() + + for txo in [self.out1, self.out2]: + txo_bin = TxOutputBinType() + txo_bin.amount = txo.amount + txo_bin.script_pubkey = output_derive_script(txo, coin, root) + bip143.add_output(txo_bin) + + self.assertEqual(hexlify(bip143.get_outputs_hash()), + b'863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5') + + def test_preimage_testdata(self): + + seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') + root = bip32.from_seed(seed, 'secp256k1') + coin = coins.by_name(self.tx.coin_name) + + bip143 = Bip143() + bip143.add_prevouts(self.inp1) + bip143.add_prevouts(self.inp2) + bip143.add_sequence(self.inp1) + bip143.add_sequence(self.inp2) + + for txo in [self.out1, self.out2]: + txo_bin = TxOutputBinType() + txo_bin.amount = txo.amount + txo_bin.script_pubkey = output_derive_script(txo, coin, root) + bip143.add_output(txo_bin) + + # test data public key hash + # only for input 2 - input 1 is not segwit + result = bip143.preimage_hash(self.tx, self.inp2, unhexlify('1d0f172a0ecb48aee1be1f2687d2963ae33f71a1')) + self.assertEqual(hexlify(result), b'c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670') + + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_apps.wallet.segwit.bip143.py b/tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py similarity index 100% rename from tests/test_apps.wallet.segwit.bip143.py rename to tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py diff --git a/tools/codegen/gen_coins.py b/tools/codegen/gen_coins.py index ebfe6c2cd7..dd7728ddcd 100755 --- a/tools/codegen/gen_coins.py +++ b/tools/codegen/gen_coins.py @@ -4,17 +4,30 @@ import json fields = [ 'coin_name', 'coin_shortcut', + 'coin_label', 'address_type', 'address_type_p2sh', 'maxfee_kb', + 'minfee_kb', 'signed_message_header', - 'xpub_magic', + 'hash_genesis_block', 'xprv_magic', + 'xpub_magic', + 'bech32_prefix', 'bip44', 'segwit', + 'forkid', + 'default_fee_b', + 'dust_limit', + 'blocktime_minutes', + 'firmware', + 'address_prefix', + 'min_address_length', + 'max_address_length', + 'bitcore', ] -coins = json.load(open('../../../trezor-common/coins.json', 'r')) +coins = json.load(open('../../vendor/trezor-common/coins.json', 'r')) print('COINS = [') for c in coins: