Skip to content

Commit f862135

Browse files
committed
trezor: Set coin name as testnet when --testnet
Also update displayaddress test to use --testnet
1 parent d662fad commit f862135

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

hwilib/devices/trezor.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def __init__(self, path, password='', expert=False):
109109
self.type = 'Trezor'
110110

111111
def _check_unlocked(self):
112+
self.coin_name = 'Testnet' if self.is_testnet else 'Bitcoin'
112113
self.client.init_device()
113114
if self.client.features.model == 'T':
114115
self.client.ui.disallow_passphrase()
@@ -124,7 +125,7 @@ def get_pubkey_at_path(self, path):
124125
expanded_path = tools.parse_path(path)
125126
except ValueError as e:
126127
raise BadArgumentError(str(e))
127-
output = btc.get_public_node(self.client, expanded_path)
128+
output = btc.get_public_node(self.client, expanded_path, coin_name=self.coin_name)
128129
if self.is_testnet:
129130
result = {'xpub': xpub_main_2_test(output.xpub)}
130131
else:
@@ -142,7 +143,7 @@ def sign_tx(self, tx):
142143
self._check_unlocked()
143144

144145
# Get this devices master key fingerprint
145-
master_key = btc.get_public_node(self.client, [0])
146+
master_key = btc.get_public_node(self.client, [0x80000000], coin_name='Bitcoin')
146147
master_fp = get_xpub_fingerprint(master_key.xpub)
147148

148149
# Do multiple passes for multisig
@@ -321,10 +322,7 @@ def ignore_input():
321322
tx_details = proto.SignTx()
322323
tx_details.version = tx.tx.nVersion
323324
tx_details.lock_time = tx.tx.nLockTime
324-
if self.is_testnet:
325-
signed_tx = btc.sign_tx(self.client, "Testnet", inputs, outputs, tx_details, prevtxs)
326-
else:
327-
signed_tx = btc.sign_tx(self.client, "Bitcoin", inputs, outputs, tx_details, prevtxs)
325+
signed_tx = btc.sign_tx(self.client, self.coin_name, inputs, outputs, tx_details, prevtxs)
328326

329327
# Each input has one signature
330328
for input_num, (psbt_in, sig) in py_enumerate(list(zip(tx.inputs, signed_tx[0]))):
@@ -346,20 +344,21 @@ def ignore_input():
346344
def sign_message(self, message, keypath):
347345
self._check_unlocked()
348346
path = tools.parse_path(keypath)
349-
result = btc.sign_message(self.client, 'Bitcoin', path, message)
347+
result = btc.sign_message(self.client, self.coin_name, path, message)
350348
return {'signature': base64.b64encode(result.signature).decode('utf-8')}
351349

352350
# Display address of specified type on the device. Only supports single-key based addresses.
353351
@trezor_exception
354352
def display_address(self, keypath, p2sh_p2wpkh, bech32):
355353
self._check_unlocked()
356354
expanded_path = tools.parse_path(keypath)
355+
script_type = proto.InputScriptType.SPENDWITNESS if bech32 else (proto.InputScriptType.SPENDP2SHWITNESS if p2sh_p2wpkh else proto.InputScriptType.SPENDADDRESS)
357356
address = btc.get_address(
358357
self.client,
359-
"Testnet" if self.is_testnet else "Bitcoin",
358+
self.coin_name,
360359
expanded_path,
361360
show_display=True,
362-
script_type=proto.InputScriptType.SPENDWITNESS if bech32 else (proto.InputScriptType.SPENDP2SHWITNESS if p2sh_p2wpkh else proto.InputScriptType.SPENDADDRESS)
361+
script_type=script_type
363362
)
364363
return {'address': address}
365364

@@ -406,6 +405,7 @@ def close(self):
406405
# Prompt for a pin on device
407406
@trezor_exception
408407
def prompt_pin(self):
408+
self.coin_name = 'Testnet' if self.is_testnet else 'Bitcoin'
409409
self.client.open()
410410
self.client.init_device()
411411
if not self.client.features.pin_protection:
@@ -414,7 +414,7 @@ def prompt_pin(self):
414414
raise DeviceAlreadyUnlockedError('The PIN has already been sent to this device')
415415
print('Use \'sendpin\' to provide the number positions for the PIN as displayed on your device\'s screen', file=sys.stderr)
416416
print(PIN_MATRIX_DESCRIPTION, file=sys.stderr)
417-
self.client.call_raw(proto.GetPublicKey(address_n=[0x8000002c, 0x80000001, 0x80000000], ecdsa_curve_name=None, show_display=False, coin_name=None, script_type=proto.InputScriptType.SPENDADDRESS))
417+
self.client.call_raw(proto.GetPublicKey(address_n=[0x8000002c, 0x80000001, 0x80000000], ecdsa_curve_name=None, show_display=False, coin_name=self.coin_name, script_type=proto.InputScriptType.SPENDADDRESS))
418418
return {'success': True}
419419

420420
# Send the pin

test/test_device.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ def test_big_tx(self):
456456
pass
457457

458458
class TestDisplayAddress(DeviceTestCase):
459+
def setUp(self):
460+
if '--testnet' not in self.dev_args:
461+
self.dev_args.append('--testnet')
462+
self.emulator.start()
463+
459464
def test_display_address_bad_args(self):
460465
result = self.do_command(self.dev_args + ['displayaddress', '--sh_wpkh', '--wpkh', '--path', 'm/49h/1h/0h/0/0'])
461466
self.assertIn('error', result)

0 commit comments

Comments
 (0)