Coverage for admin/changepin.py: 94%

31 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-05 20:41 +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. 

22 

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 

35 

36 

37def do_changepin(options): 

38 info("### -> Change pin") 

39 hsm = None 

40 

41 # Validate new pin (if given) 

42 new_pin = None 

43 if options.new_pin is not None: 

44 if not BasePin.is_valid(options.new_pin.encode(), 

45 any_pin=options.any_pin): 

46 raise AdminError( 

47 PIN_ERROR_MESSAGE if not options.any_pin else PIN_ERROR_MESSAGE_ANYCHARS) 

48 new_pin = options.new_pin.encode() 

49 

50 # Attempt to unlock without exiting to menu/app 

51 if not options.no_unlock: 

52 try: 

53 do_unlock(options, exit=False, label=False) 

54 except Exception as e: 

55 raise AdminError(f"Failed to unlock device: {str(e)}") 

56 

57 # Connection 

58 hsm = get_hsm(options.verbose) 

59 

60 # Mode check 

61 info("Finding mode... ", options.verbose) 

62 mode = hsm.get_current_mode() 

63 info(f"Mode: {mode.name.capitalize()}") 

64 

65 # We can only change the pin while in bootloader mode 

66 if mode != HSM2Dongle.MODE.BOOTLOADER: 

67 raise AdminError("Device not in bootloader mode. Disconnect and re-connect the " 

68 "ledger and try again") 

69 

70 # Ask the user for a new pin if one has not been given 

71 if new_pin is None: 

72 info("Please select a pin for the device.") 

73 new_pin = ask_for_pin(any_pin=options.any_pin) 

74 

75 # Attempt to change the pin 

76 info("Changing pin... ", options.verbose) 

77 if not hsm.new_pin(new_pin): 

78 raise AdminError("Failed to change pin") 

79 info("Pin changed. Please disconnect and re-connect the ledger.") 

80 

81 dispose_hsm(hsm)