Coverage for tests/ledger/test_signature.py: 100%
35 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 unittest import TestCase
24from ledger.signature import HSM2DongleSignature
26import logging
28logging.disable(logging.CRITICAL)
31class TestHSM2DongleSignature(TestCase):
32 def test_signature_1(self):
33 bs = bytes.fromhex(
34 "3045022100e719a1a379143ee7b598390305f4f1a991d6e26f175545c739f89728e270671"
35 "402207fcc41e525508a27bdcf9bd82f4b75709e8771dde714d0cf3d362056ed1bb07c9000"
36 )
37 expected_r = "00e719a1a379143ee7b598390305f4f1a991d6e26f175545c739f89728e2706714"
38 expected_s = "7fcc41e525508a27bdcf9bd82f4b75709e8771dde714d0cf3d362056ed1bb07c"
39 signature = HSM2DongleSignature(bs)
40 self.assertEqual(expected_r, signature.r)
41 self.assertEqual(expected_s, signature.s)
43 def test_signature_2(self):
44 bs = bytes.fromhex(
45 "3145022100f48d30ae4f01925939116c6b74c1c9afefc6d1a0b7109307a68126f3683308f"
46 "502206cec3ea71687eaf367885b44ec58daab06ff43c68a0817eb2177193218573cae9000"
47 )
48 expected_r = "00f48d30ae4f01925939116c6b74c1c9afefc6d1a0b7109307a68126f3683308f5"
49 expected_s = "6cec3ea71687eaf367885b44ec58daab06ff43c68a0817eb2177193218573cae"
50 signature = HSM2DongleSignature(bs)
51 self.assertEqual(expected_r, signature.r)
52 self.assertEqual(expected_s, signature.s)
54 def test_signature_invalid(self):
55 cases = list(
56 map(
57 lambda h: bytes.fromhex(h),
58 [
59 "",
60 "21", # Not long enough
61 "302230", # Length invalid
62 "300d0205aabbccddee0203112233", # Invalid total length
63 "300c0305aabbccddee0203112233", # Invalid R number prefix
64 "300c0205aabbccddee0303112233", # Invalid S number prefix
65 "ff0c0205aabbccddee0203112233", # Invalid DER prefix
66 ],
67 ))
68 for bs in cases:
69 with self.assertRaises(ValueError):
70 HSM2DongleSignature(bs)
72 def test_equal(self):
73 bs = bytes.fromhex(
74 "3145022100f48d30ae4f01925939116c6b74c1c9afefc6d1a0b7109307a68126f3683308f"
75 "502206cec3ea71687eaf367885b44ec58daab06ff43c68a0817eb2177193218573cae9000"
76 )
77 sig1 = HSM2DongleSignature(bs)
78 sig2 = HSM2DongleSignature(bs)
79 self.assertEqual(sig1, sig2)
81 def test_not_equal(self):
82 bs1 = bytes.fromhex(
83 "3145022100f48d30ae4f01925939116c6b74c1c9afefc6d1a0b7109307a68126f3683308f"
84 "502206cec3ea71687eaf367885b44ec58daab06ff43c68a0817eb2177193218573cae9000"
85 )
86 bs2 = bytes.fromhex(
87 "3045022100e719a1a379143ee7b598390305f4f1a991d6e26f175545c739f89728e270671"
88 "402207fcc41e525508a27bdcf9bd82f4b75709e8771dde714d0cf3d362056ed1bb07c9000"
89 )
90 sig1 = HSM2DongleSignature(bs1)
91 sig2 = HSM2DongleSignature(bs2)
92 self.assertNotEqual(sig1, sig2)