def start(self): self.emulator_log = open('trezor-{}-emulator.stdout'.format(self.model), 'a') # Start the Trezor emulator self.emulator_proc = subprocess.Popen(['./' + os.path.basename(self.emulator_path)], cwd=os.path.dirname(self.emulator_path), stdout=self.emulator_log, env={'SDL_VIDEODRIVER': 'dummy', 'PYOPT': '0'}, shell=True, preexec_fn=os.setsid) # Wait for emulator to be up # From https://github.com/trezor/trezor-firmware/blob/master/legacy/script/wait_for_emulator.py sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect(('127.0.0.1', 21324)) sock.settimeout(0) while True: try: sock.sendall(b"PINGPING") r = sock.recv(8) if r == b"PONGPONG": break except Exception: time.sleep(0.05) # Setup the emulator for dev in enumerate_devices(): # Find the udp transport, that's the emulator if isinstance(dev, UdpTransport): wirelink = dev break client = TrezorClientDebugLink(wirelink) client.init_device() device.wipe(client) load_device_by_mnemonic(client=client, mnemonic='alcohol woman abuse must during monitor noble actual mixed trade anger aisle', pin='', passphrase_protection=False, label='test') # From Trezor device tests atexit.register(self.stop) return client
def test_getxpub(self): with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/bip32_vectors.json'), encoding='utf-8') as f: vectors = json.load(f) for vec in vectors: with self.subTest(vector=vec): # Setup with mnemonic device.wipe(self.client) load_device_by_mnemonic(client=self.client, mnemonic=vec['mnemonic'], pin='', passphrase_protection=False, label='test', language='english') # Test getmasterxpub gmxp_res = self.do_command([ '-t', 'trezor', '-d', 'udp:127.0.0.1:21324', 'getmasterxpub' ]) self.assertEqual(gmxp_res['xpub'], vec['master_xpub']) # Test the path derivs for path_vec in vec['vectors']: gxp_res = self.do_command([ '-t', 'trezor', '-d', 'udp:127.0.0.1:21324', 'getxpub', path_vec['path'] ]) self.assertEqual(gxp_res['xpub'], path_vec['xpub'])
def test_pins(self): # There's no PIN result = self.do_command(self.dev_args + ['--debug', 'promptpin']) self.assertEqual(result['error'], 'This device does not need a PIN') self.assertEqual(result['code'], -11) result = self.do_command(self.dev_args + ['sendpin', '1234']) self.assertEqual(result['error'], 'This device does not need a PIN') self.assertEqual(result['code'], -11) result = self.do_command(self.dev_args + ['enumerate']) for dev in result: if dev['type'] == 'trezor' and dev['path'] == 'udp:127.0.0.1:21324': self.assertFalse(dev['needs_pin_sent']) # Set a PIN device.wipe(self.client) load_device_by_mnemonic(client=self.client, mnemonic='alcohol woman abuse must during monitor noble actual mixed trade anger aisle', pin='1234', passphrase_protection=False, label='test') self.client.call(messages.ClearSession()) result = self.do_command(self.dev_args + ['enumerate']) for dev in result: if dev['type'] == 'trezor' and dev['path'] == 'udp:127.0.0.1:21324': self.assertTrue(dev['needs_pin_sent']) result = self.do_command(self.dev_args + ['promptpin']) self.assertTrue(result['success']) # Invalid pins result = self.do_command(self.dev_args + ['sendpin', 'notnum']) self.assertEqual(result['error'], 'Non-numeric PIN provided') self.assertEqual(result['code'], -7) result = self.do_command(self.dev_args + ['sendpin', '1111']) self.assertFalse(result['success']) # Make sure we get a needs pin message result = self.do_command(self.dev_args + ['getxpub', 'm/0h']) self.assertEqual(result['code'], -12) self.assertEqual(result['error'], 'Trezor is locked. Unlock by using \'promptpin\' and then \'sendpin\'.') # Prompt pin self.client.call(messages.ClearSession()) result = self.do_command(self.dev_args + ['promptpin']) self.assertTrue(result['success']) # Send the PIN self.client.open() pin = self.client.debug.encode_pin('1234') result = self.do_command(self.dev_args + ['sendpin', pin]) self.assertTrue(result['success']) result = self.do_command(self.dev_args + ['enumerate']) for dev in result: if dev['type'] == 'trezor' and dev['path'] == 'udp:127.0.0.1:21324': self.assertFalse(dev['needs_pin_sent']) # Sending PIN after unlock result = self.do_command(self.dev_args + ['promptpin']) self.assertEqual(result['error'], 'The PIN has already been sent to this device') self.assertEqual(result['code'], -11) result = self.do_command(self.dev_args + ['sendpin', '1234']) self.assertEqual(result['error'], 'The PIN has already been sent to this device') self.assertEqual(result['code'], -11)
def start(self): # Start the Keepkey emulator self.emulator_proc = subprocess.Popen(['./' + os.path.basename(self.emulator_path)], cwd=os.path.dirname(self.emulator_path), stdout=subprocess.DEVNULL) # Wait for emulator to be up # From https://github.com/trezor/trezor-mcu/blob/master/script/wait_for_emulator.py sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect(('127.0.0.1', 21324)) sock.settimeout(0) while True: try: sock.sendall(b"PINGPING") r = sock.recv(8) if r == b"PONGPONG": break except Exception: time.sleep(0.05) # Setup the emulator for dev in enumerate_devices(): # Find the udp transport, that's the emulator if isinstance(dev, UdpTransport): wirelink = dev break client = TrezorClientDebugLink(wirelink) client.init_device() device.wipe(client) load_device_by_mnemonic(client=client, mnemonic='alcohol woman abuse must during monitor noble actual mixed trade anger aisle', pin='', passphrase_protection=False, label='test') # From Trezor device tests return client
def test_passphrase(self): # There's no passphrase result = self.do_command(self.dev_args + ['enumerate']) for dev in result: if dev['type'] == 'keepkey' and dev[ 'path'] == 'udp:127.0.0.1:21324': self.assertFalse(dev['needs_passphrase_sent']) self.assertEquals(dev['fingerprint'], '95d8f670') # Setting a passphrase won't change the fingerprint result = self.do_command(self.dev_args + ['-p', 'pass', 'enumerate']) for dev in result: if dev['type'] == 'keepkey' and dev[ 'path'] == 'udp:127.0.0.1:21324': self.assertFalse(dev['needs_passphrase_sent']) self.assertEquals(dev['fingerprint'], '95d8f670') # Set a passphrase device.wipe(self.client) self.client.set_passphrase('pass') load_device_by_mnemonic( client=self.client, mnemonic= 'alcohol woman abuse must during monitor noble actual mixed trade anger aisle', pin='', passphrase_protection=True, label='test') self.client.call(messages.ClearSession()) # A passphrase will need to be sent result = self.do_command(self.dev_args + ['enumerate']) for dev in result: if dev['type'] == 'keepkey' and dev[ 'path'] == 'udp:127.0.0.1:21324': self.assertTrue(dev['needs_passphrase_sent']) result = self.do_command(self.dev_args + ['-p', 'pass', 'enumerate']) for dev in result: if dev['type'] == 'keepkey' and dev[ 'path'] == 'udp:127.0.0.1:21324': self.assertFalse(dev['needs_passphrase_sent']) fpr = dev['fingerprint'] # A different passphrase will change the fingerprint result = self.do_command(self.dev_args + ['-p', 'pass2', 'enumerate']) for dev in result: if dev['type'] == 'keepkey' and dev[ 'path'] == 'udp:127.0.0.1:21324': self.assertFalse(dev['needs_passphrase_sent']) self.assertNotEqual(dev['fingerprint'], fpr) # Clearing the session and starting a new one with a new passphrase should change the passphrase self.client.call(messages.ClearSession()) result = self.do_command(self.dev_args + ['-p', 'pass3', 'enumerate']) for dev in result: if dev['type'] == 'keepkey' and dev[ 'path'] == 'udp:127.0.0.1:21324': self.assertFalse(dev['needs_passphrase_sent']) self.assertNotEqual(dev['fingerprint'], fpr)
def start(self): super().start() self.keepkey_log = open('keepkey-emulator.stdout', 'a') # Start the Keepkey emulator self.emulator_proc = subprocess.Popen( ['./' + os.path.basename(self.emulator_path)], cwd=os.path.dirname(self.emulator_path), stdout=self.keepkey_log) # Wait for emulator to be up # From https://github.com/trezor/trezor-firmware/blob/master/legacy/script/wait_for_emulator.py sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect(('127.0.0.1', 11044)) sock.settimeout(0) while True: try: sock.sendall(b"PINGPING") r = sock.recv(8) if r == b"PONGPONG": break except Exception: time.sleep(0.05) # Setup the emulator model = TrezorModel( name="K1-14M", minimum_version=(0, 0, 0), vendors=("keepkey.com"), usb_ids=(), # unused default_mapping=DEFAULT_MAPPING, ) model.default_mapping.register(KeepkeyFeatures) model.default_mapping.register(KeepkeyResetDevice) model.default_mapping.register(KeepkeyDebugLinkState) wirelink = UdpTransport.enumerate("127.0.0.1:11044")[0] client = TrezorClientDebugLink(wirelink, model=model) client.init_device() device.wipe(client) load_device_by_mnemonic( client=client, mnemonic= 'alcohol woman abuse must during monitor noble actual mixed trade anger aisle', pin='', passphrase_protection=False, label='test') # From Trezor device tests atexit.register(self.stop) return client
def start(self): self.keepkey_log = open('keepkey-emulator.stdout', 'a') # Start the Keepkey emulator self.emulator_proc = subprocess.Popen( ['./' + os.path.basename(self.emulator_path)], cwd=os.path.dirname(self.emulator_path), stdout=self.keepkey_log) # Wait for emulator to be up # From https://github.com/trezor/trezor-firmware/blob/master/legacy/script/wait_for_emulator.py sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect(('127.0.0.1', 21324)) sock.settimeout(0) while True: try: sock.sendall(b"PINGPING") r = sock.recv(8) if r == b"PONGPONG": break except Exception: time.sleep(0.05) # Setup the emulator wirelink = UdpTransport.enumerate()[0] client = TrezorClientDebugLink(wirelink) client.vendors = ("keepkey.com") client.minimum_versions = {"K1-14AM": (0, 0, 0)} client.map_type_to_class_override[ KeepkeyFeatures.MESSAGE_WIRE_TYPE] = KeepkeyFeatures client.map_type_to_class_override[ KeepkeyResetDevice.MESSAGE_WIRE_TYPE] = KeepkeyResetDevice client.debug.map_type_to_class_override[ KeepkeyDebugLinkState.MESSAGE_WIRE_TYPE] = KeepkeyDebugLinkState client.init_device() device.wipe(client) load_device_by_mnemonic( client=client, mnemonic= 'alcohol woman abuse must during monitor noble actual mixed trade anger aisle', pin='', passphrase_protection=False, label='test') # From Trezor device tests atexit.register(self.stop) return client