Coverage for ledger/hsm2dongle_cmds/signer_heartbeat.py: 100%
24 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 enum import IntEnum
24from .command import HSM2DongleCommand
25from ..signature import HSM2DongleSignature
28class Op(IntEnum):
29 UD_VALUE = 0x01
30 GET = 0x02
31 GET_MESSAGE = 0x03
32 APP_HASH = 0x04
33 PUBKEY = 0x05
36# Implements the signer heartbeat protocol against a
37# running signer
38class HSM2SignerHeartbeat(HSM2DongleCommand):
39 Command = 0x60
41 def run(self, ud_value):
42 error_code = None
44 try:
45 # Send user-defined value
46 self.send(Op.UD_VALUE, bytes.fromhex(ud_value))
48 # Retrieve signature
49 signature = self.send(Op.GET, self.NoData)[self.Offset.DATA:]
51 # Retrieve message
52 message = self.send(Op.GET_MESSAGE, self.NoData)[self.Offset.DATA:]
54 # Retrieve signer hash
55 signer_hash = self.send(Op.APP_HASH, self.NoData)[self.Offset.DATA:]
57 # Retrieve attestation public key
58 public_key = self.send(Op.PUBKEY, self.NoData)[self.Offset.DATA:]
60 return (True, {
61 "pubKey": public_key.hex(),
62 "message": message.hex(),
63 "signature": HSM2DongleSignature(signature),
64 "tweak": signer_hash.hex(),
65 })
66 except self.ErrorResult as e:
67 error_code = e.error_code
68 self.logger.error("Signer heartbeat returned: %s", hex(e.error_code))
69 # All possible error results from this operation (except not supported
70 # for SGX) are unexpected
71 # No need for specific error code mappings or special cases
73 return (False, error_code)