Coverage for comm/protocol_v1.py: 100%

28 statements  

« 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. 

22 

23from .protocol import HSM2Protocol 

24from .utils import is_hex_string_of_length 

25 

26 

27class HSM1Protocol(HSM2Protocol): 

28 # Auth-related error codes 

29 ERROR_CODE_INVALID_AUTH = -2 

30 ERROR_CODE_INVALID_MESSAGE = -2 

31 ERROR_CODE_INVALID_KEYID = -2 

32 

33 # Generic error codes (taken from the original v1 server) 

34 ERROR_CODE_FORMAT_ERROR = -2 

35 ERROR_CODE_INVALID_REQUEST = -2 

36 ERROR_CODE_COMMAND_UNKNOWN = -2 

37 ERROR_CODE_WRONG_VERSION = -666 

38 ERROR_CODE_DEVICE = -2 

39 ERROR_CODE_UNKNOWN = -2 

40 

41 # Protocol version 

42 VERSION = 1 

43 

44 def __init__(self): 

45 super().__init__() 

46 

47 def _validate_sign(self, request): 

48 # Validate key id 

49 # SIDE EFFECT: request["keyId"] is turned into a comm.bip32.BIP32Path instance 

50 keyid_validation = self._validate_key_id(request) 

51 if keyid_validation < self.ERROR_CODE_OK: 

52 return keyid_validation 

53 

54 # Validate message field 

55 # It must always be present and a string that must be a 32-byte hex 

56 if ( 

57 "message" not in request 

58 or type(request["message"]) != str 

59 or not is_hex_string_of_length(request["message"], 32) 

60 ): 

61 self.logger.info( 

62 "Message field not present, not a string or not a 32-byte hex" 

63 ) 

64 return self.ERROR_CODE_INVALID_MESSAGE 

65 

66 return self.ERROR_CODE_OK 

67 

68 def _init_mappings(self): 

69 self.logger.debug("Initializing mappings") 

70 self._mappings = { 

71 self.VERSION_COMMAND: self._version, 

72 self.SIGN_COMMAND: self._sign, 

73 self.GETPUBKEY_COMMAND: self._get_pubkey, 

74 } 

75 

76 # Command input validations 

77 self._validation_mappings = { 

78 self.VERSION_COMMAND: lambda r: 0, 

79 self.SIGN_COMMAND: self._validate_sign, 

80 self.GETPUBKEY_COMMAND: self._validate_get_pubkey, 

81 } 

82 self._known_commands = self._mappings.keys()