Coverage for admin/changepin.py: 94%
34 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-07-10 13:43 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-07-10 13:43 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2021 RSK Labs Ltd
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy of
6# this software and associated documentation files (the "Software"), to deal in
7# the Software without restriction, including without limitation the rights to
8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9# of the Software, and to permit persons to whom the Software is furnished to do
10# so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
23from ledger.hsm2dongle import HSM2Dongle
24from ledger.pin import BasePin
25from .misc import (
26 info,
27 get_hsm,
28 dispose_hsm,
29 ask_for_pin,
30 PIN_ERROR_MESSAGE,
31 PIN_ERROR_MESSAGE_ANYCHARS,
32 AdminError,
33)
34from .unlock import do_unlock
35from comm.platform import Platform
38def do_changepin(options):
39 info("### -> Change pin")
40 hsm = None
42 # Validate new pin (if given)
43 new_pin = None
44 if options.new_pin is not None:
45 if not BasePin.is_valid(options.new_pin.encode(),
46 any_pin=options.any_pin):
47 raise AdminError(
48 PIN_ERROR_MESSAGE if not options.any_pin else PIN_ERROR_MESSAGE_ANYCHARS)
49 new_pin = options.new_pin.encode()
51 # Attempt to unlock without exiting to menu/app
52 if not options.no_unlock:
53 try:
54 do_unlock(options, exit=False, label=False)
55 except Exception as e:
56 raise AdminError(f"Failed to unlock device: {str(e)}")
58 # Connection
59 hsm = get_hsm(options.verbose)
61 # Mode check
62 info("Finding mode... ", options.verbose)
63 mode = hsm.get_current_mode()
64 info(f"Mode: {mode.name.capitalize()}")
66 # In Ledger, we can only change the pin while in bootloader mode
67 if Platform.is_ledger() and mode != HSM2Dongle.MODE.BOOTLOADER:
68 raise AdminError("Device not in bootloader mode. "
69 f"{Platform.message('restart').capitalize()} and try again")
71 # Ask the user for a new pin if one has not been given
72 if new_pin is None:
73 info("Please select a pin for the device.")
74 new_pin = ask_for_pin(any_pin=options.any_pin)
76 # Attempt to change the pin
77 info("Changing pin... ", options.verbose)
78 if not hsm.new_pin(new_pin):
79 raise AdminError("Failed to change pin")
80 info("Pin changed.", nl=Platform.is_sgx())
81 # We require a restart in Ledger only
82 if Platform.is_ledger():
83 info(f" Please {Platform.message('restart')}.")
85 dispose_hsm(hsm)